diff options
author | Emilio Cobos Álvarez <emilio@crisal.io> | 2017-01-24 09:49:49 +0100 |
---|---|---|
committer | Emilio Cobos Álvarez <emilio@crisal.io> | 2017-01-24 22:31:32 +0100 |
commit | 5d8019b7dc2f0581fc560fbc12d504c516a8476d (patch) | |
tree | 853da88643e0d15cfc53bfde7efd105291131fea | |
parent | d83e063a5edcac20a387d50f9a900106faec5e51 (diff) |
Honor and expose the derive_debug option.
Fixes #432
-rw-r--r-- | src/ir/item.rs | 2 | ||||
-rw-r--r-- | src/lib.rs | 6 | ||||
-rw-r--r-- | src/options.rs | 7 | ||||
-rw-r--r-- | tests/expectations/tests/no-derive-debug.rs | 26 | ||||
-rw-r--r-- | tests/headers/no-derive-debug.h | 15 |
5 files changed, 55 insertions, 1 deletions
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) @@ -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<I>(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<I>(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::<bar>() , 8usize); + assert_eq!(::std::mem::align_of::<bar>() , 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; +}; |