diff options
author | Nick Fitzgerald <fitzgen@gmail.com> | 2017-09-05 11:44:01 -0700 |
---|---|---|
committer | Nick Fitzgerald <fitzgen@gmail.com> | 2017-09-06 13:46:58 -0700 |
commit | 310890c828c1b9d6c3de8bb61e67080e7d1f1966 (patch) | |
tree | 5cb2914c0e421589662d32262915230c6a78520b | |
parent | c1965e75c1c862aaf3d63c2cfd314be01d096dcf (diff) |
Don't try and debug format blacklisted types when generating `impl Debug` blocks
-rw-r--r-- | src/codegen/derive_debug.rs | 10 | ||||
-rw-r--r-- | tests/expectations/tests/blacklist-and-impl-debug.rs | 34 | ||||
-rw-r--r-- | tests/headers/blacklist-and-impl-debug.hpp | 10 |
3 files changed, 52 insertions, 2 deletions
diff --git a/src/codegen/derive_debug.rs b/src/codegen/derive_debug.rs index 066af339..82456797 100644 --- a/src/codegen/derive_debug.rs +++ b/src/codegen/derive_debug.rs @@ -53,7 +53,7 @@ pub fn gen_debug_impl( impl X { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { write!(f, $format_string $tokens) - } + } }); match impl_.unwrap().node { @@ -127,6 +127,12 @@ impl<'a> ImplDebug<'a> for Item { ) -> Option<(String, Vec<TokenTree>)> { let name_ident = ctx.rust_ident_raw(name); + // We don't know if blacklisted items `impl Debug` or not, so we can't + // add them to the format string we're building up. + if !ctx.whitelisted_items().contains(&self.id()) { + return None; + } + let ty = match self.as_type() { Some(ty) => ty, None => { @@ -168,7 +174,7 @@ impl<'a> ImplDebug<'a> for Item { } else { debug_print(ctx, name, name_ident) } - } + } // The generic is not required to implement Debug, so we can not debug print that type TypeKind::TypeParam => { diff --git a/tests/expectations/tests/blacklist-and-impl-debug.rs b/tests/expectations/tests/blacklist-and-impl-debug.rs new file mode 100644 index 00000000..d84b0256 --- /dev/null +++ b/tests/expectations/tests/blacklist-and-impl-debug.rs @@ -0,0 +1,34 @@ +/* automatically generated by rust-bindgen */ + + +#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] + +pub struct BlacklistMe(u8); + +/// Because this type contains a blacklisted type, it should not derive Debug. +#[repr(C)] +pub struct ShouldManuallyImplDebug { + pub a: BlacklistMe, +} +#[test] +fn bindgen_test_layout_ShouldManuallyImplDebug() { + assert_eq!(::std::mem::size_of::<ShouldManuallyImplDebug>() , 1usize , + concat ! ( + "Size of: " , stringify ! ( ShouldManuallyImplDebug ) )); + assert_eq! (::std::mem::align_of::<ShouldManuallyImplDebug>() , 1usize , + concat ! ( + "Alignment of " , stringify ! ( ShouldManuallyImplDebug ) )); + assert_eq! (unsafe { + & ( * ( 0 as * const ShouldManuallyImplDebug ) ) . a as * + const _ as usize } , 0usize , concat ! ( + "Alignment of field: " , stringify ! ( ShouldManuallyImplDebug + ) , "::" , stringify ! ( a ) )); +} +impl Default for ShouldManuallyImplDebug { + fn default() -> Self { unsafe { ::std::mem::zeroed() } } +} +impl ::std::fmt::Debug for ShouldManuallyImplDebug { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + write!(f , "ShouldManuallyImplDebug {{ }}") + } +} diff --git a/tests/headers/blacklist-and-impl-debug.hpp b/tests/headers/blacklist-and-impl-debug.hpp new file mode 100644 index 00000000..b4b39fe4 --- /dev/null +++ b/tests/headers/blacklist-and-impl-debug.hpp @@ -0,0 +1,10 @@ +// bindgen-flags: --impl-debug --blacklist-type BlacklistMe --raw-line 'pub struct BlacklistMe(u8);' + +struct BlacklistMe {}; + +/** + * Because this type contains a blacklisted type, it should not derive Debug. + */ +struct ShouldManuallyImplDebug { + BlacklistMe a; +}; |