Skip to content

Commit 4c4f144

Browse files
committed
[Close rust-lang#961] Add --no-debug <regex> flag
1 parent c3ec58c commit 4c4f144

10 files changed

+176
-1
lines changed

src/ir/analysis/derive_debug.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ impl<'ctx> MonotoneFramework for CannotDeriveDebug<'ctx> {
144144
}
145145
};
146146

147+
if self.ctx.no_debug_by_name(&item) {
148+
return self.insert(id);
149+
}
150+
147151
if item.is_opaque(self.ctx, &()) {
148152
let layout_can_derive = ty.layout(self.ctx).map_or(true, |l| {
149153
l.opaque().can_trivially_derive_debug()

src/ir/context.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2500,6 +2500,12 @@ impl BindgenContext {
25002500
self.options().no_copy_types.matches(&name)
25012501
}
25022502

2503+
/// Check if `--no-debug` flag is enabled for this item.
2504+
pub fn no_debug_by_name(&self, item: &Item) -> bool {
2505+
let name = item.canonical_path(self)[1..].join("::");
2506+
self.options().no_debug_types.matches(&name)
2507+
}
2508+
25032509
/// Chech if `--no-hash` flag is enabled for this item.
25042510
pub fn no_hash_by_name(&self, item: &Item) -> bool {
25052511
let name = item.canonical_path(self)[1..].join("::");

src/lib.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,20 @@ impl Builder {
538538
})
539539
.count();
540540

541+
self.options
542+
.no_debug_types
543+
.get_items()
544+
.iter()
545+
.map(|item| {
546+
output_vector.push("--no-debug".into());
547+
output_vector.push(
548+
item.trim_left_matches("^")
549+
.trim_right_matches("$")
550+
.into(),
551+
);
552+
})
553+
.count();
554+
541555
self.options
542556
.no_hash_types
543557
.get_items()
@@ -1196,6 +1210,13 @@ impl Builder {
11961210
self
11971211
}
11981212

1213+
/// Don't derive `Debug` for a given type. Regular
1214+
/// expressions are supported.
1215+
pub fn no_debug(mut self, arg: String) -> Self {
1216+
self.options.no_debug_types.insert(arg);
1217+
self
1218+
}
1219+
11991220
/// Don't derive `Hash` for a given type. Regular
12001221
/// expressions are supported.
12011222
pub fn no_hash(mut self, arg: String) -> Builder {
@@ -1384,7 +1405,6 @@ struct BindgenOptions {
13841405

13851406
/// The absolute path to the rustfmt configuration file, if None, the standard rustfmt
13861407
/// options are used.
1387-
13881408
rustfmt_configuration_file: Option<PathBuf>,
13891409

13901410
/// The set of types that we should not derive `PartialEq` for.
@@ -1393,6 +1413,9 @@ struct BindgenOptions {
13931413
/// The set of types that we should not derive `Copy` for.
13941414
no_copy_types: RegexSet,
13951415

1416+
/// The set of types that we should not derive `Debug` for.
1417+
no_debug_types: RegexSet,
1418+
13961419
/// The set of types that we should not derive `Hash` for.
13971420
no_hash_types: RegexSet,
13981421
}
@@ -1414,6 +1437,7 @@ impl BindgenOptions {
14141437
self.rustified_enums.build();
14151438
self.no_partialeq_types.build();
14161439
self.no_copy_types.build();
1440+
self.no_debug_types.build();
14171441
self.no_hash_types.build();
14181442
}
14191443

@@ -1487,6 +1511,7 @@ impl Default for BindgenOptions {
14871511
rustfmt_configuration_file: None,
14881512
no_partialeq_types: Default::default(),
14891513
no_copy_types: Default::default(),
1514+
no_debug_types: Default::default(),
14901515
no_hash_types: Default::default(),
14911516
}
14921517
}

src/options.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,13 @@ where
292292
.takes_value(true)
293293
.multiple(true)
294294
.number_of_values(1),
295+
Arg::with_name("no-debug")
296+
.long("no-debug")
297+
.help("Avoid deriving Debug for types matching <regex>.")
298+
.value_name("regex")
299+
.takes_value(true)
300+
.multiple(true)
301+
.number_of_values(1),
295302
Arg::with_name("no-hash")
296303
.long("no-hash")
297304
.help("Avoid deriving Hash for types matching <regex>.")
@@ -599,6 +606,12 @@ where
599606
}
600607
}
601608

609+
if let Some(no_debug) = matches.values_of("no-debug") {
610+
for regex in no_debug {
611+
builder = builder.no_debug(String::from(regex));
612+
}
613+
}
614+
602615
if let Some(no_hash) = matches.values_of("no-hash") {
603616
for regex in no_hash {
604617
builder = builder.no_hash(String::from(regex));
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
5+
6+
7+
#[repr(C)]
8+
#[derive(Default, Copy, Clone)]
9+
pub struct NoDebug {
10+
pub _bindgen_opaque_blob: u32,
11+
}
12+
#[test]
13+
fn bindgen_test_layout_NoDebug() {
14+
assert_eq!(
15+
::std::mem::size_of::<NoDebug>(),
16+
4usize,
17+
concat!("Size of: ", stringify!(NoDebug))
18+
);
19+
assert_eq!(
20+
::std::mem::align_of::<NoDebug>(),
21+
4usize,
22+
concat!("Alignment of ", stringify!(NoDebug))
23+
);
24+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
5+
6+
7+
#[repr(C)]
8+
#[derive(Default, Copy, Clone)]
9+
pub struct NoDebug {
10+
pub i: ::std::os::raw::c_int,
11+
}
12+
#[test]
13+
fn bindgen_test_layout_NoDebug() {
14+
assert_eq!(
15+
::std::mem::size_of::<NoDebug>(),
16+
4usize,
17+
concat!("Size of: ", stringify!(NoDebug))
18+
);
19+
assert_eq!(
20+
::std::mem::align_of::<NoDebug>(),
21+
4usize,
22+
concat!("Alignment of ", stringify!(NoDebug))
23+
);
24+
assert_eq!(
25+
unsafe { &(*(::std::ptr::null::<NoDebug>())).i as *const _ as usize },
26+
0usize,
27+
concat!(
28+
"Offset of field: ",
29+
stringify!(NoDebug),
30+
"::",
31+
stringify!(i)
32+
)
33+
);
34+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
5+
6+
7+
#[repr(C)]
8+
#[derive(Default, Copy, Clone)]
9+
pub struct NoDebug {
10+
pub _address: u8,
11+
}
12+
#[test]
13+
fn bindgen_test_layout_NoDebug() {
14+
assert_eq!(
15+
::std::mem::size_of::<NoDebug>(),
16+
1usize,
17+
concat!("Size of: ", stringify!(NoDebug))
18+
);
19+
assert_eq!(
20+
::std::mem::align_of::<NoDebug>(),
21+
1usize,
22+
concat!("Alignment of ", stringify!(NoDebug))
23+
);
24+
}
25+
#[repr(C)]
26+
#[derive(Default, Copy, Clone)]
27+
pub struct WhitelistMe {
28+
pub a: NoDebug,
29+
}
30+
#[test]
31+
fn bindgen_test_layout_WhitelistMe() {
32+
assert_eq!(
33+
::std::mem::size_of::<WhitelistMe>(),
34+
1usize,
35+
concat!("Size of: ", stringify!(WhitelistMe))
36+
);
37+
assert_eq!(
38+
::std::mem::align_of::<WhitelistMe>(),
39+
1usize,
40+
concat!("Alignment of ", stringify!(WhitelistMe))
41+
);
42+
assert_eq!(
43+
unsafe { &(*(::std::ptr::null::<WhitelistMe>())).a as *const _ as usize },
44+
0usize,
45+
concat!(
46+
"Offset of field: ",
47+
stringify!(WhitelistMe),
48+
"::",
49+
stringify!(a)
50+
)
51+
);
52+
}

tests/headers/no-debug-opaque.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// bindgen-flags: --opaque-type "NoDebug" --no-debug "NoDebug"
2+
3+
class NoDebug {
4+
int i;
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// bindgen-flags: --whitelist-type "NoDebug" --no-debug "NoDebug"
2+
3+
class NoDebug {
4+
int i;
5+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// bindgen-flags: --whitelist-type "WhitelistMe" --no-debug "NoDebug"
2+
3+
struct NoDebug {};
4+
5+
class WhitelistMe {
6+
NoDebug a;
7+
};

0 commit comments

Comments
 (0)