summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmilio Cobos Álvarez <ecoal95@gmail.com>2016-11-24 22:48:05 +0100
committerEmilio Cobos Álvarez <emilio@crisal.io>2016-11-29 20:36:05 +0100
commit673f9d313a981be3882050849d621bd12f07291c (patch)
tree45ca13a9fd14e25d8ea7581f48e7f1882fca20e0
parentebef1423cde4afa8d1f97a6f91ad466642a41b1c (diff)
ir: Don't assume our name is our base name too early when we're in a namespace.
-rw-r--r--libbindgen/src/ir/item.rs15
-rw-r--r--libbindgen/tests/expectations/tests/nested_within_namespace.rs53
-rw-r--r--libbindgen/tests/headers/nested_within_namespace.hpp15
3 files changed, 78 insertions, 5 deletions
diff --git a/libbindgen/src/ir/item.rs b/libbindgen/src/ir/item.rs
index c81eab8b..1d643a45 100644
--- a/libbindgen/src/ir/item.rs
+++ b/libbindgen/src/ir/item.rs
@@ -688,14 +688,14 @@ impl Item {
return base_name;
}
- if within_namespace {
- return ctx.rust_mangle(&base_name).into_owned();
- }
-
// Concatenate this item's ancestors' names together.
let mut names: Vec<_> = target.parent_id()
.ancestors(ctx)
.filter(|id| *id != ctx.root_module())
+ .take_while(|id| {
+ // Stop iterating ancestors once we reach a namespace.
+ !within_namespace || !ctx.resolve_item(*id).is_module()
+ })
.map(|id| {
let item = ctx.resolve_item(id);
let target = ctx.resolve_item(item.name_target(ctx, false));
@@ -703,8 +703,13 @@ impl Item {
})
.filter(|name| !name.is_empty())
.collect();
+
names.reverse();
- names.push(base_name);
+
+ if !base_name.is_empty() {
+ names.push(base_name);
+ }
+
let name = names.join("_");
ctx.rust_mangle(&name).into_owned()
diff --git a/libbindgen/tests/expectations/tests/nested_within_namespace.rs b/libbindgen/tests/expectations/tests/nested_within_namespace.rs
new file mode 100644
index 00000000..949b5adb
--- /dev/null
+++ b/libbindgen/tests/expectations/tests/nested_within_namespace.rs
@@ -0,0 +1,53 @@
+/* automatically generated by rust-bindgen */
+
+
+#![allow(non_snake_case)]
+
+
+pub mod root {
+ #[allow(unused_imports)]
+ use root;
+ pub mod foo {
+ #[allow(unused_imports)]
+ use root;
+ #[repr(C)]
+ #[derive(Debug, Copy)]
+ pub struct Bar {
+ pub foo: ::std::os::raw::c_int,
+ }
+ #[repr(C)]
+ #[derive(Debug, Copy)]
+ pub struct Bar_Baz {
+ pub foo: ::std::os::raw::c_int,
+ }
+ #[test]
+ fn bindgen_test_layout_Bar_Baz() {
+ assert_eq!(::std::mem::size_of::<Bar_Baz>() , 4usize);
+ assert_eq!(::std::mem::align_of::<Bar_Baz>() , 4usize);
+ }
+ impl Clone for Bar_Baz {
+ fn clone(&self) -> Self { *self }
+ }
+ #[test]
+ fn bindgen_test_layout_Bar() {
+ assert_eq!(::std::mem::size_of::<Bar>() , 4usize);
+ assert_eq!(::std::mem::align_of::<Bar>() , 4usize);
+ }
+ impl Clone for Bar {
+ fn clone(&self) -> Self { *self }
+ }
+ #[repr(C)]
+ #[derive(Debug, Copy)]
+ pub struct Baz {
+ pub baz: ::std::os::raw::c_int,
+ }
+ #[test]
+ fn bindgen_test_layout_Baz() {
+ assert_eq!(::std::mem::size_of::<Baz>() , 4usize);
+ assert_eq!(::std::mem::align_of::<Baz>() , 4usize);
+ }
+ impl Clone for Baz {
+ fn clone(&self) -> Self { *self }
+ }
+ }
+}
diff --git a/libbindgen/tests/headers/nested_within_namespace.hpp b/libbindgen/tests/headers/nested_within_namespace.hpp
new file mode 100644
index 00000000..a9b7c1ec
--- /dev/null
+++ b/libbindgen/tests/headers/nested_within_namespace.hpp
@@ -0,0 +1,15 @@
+// bindgen-flags: --enable-cxx-namespaces
+
+namespace foo {
+ class Bar {
+ int foo;
+
+ class Baz {
+ int foo;
+ };
+ };
+
+ class Baz {
+ int baz;
+ };
+}