Skip to content

Commit

Permalink
Auto merge of #1294 - marty-se:master, r=fitzgen
Browse files Browse the repository at this point in the history
Whitelist inner types of whitelisted types even with no-recursive-whitelisting.

This fixes issue #1285 where inner types were code generated but not properly processed by the derive analysis.
  • Loading branch information
bors-servo authored Apr 6, 2018
2 parents 81caf12 + e6516e3 commit 0b4f5be
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2316,7 +2316,11 @@ impl BindgenContext {
if self.options().whitelist_recursively {
traversal::all_edges
} else {
traversal::no_edges
// Only follow InnerType edges from the whitelisted roots.
// Such inner types (e.g. anonymous structs/unions) are
// always emitted by codegen, and they need to be whitelisted
// to make sure they are processed by e.g. the derive analysis.
traversal::only_inner_type_edges
};

let whitelisted = WhitelistedItemsTraversal::new(
Expand Down
12 changes: 7 additions & 5 deletions src/ir/traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,13 @@ pub fn all_edges(_: &BindgenContext, _: Edge) -> bool {
true
}

/// A `TraversalPredicate` implementation that never follows any edges, and
/// therefore traversals using this predicate will only visit the traversal's
/// roots.
pub fn no_edges(_: &BindgenContext, _: Edge) -> bool {
false
/// A `TraversalPredicate` implementation that only follows
/// `EdgeKind::InnerType` edges, and therefore traversals using this predicate
/// will only visit the traversal's roots and their inner types. This is used
/// in no-recursive-whitelist mode, where inner types such as anonymous
/// structs/unions still need to be processed.
pub fn only_inner_type_edges(_: &BindgenContext, edge: Edge) -> bool {
edge.kind == EdgeKind::InnerType
}

/// A `TraversalPredicate` implementation that only follows edges to items that
Expand Down
75 changes: 75 additions & 0 deletions tests/expectations/tests/issue-1285.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* automatically generated by rust-bindgen */

#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]

#[repr(C)]
pub struct foo {
pub bar: foo__bindgen_ty_1,
}
#[repr(C)]
pub union foo__bindgen_ty_1 {
pub a: ::std::os::raw::c_uint,
pub b: ::std::os::raw::c_ushort,
_bindgen_union_align: u32,
}
#[test]
fn bindgen_test_layout_foo__bindgen_ty_1() {
assert_eq!(
::std::mem::size_of::<foo__bindgen_ty_1>(),
4usize,
concat!("Size of: ", stringify!(foo__bindgen_ty_1))
);
assert_eq!(
::std::mem::align_of::<foo__bindgen_ty_1>(),
4usize,
concat!("Alignment of ", stringify!(foo__bindgen_ty_1))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<foo__bindgen_ty_1>())).a as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(foo__bindgen_ty_1),
"::",
stringify!(a)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<foo__bindgen_ty_1>())).b as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(foo__bindgen_ty_1),
"::",
stringify!(b)
)
);
}
impl Default for foo__bindgen_ty_1 {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
}
}
#[test]
fn bindgen_test_layout_foo() {
assert_eq!(
::std::mem::size_of::<foo>(),
4usize,
concat!("Size of: ", stringify!(foo))
);
assert_eq!(
::std::mem::align_of::<foo>(),
4usize,
concat!("Alignment of ", stringify!(foo))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<foo>())).bar as *const _ as usize },
0usize,
concat!("Offset of field: ", stringify!(foo), "::", stringify!(bar))
);
}
impl Default for foo {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
}
}
8 changes: 8 additions & 0 deletions tests/headers/issue-1285.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// bindgen-flags: --with-derive-hash --no-recursive-whitelist --whitelist-type "foo"

struct foo {
union {
unsigned int a;
unsigned short b;
} bar;
};

0 comments on commit 0b4f5be

Please sign in to comment.