summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors-servo <lbergstrom+bors@mozilla.com>2016-12-07 17:02:09 -0800
committerGitHub <noreply@github.com>2016-12-07 17:02:09 -0800
commit47e4ec7477a110265117a07bc290987b532861f9 (patch)
tree893205e74e5e7aca8f26a2672489d2d1322f4cea
parentec298aebd3114a8f20865eb8994c4b4f9a820a8e (diff)
parent161a590d44952d9921ab1cdd721166aceee5ed01 (diff)
Auto merge of #319 - emilio:union-in-ns, r=fitzgen
codegen: Fix bindgen-injected items in namespaces. Found while trying to use namespaces in stylo. r? @fitzgen
-rw-r--r--libbindgen/src/codegen/mod.rs32
-rw-r--r--libbindgen/tests/expectations/tests/union-in-ns.rs52
-rw-r--r--libbindgen/tests/headers/union-in-ns.hpp5
3 files changed, 78 insertions, 11 deletions
diff --git a/libbindgen/src/codegen/mod.rs b/libbindgen/src/codegen/mod.rs
index ff18ab83..d2e2b96c 100644
--- a/libbindgen/src/codegen/mod.rs
+++ b/libbindgen/src/codegen/mod.rs
@@ -303,6 +303,16 @@ impl CodeGenerator for Module {
.codegen(ctx, result, whitelisted_items, &());
}
}
+
+ if item.id() == ctx.root_module() {
+ let saw_union = result.saw_union;
+ if saw_union && !ctx.options().unstable_rust {
+ utils::prepend_union_types(ctx, &mut *result);
+ }
+ if ctx.need_bindegen_complex_type() {
+ utils::prepend_complex_type(ctx, &mut *result);
+ }
+ }
};
if !ctx.options().enable_cxx_namespaces {
@@ -916,7 +926,11 @@ impl CodeGenerator for CompInfo {
// NB: In unstable rust we use proper `union` types.
let ty = if is_union && !ctx.options().unstable_rust {
- quote_ty!(ctx.ext_cx(), __BindgenUnionField<$ty>)
+ if ctx.options().enable_cxx_namespaces {
+ quote_ty!(ctx.ext_cx(), root::__BindgenUnionField<$ty>)
+ } else {
+ quote_ty!(ctx.ext_cx(), __BindgenUnionField<$ty>)
+ }
} else {
ty
};
@@ -1747,7 +1761,11 @@ impl ToRustTy for Type {
let float_path = float_kind_rust_type(ctx, fk);
ctx.generated_bindegen_complex();
- quote_ty!(ctx.ext_cx(), __BindgenComplex<$float_path>)
+ if ctx.options().enable_cxx_namespaces {
+ quote_ty!(ctx.ext_cx(), root::__BindgenComplex<$float_path>)
+ } else {
+ quote_ty!(ctx.ext_cx(), __BindgenComplex<$float_path>)
+ }
}
TypeKind::Function(ref fs) => {
let ty = fs.to_rust_ty(ctx, item);
@@ -2008,15 +2026,7 @@ pub fn codegen(context: &mut BindgenContext) -> Vec<P<ast::Item>> {
context.resolve_item(context.root_module())
.codegen(context, &mut result, &whitelisted_items, &());
- let saw_union = result.saw_union;
- let mut result = result.items;
- if saw_union && !context.options().unstable_rust {
- utils::prepend_union_types(context, &mut result);
- }
- if context.need_bindegen_complex_type() {
- utils::prepend_complex_type(context, &mut result);
- }
- result
+ result.items
})
}
diff --git a/libbindgen/tests/expectations/tests/union-in-ns.rs b/libbindgen/tests/expectations/tests/union-in-ns.rs
new file mode 100644
index 00000000..fa511e51
--- /dev/null
+++ b/libbindgen/tests/expectations/tests/union-in-ns.rs
@@ -0,0 +1,52 @@
+/* automatically generated by rust-bindgen */
+
+
+#![allow(non_snake_case)]
+
+
+pub mod root {
+ #[repr(C)]
+ pub struct __BindgenUnionField<T>(::std::marker::PhantomData<T>);
+ impl <T> __BindgenUnionField<T> {
+ #[inline]
+ pub fn new() -> Self {
+ __BindgenUnionField(::std::marker::PhantomData)
+ }
+ #[inline]
+ pub unsafe fn as_ref(&self) -> &T { ::std::mem::transmute(self) }
+ #[inline]
+ pub unsafe fn as_mut(&mut self) -> &mut T {
+ ::std::mem::transmute(self)
+ }
+ }
+ impl <T> ::std::default::Default for __BindgenUnionField<T> {
+ #[inline]
+ fn default() -> Self { Self::new() }
+ }
+ impl <T> ::std::clone::Clone for __BindgenUnionField<T> {
+ #[inline]
+ fn clone(&self) -> Self { Self::new() }
+ }
+ impl <T> ::std::marker::Copy for __BindgenUnionField<T> { }
+ impl <T> ::std::fmt::Debug for __BindgenUnionField<T> {
+ fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
+ fmt.write_str("__BindgenUnionField")
+ }
+ }
+ #[allow(unused_imports)]
+ use self::super::root;
+ #[repr(C)]
+ #[derive(Debug, Copy)]
+ pub struct bar {
+ pub baz: root::__BindgenUnionField<::std::os::raw::c_int>,
+ pub bindgen_union_field: u32,
+ }
+ #[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 }
+ }
+}
diff --git a/libbindgen/tests/headers/union-in-ns.hpp b/libbindgen/tests/headers/union-in-ns.hpp
new file mode 100644
index 00000000..68b8f72d
--- /dev/null
+++ b/libbindgen/tests/headers/union-in-ns.hpp
@@ -0,0 +1,5 @@
+// bindgen-flags: --enable-cxx-namespaces
+
+union bar {
+ int baz;
+};