From 76c3797f43b536f2fbb541fdf7455de933768ea2 Mon Sep 17 00:00:00 2001 From: Jason Reed Date: Fri, 12 Oct 2018 17:31:36 -0400 Subject: Re-add ability to blacklist arbitrary identifiers (regardless of which sort of Item they are) --- src/ir/item.rs | 1 + src/lib.rs | 27 ++++++++++++++++++++++++ src/options.rs | 13 ++++++++++++ tests/expectations/tests/blacklist-identifier.rs | 8 +++++++ tests/headers/blacklist-identifier.h | 5 +++++ 5 files changed, 54 insertions(+) create mode 100644 tests/expectations/tests/blacklist-identifier.rs create mode 100644 tests/headers/blacklist-identifier.h diff --git a/src/ir/item.rs b/src/ir/item.rs index 92372031..396886e1 100644 --- a/src/ir/item.rs +++ b/src/ir/item.rs @@ -637,6 +637,7 @@ impl Item { let path = self.canonical_path(ctx); let name = path[1..].join("::"); + ctx.options().blacklisted_identifiers.matches(&name) || match self.kind { ItemKind::Type(..) => { ctx.options().blacklisted_types.matches(&name) || diff --git a/src/lib.rs b/src/lib.rs index b7608ce2..7149d2f6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -311,6 +311,20 @@ impl Builder { }) .count(); + self.options + .blacklisted_identifiers + .get_items() + .iter() + .map(|item| { + output_vector.push("--blacklist-identifier".into()); + output_vector.push( + item.trim_left_matches("^") + .trim_right_matches("$") + .into(), + ); + }) + .count(); + if !self.options.layout_tests { output_vector.push("--no-layout-tests".into()); } @@ -754,6 +768,13 @@ impl Builder { self } + /// Hide the given identifier from the generated bindings. Regular expressions + /// are supported. + pub fn blacklist_identifier>(mut self, arg: T) -> Builder { + self.options.blacklisted_identifiers.insert(arg); + self + } + /// Treat the given type as opaque in the generated bindings. Regular /// expressions are supported. pub fn opaque_type>(mut self, arg: T) -> Builder { @@ -1313,6 +1334,10 @@ struct BindgenOptions { /// in the generated code. blacklisted_functions: RegexSet, + /// The set of identifiers (regardless of Item type) that have + /// been blacklisted and should not appear in the generated code. + blacklisted_identifiers: RegexSet, + /// The set of types that should be treated as opaque structures in the /// generated code. opaque_types: RegexSet, @@ -1531,6 +1556,7 @@ impl BindgenOptions { self.whitelisted_functions.build(); self.blacklisted_types.build(); self.blacklisted_functions.build(); + self.blacklisted_identifiers.build(); self.opaque_types.build(); self.bitfield_enums.build(); self.constified_enums.build(); @@ -1564,6 +1590,7 @@ impl Default for BindgenOptions { rust_features: rust_target.into(), blacklisted_types: Default::default(), blacklisted_functions: Default::default(), + blacklisted_identifiers: Default::default(), opaque_types: Default::default(), rustfmt_path: Default::default(), whitelisted_types: Default::default(), diff --git a/src/options.rs b/src/options.rs index ce89c23c..7e5a5e67 100644 --- a/src/options.rs +++ b/src/options.rs @@ -78,6 +78,13 @@ where .takes_value(true) .multiple(true) .number_of_values(1), + Arg::with_name("blacklist-identifier") + .long("blacklist-identifier") + .help("Mark as hidden.") + .value_name("identifier") + .takes_value(true) + .multiple(true) + .number_of_values(1), Arg::with_name("no-layout-tests") .long("no-layout-tests") .help("Avoid generating layout tests for any type."), @@ -370,6 +377,12 @@ where } } + if let Some(hidden_identifiers) = matches.values_of("blacklist-identifier") { + for id in hidden_identifiers { + builder = builder.blacklist_identifier(id); + } + } + if matches.is_present("builtins") { builder = builder.emit_builtins(); } diff --git a/tests/expectations/tests/blacklist-identifier.rs b/tests/expectations/tests/blacklist-identifier.rs new file mode 100644 index 00000000..d6776794 --- /dev/null +++ b/tests/expectations/tests/blacklist-identifier.rs @@ -0,0 +1,8 @@ +/* automatically generated by rust-bindgen */ + +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] diff --git a/tests/headers/blacklist-identifier.h b/tests/headers/blacklist-identifier.h new file mode 100644 index 00000000..f48d50ad --- /dev/null +++ b/tests/headers/blacklist-identifier.h @@ -0,0 +1,5 @@ +// bindgen-flags: --blacklist-identifier "someFunction" --blacklist-identifier "SOME_DEFUN" --blacklist-identifier "someVar" + +void someFunction(); +extern int someVar; +#define SOME_DEFUN 123 -- cgit v1.2.3