Skip to content

Commit 33912cf

Browse files
authored
Merge pull request rust-lang#1452 from emilio/whitelist-name
Don't mangle using the parse callbacks for whitelisting / blacklisting / etc.
2 parents 4013431 + adf9cd3 commit 33912cf

File tree

5 files changed

+144
-138
lines changed

5 files changed

+144
-138
lines changed

bindgen-integration/build.rs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
extern crate bindgen;
22
extern crate gcc;
33

4+
use bindgen::callbacks::{MacroParsingBehavior, ParseCallbacks};
5+
use bindgen::Builder;
46
use std::collections::HashSet;
57
use std::env;
68
use std::path::PathBuf;
7-
use std::sync::{Arc, RwLock, Mutex};
8-
use bindgen::Builder;
9-
use bindgen::callbacks::{MacroParsingBehavior, ParseCallbacks};
9+
use std::sync::{Arc, Mutex, RwLock};
1010

1111
#[derive(Debug)]
1212
struct MacroCallback {
@@ -19,40 +19,55 @@ impl ParseCallbacks for MacroCallback {
1919
self.macros.write().unwrap().insert(name.into());
2020

2121
if name == "MY_ANNOYING_MACRO" {
22-
return MacroParsingBehavior::Ignore
22+
return MacroParsingBehavior::Ignore;
2323
}
2424

2525
MacroParsingBehavior::Default
2626
}
2727

2828
fn item_name(&self, original_item_name: &str) -> Option<String> {
2929
if original_item_name.starts_with("my_prefixed_") {
30-
Some(original_item_name.trim_start_matches("my_prefixed_").to_string())
30+
Some(
31+
original_item_name
32+
.trim_start_matches("my_prefixed_")
33+
.to_string(),
34+
)
3135
} else if original_item_name.starts_with("MY_PREFIXED_") {
32-
Some(original_item_name.trim_start_matches("MY_PREFIXED_").to_string())
36+
Some(
37+
original_item_name
38+
.trim_start_matches("MY_PREFIXED_")
39+
.to_string(),
40+
)
3341
} else {
3442
None
3543
}
3644
}
3745

3846
fn str_macro(&self, name: &str, value: &[u8]) {
3947
match &name {
40-
&"TESTMACRO_STRING_EXPANDED" |
41-
&"TESTMACRO_STRING" |
42-
&"TESTMACRO_INTEGER" => {
48+
&"TESTMACRO_STRING_EXPANDED"
49+
| &"TESTMACRO_STRING"
50+
| &"TESTMACRO_INTEGER" => {
4351
// The integer test macro is, actually, not expected to show up here at all -- but
4452
// should produce an error if it does.
45-
assert_eq!(value, b"Hello Preprocessor!", "str_macro handle received unexpected value");
53+
assert_eq!(
54+
value, b"Hello Preprocessor!",
55+
"str_macro handle received unexpected value"
56+
);
4657
*self.seen_hellos.lock().unwrap() += 1;
47-
},
58+
}
4859
_ => {}
4960
}
5061
}
5162
}
5263

5364
impl Drop for MacroCallback {
5465
fn drop(&mut self) {
55-
assert_eq!(*self.seen_hellos.lock().unwrap(), 2, "str_macro handle was not called once for all relevant macros")
66+
assert_eq!(
67+
*self.seen_hellos.lock().unwrap(),
68+
2,
69+
"str_macro handle was not called once for all relevant macros"
70+
)
5671
}
5772
}
5873

@@ -69,10 +84,15 @@ fn main() {
6984
.enable_cxx_namespaces()
7085
.rustified_enum(".*")
7186
.raw_line("pub use self::root::*;")
87+
.raw_line("extern { fn my_prefixed_function_to_remove(i: i32); }")
7288
.module_raw_line("root::testing", "pub type Bar = i32;")
7389
.header("cpp/Test.h")
7490
.clang_args(&["-x", "c++", "-std=c++11"])
75-
.parse_callbacks(Box::new(MacroCallback {macros: macros.clone(), seen_hellos: Mutex::new(0)}))
91+
.parse_callbacks(Box::new(MacroCallback {
92+
macros: macros.clone(),
93+
seen_hellos: Mutex::new(0),
94+
}))
95+
.blacklist_function("my_prefixed_function_to_remove")
7696
.generate()
7797
.expect("Unable to generate bindings");
7898

bindgen-integration/cpp/Test.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,5 @@ struct my_prefixed_bar {
195195
struct my_prefixed_foo {
196196
my_prefixed_bar member;
197197
};
198+
199+
void my_prefixed_function_to_remove();

bindgen-integration/src/lib.rs

Lines changed: 36 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ mod bindings {
55
}
66

77
use std::ffi::CStr;
8-
use std::os::raw::c_int;
98
use std::mem;
9+
use std::os::raw::c_int;
1010

1111
#[allow(unused)]
1212
use bindings::testing::Bar; // This type is generated from module_raw_line.
1313

1414
#[test]
1515
fn test_static_array() {
1616
let mut test = unsafe { bindings::Test_COUNTDOWN.as_ptr() };
17-
let expected = unsafe { bindings::Test_countdown()};
17+
let expected = unsafe { bindings::Test_countdown() };
1818
let also_expected = unsafe { bindings::Test_COUNTDOWN_PTR };
1919
assert!(!test.is_null());
2020
assert_eq!(also_expected, expected);
@@ -56,63 +56,41 @@ fn test_overload() {
5656

5757
#[test]
5858
fn test_bitfields_first() {
59-
let mut first: bindings::bitfields::First = unsafe {
60-
mem::zeroed()
61-
};
62-
assert!(unsafe {
63-
first.assert(0, 0, 0)
64-
});
59+
let mut first: bindings::bitfields::First = unsafe { mem::zeroed() };
60+
assert!(unsafe { first.assert(0, 0, 0) });
6561
first.set_three_bits_byte_one(2);
6662
first.set_six_bits_byte_two(42);
6763
first.set_two_bits_byte_two(1);
68-
assert!(unsafe {
69-
first.assert(2, 42, 1)
70-
});
64+
assert!(unsafe { first.assert(2, 42, 1) });
7165
}
7266

7367
#[test]
7468
fn test_bitfields_second() {
75-
let mut second: bindings::bitfields::Second = unsafe {
76-
mem::zeroed()
77-
};
78-
assert!(unsafe {
79-
second.assert(0, false)
80-
});
69+
let mut second: bindings::bitfields::Second = unsafe { mem::zeroed() };
70+
assert!(unsafe { second.assert(0, false) });
8171
second.set_thirty_one_bits(1337);
8272
second.set_one_bit(true);
83-
assert!(unsafe {
84-
second.assert(1337, true)
85-
});
73+
assert!(unsafe { second.assert(1337, true) });
8674
}
8775

8876
#[test]
8977
fn test_bitfields_third() {
90-
let mut third: bindings::bitfields::Third = unsafe {
91-
mem::zeroed()
92-
};
78+
let mut third: bindings::bitfields::Third = unsafe { mem::zeroed() };
9379
assert!(unsafe {
94-
third.assert(0,
95-
false,
96-
bindings::bitfields::ItemKind::ITEM_KIND_UNO)
80+
third.assert(0, false, bindings::bitfields::ItemKind::ITEM_KIND_UNO)
9781
});
9882
third.set_flags(12345);
9983
third.set_is_whatever(true);
10084
third.set_kind(bindings::bitfields::ItemKind::ITEM_KIND_TRES);
10185
assert!(unsafe {
102-
third.assert(12345,
103-
true,
104-
bindings::bitfields::ItemKind::ITEM_KIND_TRES)
86+
third.assert(12345, true, bindings::bitfields::ItemKind::ITEM_KIND_TRES)
10587
});
10688
}
10789

10890
#[test]
10991
fn test_bitfields_fourth() {
110-
let mut fourth: bindings::bitfields::Fourth = unsafe {
111-
mem::zeroed()
112-
};
113-
assert!(unsafe {
114-
fourth.assert(bindings::bitfields::MyEnum::ONE, 0)
115-
});
92+
let mut fourth: bindings::bitfields::Fourth = unsafe { mem::zeroed() };
93+
assert!(unsafe { fourth.assert(bindings::bitfields::MyEnum::ONE, 0) });
11694

11795
fourth.set_tag(bindings::bitfields::MyEnum::THREE);
11896
fourth.set_ptr(0xdeadbeef);
@@ -123,32 +101,22 @@ fn test_bitfields_fourth() {
123101

124102
#[test]
125103
fn test_bitfields_date2() {
126-
let mut date: bindings::bitfields::Date2 = unsafe {
127-
mem::zeroed()
128-
};
129-
assert!(unsafe {
130-
date.assert(0, 0, 0, 0, 0)
131-
});
104+
let mut date: bindings::bitfields::Date2 = unsafe { mem::zeroed() };
105+
assert!(unsafe { date.assert(0, 0, 0, 0, 0) });
132106

133107
date.set_nWeekDay(6); // saturdays are the best
134108
date.set_nMonthDay(20);
135109
date.set_nMonth(11);
136110
date.set_nYear(95);
137111
date.set_byte(255);
138-
assert!(unsafe {
139-
date.assert(6, 20, 11, 95, 255)
140-
});
112+
assert!(unsafe { date.assert(6, 20, 11, 95, 255) });
141113
}
142114

143115
#[test]
144116
fn test_bitfields_fifth() {
145-
let mut date: bindings::bitfields::Fifth = unsafe {
146-
mem::zeroed()
147-
};
117+
let mut date: bindings::bitfields::Fifth = unsafe { mem::zeroed() };
148118

149-
assert!(unsafe {
150-
date.assert(0, 0, 0, 0, 0)
151-
});
119+
assert!(unsafe { date.assert(0, 0, 0, 0, 0) });
152120

153121
date.byte = 255; // Set this first, to ensure we don't override it.
154122

@@ -157,40 +125,28 @@ fn test_bitfields_fifth() {
157125
date.set_nMonth(11);
158126
date.set_nYear(95);
159127

160-
assert!(unsafe {
161-
date.assert(6, 20, 11, 95, 255)
162-
});
128+
assert!(unsafe { date.assert(6, 20, 11, 95, 255) });
163129
}
164130

165131
#[test]
166132
fn test_bitfields_sixth() {
167-
let mut date: bindings::bitfields::Sixth = unsafe {
168-
mem::zeroed()
169-
};
133+
let mut date: bindings::bitfields::Sixth = unsafe { mem::zeroed() };
170134

171-
assert!(unsafe {
172-
date.assert(0, 0, 0, 0)
173-
});
135+
assert!(unsafe { date.assert(0, 0, 0, 0) });
174136

175137
date.byte = 255;
176138
date.set_nWeekDay(6); // saturdays are the best
177139
date.set_nMonthDay(20);
178140
date.set_nMonth(11);
179141

180-
assert!(unsafe {
181-
date.assert(255, 6, 11, 20)
182-
});
142+
assert!(unsafe { date.assert(255, 6, 11, 20) });
183143
}
184144

185145
#[test]
186146
fn test_bitfields_seventh() {
187-
let mut large: bindings::bitfields::Seventh = unsafe {
188-
mem::zeroed()
189-
};
147+
let mut large: bindings::bitfields::Seventh = unsafe { mem::zeroed() };
190148

191-
assert!(unsafe {
192-
large.assert(false, 0, 0, 0, 0, false, 0)
193-
});
149+
assert!(unsafe { large.assert(false, 0, 0, 0, 0, false, 0) });
194150

195151
large.set_first_one_bit(true);
196152
large.set_second_thirty_bits(375028802);
@@ -217,32 +173,26 @@ fn test_bitfields_seventh() {
217173
fn test_bitfield_constructors() {
218174
use std::mem;
219175
let mut first = bindings::bitfields::First {
220-
_bitfield_1: bindings::bitfields::First::new_bitfield_1(1, 2, 3)
176+
_bitfield_1: bindings::bitfields::First::new_bitfield_1(1, 2, 3),
221177
};
222-
assert!(unsafe {
223-
first.assert(1, 2, 3)
224-
});
178+
assert!(unsafe { first.assert(1, 2, 3) });
225179

226180
let mut second = bindings::bitfields::Second {
227181
_bitfield_1: bindings::bitfields::Second::new_bitfield_1(1337, true),
228182
__bindgen_align: [],
229183
};
230-
assert!(unsafe {
231-
second.assert(1337, true)
232-
});
184+
assert!(unsafe { second.assert(1337, true) });
233185

234186
let mut third = bindings::bitfields::Third {
235187
_bitfield_1: bindings::bitfields::Third::new_bitfield_1(
236188
42,
237189
false,
238-
bindings::bitfields::ItemKind::ITEM_KIND_TRES
190+
bindings::bitfields::ItemKind::ITEM_KIND_TRES,
239191
),
240192
__bindgen_align: [],
241193
};
242194
assert!(unsafe {
243-
third.assert(42,
244-
false,
245-
bindings::bitfields::ItemKind::ITEM_KIND_TRES)
195+
third.assert(42, false, bindings::bitfields::ItemKind::ITEM_KIND_TRES)
246196
});
247197
}
248198

@@ -266,7 +216,9 @@ fn test_destructors() {
266216

267217
impl Drop for bindings::InheritsFromVirtualDestructor {
268218
fn drop(&mut self) {
269-
unsafe { bindings::InheritsFromVirtualDestructor_InheritsFromVirtualDestructor_destructor(self) }
219+
unsafe {
220+
bindings::InheritsFromVirtualDestructor_InheritsFromVirtualDestructor_destructor(self)
221+
}
270222
}
271223
}
272224

@@ -286,9 +238,9 @@ fn test_virtual_dtor() {
286238
#[test]
287239
fn test_item_rename() {
288240
assert_eq!(bindings::CONST_VALUE, 3);
289-
assert_eq!(unsafe{ bindings::function_name() }, 4);
241+
assert_eq!(unsafe { bindings::function_name() }, 4);
290242

291-
let _foo = bindings::foo{
292-
member: bindings::bar{foo: 2}
243+
let _foo = bindings::foo {
244+
member: bindings::bar { foo: 2 },
293245
};
294-
}
246+
}

0 commit comments

Comments
 (0)