Skip to content

Commit 0c07e9e

Browse files
author
bors-servo
authored
Auto merge of #434 - emilio:no-derive-debug, r=fitzgen
Honor and expose the derive_debug option. Fixes #432 r? @fitzgen
2 parents 0318b74 + 5d8019b commit 0c07e9e

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

src/ir/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl CanDeriveDebug for Item {
219219
type Extra = ();
220220

221221
fn can_derive_debug(&self, ctx: &BindgenContext, _: ()) -> bool {
222-
match self.kind {
222+
ctx.options().derive_debug && match self.kind {
223223
ItemKind::Type(ref ty) => {
224224
if self.is_opaque(ctx) {
225225
ty.layout(ctx)

src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,12 @@ impl Builder {
268268
self
269269
}
270270

271+
/// Set whether `Debug` should be derived by default.
272+
pub fn derive_debug(mut self, doit: bool) -> Self {
273+
self.options.derive_debug = doit;
274+
self
275+
}
276+
271277
/// Emit Clang AST.
272278
pub fn emit_clang_ast(mut self) -> Builder {
273279
self.options.emit_ast = true;

src/options.rs

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ pub fn builder_from_flags<I>(args: I)
3131
.takes_value(true)
3232
.multiple(true)
3333
.number_of_values(1),
34+
Arg::with_name("no-derive-debug")
35+
.long("no-derive-debug")
36+
.help("Avoid deriving Debug on any type."),
3437
Arg::with_name("builtins")
3538
.long("builtins")
3639
.help("Output bindings for builtin definitions, e.g. \
@@ -181,6 +184,10 @@ pub fn builder_from_flags<I>(args: I)
181184
builder = builder.emit_builtins();
182185
}
183186

187+
if matches.is_present("no-derive-debug") {
188+
builder = builder.derive_debug(false);
189+
}
190+
184191
if let Some(prefix) = matches.value_of("ctypes-prefix") {
185192
builder = builder.ctypes_prefix(prefix);
186193
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(non_snake_case)]
5+
6+
#[repr(C)] #[derive(Copy, Clone)] pub struct foo { bar: ::std::os::raw::c_int, }
7+
8+
/**
9+
* bar should compile. It will normally derive debug, but our blacklist of foo
10+
* and replacement for another type that doesn't implement it would prevent it
11+
* from building if --no-derive-debug didn't work.
12+
*/
13+
#[repr(C)]
14+
#[derive(Copy)]
15+
pub struct bar {
16+
pub foo: foo,
17+
pub baz: ::std::os::raw::c_int,
18+
}
19+
#[test]
20+
fn bindgen_test_layout_bar() {
21+
assert_eq!(::std::mem::size_of::<bar>() , 8usize);
22+
assert_eq!(::std::mem::align_of::<bar>() , 4usize);
23+
}
24+
impl Clone for bar {
25+
fn clone(&self) -> Self { *self }
26+
}

tests/headers/no-derive-debug.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// bindgen-flags: --no-derive-debug --blacklist-type foo --raw-line "#[repr(C)] #[derive(Copy, Clone)] pub struct foo { bar: ::std::os::raw::c_int, }"
2+
3+
struct foo {
4+
int bar;
5+
};
6+
7+
/**
8+
* bar should compile. It will normally derive debug, but our blacklist of foo
9+
* and replacement for another type that doesn't implement it would prevent it
10+
* from building if --no-derive-debug didn't work.
11+
*/
12+
struct bar {
13+
struct foo foo;
14+
int baz;
15+
};

0 commit comments

Comments
 (0)