diff options
author | Jethro Beekman <jethro@fortanix.com> | 2021-03-10 19:16:47 +0100 |
---|---|---|
committer | Jethro Beekman <jethro@fortanix.com> | 2021-03-22 12:10:28 +0100 |
commit | 0780f804a89309417b6c99ccfc5d5941bdd38bab (patch) | |
tree | 55dacc667da5f09f4bb9381df08e8d94677ef2e0 /tests | |
parent | 2a46e2924256b229653a558435259ca4968d5bd9 (diff) |
Add callback to check derives for blocklisted types
Fixes #1454 #2003
Diffstat (limited to 'tests')
-rw-r--r-- | tests/expectations/tests/issue-1454.rs | 41 | ||||
-rw-r--r-- | tests/headers/issue-1454.h | 10 | ||||
-rw-r--r-- | tests/parse_callbacks/mod.rs | 24 |
3 files changed, 73 insertions, 2 deletions
diff --git a/tests/expectations/tests/issue-1454.rs b/tests/expectations/tests/issue-1454.rs new file mode 100644 index 00000000..e88e4697 --- /dev/null +++ b/tests/expectations/tests/issue-1454.rs @@ -0,0 +1,41 @@ +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] + +#[repr(C)] +#[derive(Debug)] +pub struct extern_type; + +#[repr(C)] +#[derive(Debug)] +pub struct local_type { + pub inner: extern_type, +} +#[test] +fn bindgen_test_layout_local_type() { + assert_eq!( + ::std::mem::size_of::<local_type>(), + 0usize, + concat!("Size of: ", stringify!(local_type)) + ); + assert_eq!( + ::std::mem::align_of::<local_type>(), + 1usize, + concat!("Alignment of ", stringify!(local_type)) + ); + assert_eq!( + unsafe { + &(*(::std::ptr::null::<local_type>())).inner as *const _ as usize + }, + 0usize, + concat!( + "Offset of field: ", + stringify!(local_type), + "::", + stringify!(inner) + ) + ); +} diff --git a/tests/headers/issue-1454.h b/tests/headers/issue-1454.h new file mode 100644 index 00000000..96645dac --- /dev/null +++ b/tests/headers/issue-1454.h @@ -0,0 +1,10 @@ +// bindgen-flags: --no-recursive-allowlist --allowlist-type "local_type" --with-derive-hash --no-derive-copy --no-derive-default --raw-line "#[repr(C)] #[derive(Debug)] pub struct extern_type;" +// bindgen-parse-callbacks: blocklisted-type-implements-trait + +struct extern_type {}; + +typedef struct +{ + struct extern_type inner; +} +local_type; diff --git a/tests/parse_callbacks/mod.rs b/tests/parse_callbacks/mod.rs index 01993cfc..b94b54de 100644 --- a/tests/parse_callbacks/mod.rs +++ b/tests/parse_callbacks/mod.rs @@ -1,4 +1,4 @@ -use bindgen::callbacks::ParseCallbacks; +use bindgen::callbacks::*; #[derive(Debug)] struct EnumVariantRename; @@ -8,15 +8,35 @@ impl ParseCallbacks for EnumVariantRename { &self, _enum_name: Option<&str>, original_variant_name: &str, - _variant_value: bindgen::callbacks::EnumVariantValue, + _variant_value: EnumVariantValue, ) -> Option<String> { Some(format!("RENAMED_{}", original_variant_name)) } } +#[derive(Debug)] +struct BlocklistedTypeImplementsTrait; + +impl ParseCallbacks for BlocklistedTypeImplementsTrait { + fn blocklisted_type_implements_trait( + &self, + _name: &str, + derive_trait: DeriveTrait, + ) -> Option<ImplementsTrait> { + if derive_trait == DeriveTrait::Hash { + Some(ImplementsTrait::No) + } else { + Some(ImplementsTrait::Yes) + } + } +} + pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> { match cb { "enum-variant-rename" => Box::new(EnumVariantRename), + "blocklisted-type-implements-trait" => { + Box::new(BlocklistedTypeImplementsTrait) + } _ => panic!("Couldn't find name ParseCallbacks: {}", cb), } } |