From b7de6ee766fdb3b2f2acdba6e31cd7eea0a62bbb Mon Sep 17 00:00:00 2001 From: Dhruvin Gandhi Date: Tue, 23 Jan 2024 13:22:45 +0530 Subject: [PATCH] feat: add blocklist_var --- bindgen-cli/options.rs | 8 ++++++++ .../tests/expectations/tests/blocklist-var.rs | 1 + bindgen-tests/tests/headers/blocklist-var.hpp | 3 +++ bindgen/ir/item.rs | 7 +++++-- bindgen/lib.rs | 4 +++- bindgen/options/mod.rs | 16 ++++++++++++++++ book/src/blocklisting.md | 2 ++ 7 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 bindgen-tests/tests/expectations/tests/blocklist-var.rs create mode 100644 bindgen-tests/tests/headers/blocklist-var.hpp diff --git a/bindgen-cli/options.rs b/bindgen-cli/options.rs index f82d391901..66bb3f1439 100644 --- a/bindgen-cli/options.rs +++ b/bindgen-cli/options.rs @@ -156,6 +156,9 @@ struct BindgenCommand { /// Mark FILE as hidden. #[arg(long, value_name = "FILE")] blocklist_file: Vec, + /// Mark VAR as hidden. + #[arg(long, value_name = "VAR")] + blocklist_var: Vec, /// Avoid generating layout tests for any type. #[arg(long)] no_layout_tests: bool, @@ -471,6 +474,7 @@ where blocklist_function, blocklist_item, blocklist_file, + blocklist_var, no_layout_tests, no_derive_copy, no_derive_debug, @@ -676,6 +680,10 @@ where builder = builder.blocklist_file(file); } + for var in blocklist_var { + builder = builder.blocklist_var(var); + } + if builtins { builder = builder.emit_builtins(); } diff --git a/bindgen-tests/tests/expectations/tests/blocklist-var.rs b/bindgen-tests/tests/expectations/tests/blocklist-var.rs new file mode 100644 index 0000000000..fe64295a68 --- /dev/null +++ b/bindgen-tests/tests/expectations/tests/blocklist-var.rs @@ -0,0 +1 @@ +#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] \ No newline at end of file diff --git a/bindgen-tests/tests/headers/blocklist-var.hpp b/bindgen-tests/tests/headers/blocklist-var.hpp new file mode 100644 index 0000000000..3c70e6f032 --- /dev/null +++ b/bindgen-tests/tests/headers/blocklist-var.hpp @@ -0,0 +1,3 @@ +// bindgen-flags: --blocklist-var should_be_blocked + +extern int should_be_blocked; diff --git a/bindgen/ir/item.rs b/bindgen/ir/item.rs index dd587b088b..2941eb81e2 100644 --- a/bindgen/ir/item.rs +++ b/bindgen/ir/item.rs @@ -668,8 +668,11 @@ impl Item { ItemKind::Function(..) => { ctx.options().blocklisted_functions.matches(&name) } - // TODO: Add constant / namespace blocklisting? - ItemKind::Var(..) | ItemKind::Module(..) => false, + ItemKind::Var(..) => { + ctx.options().blocklisted_vars.matches(&name) + } + // TODO: Add namespace blocklisting? + ItemKind::Module(..) => false, } } diff --git a/bindgen/lib.rs b/bindgen/lib.rs index 11c2fa2c4e..c4ab11486c 100644 --- a/bindgen/lib.rs +++ b/bindgen/lib.rs @@ -440,13 +440,14 @@ impl Builder { impl BindgenOptions { fn build(&mut self) { - const REGEX_SETS_LEN: usize = 28; + const REGEX_SETS_LEN: usize = 29; let regex_sets: [_; REGEX_SETS_LEN] = [ &mut self.blocklisted_types, &mut self.blocklisted_functions, &mut self.blocklisted_items, &mut self.blocklisted_files, + &mut self.blocklisted_vars, &mut self.opaque_types, &mut self.allowlisted_vars, &mut self.allowlisted_types, @@ -483,6 +484,7 @@ impl BindgenOptions { "--blocklist-function", "--blocklist-item", "--blocklist-file", + "--blocklist-var", "--opaque-type", "--allowlist-type", "--allowlist-function", diff --git a/bindgen/options/mod.rs b/bindgen/options/mod.rs index 3a82bcf397..1fc2241615 100644 --- a/bindgen/options/mod.rs +++ b/bindgen/options/mod.rs @@ -222,6 +222,22 @@ options! { }, as_args: "--blocklist-file", }, + /// Variables that have been blocklisted and should not appear in the generated code. + blocklisted_vars: RegexSet { + methods: { + regex_option! { + /// Do not generate any bindings for the given variable. + /// + /// This option is not recursive, meaning that it will only block variables whose + /// names explicitly match the argument of this method. + pub fn blocklist_var>(mut self, arg: T) -> Builder { + self.options.blocklisted_vars.insert(arg); + self + } + } + }, + as_args: "--blocklist-var", + }, /// Types that should be treated as opaque structures in the generated code. opaque_types: RegexSet { methods: { diff --git a/book/src/blocklisting.md b/book/src/blocklisting.md index 6eb5786328..535a08c8d5 100644 --- a/book/src/blocklisting.md +++ b/book/src/blocklisting.md @@ -22,6 +22,7 @@ that are transitively included. * [`bindgen::Builder::blocklist_function`](https://docs.rs/bindgen/latest/bindgen/struct.Builder.html#method.blocklist_function) * [`bindgen::Builder::blocklist_item`](https://docs.rs/bindgen/latest/bindgen/struct.Builder.html#method.blocklist_item) * [`bindgen::Builder::blocklist_type`](https://docs.rs/bindgen/latest/bindgen/struct.Builder.html#method.blocklist_type) +* [`bindgen::Builder::blocklist_var`](https://docs.rs/bindgen/latest/bindgen/struct.Builder.html#method.blocklist_var) ### Command Line @@ -29,6 +30,7 @@ that are transitively included. * `--blocklist-function ` * `--blocklist-item ` * `--blocklist-type ` +* `--blocklist-var ` ### Annotations