Skip to content

Commit

Permalink
Add --disable-whole-symbol-regex option
Browse files Browse the repository at this point in the history
When this option is enabled, bindgen won't parentesize and surround any
regex with ^ and $.

Fixes rust-lang#1783
  • Loading branch information
pvdrz committed Nov 9, 2022
1 parent ed3aa90 commit 57c5e5f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -152,6 +152,8 @@
matching a regular expression.
* new feature: allow using the `C-unwind` ABI in `--override-abi` on nightly
rust.
* new feature: `--disable-whole-symbol-regex` flag to avoid parentesizing and
wrapping any regex argument with `^` and `$`.

## Changed

Expand Down
7 changes: 7 additions & 0 deletions bindgen-cli/options.rs
Expand Up @@ -574,6 +574,9 @@ where
.value_name("override")
.multiple_occurrences(true)
.number_of_values(1),
Arg::new("disable-whole-symbol-regex")
.long("disable-whole-symbol-regex")
.help("Avoids parentesizing and surrounding any regex arguments with ^ and $."),
Arg::new("V")
.long("version")
.help("Prints the version, and exits"),
Expand Down Expand Up @@ -1106,5 +1109,9 @@ where
}
}

if matches.is_present("disable-whole-symbol-regex") {
builder = builder.whole_symbol_regex(false);
}

Ok((builder, output, verbose))
}

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

6 changes: 6 additions & 0 deletions bindgen-tests/tests/headers/disable-whole-symbol-regex.h
@@ -0,0 +1,6 @@
// bindgen-flags: --disable-whole-symbol-regex --allowlist-function="^foo"

int foo();
int foo_bar();
int bar_foo();
int bar_foo_baz();
18 changes: 17 additions & 1 deletion bindgen/lib.rs
Expand Up @@ -624,6 +624,10 @@ impl Builder {
output_vector.push("--merge-extern-blocks".into());
}

if !self.options.whole_symbol_regex {
output_vector.push("--disable-whole-symbol-regex".into());
}

// Add clang arguments

output_vector.push("--".into());
Expand Down Expand Up @@ -1792,6 +1796,14 @@ impl Builder {
.insert(arg.into());
self
}

/// If true, parenthesize and surround any regex argument with `^` and `$`.
///
/// This option is enabled by default.
pub fn whole_symbol_regex(mut self, doit: bool) -> Self {
self.options.whole_symbol_regex = doit;
self
}
}

/// Configuration options for generated bindings.
Expand Down Expand Up @@ -2129,6 +2141,8 @@ struct BindgenOptions {
merge_extern_blocks: bool,

abi_overrides: HashMap<Abi, RegexSet>,

whole_symbol_regex: bool,
}

impl BindgenOptions {
Expand Down Expand Up @@ -2163,8 +2177,9 @@ impl BindgenOptions {
&mut self.must_use_types,
];
let record_matches = self.record_matches;
let whole_symbol_regex = self.whole_symbol_regex;
for regex_set in self.abi_overrides.values_mut().chain(regex_sets) {
regex_set.build(record_matches);
regex_set.build(record_matches, whole_symbol_regex);
}
}

Expand Down Expand Up @@ -2232,6 +2247,7 @@ impl Default for BindgenOptions {
record_matches: true,
rustfmt_bindings: true,
size_t_is_usize: true,
whole_symbol_regex: true,

--default-fields--
blocklisted_types,
Expand Down
18 changes: 13 additions & 5 deletions bindgen/regex_set.rs
Expand Up @@ -27,9 +27,6 @@ impl RegexSet {
S: AsRef<str>,
{
let string = string.as_ref().to_owned();
if string == "*" {
warn!("using wildcard patterns (`*`) is no longer considered valid. Use `.*` instead");
}
self.items.push(string);
self.matched.push(Cell::new(false));
self.set = None;
Expand All @@ -56,8 +53,19 @@ impl RegexSet {
///
/// Must be called before calling `matches()`, or it will always return
/// false.
pub fn build(&mut self, record_matches: bool) {
let items = self.items.iter().map(|item| format!("^({})$", item));
pub fn build(&mut self, record_matches: bool, whole_symbol_regex: bool) {
let f = if whole_symbol_regex {
(|item| {
if item == "*" {
warn!("using wildcard patterns (`*`) with the `--whole-symbol-regex` option enabled is not considered valid. Use `.*` instead");
}
format!("^({})$", item)
}) as fn(&String) -> String
} else {
(|item| item.clone()) as fn(&String) -> String
};
let items = self.items.iter().map(f);

self.record_matches = record_matches;
self.set = match RxSet::new(items) {
Ok(x) => Some(x),
Expand Down

0 comments on commit 57c5e5f

Please sign in to comment.