summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTravis Finkenauer <tmfinken@gmail.com>2017-06-14 01:30:33 -0700
committerTravis Finkenauer <tmfinken@gmail.com>2017-06-14 01:30:33 -0700
commitfb9959a647d446a6433b669215bda42c1da70cad (patch)
tree542ff4a1093e6ad7e9608fac14ffb120b3fa76a6
parent08268464ac245f7ba78767971f0ae5c523bda53d (diff)
Add typedef/namespace tests for module const enum
-rw-r--r--src/ir/item.rs23
-rw-r--r--tests/expectations/tests/constify-module-enums-namespace.rs52
-rw-r--r--tests/expectations/tests/constify-module-enums-typedef.rs51
-rw-r--r--tests/headers/constify-module-enums-namespace.hpp17
-rw-r--r--tests/headers/constify-module-enums-typedef.h18
5 files changed, 154 insertions, 7 deletions
diff --git a/src/ir/item.rs b/src/ir/item.rs
index 735b3390..61b70673 100644
--- a/src/ir/item.rs
+++ b/src/ir/item.rs
@@ -1444,19 +1444,28 @@ impl ItemCanonicalPath for Item {
fn namespace_aware_canonical_path(&self,
ctx: &BindgenContext)
-> Vec<String> {
- let path = self.canonical_path(ctx);
- if let super::item_kind::ItemKind::Type(ref type_) = self.kind {
- if let &TypeKind::Enum(ref enum_) = type_.kind() {
- if enum_.is_constified_enum_module(ctx, self) {
- // Type alias is inside a module
- return vec![path.last().unwrap().clone(),
- CONSTIFIED_ENUM_MODULE_REPR_NAME.into()];
+ let mut path = self.canonical_path(ctx);
+ let mut is_constified_module_enum = false;
+ if let ItemKind::Type(ref type_) = self.kind {
+ if let Some(ref type_) = type_.safe_canonical_type(ctx) {
+ if let TypeKind::Enum(ref enum_) = *type_.kind() {
+ if enum_.is_constified_enum_module(ctx, self) {
+ // Type alias is inside a module
+ is_constified_module_enum = true;
+ }
}
}
}
if ctx.options().enable_cxx_namespaces {
+ if is_constified_module_enum {
+ path.push(CONSTIFIED_ENUM_MODULE_REPR_NAME.into());
+ }
return path;
}
+ if is_constified_module_enum {
+ return vec![path.last().unwrap().clone(),
+ CONSTIFIED_ENUM_MODULE_REPR_NAME.into()];
+ }
if ctx.options().disable_name_namespacing {
return vec![path.last().unwrap().clone()];
}
diff --git a/tests/expectations/tests/constify-module-enums-namespace.rs b/tests/expectations/tests/constify-module-enums-namespace.rs
new file mode 100644
index 00000000..4db009be
--- /dev/null
+++ b/tests/expectations/tests/constify-module-enums-namespace.rs
@@ -0,0 +1,52 @@
+/* automatically generated by rust-bindgen */
+
+
+#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
+
+
+#[allow(non_snake_case, non_camel_case_types, non_upper_case_globals)]
+pub mod root {
+ #[allow(unused_imports)]
+ use self::super::root;
+ pub mod ns1 {
+ #[allow(unused_imports)]
+ use self::super::super::root;
+ pub mod ns2 {
+ #[allow(unused_imports)]
+ use self::super::super::super::root;
+ pub mod foo {
+ pub type Type = ::std::os::raw::c_uint;
+ pub const THIS: Type = 0;
+ pub const SHOULD_BE: Type = 1;
+ pub const A_CONSTANT: Type = 2;
+ }
+ }
+ pub mod ns3 {
+ #[allow(unused_imports)]
+ use self::super::super::super::root;
+ #[repr(C)]
+ #[derive(Debug, Copy)]
+ pub struct bar {
+ pub this_should_work: root::ns1::ns2::foo::Type,
+ }
+ #[test]
+ fn bindgen_test_layout_bar() {
+ assert_eq!(::std::mem::size_of::<bar>() , 4usize , concat ! (
+ "Size of: " , stringify ! ( bar ) ));
+ assert_eq! (::std::mem::align_of::<bar>() , 4usize , concat !
+ ( "Alignment of " , stringify ! ( bar ) ));
+ assert_eq! (unsafe {
+ & ( * ( 0 as * const bar ) ) . this_should_work as
+ * const _ as usize } , 0usize , concat ! (
+ "Alignment of field: " , stringify ! ( bar ) ,
+ "::" , stringify ! ( this_should_work ) ));
+ }
+ impl Clone for bar {
+ fn clone(&self) -> Self { *self }
+ }
+ impl Default for bar {
+ fn default() -> Self { unsafe { ::std::mem::zeroed() } }
+ }
+ }
+ }
+}
diff --git a/tests/expectations/tests/constify-module-enums-typedef.rs b/tests/expectations/tests/constify-module-enums-typedef.rs
new file mode 100644
index 00000000..55eeb915
--- /dev/null
+++ b/tests/expectations/tests/constify-module-enums-typedef.rs
@@ -0,0 +1,51 @@
+/* automatically generated by rust-bindgen */
+
+
+#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
+
+
+pub mod foo {
+ pub type Type = ::std::os::raw::c_uint;
+ pub const THIS: Type = 0;
+ pub const SHOULD_BE: Type = 1;
+ pub const A_CONSTANT: Type = 2;
+ pub const ALSO_THIS: Type = 42;
+ pub const AND_ALSO_THIS: Type = 42;
+}
+pub use self::foo::Type as foo_alias1;
+pub use self::foo_alias1 as foo_alias2;
+#[repr(C)]
+#[derive(Debug, Copy)]
+pub struct bar {
+ pub member1: foo::Type,
+ pub member2: foo_alias1,
+ pub member3: foo_alias2,
+}
+#[test]
+fn bindgen_test_layout_bar() {
+ assert_eq!(::std::mem::size_of::<bar>() , 12usize , concat ! (
+ "Size of: " , stringify ! ( bar ) ));
+ assert_eq! (::std::mem::align_of::<bar>() , 4usize , concat ! (
+ "Alignment of " , stringify ! ( bar ) ));
+ assert_eq! (unsafe {
+ & ( * ( 0 as * const bar ) ) . member1 as * const _ as usize }
+ , 0usize , concat ! (
+ "Alignment of field: " , stringify ! ( bar ) , "::" ,
+ stringify ! ( member1 ) ));
+ assert_eq! (unsafe {
+ & ( * ( 0 as * const bar ) ) . member2 as * const _ as usize }
+ , 4usize , concat ! (
+ "Alignment of field: " , stringify ! ( bar ) , "::" ,
+ stringify ! ( member2 ) ));
+ assert_eq! (unsafe {
+ & ( * ( 0 as * const bar ) ) . member3 as * const _ as usize }
+ , 8usize , concat ! (
+ "Alignment of field: " , stringify ! ( bar ) , "::" ,
+ stringify ! ( member3 ) ));
+}
+impl Clone for bar {
+ fn clone(&self) -> Self { *self }
+}
+impl Default for bar {
+ fn default() -> Self { unsafe { ::std::mem::zeroed() } }
+}
diff --git a/tests/headers/constify-module-enums-namespace.hpp b/tests/headers/constify-module-enums-namespace.hpp
new file mode 100644
index 00000000..397f700f
--- /dev/null
+++ b/tests/headers/constify-module-enums-namespace.hpp
@@ -0,0 +1,17 @@
+// bindgen-flags: --enable-cxx-namespaces --constified-enum-module foo
+
+namespace ns1 {
+ namespace ns2 {
+ enum foo {
+ THIS,
+ SHOULD_BE,
+ A_CONSTANT,
+ };
+ }
+
+ namespace ns3 {
+ struct bar {
+ enum ns2::foo this_should_work;
+ };
+ }
+} \ No newline at end of file
diff --git a/tests/headers/constify-module-enums-typedef.h b/tests/headers/constify-module-enums-typedef.h
new file mode 100644
index 00000000..e82afc6b
--- /dev/null
+++ b/tests/headers/constify-module-enums-typedef.h
@@ -0,0 +1,18 @@
+// bindgen-flags: --constified-enum-module foo
+
+typedef enum foo {
+ THIS,
+ SHOULD_BE,
+ A_CONSTANT,
+ ALSO_THIS = 42,
+ AND_ALSO_THIS = 42,
+} foo;
+
+typedef foo foo_alias1;
+typedef foo_alias1 foo_alias2;
+
+typedef struct bar {
+ foo member1;
+ foo_alias1 member2;
+ foo_alias2 member3;
+} bar;