summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Froyd <froydnj@gmail.com>2020-05-18 15:32:14 -0400
committerEmilio Cobos Álvarez <emilio@crisal.io>2020-05-18 22:21:48 +0200
commitb10fa6f894c1314dd06cd0720e95b4e4a8d3117f (patch)
tree92b4c169e39c551f92c8186dc82504a170b29c01
parentb1a1ebc918e44f4aa928878dae20fa7be6c87043 (diff)
add command-line option for disabling untagged unions
One more thing that we can configure from the command line.
-rw-r--r--src/options.rs9
-rw-r--r--tests/expectations/tests/disable-untagged-union.rs82
-rw-r--r--tests/headers/disable-untagged-union.hpp6
3 files changed, 97 insertions, 0 deletions
diff --git a/src/options.rs b/src/options.rs
index b630bb4b..c2de1e5a 100644
--- a/src/options.rs
+++ b/src/options.rs
@@ -267,6 +267,11 @@ where
names like \"bar\" instead of \"foo_bar\" for a nested \
definition \"struct foo { struct bar { } b; };\"."
),
+ Arg::with_name("disable-untagged-union")
+ .long("disable-untagged-union")
+ .help(
+ "Disable support for native Rust unions.",
+ ),
Arg::with_name("ignore-functions")
.long("ignore-functions")
.help(
@@ -667,6 +672,10 @@ where
builder = builder.disable_nested_struct_naming();
}
+ if matches.is_present("disable-untagged-union") {
+ builder = builder.disable_untagged_union();
+ }
+
if matches.is_present("ignore-functions") {
builder = builder.ignore_functions();
}
diff --git a/tests/expectations/tests/disable-untagged-union.rs b/tests/expectations/tests/disable-untagged-union.rs
new file mode 100644
index 00000000..9661ef79
--- /dev/null
+++ b/tests/expectations/tests/disable-untagged-union.rs
@@ -0,0 +1,82 @@
+/* automatically generated by rust-bindgen */
+
+#![allow(
+ dead_code,
+ non_snake_case,
+ non_camel_case_types,
+ non_upper_case_globals
+)]
+
+#[repr(C)]
+pub struct __BindgenUnionField<T>(::std::marker::PhantomData<T>);
+impl<T> __BindgenUnionField<T> {
+ #[inline]
+ pub const 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")
+ }
+}
+impl<T> ::std::hash::Hash for __BindgenUnionField<T> {
+ fn hash<H: ::std::hash::Hasher>(&self, _state: &mut H) {}
+}
+impl<T> ::std::cmp::PartialEq for __BindgenUnionField<T> {
+ fn eq(&self, _other: &__BindgenUnionField<T>) -> bool {
+ true
+ }
+}
+impl<T> ::std::cmp::Eq for __BindgenUnionField<T> {}
+#[repr(C)]
+#[derive(Debug, Default, Copy, Clone)]
+pub struct Foo {
+ pub bar: __BindgenUnionField<::std::os::raw::c_int>,
+ pub baz: __BindgenUnionField<::std::os::raw::c_uint>,
+ pub bindgen_union_field: u32,
+}
+#[test]
+fn bindgen_test_layout_Foo() {
+ assert_eq!(
+ ::std::mem::size_of::<Foo>(),
+ 4usize,
+ concat!("Size of: ", stringify!(Foo))
+ );
+ assert_eq!(
+ ::std::mem::align_of::<Foo>(),
+ 4usize,
+ concat!("Alignment of ", stringify!(Foo))
+ );
+ assert_eq!(
+ unsafe { &(*(::std::ptr::null::<Foo>())).bar as *const _ as usize },
+ 0usize,
+ concat!("Offset of field: ", stringify!(Foo), "::", stringify!(bar))
+ );
+ assert_eq!(
+ unsafe { &(*(::std::ptr::null::<Foo>())).baz as *const _ as usize },
+ 0usize,
+ concat!("Offset of field: ", stringify!(Foo), "::", stringify!(baz))
+ );
+}
diff --git a/tests/headers/disable-untagged-union.hpp b/tests/headers/disable-untagged-union.hpp
new file mode 100644
index 00000000..44623181
--- /dev/null
+++ b/tests/headers/disable-untagged-union.hpp
@@ -0,0 +1,6 @@
+// bindgen-flags: --disable-untagged-union
+
+union Foo {
+ int bar;
+ unsigned int baz;
+};