From 5d8019b7dc2f0581fc560fbc12d504c516a8476d Mon Sep 17 00:00:00 2001 From: Emilio Cobos Álvarez Date: Tue, 24 Jan 2017 09:49:49 +0100 Subject: Honor and expose the derive_debug option. Fixes #432 --- src/ir/item.rs | 2 +- src/lib.rs | 6 ++++++ src/options.rs | 7 +++++++ tests/expectations/tests/no-derive-debug.rs | 26 ++++++++++++++++++++++++++ tests/headers/no-derive-debug.h | 15 +++++++++++++++ 5 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 tests/expectations/tests/no-derive-debug.rs create mode 100644 tests/headers/no-derive-debug.h diff --git a/src/ir/item.rs b/src/ir/item.rs index ac2d122e..7b18b331 100644 --- a/src/ir/item.rs +++ b/src/ir/item.rs @@ -219,7 +219,7 @@ impl CanDeriveDebug for Item { type Extra = (); fn can_derive_debug(&self, ctx: &BindgenContext, _: ()) -> bool { - match self.kind { + ctx.options().derive_debug && match self.kind { ItemKind::Type(ref ty) => { if self.is_opaque(ctx) { ty.layout(ctx) diff --git a/src/lib.rs b/src/lib.rs index 88da1a48..bb621718 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -268,6 +268,12 @@ impl Builder { self } + /// Set whether `Debug` should be derived by default. + pub fn derive_debug(mut self, doit: bool) -> Self { + self.options.derive_debug = doit; + self + } + /// Emit Clang AST. pub fn emit_clang_ast(mut self) -> Builder { self.options.emit_ast = true; diff --git a/src/options.rs b/src/options.rs index 3456bfea..c5c80d63 100644 --- a/src/options.rs +++ b/src/options.rs @@ -31,6 +31,9 @@ pub fn builder_from_flags(args: I) .takes_value(true) .multiple(true) .number_of_values(1), + Arg::with_name("no-derive-debug") + .long("no-derive-debug") + .help("Avoid deriving Debug on any type."), Arg::with_name("builtins") .long("builtins") .help("Output bindings for builtin definitions, e.g. \ @@ -181,6 +184,10 @@ pub fn builder_from_flags(args: I) builder = builder.emit_builtins(); } + if matches.is_present("no-derive-debug") { + builder = builder.derive_debug(false); + } + if let Some(prefix) = matches.value_of("ctypes-prefix") { builder = builder.ctypes_prefix(prefix); } diff --git a/tests/expectations/tests/no-derive-debug.rs b/tests/expectations/tests/no-derive-debug.rs new file mode 100644 index 00000000..e45b2678 --- /dev/null +++ b/tests/expectations/tests/no-derive-debug.rs @@ -0,0 +1,26 @@ +/* automatically generated by rust-bindgen */ + + +#![allow(non_snake_case)] + +#[repr(C)] #[derive(Copy, Clone)] pub struct foo { bar: ::std::os::raw::c_int, } + +/** + * bar should compile. It will normally derive debug, but our blacklist of foo + * and replacement for another type that doesn't implement it would prevent it + * from building if --no-derive-debug didn't work. + */ +#[repr(C)] +#[derive(Copy)] +pub struct bar { + pub foo: foo, + pub baz: ::std::os::raw::c_int, +} +#[test] +fn bindgen_test_layout_bar() { + assert_eq!(::std::mem::size_of::() , 8usize); + assert_eq!(::std::mem::align_of::() , 4usize); +} +impl Clone for bar { + fn clone(&self) -> Self { *self } +} diff --git a/tests/headers/no-derive-debug.h b/tests/headers/no-derive-debug.h new file mode 100644 index 00000000..4a49e404 --- /dev/null +++ b/tests/headers/no-derive-debug.h @@ -0,0 +1,15 @@ +// bindgen-flags: --no-derive-debug --blacklist-type foo --raw-line "#[repr(C)] #[derive(Copy, Clone)] pub struct foo { bar: ::std::os::raw::c_int, }" + +struct foo { + int bar; +}; + +/** + * bar should compile. It will normally derive debug, but our blacklist of foo + * and replacement for another type that doesn't implement it would prevent it + * from building if --no-derive-debug didn't work. + */ +struct bar { + struct foo foo; + int baz; +}; -- cgit v1.2.3