@@ -28,21 +28,14 @@ impl ParseCallbacks for MacroCallback {
28
28
MacroParsingBehavior :: Default
29
29
}
30
30
31
- fn item_name ( & self , original_item_name : & str ) -> Option < String > {
32
- if original_item_name. starts_with ( "my_prefixed_" ) {
33
- Some (
34
- original_item_name
35
- . trim_start_matches ( "my_prefixed_" )
36
- . to_string ( ) ,
37
- )
38
- } else if original_item_name. starts_with ( "MY_PREFIXED_" ) {
39
- Some (
40
- original_item_name
41
- . trim_start_matches ( "MY_PREFIXED_" )
42
- . to_string ( ) ,
43
- )
44
- } else {
45
- None
31
+ fn int_macro ( & self , name : & str , _value : i64 ) -> Option < IntKind > {
32
+ match name {
33
+ "TESTMACRO_CUSTOMINTKIND_PATH" => Some ( IntKind :: Custom {
34
+ name : "crate::MacroInteger" ,
35
+ is_signed : true ,
36
+ } ) ,
37
+
38
+ _ => None ,
46
39
}
47
40
}
48
41
@@ -67,17 +60,6 @@ impl ParseCallbacks for MacroCallback {
67
60
}
68
61
}
69
62
70
- fn int_macro ( & self , name : & str , _value : i64 ) -> Option < IntKind > {
71
- match name {
72
- "TESTMACRO_CUSTOMINTKIND_PATH" => Some ( IntKind :: Custom {
73
- name : "crate::MacroInteger" ,
74
- is_signed : true ,
75
- } ) ,
76
-
77
- _ => None ,
78
- }
79
- }
80
-
81
63
fn func_macro ( & self , name : & str , value : & [ & [ u8 ] ] ) {
82
64
match name {
83
65
"TESTMACRO_NONFUNCTIONAL" => {
@@ -122,6 +104,24 @@ impl ParseCallbacks for MacroCallback {
122
104
}
123
105
}
124
106
107
+ fn item_name ( & self , original_item_name : & str ) -> Option < String > {
108
+ if original_item_name. starts_with ( "my_prefixed_" ) {
109
+ Some (
110
+ original_item_name
111
+ . trim_start_matches ( "my_prefixed_" )
112
+ . to_string ( ) ,
113
+ )
114
+ } else if original_item_name. starts_with ( "MY_PREFIXED_" ) {
115
+ Some (
116
+ original_item_name
117
+ . trim_start_matches ( "MY_PREFIXED_" )
118
+ . to_string ( ) ,
119
+ )
120
+ } else {
121
+ None
122
+ }
123
+ }
124
+
125
125
// Test the "custom derives" capability by adding `PartialEq` to the `Test` struct.
126
126
fn add_derives ( & self , info : & DeriveInfo < ' _ > ) -> Vec < String > {
127
127
if info. name == "Test" {
@@ -149,7 +149,7 @@ impl Drop for MacroCallback {
149
149
}
150
150
}
151
151
152
- fn main ( ) {
152
+ fn setup_macro_test ( ) {
153
153
cc:: Build :: new ( )
154
154
. cpp ( true )
155
155
. file ( "cpp/Test.cc" )
@@ -204,3 +204,46 @@ fn main() {
204
204
"including stub via include dir must produce correct dep path" ,
205
205
) ;
206
206
}
207
+
208
+ fn setup_extern_test ( ) {
209
+ // GH-1090: https://github.com/rust-lang/rust-bindgen/issues/1090
210
+ cc:: Build :: new ( )
211
+ . cpp ( true )
212
+ . file ( "cpp/extern.c" )
213
+ . include ( "include" )
214
+ . compile ( "libextern.a" ) ;
215
+ let out_path = PathBuf :: from ( env:: var ( "OUT_DIR" ) . unwrap ( ) ) ;
216
+ let out_rust_file = out_path. join ( "extern.rs" ) ;
217
+ let out_rust_file_relative = out_rust_file
218
+ . strip_prefix ( std:: env:: current_dir ( ) . unwrap ( ) . parent ( ) . unwrap ( ) )
219
+ . unwrap ( ) ;
220
+ let out_dep_file = out_path. join ( "extern.d" ) ;
221
+
222
+ let bindings = Builder :: default ( )
223
+ . rustfmt_bindings ( false )
224
+ . header ( "cpp/extern.h" )
225
+ . clang_args ( & [ "-x" , "c++" , "-I" , "include" ] )
226
+ . depfile ( out_rust_file_relative. display ( ) . to_string ( ) , & out_dep_file)
227
+ . generate_extern_functions ( true )
228
+ . generate ( )
229
+ . expect ( "Unable to generate extern bindings" ) ;
230
+
231
+ bindings
232
+ . write_to_file ( & out_rust_file)
233
+ . expect ( "Couldn't write bindings!" ) ;
234
+ let observed_deps =
235
+ std:: fs:: read_to_string ( out_dep_file) . expect ( "Couldn't read depfile!" ) ;
236
+ let expected_deps = format ! (
237
+ "{}: /tmp/bindgen/extern.hpp cpp/extern.h include/extern_stub.h" ,
238
+ out_rust_file_relative. display( )
239
+ ) ;
240
+ assert_eq ! (
241
+ observed_deps, expected_deps,
242
+ "including stub via include dir must produce correct dep path" ,
243
+ ) ;
244
+ }
245
+
246
+ fn main ( ) {
247
+ setup_macro_test ( ) ;
248
+ setup_extern_test ( ) ;
249
+ }
0 commit comments