From 588bbc50e999ea48c597a86546796ba421fdd911 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Mon, 8 Aug 2022 10:59:52 -0500 Subject: [PATCH 01/11] add test for sorted items --- tests/expectations/tests/sorted-items.rs | 47 ++++++++++++++++++++++++ tests/headers/sorted-items.h | 9 +++++ 2 files changed, 56 insertions(+) create mode 100644 tests/expectations/tests/sorted-items.rs create mode 100644 tests/headers/sorted-items.h diff --git a/tests/expectations/tests/sorted-items.rs b/tests/expectations/tests/sorted-items.rs new file mode 100644 index 0000000000..5a5a1ff515 --- /dev/null +++ b/tests/expectations/tests/sorted-items.rs @@ -0,0 +1,47 @@ +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] + +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; +} +#[test] +fn bindgen_test_layout_Point() { + assert_eq!( + ::std::mem::size_of::(), + 8usize, + concat!("Size of: ", stringify!(Point)) + ); + assert_eq!( + ::std::mem::align_of::(), + 4usize, + concat!("Alignment of ", stringify!(Point)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).x as *const _ as usize }, + 0usize, + concat!("Offset of field: ", stringify!(Point), "::", stringify!(x)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).y as *const _ as usize }, + 4usize, + concat!("Offset of field: ", stringify!(Point), "::", stringify!(y)) + ); +} +pub const NUMBER: number = 42; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct Point { + pub x: number, + pub y: number, +} +pub type number = ::std::os::raw::c_int; diff --git a/tests/headers/sorted-items.h b/tests/headers/sorted-items.h new file mode 100644 index 0000000000..73d70b2070 --- /dev/null +++ b/tests/headers/sorted-items.h @@ -0,0 +1,9 @@ +int foo(); +typedef int number; +int bar(number x); +struct Point { + number x; + number y; +}; +int baz(struct Point point); +const number NUMBER = 42; From 4344d9996f0bf8645aa7994fee03609f3dfce810 Mon Sep 17 00:00:00 2001 From: Amanjeev Sethi Date: Thu, 11 Aug 2022 16:32:03 -0400 Subject: [PATCH 02/11] feat(lib sorting): add semantic sorting of the output Generated code needs some sorting in a way that is semantically appealing. The request[1] asks for basic sorting like "ypes are declared first, then all structs, then all consts, then all function signatures, etc. [1] https://github.com/rust-lang/rust-bindgen/issues/1743 --- Cargo.lock | 24 +++++++++++++++----- Cargo.toml | 1 + src/lib.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/options.rs | 1 + 4 files changed, 80 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0c7c2f7851..fa908ea9d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -47,6 +47,7 @@ dependencies = [ "regex", "rustc-hash", "shlex", + "syn", "tempfile", "which", ] @@ -260,11 +261,11 @@ checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" [[package]] name = "proc-macro2" -version = "1.0.28" +version = "1.0.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c7ed8b8c7b886ea3ed7dde405212185f423ab44682667c8c6dd14aa1d9f6612" +checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab" dependencies = [ - "unicode-xid", + "unicode-ident", ] [[package]] @@ -369,6 +370,17 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +[[package]] +name = "syn" +version = "1.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "tempfile" version = "3.2.0" @@ -399,10 +411,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" [[package]] -name = "unicode-xid" -version = "0.2.2" +name = "unicode-ident" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" +checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf" [[package]] name = "version_check" diff --git a/Cargo.toml b/Cargo.toml index ed7571ae99..311110b3ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,6 +57,7 @@ lazycell = "1" lazy_static = "1" peeking_take_while = "0.1.2" quote = { version = "1", default-features = false } +syn = { version = "1.0.99", features = ["full", "extra-traits"]} regex = { version = "1.5", default-features = false , features = ["std", "unicode"] } which = { version = "4.2.1", optional = true, default-features = false } shlex = "1" diff --git a/src/lib.rs b/src/lib.rs index 3d09ab71f1..37c28147a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -86,6 +86,7 @@ use std::{env, iter}; // Some convenient typedefs for a fast hash map and hash set. type HashMap = ::rustc_hash::FxHashMap; type HashSet = ::rustc_hash::FxHashSet; +use quote::ToTokens; pub(crate) use std::collections::hash_map::Entry; /// Default prefix for the anon fields. @@ -587,6 +588,10 @@ impl Builder { output_vector.push("--vtable-generation".into()); } + if self.options.sort_semantically { + output_vector.push("--sort-semantically".into()); + } + // Add clang arguments output_vector.push("--".into()); @@ -1476,6 +1481,14 @@ impl Builder { self } + /// If true, enables the sorting of the output in a predefined manner + /// + /// TODO: Perhaps move the sorting order out into a config + pub fn sort_semantically(mut self, doit: bool) -> Self { + self.options.sort_semantically = doit; + self + } + /// Generate the Rust bindings using the options built up thus far. pub fn generate(mut self) -> Result { // Add any extra arguments from the environment to the clang command line. @@ -2005,6 +2018,9 @@ struct BindgenOptions { /// Emit vtable functions. vtable_generation: bool, + + /// Sort the code generation + sort_semantically: bool, } /// TODO(emilio): This is sort of a lie (see the error message that results from @@ -2153,6 +2169,7 @@ impl Default for BindgenOptions { c_naming: false, force_explicit_padding: false, vtable_generation: false, + sort_semantically: false, } } } @@ -2437,6 +2454,49 @@ impl Bindings { let (items, options) = codegen::codegen(context); + if options.sort_semantically { + let module_wrapped_tokens = + quote!(mod wrapper_for_sorting_hack { #( #items )* }); + let mut syn_parsed_items = + syn::parse2::(module_wrapped_tokens) + .unwrap() + .content + .unwrap() + .1; + + syn_parsed_items.sort_by_key(|item| match item { + syn::Item::Type(_) => 0, + syn::Item::Struct(_) => 1, + syn::Item::Const(_) => 2, + syn::Item::Fn(_) => 3, + syn::Item::Enum(_) => 4, + syn::Item::Union(_) => 5, + syn::Item::Static(_) => 6, + syn::Item::Trait(_) => 7, + syn::Item::TraitAlias(_) => 8, + syn::Item::Impl(_) => 9, + syn::Item::Mod(_) => 10, + syn::Item::Use(_) => 11, + syn::Item::Verbatim(_) => 12, + syn::Item::ExternCrate(_) => 13, + syn::Item::ForeignMod(_) => 14, + syn::Item::Macro(_) => 15, + syn::Item::Macro2(_) => 16, + _ => 18, + }); + + let items = syn_parsed_items + .into_iter() + .map(|item| item.into_token_stream()); + + return Ok(Bindings { + options, + module: quote! { + #( #items )* + }, + }); + } + Ok(Bindings { options, module: quote! { diff --git a/src/options.rs b/src/options.rs index a11f3ddd7c..d7cf09b6b7 100644 --- a/src/options.rs +++ b/src/options.rs @@ -515,6 +515,7 @@ where Arg::new("vtable-generation") .long("vtable-generation") .help("Enables generation of vtable functions."), + Arg::new("sort-semantically").long("sort-semantically").help("Enables sorting of code generation in a predefined manner"), Arg::new("V") .long("version") .help("Prints the version, and exits"), From 29740bb7410662da7a1c9b73f04c65a614acc4ab Mon Sep 17 00:00:00 2001 From: Amanjeev Sethi Date: Thu, 11 Aug 2022 16:42:49 -0400 Subject: [PATCH 03/11] feat(lib sorting): test fix --- tests/expectations/tests/sorted-items.rs | 25 +++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/tests/expectations/tests/sorted-items.rs b/tests/expectations/tests/sorted-items.rs index 5a5a1ff515..2d336738b6 100644 --- a/tests/expectations/tests/sorted-items.rs +++ b/tests/expectations/tests/sorted-items.rs @@ -8,14 +8,21 @@ 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; } -extern "C" { - pub fn baz(point: Point) -> ::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 = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); assert_eq!( ::std::mem::size_of::(), 8usize, @@ -27,21 +34,17 @@ fn bindgen_test_layout_Point() { concat!("Alignment of ", stringify!(Point)) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).x as *const _ as usize }, + 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::null::())).y as *const _ as usize }, + unsafe { ::std::ptr::addr_of!((*ptr).y) as usize - ptr as usize }, 4usize, concat!("Offset of field: ", stringify!(Point), "::", stringify!(y)) ); } -pub const NUMBER: number = 42; -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct Point { - pub x: number, - pub y: number, +extern "C" { + pub fn baz(point: Point) -> ::std::os::raw::c_int; } -pub type number = ::std::os::raw::c_int; +pub const NUMBER: number = 42; From 8c456670065303451ed22aa728e7bda98f27b80d Mon Sep 17 00:00:00 2001 From: Amanjeev Sethi Date: Thu, 11 Aug 2022 17:42:58 -0400 Subject: [PATCH 04/11] feat(lib sorting): CLI options --- src/lib.rs | 4 ++-- src/options.rs | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 37c28147a2..900190f213 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2485,14 +2485,14 @@ impl Bindings { _ => 18, }); - let items = syn_parsed_items + let synful_items = syn_parsed_items .into_iter() .map(|item| item.into_token_stream()); return Ok(Bindings { options, module: quote! { - #( #items )* + #( #synful_items )* }, }); } diff --git a/src/options.rs b/src/options.rs index d7cf09b6b7..e53e5272ff 100644 --- a/src/options.rs +++ b/src/options.rs @@ -515,7 +515,9 @@ where Arg::new("vtable-generation") .long("vtable-generation") .help("Enables generation of vtable functions."), - Arg::new("sort-semantically").long("sort-semantically").help("Enables sorting of code generation in a predefined manner"), + Arg::new("sort-semantically") + .long("sort-semantically") + .help("Enables sorting of code generation in a predefined manner"), Arg::new("V") .long("version") .help("Prints the version, and exits"), @@ -1001,5 +1003,9 @@ where builder = builder.vtable_generation(true); } + if matches.is_present("sort-semantically") { + builder = builder.sort_semantically(true); + } + Ok((builder, output, verbose)) } From 9eaba896a83d6ee8adee0b3a7b381c859d7b2b00 Mon Sep 17 00:00:00 2001 From: Amanjeev Sethi Date: Thu, 11 Aug 2022 17:44:24 -0400 Subject: [PATCH 05/11] feat(lib sorting): tests - sorted and unsorted We have this feature behind a feature flag - sort-semantically whose default value is `false`. * for sorted, a custom test is required in tests.rs - added * for unsorted, regular builder, an unsorted header file is enough. If we make this feature default, we need to get rid of the flag check and change all the header outputs because it will break almost all tests. --- tests/expectations/tests/sorted-items.rs | 55 +++++++++++---- tests/expectations/tests/unsorted-items.rs | 82 ++++++++++++++++++++++ tests/headers/{ => sorted}/sorted-items.h | 8 ++- tests/headers/unsorted-items.h | 15 ++++ tests/tests.rs | 23 ++++++ 5 files changed, 167 insertions(+), 16 deletions(-) create mode 100644 tests/expectations/tests/unsorted-items.rs rename tests/headers/{ => sorted}/sorted-items.h (69%) create mode 100644 tests/headers/unsorted-items.h diff --git a/tests/expectations/tests/sorted-items.rs b/tests/expectations/tests/sorted-items.rs index 2d336738b6..9ba25d10a2 100644 --- a/tests/expectations/tests/sorted-items.rs +++ b/tests/expectations/tests/sorted-items.rs @@ -1,23 +1,17 @@ -#![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)] +#[derive(Debug, Copy, Clone)] pub struct Point { pub x: number, pub y: number, } +#[repr(C)] +#[derive(Debug, 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 = @@ -44,7 +38,38 @@ fn bindgen_test_layout_Point() { concat!("Offset of field: ", stringify!(Point), "::", stringify!(y)) ); } +#[test] +fn bindgen_test_layout_Angle() { + const UNINIT: ::std::mem::MaybeUninit = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::(), + 8usize, + concat!("Size of: ", stringify!(Angle)) + ); + assert_eq!( + ::std::mem::align_of::(), + 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; } -pub const NUMBER: number = 42; diff --git a/tests/expectations/tests/unsorted-items.rs b/tests/expectations/tests/unsorted-items.rs new file mode 100644 index 0000000000..ce0c5f3f66 --- /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 = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::(), + 8usize, + concat!("Size of: ", stringify!(Point)) + ); + assert_eq!( + ::std::mem::align_of::(), + 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 = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::(), + 8usize, + concat!("Size of: ", stringify!(Angle)) + ); + assert_eq!( + ::std::mem::align_of::(), + 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/sorted-items.h similarity index 69% rename from tests/headers/sorted-items.h rename to tests/headers/sorted/sorted-items.h index 73d70b2070..23962d18d8 100644 --- a/tests/headers/sorted-items.h +++ b/tests/headers/sorted/sorted-items.h @@ -1,9 +1,15 @@ int foo(); typedef int number; int bar(number x); -struct Point { +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 0000000000..23962d18d8 --- /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; diff --git a/tests/tests.rs b/tests/tests.rs index cc64cfdd29..949f3a4ec5 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -670,3 +670,26 @@ fn dump_preprocessed_input() { "cpp-empty-layout.hpp is in the preprocessed file" ); } + +#[test] +fn test_sort_semantically() { + let actual = builder() + .header(concat!( + env!("CARGO_MANIFEST_DIR"), + "/tests/headers/sorted/sorted-items.h" + )) + .clang_arg("--target=x86_64-unknown-linux") + .sort_semantically(true) + .generate() + .unwrap() + .to_string(); + + let (actual, _) = rustfmt(actual); + let expected = include_str!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/tests/expectations/tests/sorted-items.rs" + )); + let (expected, _) = rustfmt(expected.into()); + + assert_eq!(actual, expected); +} From 9a4714ad01a6c734bea8155faafce467ea460048 Mon Sep 17 00:00:00 2001 From: Amanjeev Sethi Date: Thu, 11 Aug 2022 18:55:04 -0400 Subject: [PATCH 06/11] feat(lib sorting): comment explaining the hack --- src/lib.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 900190f213..2cbb7ac2a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2457,6 +2457,16 @@ impl Bindings { if options.sort_semantically { let module_wrapped_tokens = quote!(mod wrapper_for_sorting_hack { #( #items )* }); + + // This semantically sorting business is a hack, for now. This means that we are + // re-parsing already generated code using `syn` (as opposed to `quote`) because + // `syn` provides us more control over the elements. + // One caveat is that some of the items coming from `quote`d output might have + // multiple items within them. Hence, we have to wrap the incoming in a `mod`. + // The two `unwrap`s here are deliberate because + // The first one won't panic because we build the `mod` and know it is there + // The scond one won't panic because we know original output has something in + // it already. let mut syn_parsed_items = syn::parse2::(module_wrapped_tokens) .unwrap() From f021cfdf55745824305c1dda38bc6945ba3555a2 Mon Sep 17 00:00:00 2001 From: Amanjeev Sethi Date: Fri, 12 Aug 2022 15:25:42 -0400 Subject: [PATCH 07/11] feat(lib sorting): missing period/full stop at the end Co-authored-by: Darren Kulp --- src/options.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/options.rs b/src/options.rs index e53e5272ff..83da21f42f 100644 --- a/src/options.rs +++ b/src/options.rs @@ -517,7 +517,7 @@ where .help("Enables generation of vtable functions."), Arg::new("sort-semantically") .long("sort-semantically") - .help("Enables sorting of code generation in a predefined manner"), + .help("Enables sorting of code generation in a predefined manner."), Arg::new("V") .long("version") .help("Prints the version, and exits"), From 96b645e9ac8b5068f0d28cdda4da76b9fa77d5e1 Mon Sep 17 00:00:00 2001 From: Amanjeev Sethi Date: Fri, 12 Aug 2022 15:25:58 -0400 Subject: [PATCH 08/11] feat(lib sorting): typo Co-authored-by: Darren Kulp --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 2cbb7ac2a2..287aba4166 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2465,7 +2465,7 @@ impl Bindings { // multiple items within them. Hence, we have to wrap the incoming in a `mod`. // The two `unwrap`s here are deliberate because // The first one won't panic because we build the `mod` and know it is there - // The scond one won't panic because we know original output has something in + // The second one won't panic because we know original output has something in // it already. let mut syn_parsed_items = syn::parse2::(module_wrapped_tokens) From e95764d464dda363fcb3143343be41e83501ea1e Mon Sep 17 00:00:00 2001 From: Amanjeev Sethi Date: Thu, 18 Aug 2022 22:07:17 -0400 Subject: [PATCH 09/11] feat(lib sorting): refactor test to use // bindgen-flags: method Signed-off-by: Amanjeev Sethi --- tests/expectations/tests/sorted-items.rs | 11 +++++++++-- tests/headers/{sorted => }/sorted-items.h | 2 ++ tests/tests.rs | 23 ----------------------- 3 files changed, 11 insertions(+), 25 deletions(-) rename tests/headers/{sorted => }/sorted-items.h (73%) diff --git a/tests/expectations/tests/sorted-items.rs b/tests/expectations/tests/sorted-items.rs index 9ba25d10a2..7df7c3d71e 100644 --- a/tests/expectations/tests/sorted-items.rs +++ b/tests/expectations/tests/sorted-items.rs @@ -1,12 +1,19 @@ +#![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, Copy, Clone)] +#[derive(Debug, Default, Copy, Clone)] pub struct Point { pub x: number, pub y: number, } #[repr(C)] -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Default, Copy, Clone)] pub struct Angle { pub a: number, pub b: number, diff --git a/tests/headers/sorted/sorted-items.h b/tests/headers/sorted-items.h similarity index 73% rename from tests/headers/sorted/sorted-items.h rename to tests/headers/sorted-items.h index 23962d18d8..11fc2ef40b 100644 --- a/tests/headers/sorted/sorted-items.h +++ b/tests/headers/sorted-items.h @@ -1,3 +1,5 @@ +// bindgen-flags: --sort-semantically -- --target=x86_64-unknown-linux + int foo(); typedef int number; int bar(number x); diff --git a/tests/tests.rs b/tests/tests.rs index 949f3a4ec5..cc64cfdd29 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -670,26 +670,3 @@ fn dump_preprocessed_input() { "cpp-empty-layout.hpp is in the preprocessed file" ); } - -#[test] -fn test_sort_semantically() { - let actual = builder() - .header(concat!( - env!("CARGO_MANIFEST_DIR"), - "/tests/headers/sorted/sorted-items.h" - )) - .clang_arg("--target=x86_64-unknown-linux") - .sort_semantically(true) - .generate() - .unwrap() - .to_string(); - - let (actual, _) = rustfmt(actual); - let expected = include_str!(concat!( - env!("CARGO_MANIFEST_DIR"), - "/tests/expectations/tests/sorted-items.rs" - )); - let (expected, _) = rustfmt(expected.into()); - - assert_eq!(actual, expected); -} From a64c197b1ad4cfeebd686f841eb68b37a895b26b Mon Sep 17 00:00:00 2001 From: Amanjeev Sethi Date: Thu, 18 Aug 2022 22:25:49 -0400 Subject: [PATCH 10/11] feat(lib sorting): CHANGELOG Signed-off-by: Amanjeev Sethi --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b9b164ae9..6c2e64e670 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -143,6 +143,8 @@ ## Added + * new feature: `--sort-semantically` flag to sort the output in a predefined manner [(#1743)] + ## Changed * clap has been updated, new msrv is 1.57. @@ -153,6 +155,8 @@ ## Security +[(#1743)]: https://github.com/rust-lang/rust-bindgen/issues/1743 + # 0.60.1 Released 2022/06/06 From bdd1f38f7c1029a33ec956d90af6857f8b2b3450 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Fri, 19 Aug 2022 11:14:14 -0500 Subject: [PATCH 11/11] add `warning` field to `Bindings` --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index caa50ed1fe..b90faba613 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2502,6 +2502,7 @@ impl Bindings { return Ok(Bindings { options, + warnings, module: quote! { #( #synful_items )* },