summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTravis Finkenauer <tmfinken@gmail.com>2018-03-17 16:55:53 -0400
committerTravis Finkenauer <tmfinken@gmail.com>2018-03-17 17:15:36 -0400
commitc0c1dcafe0b470992ae3624bddb6fa102680284b (patch)
treec69b21fc8f6eb4529e2b62c4d9ebe4dc10d269d8
parent22041e13d08dbd4cb0ea777fa540897735d9e7c2 (diff)
Handle bitfield enum pattern aliasing
The previous fix for issue #1198 was incomplete.
-rw-r--r--src/codegen/mod.rs6
-rw-r--r--src/lib.rs8
-rw-r--r--tests/expectations/tests/issue-1198-alias-rust-bitfield-enum.rs68
-rw-r--r--tests/expectations/tests/issue-1198-alias-rust-const-mod-bitfield-enum.rs16
-rw-r--r--tests/headers/issue-1198-alias-rust-bitfield-enum.h13
-rw-r--r--tests/headers/issue-1198-alias-rust-const-mod-bitfield-enum.h13
6 files changed, 117 insertions, 7 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs
index cf1a4f3b..5e104360 100644
--- a/src/codegen/mod.rs
+++ b/src/codegen/mod.rs
@@ -2479,10 +2479,10 @@ impl CodeGenerator for Enum {
// ModuleConsts has higher precedence before Rust in order to avoid problems with
// overlapping match patterns
- let variation = if self.is_bitfield(ctx, item) {
- EnumVariation::Bitfield
- } else if self.is_constified_enum_module(ctx, item) {
+ let variation = if self.is_constified_enum_module(ctx, item) {
EnumVariation::ModuleConsts
+ } else if self.is_bitfield(ctx, item) {
+ EnumVariation::Bitfield
} else if self.is_rustified_enum(ctx, item) {
EnumVariation::Rust
} else {
diff --git a/src/lib.rs b/src/lib.rs
index 36378316..f12df674 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -161,14 +161,14 @@ impl Default for CodegenConfig {
/// Bindgen can map C/C++ enums into Rust in different ways. The way bindgen maps enums depends on
/// the pattern passed to several methods:
///
-/// 1. [`bitfield_enum()`](#method.bitfield_enum)
-/// 2. [`constified_enum_module()`](#method.constified_enum_module)
+/// 1. [`constified_enum_module()`](#method.constified_enum_module)
+/// 2. [`bitfield_enum()`](#method.bitfield_enum)
/// 3. [`rustified_enum()`](#method.rustified_enum)
///
/// For each C enum, bindgen tries to match the pattern in the following order:
///
-/// 1. Bitfield enum
-/// 2. Constified enum module
+/// 1. Constified enum module
+/// 2. Bitfield enum
/// 3. Rustified enum
///
/// If none of the above patterns match, then bindgen will generate a set of Rust constants.
diff --git a/tests/expectations/tests/issue-1198-alias-rust-bitfield-enum.rs b/tests/expectations/tests/issue-1198-alias-rust-bitfield-enum.rs
new file mode 100644
index 00000000..aa836077
--- /dev/null
+++ b/tests/expectations/tests/issue-1198-alias-rust-bitfield-enum.rs
@@ -0,0 +1,68 @@
+/* automatically generated by rust-bindgen */
+
+#![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 ::std::ops::BitOr<MyDupeEnum> for MyDupeEnum {
+ type Output = Self;
+ #[inline]
+ fn bitor(self, other: Self) -> Self {
+ MyDupeEnum(self.0 | other.0)
+ }
+}
+impl ::std::ops::BitOrAssign for MyDupeEnum {
+ #[inline]
+ fn bitor_assign(&mut self, rhs: MyDupeEnum) {
+ self.0 |= rhs.0;
+ }
+}
+impl ::std::ops::BitAnd<MyDupeEnum> for MyDupeEnum {
+ type Output = Self;
+ #[inline]
+ fn bitand(self, other: Self) -> Self {
+ MyDupeEnum(self.0 & other.0)
+ }
+}
+impl ::std::ops::BitAndAssign for MyDupeEnum {
+ #[inline]
+ fn bitand_assign(&mut self, rhs: MyDupeEnum) {
+ self.0 &= rhs.0;
+ }
+}
+#[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 ::std::ops::BitOr<MyOtherDupeEnum> for MyOtherDupeEnum {
+ type Output = Self;
+ #[inline]
+ fn bitor(self, other: Self) -> Self {
+ MyOtherDupeEnum(self.0 | other.0)
+ }
+}
+impl ::std::ops::BitOrAssign for MyOtherDupeEnum {
+ #[inline]
+ fn bitor_assign(&mut self, rhs: MyOtherDupeEnum) {
+ self.0 |= rhs.0;
+ }
+}
+impl ::std::ops::BitAnd<MyOtherDupeEnum> for MyOtherDupeEnum {
+ type Output = Self;
+ #[inline]
+ fn bitand(self, other: Self) -> Self {
+ MyOtherDupeEnum(self.0 & other.0)
+ }
+}
+impl ::std::ops::BitAndAssign for MyOtherDupeEnum {
+ #[inline]
+ fn bitand_assign(&mut self, rhs: MyOtherDupeEnum) {
+ self.0 &= rhs.0;
+ }
+}
+#[repr(C)]
+#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
+pub struct MyOtherDupeEnum(pub u32);
diff --git a/tests/expectations/tests/issue-1198-alias-rust-const-mod-bitfield-enum.rs b/tests/expectations/tests/issue-1198-alias-rust-const-mod-bitfield-enum.rs
new file mode 100644
index 00000000..ed53fdf9
--- /dev/null
+++ b/tests/expectations/tests/issue-1198-alias-rust-const-mod-bitfield-enum.rs
@@ -0,0 +1,16 @@
+/* automatically generated by rust-bindgen */
+
+#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
+
+pub mod MyDupeEnum {
+ pub type Type = u32;
+ pub const A: Type = 0;
+ pub const A_alias: Type = 0;
+ pub const B: Type = 1;
+}
+pub mod MyOtherDupeEnum {
+ pub type Type = u32;
+ pub const C: Type = 0;
+ pub const C_alias: Type = 0;
+ pub const D: Type = 1;
+}
diff --git a/tests/headers/issue-1198-alias-rust-bitfield-enum.h b/tests/headers/issue-1198-alias-rust-bitfield-enum.h
new file mode 100644
index 00000000..5bccb0d4
--- /dev/null
+++ b/tests/headers/issue-1198-alias-rust-bitfield-enum.h
@@ -0,0 +1,13 @@
+// bindgen-flags: --rustified-enum '.*' --bitfield-enum '.*'
+
+typedef enum MyDupeEnum {
+ A = 0,
+ A_alias = 0,
+ B,
+} MyDupeEnum;
+
+enum MyOtherDupeEnum {
+ C = 0,
+ C_alias = 0,
+ D,
+};
diff --git a/tests/headers/issue-1198-alias-rust-const-mod-bitfield-enum.h b/tests/headers/issue-1198-alias-rust-const-mod-bitfield-enum.h
new file mode 100644
index 00000000..ecdf8c3f
--- /dev/null
+++ b/tests/headers/issue-1198-alias-rust-const-mod-bitfield-enum.h
@@ -0,0 +1,13 @@
+// bindgen-flags: --rustified-enum '.*' --constified-enum-module '.*' --bitfield-enum '.*'
+
+typedef enum MyDupeEnum {
+ A = 0,
+ A_alias = 0,
+ B,
+} MyDupeEnum;
+
+enum MyOtherDupeEnum {
+ C = 0,
+ C_alias = 0,
+ D,
+};