@@ -38,21 +38,24 @@ struct CodegenResult {
38
38
items : Vec < P < ast:: Item > > ,
39
39
saw_union : bool ,
40
40
items_seen : HashSet < ItemId > ,
41
- /// The set of generated function names, needed because in C/C++ is legal to
41
+ /// The set of generated function/var names, needed because in C/C++ is legal to
42
42
/// do something like:
43
43
///
44
44
/// ```
45
45
/// extern "C" {
46
46
/// void foo();
47
+ /// extern int bar;
47
48
/// }
48
49
///
49
50
/// extern "C" {
50
51
/// void foo();
52
+ /// extern int bar;
51
53
/// }
52
54
/// ```
53
55
///
54
56
/// Being these two different declarations.
55
57
functions_seen : HashSet < String > ,
58
+ vars_seen : HashSet < String > ,
56
59
}
57
60
58
61
impl CodegenResult {
@@ -62,6 +65,7 @@ impl CodegenResult {
62
65
saw_union : false ,
63
66
items_seen : Default :: default ( ) ,
64
67
functions_seen : Default :: default ( ) ,
68
+ vars_seen : Default :: default ( ) ,
65
69
}
66
70
}
67
71
@@ -85,6 +89,14 @@ impl CodegenResult {
85
89
self . functions_seen . insert ( name. into ( ) ) ;
86
90
}
87
91
92
+ fn seen_var ( & self , name : & str ) -> bool {
93
+ self . vars_seen . contains ( name)
94
+ }
95
+
96
+ fn saw_var ( & mut self , name : & str ) {
97
+ self . vars_seen . insert ( name. into ( ) ) ;
98
+ }
99
+
88
100
fn inner < F > ( & mut self , cb : F ) -> Vec < P < ast:: Item > >
89
101
where F : FnOnce ( & mut Self )
90
102
{
@@ -265,23 +277,31 @@ impl CodeGenerator for Var {
265
277
ctx : & BindgenContext ,
266
278
result : & mut CodegenResult ,
267
279
item : & Item ) {
268
- let name = item. canonical_name ( ctx) ;
280
+ let canonical_name = item. canonical_name ( ctx) ;
281
+
282
+ // TODO: Maybe warn here if there's a type mismatch
283
+ if result. seen_var ( & canonical_name) {
284
+ return ;
285
+ }
286
+ result. saw_var ( & canonical_name) ;
287
+
269
288
let ty = self . ty ( ) . to_rust_ty ( ctx) ;
270
289
271
290
if let Some ( val) = self . val ( ) {
272
- let const_item = aster:: AstBuilder :: new ( ) . item ( ) . pub_ ( ) . const_ ( name)
291
+ let const_item = aster:: AstBuilder :: new ( ) . item ( ) . pub_ ( )
292
+ . const_ ( canonical_name)
273
293
. expr ( ) . int ( val) . build ( ty) ;
274
294
result. push ( const_item)
275
295
} else {
276
296
let mut attrs = vec ! [ ] ;
277
297
if let Some ( mangled) = self . mangled_name ( ) {
278
298
attrs. push ( attributes:: link_name ( mangled) ) ;
279
- } else if name != self . name ( ) {
299
+ } else if canonical_name != self . name ( ) {
280
300
attrs. push ( attributes:: link_name ( self . name ( ) ) ) ;
281
301
}
282
302
283
303
let item = ast:: ForeignItem {
284
- ident : ctx. rust_ident_raw ( & name ) ,
304
+ ident : ctx. rust_ident_raw ( & canonical_name ) ,
285
305
attrs : attrs,
286
306
node : ast:: ForeignItemKind :: Static ( ty, !self . is_const ( ) ) ,
287
307
id : ast:: DUMMY_NODE_ID ,
0 commit comments