7
7
use crate :: ir:: context:: BindgenContext ;
8
8
use clang_sys:: * ;
9
9
use std:: cmp;
10
+ use std:: path:: { Path , PathBuf } ;
11
+ use tempfile:: TempDir ;
10
12
11
13
use std:: ffi:: { CStr , CString } ;
12
14
use std:: fmt;
@@ -1822,12 +1824,15 @@ impl TranslationUnit {
1822
1824
/// Parse a source file into a translation unit.
1823
1825
pub ( crate ) fn parse (
1824
1826
ix : & Index ,
1825
- file : & str ,
1827
+ file : Option < & Path > ,
1826
1828
cmd_args : & [ Box < str > ] ,
1827
1829
unsaved : & [ UnsavedFile ] ,
1828
1830
opts : CXTranslationUnit_Flags ,
1829
1831
) -> Option < TranslationUnit > {
1830
- let fname = CString :: new ( file) . unwrap ( ) ;
1832
+ let fname = match file {
1833
+ Some ( file) => path_to_cstring ( file) ,
1834
+ None => CString :: new ( vec ! [ ] ) . unwrap ( ) ,
1835
+ } ;
1831
1836
let _c_args: Vec < CString > = cmd_args
1832
1837
. iter ( )
1833
1838
. map ( |s| CString :: new ( s. as_bytes ( ) ) . unwrap ( ) )
@@ -1879,10 +1884,8 @@ impl TranslationUnit {
1879
1884
}
1880
1885
1881
1886
/// Save a translation unit to the given file.
1882
- pub ( crate ) fn save ( & mut self , file : & str ) -> Result < ( ) , CXSaveError > {
1883
- let Ok ( file) = CString :: new ( file) else {
1884
- return Err ( CXSaveError_Unknown ) ;
1885
- } ;
1887
+ pub ( crate ) fn save ( & mut self , file : & Path ) -> Result < ( ) , CXSaveError > {
1888
+ let file = path_to_cstring ( file) ;
1886
1889
let ret = unsafe {
1887
1890
clang_saveTranslationUnit (
1888
1891
self . x ,
@@ -1913,9 +1916,10 @@ impl Drop for TranslationUnit {
1913
1916
1914
1917
/// Translation unit used for macro fallback parsing
1915
1918
pub ( crate ) struct FallbackTranslationUnit {
1916
- file_path : String ,
1917
- header_path : String ,
1918
- pch_path : String ,
1919
+ temp_dir : TempDir ,
1920
+ file_path : PathBuf ,
1921
+ header_path : PathBuf ,
1922
+ pch_path : PathBuf ,
1919
1923
idx : Box < Index > ,
1920
1924
tu : TranslationUnit ,
1921
1925
}
@@ -1929,9 +1933,10 @@ impl fmt::Debug for FallbackTranslationUnit {
1929
1933
impl FallbackTranslationUnit {
1930
1934
/// Create a new fallback translation unit
1931
1935
pub ( crate ) fn new (
1932
- file : String ,
1933
- header_path : String ,
1934
- pch_path : String ,
1936
+ temp_dir : TempDir ,
1937
+ file : PathBuf ,
1938
+ header_path : PathBuf ,
1939
+ pch_path : PathBuf ,
1935
1940
c_args : & [ Box < str > ] ,
1936
1941
) -> Option < Self > {
1937
1942
// Create empty file
@@ -1945,12 +1950,13 @@ impl FallbackTranslationUnit {
1945
1950
let f_index = Box :: new ( Index :: new ( true , false ) ) ;
1946
1951
let f_translation_unit = TranslationUnit :: parse (
1947
1952
& f_index,
1948
- & file,
1953
+ Some ( & file) ,
1949
1954
c_args,
1950
1955
& [ ] ,
1951
1956
CXTranslationUnit_None ,
1952
1957
) ?;
1953
1958
Some ( FallbackTranslationUnit {
1959
+ temp_dir,
1954
1960
file_path : file,
1955
1961
header_path,
1956
1962
pch_path,
@@ -1988,14 +1994,6 @@ impl FallbackTranslationUnit {
1988
1994
}
1989
1995
}
1990
1996
1991
- impl Drop for FallbackTranslationUnit {
1992
- fn drop ( & mut self ) {
1993
- let _ = std:: fs:: remove_file ( & self . file_path ) ;
1994
- let _ = std:: fs:: remove_file ( & self . header_path ) ;
1995
- let _ = std:: fs:: remove_file ( & self . pch_path ) ;
1996
- }
1997
- }
1998
-
1999
1997
/// A diagnostic message generated while parsing a translation unit.
2000
1998
pub ( crate ) struct Diagnostic {
2001
1999
x : CXDiagnostic ,
@@ -2036,9 +2034,9 @@ pub(crate) struct UnsavedFile {
2036
2034
}
2037
2035
2038
2036
impl UnsavedFile {
2039
- /// Construct a new unsaved file with the given `name ` and `contents`.
2040
- pub ( crate ) fn new ( name : & str , contents : & str ) -> UnsavedFile {
2041
- let name = CString :: new ( name . as_bytes ( ) ) . unwrap ( ) ;
2037
+ /// Construct a new unsaved file with the given `path ` and `contents`.
2038
+ pub ( crate ) fn new ( path : & Path , contents : & str ) -> UnsavedFile {
2039
+ let name = path_to_cstring ( path ) ;
2042
2040
let contents = CString :: new ( contents. as_bytes ( ) ) . unwrap ( ) ;
2043
2041
let x = CXUnsavedFile {
2044
2042
Filename : name. as_ptr ( ) ,
@@ -2450,3 +2448,7 @@ impl TargetInfo {
2450
2448
}
2451
2449
}
2452
2450
}
2451
+
2452
+ fn path_to_cstring ( path : & Path ) -> CString {
2453
+ CString :: new ( path. to_string_lossy ( ) . as_bytes ( ) ) . unwrap ( )
2454
+ }
0 commit comments