diff options
author | Amanjeev Sethi <aj@amanjeev.com> | 2022-08-24 18:45:03 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-25 00:45:03 +0200 |
commit | 6dfc3e70df1676a3321cb83624f7c83db6fb92df (patch) | |
tree | 788b6ebe130e5f5daa9757f94cf1a85da0a86d0f /tests | |
parent | ebb1ce98c6e92ce2384a5867bdcfeb933ed12433 (diff) |
Sorting the output semantically (#2254)
Generated code needs some sorting in a way that is
semantically appealing. The request[1] asks for basic
sorting like "types are declared first, then all structs,
then all consts, then all function signatures, etc.
[1] https://github.com/rust-lang/rust-bindgen/issues/1743
Signed-off-by: Amanjeev Sethi <aj@amanjeev.com>
Co-authored-by: Christian Poveda <christian.poveda@ferrous-systems.com>
Co-authored-by: Darren Kulp <darren@kulp.ch>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/expectations/tests/sorted-items.rs | 82 | ||||
-rw-r--r-- | tests/expectations/tests/unsorted-items.rs | 82 | ||||
-rw-r--r-- | tests/headers/sorted-items.h | 17 | ||||
-rw-r--r-- | tests/headers/unsorted-items.h | 15 |
4 files changed, 196 insertions, 0 deletions
diff --git a/tests/expectations/tests/sorted-items.rs b/tests/expectations/tests/sorted-items.rs new file mode 100644 index 00000000..7df7c3d7 --- /dev/null +++ b/tests/expectations/tests/sorted-items.rs @@ -0,0 +1,82 @@ +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] + +pub type number = ::std::os::raw::c_int; +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct Point { + pub x: number, + pub y: number, +} +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct Angle { + pub a: number, + pub b: number, +} +pub const NUMBER: number = 42; +#[test] +fn bindgen_test_layout_Point() { + const UNINIT: ::std::mem::MaybeUninit<Point> = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::<Point>(), + 8usize, + concat!("Size of: ", stringify!(Point)) + ); + assert_eq!( + ::std::mem::align_of::<Point>(), + 4usize, + concat!("Alignment of ", stringify!(Point)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).x) as usize - ptr as usize }, + 0usize, + concat!("Offset of field: ", stringify!(Point), "::", stringify!(x)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).y) as usize - ptr as usize }, + 4usize, + concat!("Offset of field: ", stringify!(Point), "::", stringify!(y)) + ); +} +#[test] +fn bindgen_test_layout_Angle() { + const UNINIT: ::std::mem::MaybeUninit<Angle> = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::<Angle>(), + 8usize, + concat!("Size of: ", stringify!(Angle)) + ); + assert_eq!( + ::std::mem::align_of::<Angle>(), + 4usize, + concat!("Alignment of ", stringify!(Angle)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).a) as usize - ptr as usize }, + 0usize, + concat!("Offset of field: ", stringify!(Angle), "::", stringify!(a)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).b) as usize - ptr as usize }, + 4usize, + concat!("Offset of field: ", stringify!(Angle), "::", stringify!(b)) + ); +} +extern "C" { + pub fn foo() -> ::std::os::raw::c_int; +} +extern "C" { + pub fn bar(x: number) -> ::std::os::raw::c_int; +} +extern "C" { + pub fn baz(point: Point) -> ::std::os::raw::c_int; +} diff --git a/tests/expectations/tests/unsorted-items.rs b/tests/expectations/tests/unsorted-items.rs new file mode 100644 index 00000000..ce0c5f3f --- /dev/null +++ b/tests/expectations/tests/unsorted-items.rs @@ -0,0 +1,82 @@ +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] + +extern "C" { + pub fn foo() -> ::std::os::raw::c_int; +} +pub type number = ::std::os::raw::c_int; +extern "C" { + pub fn bar(x: number) -> ::std::os::raw::c_int; +} +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct Point { + pub x: number, + pub y: number, +} +#[test] +fn bindgen_test_layout_Point() { + const UNINIT: ::std::mem::MaybeUninit<Point> = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::<Point>(), + 8usize, + concat!("Size of: ", stringify!(Point)) + ); + assert_eq!( + ::std::mem::align_of::<Point>(), + 4usize, + concat!("Alignment of ", stringify!(Point)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).x) as usize - ptr as usize }, + 0usize, + concat!("Offset of field: ", stringify!(Point), "::", stringify!(x)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).y) as usize - ptr as usize }, + 4usize, + concat!("Offset of field: ", stringify!(Point), "::", stringify!(y)) + ); +} +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct Angle { + pub a: number, + pub b: number, +} +#[test] +fn bindgen_test_layout_Angle() { + const UNINIT: ::std::mem::MaybeUninit<Angle> = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::<Angle>(), + 8usize, + concat!("Size of: ", stringify!(Angle)) + ); + assert_eq!( + ::std::mem::align_of::<Angle>(), + 4usize, + concat!("Alignment of ", stringify!(Angle)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).a) as usize - ptr as usize }, + 0usize, + concat!("Offset of field: ", stringify!(Angle), "::", stringify!(a)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).b) as usize - ptr as usize }, + 4usize, + concat!("Offset of field: ", stringify!(Angle), "::", stringify!(b)) + ); +} +extern "C" { + pub fn baz(point: Point) -> ::std::os::raw::c_int; +} +pub const NUMBER: number = 42; diff --git a/tests/headers/sorted-items.h b/tests/headers/sorted-items.h new file mode 100644 index 00000000..11fc2ef4 --- /dev/null +++ b/tests/headers/sorted-items.h @@ -0,0 +1,17 @@ +// bindgen-flags: --sort-semantically -- --target=x86_64-unknown-linux + +int foo(); +typedef int number; +int bar(number x); +struct Point +{ + number x; + number y; +}; +struct Angle +{ + number a; + number b; +}; +int baz(struct Point point); +const number NUMBER = 42; diff --git a/tests/headers/unsorted-items.h b/tests/headers/unsorted-items.h new file mode 100644 index 00000000..23962d18 --- /dev/null +++ b/tests/headers/unsorted-items.h @@ -0,0 +1,15 @@ +int foo(); +typedef int number; +int bar(number x); +struct Point +{ + number x; + number y; +}; +struct Angle +{ + number a; + number b; +}; +int baz(struct Point point); +const number NUMBER = 42; |