diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/codegen/mod.rs | 8 | ||||
-rw-r--r-- | src/ir/analysis/derive.rs | 1 | ||||
-rw-r--r-- | src/ir/annotations.rs | 9 | ||||
-rw-r--r-- | src/ir/context.rs | 6 | ||||
-rw-r--r-- | src/lib.rs | 13 | ||||
-rw-r--r-- | src/options.rs | 13 |
6 files changed, 47 insertions, 3 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 20bf54c4..00aa68f3 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -112,7 +112,7 @@ bitflags! { fn derives_of_item(item: &Item, ctx: &BindgenContext) -> DerivableTraits { let mut derivable_traits = DerivableTraits::empty(); - if item.can_derive_debug(ctx) { + if item.can_derive_debug(ctx) && !item.annotations().disallow_debug() { derivable_traits |= DerivableTraits::DEBUG; } @@ -1885,8 +1885,10 @@ impl CodeGenerator for CompInfo { let derivable_traits = derives_of_item(item, ctx); if !derivable_traits.contains(DerivableTraits::DEBUG) { - needs_debug_impl = - ctx.options().derive_debug && ctx.options().impl_debug + needs_debug_impl = ctx.options().derive_debug && + ctx.options().impl_debug && + !ctx.no_debug_by_name(item) && + !item.annotations().disallow_debug(); } if !derivable_traits.contains(DerivableTraits::DEFAULT) { diff --git a/src/ir/analysis/derive.rs b/src/ir/analysis/derive.rs index 6858d7f1..48c544f6 100644 --- a/src/ir/analysis/derive.rs +++ b/src/ir/analysis/derive.rs @@ -445,6 +445,7 @@ impl DeriveTrait { fn not_by_name(&self, ctx: &BindgenContext, item: &Item) -> bool { match self { DeriveTrait::Copy => ctx.no_copy_by_name(item), + DeriveTrait::Debug => ctx.no_debug_by_name(item), DeriveTrait::Hash => ctx.no_hash_by_name(item), DeriveTrait::PartialEqOrPartialOrd => { ctx.no_partialeq_by_name(item) diff --git a/src/ir/annotations.rs b/src/ir/annotations.rs index 98a0c042..da4ef4f7 100644 --- a/src/ir/annotations.rs +++ b/src/ir/annotations.rs @@ -38,6 +38,8 @@ pub struct Annotations { /// Manually disable deriving copy/clone on this type. Only applies to /// struct or union types. disallow_copy: bool, + /// Manually disable deriving debug on this type. + disallow_debug: bool, /// Whether fields should be marked as private or not. You can set this on /// structs (it will apply to all the fields), or individual fields. private_fields: Option<bool>, @@ -78,6 +80,7 @@ impl Default for Annotations { hide: false, use_instead_of: None, disallow_copy: false, + disallow_debug: false, private_fields: None, accessor_kind: None, constify_enum_variant: false, @@ -147,6 +150,11 @@ impl Annotations { self.disallow_copy } + /// Should we avoid implementing the `Debug` trait? + pub fn disallow_debug(&self) -> bool { + self.disallow_debug + } + /// Should the fields be private? pub fn private_fields(&self) -> Option<bool> { self.private_fields @@ -172,6 +180,7 @@ impl Annotations { "opaque" => self.opaque = true, "hide" => self.hide = true, "nocopy" => self.disallow_copy = true, + "nodebug" => self.disallow_debug = true, "replaces" => { self.use_instead_of = Some( attr.value.split("::").map(Into::into).collect(), diff --git a/src/ir/context.rs b/src/ir/context.rs index 38d73b7e..9513a41c 100644 --- a/src/ir/context.rs +++ b/src/ir/context.rs @@ -2635,6 +2635,12 @@ If you encounter an error missing from this list, please file an issue or a PR!" self.options().no_copy_types.matches(&name) } + /// Check if `--no-debug` flag is enabled for this item. + pub fn no_debug_by_name(&self, item: &Item) -> bool { + let name = item.path_for_whitelisting(self)[1..].join("::"); + self.options().no_debug_types.matches(&name) + } + /// Check if `--no-hash` flag is enabled for this item. pub fn no_hash_by_name(&self, item: &Item) -> bool { let name = item.path_for_whitelisting(self)[1..].join("::"); @@ -295,6 +295,7 @@ impl Builder { (&self.options.whitelisted_vars, "--whitelist-var"), (&self.options.no_partialeq_types, "--no-partialeq"), (&self.options.no_copy_types, "--no-copy"), + (&self.options.no_debug_types, "--no-debug"), (&self.options.no_hash_types, "--no-hash"), ]; @@ -1410,6 +1411,13 @@ impl Builder { self } + /// Don't derive `Debug` for a given type. Regular + /// expressions are supported. + pub fn no_debug<T: Into<String>>(mut self, arg: T) -> Self { + self.options.no_debug_types.insert(arg.into()); + self + } + /// Don't derive `Hash` for a given type. Regular /// expressions are supported. pub fn no_hash<T: Into<String>>(mut self, arg: T) -> Builder { @@ -1691,6 +1699,9 @@ struct BindgenOptions { /// The set of types that we should not derive `Copy` for. no_copy_types: RegexSet, + /// The set of types that we should not derive `Debug` for. + no_debug_types: RegexSet, + /// The set of types that we should not derive `Hash` for. no_hash_types: RegexSet, @@ -1727,6 +1738,7 @@ impl BindgenOptions { &mut self.new_type_alias_deref, &mut self.no_partialeq_types, &mut self.no_copy_types, + &mut self.no_debug_types, &mut self.no_hash_types, ]; let record_matches = self.record_matches; @@ -1824,6 +1836,7 @@ impl Default for BindgenOptions { rustfmt_configuration_file: None, no_partialeq_types: Default::default(), no_copy_types: Default::default(), + no_debug_types: Default::default(), no_hash_types: Default::default(), array_pointers_in_arguments: false, wasm_import_module_name: None, diff --git a/src/options.rs b/src/options.rs index 0add7b4c..13fbf7a4 100644 --- a/src/options.rs +++ b/src/options.rs @@ -430,6 +430,13 @@ where .takes_value(true) .multiple(true) .number_of_values(1), + Arg::with_name("no-debug") + .long("no-debug") + .help("Avoid deriving Debug for types matching <regex>.") + .value_name("regex") + .takes_value(true) + .multiple(true) + .number_of_values(1), Arg::with_name("no-hash") .long("no-hash") .help("Avoid deriving Hash for types matching <regex>.") @@ -831,6 +838,12 @@ where } } + if let Some(no_debug) = matches.values_of("no-debug") { + for regex in no_debug { + builder = builder.no_debug(regex); + } + } + if let Some(no_hash) = matches.values_of("no-hash") { for regex in no_hash { builder = builder.no_hash(regex); |