Skip to content

Commit 814d28e

Browse files
committed
Simplify is_constified_enum_module
Used suggested code from @emilio and also added a test for an alias to an anonymous enum.
1 parent 465e242 commit 814d28e

File tree

3 files changed

+70
-37
lines changed

3 files changed

+70
-37
lines changed

src/ir/item.rs

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -829,46 +829,32 @@ impl Item {
829829

830830
/// Returns whether the item is a constified module enum
831831
fn is_constified_enum_module(&self, ctx: &BindgenContext) -> bool {
832-
if let ItemKind::Type(ref type_) = self.kind {
833-
// Do not count an "alias of an alias" as a constified module enum;
834-
// otherwise, we will get:
835-
// pub mod foo {
836-
// pub type Type = ::std::os::raw::c_uint;
837-
// ...
838-
// }
839-
// pub use foo::Type as foo_alias1;
840-
// pub use foo_alias1::Type as foo_alias2;
841-
// pub use foo_alias2::Type as foo_alias3;
842-
// ...
843-
//
844-
// (We do not want the '::Type' appended to the alias types; only the base type)
845-
if let TypeKind::Alias(inner_id) = *type_.kind() {
846-
let inner_item = ctx.resolve_item(inner_id);
847-
if let ItemKind::Type(ref inner_type) = *inner_item.kind() {
848-
match *inner_type.kind() {
849-
TypeKind::Alias(..) => { return false; }
850-
TypeKind::ResolvedTypeRef(resolved_id) => {
851-
// We need to handle:
852-
// Alias -> ResolvedTypeRef -> Alias
853-
let resolved_item = ctx.resolve_item(resolved_id);
854-
if let ItemKind::Type(ref resolved_type) = *resolved_item.kind() {
855-
if let TypeKind::Alias(..) = *resolved_type.kind() {
856-
return false;
857-
}
858-
}
859-
}
860-
_ => (),
861-
}
862-
}
832+
// Do not jump through aliases, except for aliases that point to a type
833+
// with the same name, since we dont generate coe for them.
834+
let item = self.id.into_resolver().through_type_refs().resolve(ctx);
835+
let type_ = match *item.kind() {
836+
ItemKind::Type(ref type_) => type_,
837+
_ => return false,
838+
};
839+
840+
match *type_.kind() {
841+
TypeKind::Enum(ref enum_) => {
842+
enum_.is_constified_enum_module(ctx, self)
863843
}
864-
if let Some(ref type_) = type_.safe_canonical_type(ctx) {
865-
if let TypeKind::Enum(ref enum_) = *type_.kind() {
866-
return enum_.is_constified_enum_module(ctx, self);
844+
TypeKind::Alias(inner_id) => {
845+
// TODO(emilio): Make this "hop through type aliases that aren't
846+
// really generated" an option in `ItemResolver`?
847+
let inner_item = ctx.resolve_item(inner_id);
848+
let name = item.canonical_name(ctx);
849+
850+
if inner_item.canonical_name(ctx) == name {
851+
inner_item.is_constified_enum_module(ctx)
852+
} else {
853+
false
867854
}
868855
}
856+
_ => false,
869857
}
870-
871-
return false;
872858
}
873859
}
874860

tests/expectations/tests/constify-module-enums-types.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ pub mod foo {
1212
pub const ALSO_THIS: Type = 42;
1313
pub const AND_ALSO_THIS: Type = 42;
1414
}
15+
pub mod anon_enum {
16+
pub type Type = ::std::os::raw::c_uint;
17+
pub const Variant1: Type = 0;
18+
pub const Variant2: Type = 1;
19+
pub const Variant3: Type = 2;
20+
}
1521
pub mod ns1_foo {
1622
pub type Type = ::std::os::raw::c_uint;
1723
pub const THIS: Type = 0;
@@ -27,6 +33,9 @@ pub mod ns2_Foo {
2733
pub use self::foo::Type as foo_alias1;
2834
pub use self::foo_alias1 as foo_alias2;
2935
pub use self::foo_alias2 as foo_alias3;
36+
pub use self::anon_enum::Type as anon_enum_alias1;
37+
pub use self::anon_enum_alias1 as anon_enum_alias2;
38+
pub use self::anon_enum_alias2 as anon_enum_alias3;
3039
#[repr(C)]
3140
#[derive(Debug, Copy)]
3241
pub struct bar {
@@ -36,10 +45,14 @@ pub struct bar {
3645
pub member4: foo_alias3,
3746
pub member5: ns1_foo::Type,
3847
pub member6: *mut ns2_Foo::Type,
48+
pub member7: anon_enum::Type,
49+
pub member8: anon_enum_alias1,
50+
pub member9: anon_enum_alias2,
51+
pub member10: anon_enum_alias3,
3952
}
4053
#[test]
4154
fn bindgen_test_layout_bar() {
42-
assert_eq!(::std::mem::size_of::<bar>() , 32usize , concat ! (
55+
assert_eq!(::std::mem::size_of::<bar>() , 48usize , concat ! (
4356
"Size of: " , stringify ! ( bar ) ));
4457
assert_eq! (::std::mem::align_of::<bar>() , 8usize , concat ! (
4558
"Alignment of " , stringify ! ( bar ) ));
@@ -73,6 +86,26 @@ fn bindgen_test_layout_bar() {
7386
, 24usize , concat ! (
7487
"Alignment of field: " , stringify ! ( bar ) , "::" ,
7588
stringify ! ( member6 ) ));
89+
assert_eq! (unsafe {
90+
& ( * ( 0 as * const bar ) ) . member7 as * const _ as usize }
91+
, 32usize , concat ! (
92+
"Alignment of field: " , stringify ! ( bar ) , "::" ,
93+
stringify ! ( member7 ) ));
94+
assert_eq! (unsafe {
95+
& ( * ( 0 as * const bar ) ) . member8 as * const _ as usize }
96+
, 36usize , concat ! (
97+
"Alignment of field: " , stringify ! ( bar ) , "::" ,
98+
stringify ! ( member8 ) ));
99+
assert_eq! (unsafe {
100+
& ( * ( 0 as * const bar ) ) . member9 as * const _ as usize }
101+
, 40usize , concat ! (
102+
"Alignment of field: " , stringify ! ( bar ) , "::" ,
103+
stringify ! ( member9 ) ));
104+
assert_eq! (unsafe {
105+
& ( * ( 0 as * const bar ) ) . member10 as * const _ as usize
106+
} , 44usize , concat ! (
107+
"Alignment of field: " , stringify ! ( bar ) , "::" ,
108+
stringify ! ( member10 ) ));
76109
}
77110
impl Clone for bar {
78111
fn clone(&self) -> Self { *self }

tests/headers/constify-module-enums-types.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ typedef enum foo {
88
AND_ALSO_THIS = 42,
99
} foo;
1010

11+
12+
typedef enum {
13+
Variant1, Variant2, Variant3,
14+
} anon_enum;
15+
16+
1117
namespace ns1 {
1218
typedef enum {
1319
THIS,
@@ -28,13 +34,21 @@ typedef foo foo_alias1;
2834
typedef foo_alias1 foo_alias2;
2935
typedef foo_alias2 foo_alias3;
3036

37+
typedef anon_enum anon_enum_alias1;
38+
typedef anon_enum_alias1 anon_enum_alias2;
39+
typedef anon_enum_alias2 anon_enum_alias3;
40+
3141
typedef struct bar {
3242
foo member1;
3343
foo_alias1 member2;
3444
foo_alias2 member3;
3545
foo_alias3 member4;
3646
ns1::foo member5;
3747
ns2::Foo *member6;
48+
anon_enum member7;
49+
anon_enum_alias1 member8;
50+
anon_enum_alias2 member9;
51+
anon_enum_alias3 member10;
3852
} bar;
3953

4054
class Baz {

0 commit comments

Comments
 (0)