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,17 @@ 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) => {
1834+ CString :: new ( file. as_os_str ( ) . as_encoded_bytes ( ) ) . unwrap ( )
1835+ }
1836+ None => CString :: new ( & [ ] ) . unwrap ( ) ,
1837+ } ;
18311838 let _c_args: Vec < CString > = cmd_args
18321839 . iter ( )
18331840 . map ( |s| CString :: new ( s. as_bytes ( ) ) . unwrap ( ) )
@@ -1879,8 +1886,8 @@ impl TranslationUnit {
18791886 }
18801887
18811888 /// 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 {
1889+ pub ( crate ) fn save ( & mut self , file : & Path ) -> Result < ( ) , CXSaveError > {
1890+ let Ok ( file) = CString :: new ( file. as_os_str ( ) . as_encoded_bytes ( ) ) else {
18841891 return Err ( CXSaveError_Unknown ) ;
18851892 } ;
18861893 let ret = unsafe {
@@ -1913,9 +1920,10 @@ impl Drop for TranslationUnit {
19131920
19141921/// Translation unit used for macro fallback parsing
19151922pub ( crate ) struct FallbackTranslationUnit {
1916- file_path : String ,
1917- header_path : String ,
1918- pch_path : String ,
1923+ temp_dir : TempDir ,
1924+ file_path : PathBuf ,
1925+ header_path : PathBuf ,
1926+ pch_path : PathBuf ,
19191927 idx : Box < Index > ,
19201928 tu : TranslationUnit ,
19211929}
@@ -1929,9 +1937,10 @@ impl fmt::Debug for FallbackTranslationUnit {
19291937impl FallbackTranslationUnit {
19301938 /// Create a new fallback translation unit
19311939 pub ( crate ) fn new (
1932- file : String ,
1933- header_path : String ,
1934- pch_path : String ,
1940+ temp_dir : TempDir ,
1941+ file : PathBuf ,
1942+ header_path : PathBuf ,
1943+ pch_path : PathBuf ,
19351944 c_args : & [ Box < str > ] ,
19361945 ) -> Option < Self > {
19371946 // Create empty file
@@ -1945,12 +1954,13 @@ impl FallbackTranslationUnit {
19451954 let f_index = Box :: new ( Index :: new ( true , false ) ) ;
19461955 let f_translation_unit = TranslationUnit :: parse (
19471956 & f_index,
1948- & file,
1957+ Some ( & file) ,
19491958 c_args,
19501959 & [ ] ,
19511960 CXTranslationUnit_None ,
19521961 ) ?;
19531962 Some ( FallbackTranslationUnit {
1963+ temp_dir,
19541964 file_path : file,
19551965 header_path,
19561966 pch_path,
@@ -1988,14 +1998,6 @@ impl FallbackTranslationUnit {
19881998 }
19891999}
19902000
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-
19992001/// A diagnostic message generated while parsing a translation unit.
20002002pub ( crate ) struct Diagnostic {
20012003 x : CXDiagnostic ,
@@ -2036,9 +2038,9 @@ pub(crate) struct UnsavedFile {
20362038}
20372039
20382040impl 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 ( ) ;
2041+ /// Construct a new unsaved file with the given `path ` and `contents`.
2042+ pub ( crate ) fn new ( path : & Path , contents : & str ) -> UnsavedFile {
2043+ let name = CString :: new ( path . as_os_str ( ) . as_encoded_bytes ( ) ) . unwrap ( ) ;
20422044 let contents = CString :: new ( contents. as_bytes ( ) ) . unwrap ( ) ;
20432045 let x = CXUnsavedFile {
20442046 Filename : name. as_ptr ( ) ,
0 commit comments