diff options
author | Nick Fitzgerald <fitzgen@gmail.com> | 2017-10-25 13:18:13 -0700 |
---|---|---|
committer | Nick Fitzgerald <fitzgen@gmail.com> | 2017-10-25 13:18:13 -0700 |
commit | fd7afb63bdcd85c1aaeb4338d1f0f9508b2081eb (patch) | |
tree | cbc7b5e394e3baf59f7c83f940ee8990eeb14fa3 | |
parent | d018f421338318f5c21bc1809acc5b5dd14b6d98 (diff) |
Sanity tests for zero-sized and flexible arrays
-rw-r--r-- | tests/expectations/tests/zero-sized-array.rs | 159 | ||||
-rw-r--r-- | tests/headers/zero-sized-array.hpp | 45 |
2 files changed, 204 insertions, 0 deletions
diff --git a/tests/expectations/tests/zero-sized-array.rs b/tests/expectations/tests/zero-sized-array.rs new file mode 100644 index 00000000..15e5bc1f --- /dev/null +++ b/tests/expectations/tests/zero-sized-array.rs @@ -0,0 +1,159 @@ +/* automatically generated by rust-bindgen */ + + +#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] + + +#[repr(C)] +#[derive(Default)] +pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>); +impl<T> __IncompleteArrayField<T> { + #[inline] + pub fn new() -> Self { + __IncompleteArrayField(::std::marker::PhantomData) + } + #[inline] + pub unsafe fn as_ptr(&self) -> *const T { + ::std::mem::transmute(self) + } + #[inline] + pub unsafe fn as_mut_ptr(&mut self) -> *mut T { + ::std::mem::transmute(self) + } + #[inline] + pub unsafe fn as_slice(&self, len: usize) -> &[T] { + ::std::slice::from_raw_parts(self.as_ptr(), len) + } + #[inline] + pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { + ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) + } +} +impl<T> ::std::fmt::Debug for __IncompleteArrayField<T> { + fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + fmt.write_str("__IncompleteArrayField") + } +} +impl<T> ::std::clone::Clone for __IncompleteArrayField<T> { + #[inline] + fn clone(&self) -> Self { + Self::new() + } +} +impl<T> ::std::marker::Copy for __IncompleteArrayField<T> {} +/// Bizarrely enough, this should *not* get an `_address` field. +#[repr(C)] +#[derive(Debug, Default)] +pub struct ZeroSizedArray { + pub arr: __IncompleteArrayField<::std::os::raw::c_char>, +} +#[test] +fn bindgen_test_layout_ZeroSizedArray() { + assert_eq!( + ::std::mem::size_of::<ZeroSizedArray>(), + 0usize, + concat!("Size of: ", stringify!(ZeroSizedArray)) + ); + assert_eq!( + ::std::mem::align_of::<ZeroSizedArray>(), + 1usize, + concat!("Alignment of ", stringify!(ZeroSizedArray)) + ); + assert_eq!( + unsafe { &(*(0 as *const ZeroSizedArray)).arr as *const _ as usize }, + 0usize, + concat!( + "Alignment of field: ", + stringify!(ZeroSizedArray), + "::", + stringify!(arr) + ) + ); +} +/// And nor should this get an `_address` field. +#[repr(C)] +#[derive(Debug, Default)] +pub struct ContainsZeroSizedArray { + pub zsa: ZeroSizedArray, +} +#[test] +fn bindgen_test_layout_ContainsZeroSizedArray() { + assert_eq!( + ::std::mem::size_of::<ContainsZeroSizedArray>(), + 0usize, + concat!("Size of: ", stringify!(ContainsZeroSizedArray)) + ); + assert_eq!( + ::std::mem::align_of::<ContainsZeroSizedArray>(), + 1usize, + concat!("Alignment of ", stringify!(ContainsZeroSizedArray)) + ); + assert_eq!( + unsafe { &(*(0 as *const ContainsZeroSizedArray)).zsa as *const _ as usize }, + 0usize, + concat!( + "Alignment of field: ", + stringify!(ContainsZeroSizedArray), + "::", + stringify!(zsa) + ) + ); +} +/// Inheriting from ZeroSizedArray shouldn't cause an `_address` to be inserted +/// either. +#[repr(C)] +#[derive(Debug, Default)] +pub struct InheritsZeroSizedArray { + pub _base: ZeroSizedArray, +} +#[test] +fn bindgen_test_layout_InheritsZeroSizedArray() { + assert_eq!( + ::std::mem::size_of::<InheritsZeroSizedArray>(), + 0usize, + concat!("Size of: ", stringify!(InheritsZeroSizedArray)) + ); + assert_eq!( + ::std::mem::align_of::<InheritsZeroSizedArray>(), + 1usize, + concat!("Alignment of ", stringify!(InheritsZeroSizedArray)) + ); +} +/// And this should not get an `_address` field either. +#[repr(C, packed)] +#[derive(Debug, Default)] +pub struct DynamicallySizedArray { + pub arr: __IncompleteArrayField<::std::os::raw::c_char>, +} +#[test] +fn bindgen_test_layout_DynamicallySizedArray() { + assert_eq!( + ::std::mem::size_of::<DynamicallySizedArray>(), + 0usize, + concat!("Size of: ", stringify!(DynamicallySizedArray)) + ); + assert_eq!( + ::std::mem::align_of::<DynamicallySizedArray>(), + 1usize, + concat!("Alignment of ", stringify!(DynamicallySizedArray)) + ); +} +/// No `_address` field here either. +#[repr(C)] +#[derive(Debug, Default)] +pub struct ContainsDynamicallySizedArray { + pub dsa: DynamicallySizedArray, +} +#[test] +fn bindgen_test_layout_ContainsDynamicallySizedArray() { + assert_eq!( + ::std::mem::size_of::<ContainsDynamicallySizedArray>(), + 0usize, + concat!("Size of: ", stringify!(ContainsDynamicallySizedArray)) + ); + assert_eq!( + ::std::mem::align_of::<ContainsDynamicallySizedArray>(), + 1usize, + concat!("Alignment of ", stringify!(ContainsDynamicallySizedArray)) + ); +} diff --git a/tests/headers/zero-sized-array.hpp b/tests/headers/zero-sized-array.hpp new file mode 100644 index 00000000..ae6d0554 --- /dev/null +++ b/tests/headers/zero-sized-array.hpp @@ -0,0 +1,45 @@ +// These classes are technically zero-sized, but despite that they still don't +// get an `_address` field inserted. + +/** + * Bizarrely enough, this should *not* get an `_address` field. + */ +class ZeroSizedArray { + char arr[0]; +}; + +/** + * And nor should this get an `_address` field. + */ +class ContainsZeroSizedArray { + ZeroSizedArray zsa; +}; + +/** + * Inheriting from ZeroSizedArray shouldn't cause an `_address` to be inserted + * either. + */ +class InheritsZeroSizedArray : ZeroSizedArray {}; + +// These are dynamically sized, which means that `sizeof` yields `0` but it +// isn't really true. We shouldn't add an `_address` field to them. + +/** + * And this should not get an `_address` field either. + */ +class DynamicallySizedArray { + char arr[]; +}; + +/** + * No `_address` field here either. + */ +class ContainsDynamicallySizedArray { + DynamicallySizedArray dsa; +}; + +// Note: this is disallowed: +// +// error: base class 'DynamicallySizedArray' has a flexible array member +// +// class InheritsDynamicallySizedArray : DynamicallySizedArray {}; |