Skip to content

Commit

Permalink
feat: add blocklist_var
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhruvin Gandhi authored and emilio committed Jan 31, 2024
1 parent ee94c43 commit b7de6ee
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 3 deletions.
8 changes: 8 additions & 0 deletions bindgen-cli/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ struct BindgenCommand {
/// Mark FILE as hidden.
#[arg(long, value_name = "FILE")]
blocklist_file: Vec<String>,
/// Mark VAR as hidden.
#[arg(long, value_name = "VAR")]
blocklist_var: Vec<String>,
/// Avoid generating layout tests for any type.
#[arg(long)]
no_layout_tests: bool,
Expand Down Expand Up @@ -471,6 +474,7 @@ where
blocklist_function,
blocklist_item,
blocklist_file,
blocklist_var,
no_layout_tests,
no_derive_copy,
no_derive_debug,
Expand Down Expand Up @@ -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();
}
Expand Down
1 change: 1 addition & 0 deletions bindgen-tests/tests/expectations/tests/blocklist-var.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions bindgen-tests/tests/headers/blocklist-var.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// bindgen-flags: --blocklist-var should_be_blocked

extern int should_be_blocked;
7 changes: 5 additions & 2 deletions bindgen/ir/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
4 changes: 3 additions & 1 deletion bindgen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -483,6 +484,7 @@ impl BindgenOptions {
"--blocklist-function",
"--blocklist-item",
"--blocklist-file",
"--blocklist-var",
"--opaque-type",
"--allowlist-type",
"--allowlist-function",
Expand Down
16 changes: 16 additions & 0 deletions bindgen/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: AsRef<str>>(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: {
Expand Down
2 changes: 2 additions & 0 deletions book/src/blocklisting.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ 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

* `--blocklist-file <path>`
* `--blocklist-function <function>`
* `--blocklist-item <item>`
* `--blocklist-type <type>`
* `--blocklist-var <var>`


### Annotations
Expand Down

0 comments on commit b7de6ee

Please sign in to comment.