77use crate :: ir:: context:: BindgenContext ;
88use clang_sys:: * ;
99use std:: cmp;
10+ use std:: path:: { Path , PathBuf } ;
11+ use tempfile:: TempDir ;
1012
1113use std:: ffi:: { CStr , CString } ;
1214use std:: fmt;
@@ -1822,12 +1824,15 @@ impl TranslationUnit {
18221824 /// Parse a source file into a translation unit.
18231825 pub ( crate ) fn parse (
18241826 ix : & Index ,
1825- file : & str ,
1827+ file : Option < & Path > ,
18261828 cmd_args : & [ Box < str > ] ,
18271829 unsaved : & [ UnsavedFile ] ,
18281830 opts : CXTranslationUnit_Flags ,
18291831 ) -> 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+ } ;
18311836 let _c_args: Vec < CString > = cmd_args
18321837 . iter ( )
18331838 . map ( |s| CString :: new ( s. as_bytes ( ) ) . unwrap ( ) )
@@ -1879,10 +1884,8 @@ impl TranslationUnit {
18791884 }
18801885
18811886 /// 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) ;
18861889 let ret = unsafe {
18871890 clang_saveTranslationUnit (
18881891 self . x ,
@@ -1913,9 +1916,10 @@ impl Drop for TranslationUnit {
19131916
19141917/// Translation unit used for macro fallback parsing
19151918pub ( 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 ,
19191923 idx : Box < Index > ,
19201924 tu : TranslationUnit ,
19211925}
@@ -1929,9 +1933,10 @@ impl fmt::Debug for FallbackTranslationUnit {
19291933impl FallbackTranslationUnit {
19301934 /// Create a new fallback translation unit
19311935 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 ,
19351940 c_args : & [ Box < str > ] ,
19361941 ) -> Option < Self > {
19371942 // Create empty file
@@ -1945,12 +1950,13 @@ impl FallbackTranslationUnit {
19451950 let f_index = Box :: new ( Index :: new ( true , false ) ) ;
19461951 let f_translation_unit = TranslationUnit :: parse (
19471952 & f_index,
1948- & file,
1953+ Some ( & file) ,
19491954 c_args,
19501955 & [ ] ,
19511956 CXTranslationUnit_None ,
19521957 ) ?;
19531958 Some ( FallbackTranslationUnit {
1959+ temp_dir,
19541960 file_path : file,
19551961 header_path,
19561962 pch_path,
@@ -1988,14 +1994,6 @@ impl FallbackTranslationUnit {
19881994 }
19891995}
19901996
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-
19991997/// A diagnostic message generated while parsing a translation unit.
20001998pub ( crate ) struct Diagnostic {
20011999 x : CXDiagnostic ,
@@ -2036,9 +2034,9 @@ pub(crate) struct UnsavedFile {
20362034}
20372035
20382036impl 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 ) ;
20422040 let contents = CString :: new ( contents. as_bytes ( ) ) . unwrap ( ) ;
20432041 let x = CXUnsavedFile {
20442042 Filename : name. as_ptr ( ) ,
@@ -2450,3 +2448,7 @@ impl TargetInfo {
24502448 }
24512449 }
24522450}
2451+
2452+ fn path_to_cstring ( path : & Path ) -> CString {
2453+ CString :: new ( path. to_string_lossy ( ) . as_bytes ( ) ) . unwrap ( )
2454+ }
0 commit comments