summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/codegen/mod.rs40
-rw-r--r--src/features.rs8
-rw-r--r--src/ir/context.rs6
-rw-r--r--src/ir/traversal.rs12
-rw-r--r--tests/expectations/tests/bitfield-enum-basic.rs32
-rw-r--r--tests/expectations/tests/enum-doc-bitfield.rs42
-rw-r--r--tests/expectations/tests/issue-1198-alias-rust-bitfield-enum.rs24
-rw-r--r--tests/expectations/tests/issue-1285.rs75
-rw-r--r--tests/headers/issue-1285.h8
9 files changed, 198 insertions, 49 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs
index 63e1e11e..6483412e 100644
--- a/src/codegen/mod.rs
+++ b/src/codegen/mod.rs
@@ -2127,7 +2127,7 @@ impl EnumVariation {
fn is_bitfield(&self) -> bool {
match *self {
- EnumVariation::Bitfield => true,
+ EnumVariation::Bitfield {..} => true,
_ => false
}
}
@@ -2248,6 +2248,7 @@ impl<'a> EnumBuilder<'a> {
mangling_prefix: Option<&str>,
rust_ty: quote::Tokens,
result: &mut CodegenResult<'b>,
+ is_ty_named: bool,
) -> Self {
let variant_name = ctx.rust_mangle(variant.name());
let expr = match variant.val() {
@@ -2279,19 +2280,28 @@ impl<'a> EnumBuilder<'a> {
}
}
- EnumBuilder::Bitfield { .. } => {
- let constant_name = match mangling_prefix {
- Some(prefix) => {
- Cow::Owned(format!("{}_{}", prefix, variant_name))
- }
- None => variant_name,
- };
-
- let ident = ctx.rust_ident(constant_name);
- result.push(quote! {
- #doc
- pub const #ident : #rust_ty = #rust_ty ( #expr );
- });
+ EnumBuilder::Bitfield { canonical_name, .. } => {
+ if ctx.options().rust_features().associated_const && is_ty_named {
+ let enum_ident = ctx.rust_ident(canonical_name);
+ let variant_ident = ctx.rust_ident(variant_name);
+ result.push(quote! {
+ impl #enum_ident {
+ #doc
+ pub const #variant_ident : #rust_ty = #rust_ty ( #expr );
+ }
+ });
+ } else {
+ let ident = ctx.rust_ident(match mangling_prefix {
+ Some(prefix) => {
+ Cow::Owned(format!("{}_{}", prefix, variant_name))
+ }
+ None => variant_name,
+ });
+ result.push(quote! {
+ #doc
+ pub const #ident : #rust_ty = #rust_ty ( #expr );
+ });
+ }
self
}
@@ -2626,6 +2636,7 @@ impl CodeGenerator for Enum {
constant_mangling_prefix,
enum_rust_ty.clone(),
result,
+ enum_ty.name().is_some(),
);
}
}
@@ -2636,6 +2647,7 @@ impl CodeGenerator for Enum {
constant_mangling_prefix,
enum_rust_ty.clone(),
result,
+ enum_ty.name().is_some(),
);
let variant_name = ctx.rust_ident(variant.name());
diff --git a/src/features.rs b/src/features.rs
index 866d7126..93ebbc34 100644
--- a/src/features.rs
+++ b/src/features.rs
@@ -90,6 +90,8 @@ macro_rules! rust_target_base {
=> Stable_1_0 => 1.0;
/// Rust stable 1.19
=> Stable_1_19 => 1.19;
+ /// Rust stable 1.20
+ => Stable_1_20 => 1.20;
/// Rust stable 1.21
=> Stable_1_21 => 1.21;
/// Rust stable 1.25
@@ -142,6 +144,8 @@ rust_feature_def!(
=> builtin_clone_impls;
/// repr(align) https://github.com/rust-lang/rust/pull/47006
=> repr_align;
+ /// associated constants https://github.com/rust-lang/rust/issues/29646
+ => associated_const;
);
impl From<RustTarget> for RustFeatures {
@@ -152,6 +156,10 @@ impl From<RustTarget> for RustFeatures {
features.untagged_union = true;
}
+ if rust_target >= RustTarget::Stable_1_20 {
+ features.associated_const = true;
+ }
+
if rust_target >= RustTarget::Stable_1_21 {
features.builtin_clone_impls = true;
}
diff --git a/src/ir/context.rs b/src/ir/context.rs
index 6ced43e8..58a90ba6 100644
--- a/src/ir/context.rs
+++ b/src/ir/context.rs
@@ -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(
diff --git a/src/ir/traversal.rs b/src/ir/traversal.rs
index 491a15f8..c37b0b5f 100644
--- a/src/ir/traversal.rs
+++ b/src/ir/traversal.rs
@@ -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
diff --git a/tests/expectations/tests/bitfield-enum-basic.rs b/tests/expectations/tests/bitfield-enum-basic.rs
index 091dc70b..a8140b2e 100644
--- a/tests/expectations/tests/bitfield-enum-basic.rs
+++ b/tests/expectations/tests/bitfield-enum-basic.rs
@@ -2,10 +2,18 @@
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
-pub const Foo_Bar: Foo = Foo(2);
-pub const Foo_Baz: Foo = Foo(4);
-pub const Foo_Duplicated: Foo = Foo(4);
-pub const Foo_Negative: Foo = Foo(-3);
+impl Foo {
+ pub const Bar: Foo = Foo(2);
+}
+impl Foo {
+ pub const Baz: Foo = Foo(4);
+}
+impl Foo {
+ pub const Duplicated: Foo = Foo(4);
+}
+impl Foo {
+ pub const Negative: Foo = Foo(-3);
+}
impl ::std::ops::BitOr<Foo> for Foo {
type Output = Self;
#[inline]
@@ -35,10 +43,18 @@ impl ::std::ops::BitAndAssign for Foo {
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Foo(pub i32);
-pub const Buz_Bar: Buz = Buz(2);
-pub const Buz_Baz: Buz = Buz(4);
-pub const Buz_Duplicated: Buz = Buz(4);
-pub const Buz_Negative: Buz = Buz(-3);
+impl Buz {
+ pub const Bar: Buz = Buz(2);
+}
+impl Buz {
+ pub const Baz: Buz = Buz(4);
+}
+impl Buz {
+ pub const Duplicated: Buz = Buz(4);
+}
+impl Buz {
+ pub const Negative: Buz = Buz(-3);
+}
impl ::std::ops::BitOr<Buz> for Buz {
type Output = Self;
#[inline]
diff --git a/tests/expectations/tests/enum-doc-bitfield.rs b/tests/expectations/tests/enum-doc-bitfield.rs
index 081b39ec..93d1f5f0 100644
--- a/tests/expectations/tests/enum-doc-bitfield.rs
+++ b/tests/expectations/tests/enum-doc-bitfield.rs
@@ -2,21 +2,33 @@
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
-/// Document field with three slashes
-pub const B_VAR_A: B = B(0);
-/// Document field with preceeding star
-pub const B_VAR_B: B = B(1);
-/// Document field with preceeding exclamation
-pub const B_VAR_C: B = B(2);
-/// < Document field with following star
-pub const B_VAR_D: B = B(3);
-/// < Document field with following exclamation
-pub const B_VAR_E: B = B(4);
-/// Document field with preceeding star, with a loong long multiline
-/// comment.
-///
-/// Very interesting documentation, definitely.
-pub const B_VAR_F: B = B(5);
+impl B {
+ /// Document field with three slashes
+ pub const VAR_A: B = B(0);
+}
+impl B {
+ /// Document field with preceeding star
+ pub const VAR_B: B = B(1);
+}
+impl B {
+ /// Document field with preceeding exclamation
+ pub const VAR_C: B = B(2);
+}
+impl B {
+ /// < Document field with following star
+ pub const VAR_D: B = B(3);
+}
+impl B {
+ /// < Document field with following exclamation
+ pub const VAR_E: B = B(4);
+}
+impl B {
+ /// Document field with preceeding star, with a loong long multiline
+ /// comment.
+ ///
+ /// Very interesting documentation, definitely.
+ pub const VAR_F: B = B(5);
+}
impl ::std::ops::BitOr<B> for B {
type Output = Self;
#[inline]
diff --git a/tests/expectations/tests/issue-1198-alias-rust-bitfield-enum.rs b/tests/expectations/tests/issue-1198-alias-rust-bitfield-enum.rs
index aa836077..8c70625e 100644
--- a/tests/expectations/tests/issue-1198-alias-rust-bitfield-enum.rs
+++ b/tests/expectations/tests/issue-1198-alias-rust-bitfield-enum.rs
@@ -2,9 +2,15 @@
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
-pub const MyDupeEnum_A: MyDupeEnum = MyDupeEnum(0);
-pub const MyDupeEnum_A_alias: MyDupeEnum = MyDupeEnum(0);
-pub const MyDupeEnum_B: MyDupeEnum = MyDupeEnum(1);
+impl MyDupeEnum {
+ pub const A: MyDupeEnum = MyDupeEnum(0);
+}
+impl MyDupeEnum {
+ pub const A_alias: MyDupeEnum = MyDupeEnum(0);
+}
+impl MyDupeEnum {
+ pub const B: MyDupeEnum = MyDupeEnum(1);
+}
impl ::std::ops::BitOr<MyDupeEnum> for MyDupeEnum {
type Output = Self;
#[inline]
@@ -34,9 +40,15 @@ impl ::std::ops::BitAndAssign for MyDupeEnum {
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct MyDupeEnum(pub u32);
-pub const MyOtherDupeEnum_C: MyOtherDupeEnum = MyOtherDupeEnum(0);
-pub const MyOtherDupeEnum_C_alias: MyOtherDupeEnum = MyOtherDupeEnum(0);
-pub const MyOtherDupeEnum_D: MyOtherDupeEnum = MyOtherDupeEnum(1);
+impl MyOtherDupeEnum {
+ pub const C: MyOtherDupeEnum = MyOtherDupeEnum(0);
+}
+impl MyOtherDupeEnum {
+ pub const C_alias: MyOtherDupeEnum = MyOtherDupeEnum(0);
+}
+impl MyOtherDupeEnum {
+ pub const D: MyOtherDupeEnum = MyOtherDupeEnum(1);
+}
impl ::std::ops::BitOr<MyOtherDupeEnum> for MyOtherDupeEnum {
type Output = Self;
#[inline]
diff --git a/tests/expectations/tests/issue-1285.rs b/tests/expectations/tests/issue-1285.rs
new file mode 100644
index 00000000..7b2c0ae9
--- /dev/null
+++ b/tests/expectations/tests/issue-1285.rs
@@ -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() }
+ }
+}
diff --git a/tests/headers/issue-1285.h b/tests/headers/issue-1285.h
new file mode 100644
index 00000000..7fb742ec
--- /dev/null
+++ b/tests/headers/issue-1285.h
@@ -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;
+}; \ No newline at end of file