Skip to content

Commit 820ca42

Browse files
authored
Add --allowlist-item (#2601)
* Add `allowlist_item` method and flag * Add `allowlist_item` basic functionality * Add test * Update changelog
1 parent bb8db7e commit 820ca42

File tree

7 files changed

+92
-6
lines changed

7 files changed

+92
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@
172172

173173
## Added
174174
- The `system` ABI is now supported as an option for the `--override-abi` flag.
175+
- The `allowlist_item` method and the `--allowlist-item` flag have been
176+
included to filter items regardless or their kind.
175177
## Changed
176178
- The `Clone` implementation for `_BindgenUnionField` has been changed to pass
177179
the `incorrect_clone_impl_on_copy_type` Clippy lint.

bindgen-cli/options.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ struct BindgenCommand {
258258
/// Allowlist all contents of PATH.
259259
#[arg(long, value_name = "PATH")]
260260
allowlist_file: Vec<String>,
261+
/// Allowlist all items matching REGEX. Other non-allowlisted items will not be generated.
262+
#[arg(long, value_name = "REGEX")]
263+
allowlist_item: Vec<String>,
261264
/// Print verbose error messages.
262265
#[arg(long)]
263266
verbose: bool,
@@ -471,6 +474,7 @@ where
471474
allowlist_type,
472475
allowlist_var,
473476
allowlist_file,
477+
allowlist_item,
474478
verbose,
475479
dump_preprocessed_input,
476480
no_record_matches,
@@ -829,6 +833,10 @@ where
829833
builder = builder.allowlist_file(file);
830834
}
831835

836+
for item in allowlist_item {
837+
builder = builder.allowlist_item(item);
838+
}
839+
832840
for arg in clang_args {
833841
builder = builder.clang_arg(arg);
834842
}

bindgen-tests/tests/expectations/tests/allowlist_item.rs

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// bindgen-flags: --allowlist-item 'Foo.*'
2+
3+
struct Foo {
4+
int field;
5+
};
6+
7+
struct Foo FooNew(int value);
8+
9+
#define FooDefault 0
10+
11+
struct Bar {
12+
int field;
13+
};
14+
15+
struct Foo BarNew(int value);
16+
17+
#define BarDefault 0

bindgen/ir/context.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2343,7 +2343,8 @@ If you encounter an error missing from this list, please file an issue or a PR!"
23432343
if self.options().allowlisted_types.is_empty() &&
23442344
self.options().allowlisted_functions.is_empty() &&
23452345
self.options().allowlisted_vars.is_empty() &&
2346-
self.options().allowlisted_files.is_empty()
2346+
self.options().allowlisted_files.is_empty() &&
2347+
self.options().allowlisted_items.is_empty()
23472348
{
23482349
return true;
23492350
}
@@ -2373,6 +2374,11 @@ If you encounter an error missing from this list, please file an issue or a PR!"
23732374

23742375
let name = item.path_for_allowlisting(self)[1..].join("::");
23752376
debug!("allowlisted_items: testing {:?}", name);
2377+
2378+
if self.options().allowlisted_items.matches(&name) {
2379+
return true;
2380+
}
2381+
23762382
match *item.kind() {
23772383
ItemKind::Module(..) => true,
23782384
ItemKind::Function(_) => {
@@ -2496,6 +2502,10 @@ If you encounter an error missing from this list, please file an issue or a PR!"
24962502
for item in self.options().allowlisted_types.unmatched_items() {
24972503
unused_regex_diagnostic(item, "--allowlist-type", self);
24982504
}
2505+
2506+
for item in self.options().allowlisted_items.unmatched_items() {
2507+
unused_regex_diagnostic(item, "--allowlist-items", self);
2508+
}
24992509
}
25002510

25012511
/// Convenient method for getting the prefix to use for most traits in

bindgen/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -435,18 +435,19 @@ impl Builder {
435435

436436
impl BindgenOptions {
437437
fn build(&mut self) {
438-
const REGEX_SETS_LEN: usize = 27;
438+
const REGEX_SETS_LEN: usize = 28;
439439

440440
let regex_sets: [_; REGEX_SETS_LEN] = [
441-
&mut self.allowlisted_vars,
442-
&mut self.allowlisted_types,
443-
&mut self.allowlisted_functions,
444-
&mut self.allowlisted_files,
445441
&mut self.blocklisted_types,
446442
&mut self.blocklisted_functions,
447443
&mut self.blocklisted_items,
448444
&mut self.blocklisted_files,
449445
&mut self.opaque_types,
446+
&mut self.allowlisted_vars,
447+
&mut self.allowlisted_types,
448+
&mut self.allowlisted_functions,
449+
&mut self.allowlisted_files,
450+
&mut self.allowlisted_items,
450451
&mut self.bitfield_enums,
451452
&mut self.constified_enums,
452453
&mut self.constified_enum_modules,
@@ -482,6 +483,7 @@ impl BindgenOptions {
482483
"--allowlist-function",
483484
"--allowlist-var",
484485
"--allowlist-file",
486+
"--allowlist-item",
485487
"--bitfield-enum",
486488
"--newtype-enum",
487489
"--newtype-global-enum",

bindgen/options/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,23 @@ options! {
345345
},
346346
as_args: "--allowlist-file",
347347
},
348+
/// Items that have been allowlisted and should appear in the generated code.
349+
allowlisted_items: RegexSet {
350+
methods: {
351+
regex_option! {
352+
/// Generate bindings for the given item, regardless of whether it is a type,
353+
/// function, module, etc.
354+
///
355+
/// This option is transitive by default. Check the documentation of the
356+
/// [`Builder::allowlist_recursively`] method for further information.
357+
pub fn allowlist_item<T: AsRef<str>>(mut self, arg: T) -> Builder {
358+
self.options.allowlisted_items.insert(arg);
359+
self
360+
}
361+
}
362+
},
363+
as_args: "--allowlist-item",
364+
},
348365
/// The default style of for generated `enum`s.
349366
default_enum_style: EnumVariation {
350367
methods: {

0 commit comments

Comments
 (0)