Skip to content

Commit 14d4cfe

Browse files
committed
ir: Allow whitelisting non-recursively
Fixes rust-lang#429
1 parent 3fd3062 commit 14d4cfe

File tree

6 files changed

+62
-13
lines changed

6 files changed

+62
-13
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ir/context.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -1155,15 +1155,15 @@ pub struct WhitelistedItemsIter<'ctx, 'gen>
11551155
{
11561156
ctx: &'ctx BindgenContext<'gen>,
11571157

1158-
// The set of whitelisted items we have seen. If you think of traversing
1159-
// whitelisted items like GC tracing, this is the mark bits, and contains
1160-
// both black and gray items.
1158+
/// The set of whitelisted items we have seen. If you think of traversing
1159+
/// whitelisted items like GC tracing, this is the mark bits, and contains
1160+
/// both black and gray items.
11611161
seen: ItemSet,
11621162

1163-
// The set of whitelisted items that we have seen but have yet to iterate
1164-
// over and collect transitive references from. To return to the GC analogy,
1165-
// this is the mark stack, containing the set of gray items which we have
1166-
// not finished tracing yet.
1163+
/// The set of whitelisted items that we have seen but have yet to iterate
1164+
/// over and collect transitive references from. To return to the GC analogy,
1165+
/// this is the mark stack, containing the set of gray items which we have
1166+
/// not finished tracing yet.
11671167
to_iterate: Vec<ItemId>,
11681168
}
11691169

@@ -1181,12 +1181,14 @@ impl<'ctx, 'gen> Iterator for WhitelistedItemsIter<'ctx, 'gen>
11811181
debug_assert!(self.seen.contains(&id));
11821182
debug_assert!(self.ctx.items.contains_key(&id));
11831183

1184-
let mut sub_types = ItemSet::new();
1185-
id.collect_types(self.ctx, &mut sub_types, &());
1184+
if self.ctx.options().whitelist_recursively {
1185+
let mut sub_types = ItemSet::new();
1186+
id.collect_types(self.ctx, &mut sub_types, &());
11861187

1187-
for id in sub_types {
1188-
if self.seen.insert(id) {
1189-
self.to_iterate.push(id);
1188+
for id in sub_types {
1189+
if self.seen.insert(id) {
1190+
self.to_iterate.push(id);
1191+
}
11901192
}
11911193
}
11921194

src/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,15 @@ impl Builder {
188188
self
189189
}
190190

191+
/// Whether to whitelist types recursively or not. Defaults to true.
192+
///
193+
/// See https://github.com/servo/rust-bindgen/issues/429 for different use
194+
/// cases for this.
195+
pub fn whitelist_recursively(mut self, doit: bool) -> Self {
196+
self.options.whitelist_recursively = doit;
197+
self
198+
}
199+
191200
/// Generate a C/C++ file that includes the header and has dummy uses of
192201
/// every type defined in the header.
193202
pub fn dummy_uses<T: Into<String>>(mut self, dummy_uses: T) -> Builder {
@@ -529,6 +538,9 @@ pub struct BindgenOptions {
529538
/// Wether to keep documentation comments in the generated output. See the
530539
/// documentation for more details.
531540
pub generate_comments: bool,
541+
542+
/// Wether to whitelist types recursively. Defaults to true.
543+
pub whitelist_recursively: bool,
532544
}
533545

534546
impl BindgenOptions {
@@ -574,6 +586,7 @@ impl Default for BindgenOptions {
574586
codegen_config: CodegenConfig::all(),
575587
conservative_inline_namespaces: false,
576588
generate_comments: true,
589+
whitelist_recursively: true,
577590
}
578591
}
579592
}

src/options.rs

+7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ pub fn builder_from_flags<I>(args: I)
4646
.long("no-doc-comments")
4747
.help("Avoid including doc comments in the output, see: \
4848
https://github.com/servo/rust-bindgen/issues/426"),
49+
Arg::with_name("no-recursive-whitelist")
50+
.long("no-recursive-whitelist")
51+
.help("Avoid whitelisting types recursively"),
4952
Arg::with_name("builtins")
5053
.long("builtins")
5154
.help("Output bindings for builtin definitions, e.g. \
@@ -279,6 +282,10 @@ pub fn builder_from_flags<I>(args: I)
279282
builder = builder.generate_comments(false);
280283
}
281284

285+
if matches.is_present("no-recursive-whitelist") {
286+
builder = builder.whitelist_recursively(false);
287+
}
288+
282289
if let Some(opaque_types) = matches.values_of("opaque-type") {
283290
for ty in opaque_types {
284291
builder = builder.opaque_type(ty);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(non_snake_case)]
5+
6+
pub enum Bar {}
7+
8+
#[repr(C)]
9+
#[derive(Debug, Copy)]
10+
pub struct Foo {
11+
pub baz: *mut Bar,
12+
}
13+
#[test]
14+
fn bindgen_test_layout_Foo() {
15+
assert_eq!(::std::mem::size_of::<Foo>() , 8usize);
16+
assert_eq!(::std::mem::align_of::<Foo>() , 8usize);
17+
}
18+
impl Clone for Foo {
19+
fn clone(&self) -> Self { *self }
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// bindgen-flags: --no-recursive-whitelist --whitelist-type "Foo" --raw-line "pub enum Bar {}"
2+
3+
struct Bar;
4+
5+
struct Foo {
6+
struct Bar* baz;
7+
};

0 commit comments

Comments
 (0)