diff options
author | Emilio Cobos Álvarez <ecoal95@gmail.com> | 2016-11-22 20:01:29 +0100 |
---|---|---|
committer | Emilio Cobos Álvarez <ecoal95@gmail.com> | 2016-11-22 20:01:29 +0100 |
commit | 1cdec58024f260cfe92917aef783145b528663c6 (patch) | |
tree | e0b8e4b4d835272008a7bfcedeba3a74c2bc3cdb | |
parent | 2fa8409d1652b5f81c01d8ccb53bc30202249d95 (diff) |
ir: Rework how we discover children of modules.
-rw-r--r-- | libbindgen/src/codegen/mod.rs | 13 | ||||
-rw-r--r-- | libbindgen/src/ir/context.rs | 24 | ||||
-rw-r--r-- | libbindgen/src/ir/module.rs | 4 | ||||
-rw-r--r-- | libbindgen/src/lib.rs | 11 | ||||
-rw-r--r-- | libbindgen/tests/expectations/tests/anon_union.rs | 14 | ||||
-rw-r--r-- | libbindgen/tests/expectations/tests/crtp.rs | 24 | ||||
-rw-r--r-- | libbindgen/tests/expectations/tests/forward-inherit-struct-with-fields.rs | 10 | ||||
-rw-r--r-- | libbindgen/tests/expectations/tests/forward-inherit-struct.rs | 4 | ||||
-rw-r--r-- | libbindgen/tests/expectations/tests/vtable_recursive_sig.rs | 26 |
9 files changed, 63 insertions, 67 deletions
diff --git a/libbindgen/src/codegen/mod.rs b/libbindgen/src/codegen/mod.rs index ba52fa83..ad9ac2de 100644 --- a/libbindgen/src/codegen/mod.rs +++ b/libbindgen/src/codegen/mod.rs @@ -270,16 +270,11 @@ impl CodeGenerator for Module { debug!("<Module as CodeGenerator>::codegen: item = {:?}", item); let codegen_self = |result: &mut CodegenResult, found_any: &mut bool| { - // FIXME: This could be less expensive, I guess. - for &whitelisted_item in whitelisted_items { - if whitelisted_item == item.id() { - continue; - } - - let child = ctx.resolve_item(whitelisted_item); - if child.parent_id() == item.id() { + for child in self.children() { + if whitelisted_items.contains(child) { *found_any = true; - child.codegen(ctx, result, whitelisted_items, &()); + ctx.resolve_item(*child) + .codegen(ctx, result, whitelisted_items, &()); } } }; diff --git a/libbindgen/src/ir/context.rs b/libbindgen/src/ir/context.rs index e599160e..67db2a59 100644 --- a/libbindgen/src/ir/context.rs +++ b/libbindgen/src/ir/context.rs @@ -184,6 +184,17 @@ impl<'ctx> BindgenContext<'ctx> { let id = item.id(); let is_type = item.kind().is_type(); let is_unnamed = is_type && item.expect_type().name().is_none(); + + // Be sure to track all the generated children under namespace, even + // those generated after resolving typerefs, etc. + if item.id() != item.parent_id() { + if let Some(mut parent) = self.items.get_mut(&item.parent_id()) { + if let Some(mut module) = parent.as_module_mut() { + module.children_mut().push(item.id()); + } + } + } + let old_item = self.items.insert(id, item); assert!(old_item.is_none(), "Inserted type twice?"); @@ -898,23 +909,14 @@ impl<'ctx> BindgenContext<'ctx> { /// Start traversing the module with the given `module_id`, invoke the /// callback `cb`, and then return to traversing the original module. pub fn with_module<F>(&mut self, module_id: ItemId, cb: F) - where F: FnOnce(&mut Self, &mut Vec<ItemId>), + where F: FnOnce(&mut Self), { debug_assert!(self.resolve_item(module_id).kind().is_module(), "Wat"); let previous_id = self.current_module; self.current_module = module_id; - let mut children = vec![]; - cb(self, &mut children); - - self.items - .get_mut(&module_id) - .unwrap() - .as_module_mut() - .expect("Not a module?") - .children_mut() - .extend(children.into_iter()); + cb(self); self.current_module = previous_id; } diff --git a/libbindgen/src/ir/module.rs b/libbindgen/src/ir/module.rs index c5d8cfa7..7546a63c 100644 --- a/libbindgen/src/ir/module.rs +++ b/libbindgen/src/ir/module.rs @@ -47,9 +47,9 @@ impl ClangSubItemParser for Module { match cursor.kind() { CXCursor_Namespace => { let module_id = ctx.module(cursor); - ctx.with_module(module_id, |ctx, children| { + ctx.with_module(module_id, |ctx| { cursor.visit(|cursor| { - parse_one(ctx, cursor, Some(module_id), children) + parse_one(ctx, cursor, Some(module_id)) }) }); diff --git a/libbindgen/src/lib.rs b/libbindgen/src/lib.rs index 84c69310..10961e36 100644 --- a/libbindgen/src/lib.rs +++ b/libbindgen/src/lib.rs @@ -544,8 +544,7 @@ fn filter_builtins(ctx: &BindgenContext, cursor: &clang::Cursor) -> bool { /// Parse one `Item` from the Clang cursor. pub fn parse_one(ctx: &mut BindgenContext, cursor: clang::Cursor, - parent: Option<ItemId>, - children: &mut Vec<ItemId>) + parent: Option<ItemId>) -> clangll::Enum_CXVisitorResult { if !filter_builtins(ctx, &cursor) { return CXChildVisit_Continue; @@ -553,10 +552,10 @@ pub fn parse_one(ctx: &mut BindgenContext, use clangll::CXChildVisit_Continue; match Item::parse(cursor, parent, ctx) { - Ok(id) => children.push(id), + Ok(..) => {}, Err(ParseError::Continue) => {} Err(ParseError::Recurse) => { - cursor.visit(|child| parse_one(ctx, child, parent, children)); + cursor.visit(|child| parse_one(ctx, child, parent)); } } CXChildVisit_Continue @@ -579,8 +578,8 @@ fn parse(context: &mut BindgenContext) { } let root = context.root_module(); - context.with_module(root, |context, children| { - cursor.visit(|cursor| parse_one(context, cursor, None, children)) + context.with_module(root, |context| { + cursor.visit(|cursor| parse_one(context, cursor, None)) }); assert!(context.current_module() == context.root_module(), diff --git a/libbindgen/tests/expectations/tests/anon_union.rs b/libbindgen/tests/expectations/tests/anon_union.rs index 915dfbd5..db03e177 100644 --- a/libbindgen/tests/expectations/tests/anon_union.rs +++ b/libbindgen/tests/expectations/tests/anon_union.rs @@ -62,6 +62,13 @@ pub struct TErrorResult__bindgen_ty_1<T> { pub bindgen_union_field: u64, pub _phantom_0: ::std::marker::PhantomData<T>, } +#[test] +fn __bindgen_test_layout_template_17() { + assert_eq!(::std::mem::size_of::<TErrorResult<::std::os::raw::c_int>>() , + 24usize); + assert_eq!(::std::mem::align_of::<TErrorResult<::std::os::raw::c_int>>() , + 8usize); +} #[repr(C)] #[derive(Debug, Copy)] pub struct ErrorResult { @@ -75,10 +82,3 @@ fn bindgen_test_layout_ErrorResult() { impl Clone for ErrorResult { fn clone(&self) -> Self { *self } } -#[test] -fn __bindgen_test_layout_template_17() { - assert_eq!(::std::mem::size_of::<TErrorResult<::std::os::raw::c_int>>() , - 24usize); - assert_eq!(::std::mem::align_of::<TErrorResult<::std::os::raw::c_int>>() , - 8usize); -} diff --git a/libbindgen/tests/expectations/tests/crtp.rs b/libbindgen/tests/expectations/tests/crtp.rs index a50e05e4..e21c5de2 100644 --- a/libbindgen/tests/expectations/tests/crtp.rs +++ b/libbindgen/tests/expectations/tests/crtp.rs @@ -10,6 +10,11 @@ pub struct Base<T> { pub _address: u8, pub _phantom_0: ::std::marker::PhantomData<T>, } +#[test] +fn __bindgen_test_layout_template_5() { + assert_eq!(::std::mem::size_of::<Base<Derived>>() , 1usize); + assert_eq!(::std::mem::align_of::<Base<Derived>>() , 1usize); +} #[repr(C)] #[derive(Debug, Copy)] pub struct Derived { @@ -23,17 +28,19 @@ fn bindgen_test_layout_Derived() { impl Clone for Derived { fn clone(&self) -> Self { *self } } -#[test] -fn __bindgen_test_layout_template_5() { - assert_eq!(::std::mem::size_of::<Base<Derived>>() , 1usize); - assert_eq!(::std::mem::align_of::<Base<Derived>>() , 1usize); -} #[repr(C)] #[derive(Debug)] pub struct BaseWithDestructor<T> { pub _address: u8, pub _phantom_0: ::std::marker::PhantomData<T>, } +#[test] +fn __bindgen_test_layout_template_12() { + assert_eq!(::std::mem::size_of::<BaseWithDestructor<DerivedFromBaseWithDestructor>>() + , 1usize); + assert_eq!(::std::mem::align_of::<BaseWithDestructor<DerivedFromBaseWithDestructor>>() + , 1usize); +} #[repr(C)] #[derive(Debug)] pub struct DerivedFromBaseWithDestructor { @@ -46,10 +53,3 @@ fn bindgen_test_layout_DerivedFromBaseWithDestructor() { assert_eq!(::std::mem::align_of::<DerivedFromBaseWithDestructor>() , 1usize); } -#[test] -fn __bindgen_test_layout_template_12() { - assert_eq!(::std::mem::size_of::<BaseWithDestructor<DerivedFromBaseWithDestructor>>() - , 1usize); - assert_eq!(::std::mem::align_of::<BaseWithDestructor<DerivedFromBaseWithDestructor>>() - , 1usize); -} diff --git a/libbindgen/tests/expectations/tests/forward-inherit-struct-with-fields.rs b/libbindgen/tests/expectations/tests/forward-inherit-struct-with-fields.rs index 84104971..fc24e989 100644 --- a/libbindgen/tests/expectations/tests/forward-inherit-struct-with-fields.rs +++ b/libbindgen/tests/expectations/tests/forward-inherit-struct-with-fields.rs @@ -6,12 +6,12 @@ #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct Rooted<T> { - pub _base: RootedBase<T>, -} -#[repr(C)] -#[derive(Debug, Copy, Clone)] pub struct RootedBase<T> { pub foo: *mut T, pub next: *mut Rooted<T>, } +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct Rooted<T> { + pub _base: RootedBase<T>, +} diff --git a/libbindgen/tests/expectations/tests/forward-inherit-struct.rs b/libbindgen/tests/expectations/tests/forward-inherit-struct.rs index e053adcd..a58058b0 100644 --- a/libbindgen/tests/expectations/tests/forward-inherit-struct.rs +++ b/libbindgen/tests/expectations/tests/forward-inherit-struct.rs @@ -6,13 +6,13 @@ #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct Rooted<T> { +pub struct RootedBase<T> { pub _address: u8, pub _phantom_0: ::std::marker::PhantomData<T>, } #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct RootedBase<T> { +pub struct Rooted<T> { pub _address: u8, pub _phantom_0: ::std::marker::PhantomData<T>, } diff --git a/libbindgen/tests/expectations/tests/vtable_recursive_sig.rs b/libbindgen/tests/expectations/tests/vtable_recursive_sig.rs index ce62eeb0..77312336 100644 --- a/libbindgen/tests/expectations/tests/vtable_recursive_sig.rs +++ b/libbindgen/tests/expectations/tests/vtable_recursive_sig.rs @@ -5,19 +5,6 @@ #[repr(C)] -#[derive(Debug, Copy)] -pub struct Derived { - pub _base: Base, -} -#[test] -fn bindgen_test_layout_Derived() { - assert_eq!(::std::mem::size_of::<Derived>() , 8usize); - assert_eq!(::std::mem::align_of::<Derived>() , 8usize); -} -impl Clone for Derived { - fn clone(&self) -> Self { *self } -} -#[repr(C)] pub struct Base__bindgen_vtable { } #[repr(C)] @@ -33,3 +20,16 @@ fn bindgen_test_layout_Base() { impl Clone for Base { fn clone(&self) -> Self { *self } } +#[repr(C)] +#[derive(Debug, Copy)] +pub struct Derived { + pub _base: Base, +} +#[test] +fn bindgen_test_layout_Derived() { + assert_eq!(::std::mem::size_of::<Derived>() , 8usize); + assert_eq!(::std::mem::align_of::<Derived>() , 8usize); +} +impl Clone for Derived { + fn clone(&self) -> Self { *self } +} |