Skip to content

Commit b7de6ee

Browse files
Dhruvin Gandhiemilio
Dhruvin Gandhi
authored andcommitted
feat: add blocklist_var
1 parent ee94c43 commit b7de6ee

File tree

7 files changed

+38
-3
lines changed

7 files changed

+38
-3
lines changed

bindgen-cli/options.rs

+8
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ struct BindgenCommand {
156156
/// Mark FILE as hidden.
157157
#[arg(long, value_name = "FILE")]
158158
blocklist_file: Vec<String>,
159+
/// Mark VAR as hidden.
160+
#[arg(long, value_name = "VAR")]
161+
blocklist_var: Vec<String>,
159162
/// Avoid generating layout tests for any type.
160163
#[arg(long)]
161164
no_layout_tests: bool,
@@ -471,6 +474,7 @@ where
471474
blocklist_function,
472475
blocklist_item,
473476
blocklist_file,
477+
blocklist_var,
474478
no_layout_tests,
475479
no_derive_copy,
476480
no_derive_debug,
@@ -676,6 +680,10 @@ where
676680
builder = builder.blocklist_file(file);
677681
}
678682

683+
for var in blocklist_var {
684+
builder = builder.blocklist_var(var);
685+
}
686+
679687
if builtins {
680688
builder = builder.emit_builtins();
681689
}

bindgen-tests/tests/expectations/tests/blocklist-var.rs

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// bindgen-flags: --blocklist-var should_be_blocked
2+
3+
extern int should_be_blocked;

bindgen/ir/item.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,11 @@ impl Item {
668668
ItemKind::Function(..) => {
669669
ctx.options().blocklisted_functions.matches(&name)
670670
}
671-
// TODO: Add constant / namespace blocklisting?
672-
ItemKind::Var(..) | ItemKind::Module(..) => false,
671+
ItemKind::Var(..) => {
672+
ctx.options().blocklisted_vars.matches(&name)
673+
}
674+
// TODO: Add namespace blocklisting?
675+
ItemKind::Module(..) => false,
673676
}
674677
}
675678

bindgen/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -440,13 +440,14 @@ impl Builder {
440440

441441
impl BindgenOptions {
442442
fn build(&mut self) {
443-
const REGEX_SETS_LEN: usize = 28;
443+
const REGEX_SETS_LEN: usize = 29;
444444

445445
let regex_sets: [_; REGEX_SETS_LEN] = [
446446
&mut self.blocklisted_types,
447447
&mut self.blocklisted_functions,
448448
&mut self.blocklisted_items,
449449
&mut self.blocklisted_files,
450+
&mut self.blocklisted_vars,
450451
&mut self.opaque_types,
451452
&mut self.allowlisted_vars,
452453
&mut self.allowlisted_types,
@@ -483,6 +484,7 @@ impl BindgenOptions {
483484
"--blocklist-function",
484485
"--blocklist-item",
485486
"--blocklist-file",
487+
"--blocklist-var",
486488
"--opaque-type",
487489
"--allowlist-type",
488490
"--allowlist-function",

bindgen/options/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,22 @@ options! {
222222
},
223223
as_args: "--blocklist-file",
224224
},
225+
/// Variables that have been blocklisted and should not appear in the generated code.
226+
blocklisted_vars: RegexSet {
227+
methods: {
228+
regex_option! {
229+
/// Do not generate any bindings for the given variable.
230+
///
231+
/// This option is not recursive, meaning that it will only block variables whose
232+
/// names explicitly match the argument of this method.
233+
pub fn blocklist_var<T: AsRef<str>>(mut self, arg: T) -> Builder {
234+
self.options.blocklisted_vars.insert(arg);
235+
self
236+
}
237+
}
238+
},
239+
as_args: "--blocklist-var",
240+
},
225241
/// Types that should be treated as opaque structures in the generated code.
226242
opaque_types: RegexSet {
227243
methods: {

book/src/blocklisting.md

+2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ that are transitively included.
2222
* [`bindgen::Builder::blocklist_function`](https://docs.rs/bindgen/latest/bindgen/struct.Builder.html#method.blocklist_function)
2323
* [`bindgen::Builder::blocklist_item`](https://docs.rs/bindgen/latest/bindgen/struct.Builder.html#method.blocklist_item)
2424
* [`bindgen::Builder::blocklist_type`](https://docs.rs/bindgen/latest/bindgen/struct.Builder.html#method.blocklist_type)
25+
* [`bindgen::Builder::blocklist_var`](https://docs.rs/bindgen/latest/bindgen/struct.Builder.html#method.blocklist_var)
2526

2627
### Command Line
2728

2829
* `--blocklist-file <path>`
2930
* `--blocklist-function <function>`
3031
* `--blocklist-item <item>`
3132
* `--blocklist-type <type>`
33+
* `--blocklist-var <var>`
3234

3335

3436
### Annotations

0 commit comments

Comments
 (0)