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;
@@ -246,11 +248,10 @@ impl Cursor {
246
248
pub ( crate ) fn is_toplevel ( & self ) -> bool {
247
249
let mut semantic_parent = self . fallible_semantic_parent ( ) ;
248
250
249
- while semantic_parent. is_some ( ) &&
250
- ( semantic_parent. unwrap ( ) . kind ( ) == CXCursor_Namespace ||
251
- semantic_parent. unwrap ( ) . kind ( ) ==
252
- CXCursor_NamespaceAlias ||
253
- semantic_parent. unwrap ( ) . kind ( ) == CXCursor_NamespaceRef )
251
+ while semantic_parent. is_some ( )
252
+ && ( semantic_parent. unwrap ( ) . kind ( ) == CXCursor_Namespace
253
+ || semantic_parent. unwrap ( ) . kind ( ) == CXCursor_NamespaceAlias
254
+ || semantic_parent. unwrap ( ) . kind ( ) == CXCursor_NamespaceRef )
254
255
{
255
256
semantic_parent =
256
257
semantic_parent. unwrap ( ) . fallible_semantic_parent ( ) ;
@@ -267,9 +268,9 @@ impl Cursor {
267
268
pub ( crate ) fn is_template_like ( & self ) -> bool {
268
269
matches ! (
269
270
self . kind( ) ,
270
- CXCursor_ClassTemplate |
271
- CXCursor_ClassTemplatePartialSpecialization |
272
- CXCursor_TypeAliasTemplateDecl
271
+ CXCursor_ClassTemplate
272
+ | CXCursor_ClassTemplatePartialSpecialization
273
+ | CXCursor_TypeAliasTemplateDecl
273
274
)
274
275
}
275
276
@@ -296,9 +297,9 @@ impl Cursor {
296
297
/// Is the referent a fully specialized template specialization without any
297
298
/// remaining free template arguments?
298
299
pub ( crate ) fn is_fully_specialized_template ( & self ) -> bool {
299
- self . is_template_specialization ( ) &&
300
- self . kind ( ) != CXCursor_ClassTemplatePartialSpecialization &&
301
- self . num_template_args ( ) . unwrap_or ( 0 ) > 0
300
+ self . is_template_specialization ( )
301
+ && self . kind ( ) != CXCursor_ClassTemplatePartialSpecialization
302
+ && self . num_template_args ( ) . unwrap_or ( 0 ) > 0
302
303
}
303
304
304
305
/// Is the referent a template specialization that still has remaining free
@@ -324,9 +325,9 @@ impl Cursor {
324
325
pub ( crate ) fn is_template_parameter ( & self ) -> bool {
325
326
matches ! (
326
327
self . kind( ) ,
327
- CXCursor_TemplateTemplateParameter |
328
- CXCursor_TemplateTypeParameter |
329
- CXCursor_NonTypeTemplateParameter
328
+ CXCursor_TemplateTemplateParameter
329
+ | CXCursor_TemplateTypeParameter
330
+ | CXCursor_NonTypeTemplateParameter
330
331
)
331
332
}
332
333
@@ -664,9 +665,9 @@ impl Cursor {
664
665
// inline function without a definition, and it's not a defaulted
665
666
// function, we can reasonably safely conclude that it's a deleted
666
667
// function.
667
- self . is_inlined_function ( ) &&
668
- self . definition ( ) . is_none ( ) &&
669
- !self . is_defaulted_function ( )
668
+ self . is_inlined_function ( )
669
+ && self . definition ( ) . is_none ( )
670
+ && !self . is_defaulted_function ( )
670
671
}
671
672
672
673
/// Is the referent a bit field declaration?
@@ -788,11 +789,11 @@ impl Cursor {
788
789
let found_attr = & mut found_attrs[ idx] ;
789
790
if !* found_attr {
790
791
// `attr.name` and` attr.token_kind` are checked against unexposed attributes only.
791
- if attr. kind == Some ( kind) ||
792
- ( kind == CXCursor_UnexposedAttr &&
793
- cur. tokens ( ) . iter ( ) . any ( |t| {
794
- t. kind == attr. token_kind &&
795
- t. spelling ( ) == attr. name
792
+ if attr. kind == Some ( kind)
793
+ || ( kind == CXCursor_UnexposedAttr
794
+ && cur. tokens ( ) . iter ( ) . any ( |t| {
795
+ t. kind == attr. token_kind
796
+ && t. spelling ( ) == attr. name
796
797
} ) )
797
798
{
798
799
* found_attr = true ;
@@ -1408,12 +1409,12 @@ impl Type {
1408
1409
/// to.
1409
1410
pub ( crate ) fn pointee_type ( & self ) -> Option < Type > {
1410
1411
match self . kind ( ) {
1411
- CXType_Pointer |
1412
- CXType_RValueReference |
1413
- CXType_LValueReference |
1414
- CXType_MemberPointer |
1415
- CXType_BlockPointer |
1416
- CXType_ObjCObjectPointer => {
1412
+ CXType_Pointer
1413
+ | CXType_RValueReference
1414
+ | CXType_LValueReference
1415
+ | CXType_MemberPointer
1416
+ | CXType_BlockPointer
1417
+ | CXType_ObjCObjectPointer => {
1417
1418
let ret = Type {
1418
1419
x : unsafe { clang_getPointeeType ( self . x ) } ,
1419
1420
} ;
@@ -1516,12 +1517,12 @@ impl Type {
1516
1517
// Yep, the spelling of this containing type-parameter is extremely
1517
1518
// nasty... But can happen in <type_traits>. Unfortunately I couldn't
1518
1519
// reduce it enough :(
1519
- self . template_args ( ) . is_some_and ( |args| args. len ( ) > 0 ) &&
1520
- !matches ! (
1520
+ self . template_args ( ) . is_some_and ( |args| args. len ( ) > 0 )
1521
+ && !matches ! (
1521
1522
self . declaration( ) . kind( ) ,
1522
- CXCursor_ClassTemplatePartialSpecialization |
1523
- CXCursor_TypeAliasTemplateDecl |
1524
- CXCursor_TemplateTemplateParameter
1523
+ CXCursor_ClassTemplatePartialSpecialization
1524
+ | CXCursor_TypeAliasTemplateDecl
1525
+ | CXCursor_TemplateTemplateParameter
1525
1526
)
1526
1527
}
1527
1528
@@ -1546,9 +1547,9 @@ impl Type {
1546
1547
. is_match ( spelling. as_ref ( ) )
1547
1548
}
1548
1549
1549
- self . kind ( ) == CXType_Unexposed &&
1550
- ( hacky_parse_associated_type ( self . spelling ( ) ) ||
1551
- hacky_parse_associated_type (
1550
+ self . kind ( ) == CXType_Unexposed
1551
+ && ( hacky_parse_associated_type ( self . spelling ( ) )
1552
+ || hacky_parse_associated_type (
1552
1553
self . canonical_type ( ) . spelling ( ) ,
1553
1554
) )
1554
1555
}
@@ -1822,12 +1823,15 @@ impl TranslationUnit {
1822
1823
/// Parse a source file into a translation unit.
1823
1824
pub ( crate ) fn parse (
1824
1825
ix : & Index ,
1825
- file : & str ,
1826
+ file : Option < & Path > ,
1826
1827
cmd_args : & [ Box < str > ] ,
1827
1828
unsaved : & [ UnsavedFile ] ,
1828
1829
opts : CXTranslationUnit_Flags ,
1829
1830
) -> Option < TranslationUnit > {
1830
- let fname = CString :: new ( file) . unwrap ( ) ;
1831
+ let fname = match file {
1832
+ Some ( file) => path_to_cstring ( file) ,
1833
+ None => CString :: new ( vec ! [ ] ) . unwrap ( ) ,
1834
+ } ;
1831
1835
let _c_args: Vec < CString > = cmd_args
1832
1836
. iter ( )
1833
1837
. map ( |s| CString :: new ( s. as_bytes ( ) ) . unwrap ( ) )
@@ -1879,10 +1883,8 @@ impl TranslationUnit {
1879
1883
}
1880
1884
1881
1885
/// 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
- } ;
1886
+ pub ( crate ) fn save ( & mut self , file : & Path ) -> Result < ( ) , CXSaveError > {
1887
+ let file = path_to_cstring ( file) ;
1886
1888
let ret = unsafe {
1887
1889
clang_saveTranslationUnit (
1888
1890
self . x ,
@@ -1913,8 +1915,9 @@ impl Drop for TranslationUnit {
1913
1915
1914
1916
/// Translation unit used for macro fallback parsing
1915
1917
pub ( crate ) struct FallbackTranslationUnit {
1916
- file_path : String ,
1917
- pch_path : String ,
1918
+ temp_dir : TempDir ,
1919
+ file_path : PathBuf ,
1920
+ pch_path : PathBuf ,
1918
1921
idx : Box < Index > ,
1919
1922
tu : TranslationUnit ,
1920
1923
}
@@ -1928,8 +1931,9 @@ impl fmt::Debug for FallbackTranslationUnit {
1928
1931
impl FallbackTranslationUnit {
1929
1932
/// Create a new fallback translation unit
1930
1933
pub ( crate ) fn new (
1931
- file : String ,
1932
- pch_path : String ,
1934
+ temp_dir : TempDir ,
1935
+ file : PathBuf ,
1936
+ pch_path : PathBuf ,
1933
1937
c_args : & [ Box < str > ] ,
1934
1938
) -> Option < Self > {
1935
1939
// Create empty file
@@ -1943,12 +1947,13 @@ impl FallbackTranslationUnit {
1943
1947
let f_index = Box :: new ( Index :: new ( true , false ) ) ;
1944
1948
let f_translation_unit = TranslationUnit :: parse (
1945
1949
& f_index,
1946
- & file,
1950
+ Some ( & file) ,
1947
1951
c_args,
1948
1952
& [ ] ,
1949
1953
CXTranslationUnit_None ,
1950
1954
) ?;
1951
1955
Some ( FallbackTranslationUnit {
1956
+ temp_dir,
1952
1957
file_path : file,
1953
1958
pch_path,
1954
1959
tu : f_translation_unit,
@@ -1985,13 +1990,6 @@ impl FallbackTranslationUnit {
1985
1990
}
1986
1991
}
1987
1992
1988
- impl Drop for FallbackTranslationUnit {
1989
- fn drop ( & mut self ) {
1990
- let _ = std:: fs:: remove_file ( & self . file_path ) ;
1991
- let _ = std:: fs:: remove_file ( & self . pch_path ) ;
1992
- }
1993
- }
1994
-
1995
1993
/// A diagnostic message generated while parsing a translation unit.
1996
1994
pub ( crate ) struct Diagnostic {
1997
1995
x : CXDiagnostic ,
@@ -2032,9 +2030,9 @@ pub(crate) struct UnsavedFile {
2032
2030
}
2033
2031
2034
2032
impl UnsavedFile {
2035
- /// Construct a new unsaved file with the given `name ` and `contents`.
2036
- pub ( crate ) fn new ( name : & str , contents : & str ) -> UnsavedFile {
2037
- let name = CString :: new ( name . as_bytes ( ) ) . unwrap ( ) ;
2033
+ /// Construct a new unsaved file with the given `path ` and `contents`.
2034
+ pub ( crate ) fn new ( path : & Path , contents : & str ) -> UnsavedFile {
2035
+ let name = path_to_cstring ( path ) ;
2038
2036
let contents = CString :: new ( contents. as_bytes ( ) ) . unwrap ( ) ;
2039
2037
let x = CXUnsavedFile {
2040
2038
Filename : name. as_ptr ( ) ,
@@ -2309,8 +2307,8 @@ impl EvalResult {
2309
2307
{
2310
2308
let mut found_cant_eval = false ;
2311
2309
cursor. visit ( |c| {
2312
- if c. kind ( ) == CXCursor_TypeRef &&
2313
- c. cur_type ( ) . canonical_type ( ) . kind ( ) == CXType_Unexposed
2310
+ if c. kind ( ) == CXCursor_TypeRef
2311
+ && c. cur_type ( ) . canonical_type ( ) . kind ( ) == CXType_Unexposed
2314
2312
{
2315
2313
found_cant_eval = true ;
2316
2314
return CXChildVisit_Break ;
@@ -2446,3 +2444,7 @@ impl TargetInfo {
2446
2444
}
2447
2445
}
2448
2446
}
2447
+
2448
+ fn path_to_cstring ( path : & Path ) -> CString {
2449
+ CString :: new ( path. to_string_lossy ( ) . as_bytes ( ) ) . unwrap ( )
2450
+ }
0 commit comments