Skip to content

Handle bitfield enum pattern aliasing #1277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
68 changes: 68 additions & 0 deletions tests/expectations/tests/issue-1198-alias-rust-bitfield-enum.rs
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -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;
}
13 changes: 13 additions & 0 deletions tests/headers/issue-1198-alias-rust-bitfield-enum.h
Original file line number Diff line number Diff line change
@@ -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,
};
13 changes: 13 additions & 0 deletions tests/headers/issue-1198-alias-rust-const-mod-bitfield-enum.h
Original file line number Diff line number Diff line change
@@ -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,
};