Skip to content

Commit 57c5e5f

Browse files
committed
Add --disable-whole-symbol-regex option
When this option is enabled, bindgen won't parentesize and surround any regex with ^ and $. Fixes rust-lang#1783
1 parent ed3aa90 commit 57c5e5f

File tree

6 files changed

+58
-6
lines changed

6 files changed

+58
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@
152152
matching a regular expression.
153153
* new feature: allow using the `C-unwind` ABI in `--override-abi` on nightly
154154
rust.
155+
* new feature: `--disable-whole-symbol-regex` flag to avoid parentesizing and
156+
wrapping any regex argument with `^` and `$`.
155157

156158
## Changed
157159

bindgen-cli/options.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,9 @@ where
574574
.value_name("override")
575575
.multiple_occurrences(true)
576576
.number_of_values(1),
577+
Arg::new("disable-whole-symbol-regex")
578+
.long("disable-whole-symbol-regex")
579+
.help("Avoids parentesizing and surrounding any regex arguments with ^ and $."),
577580
Arg::new("V")
578581
.long("version")
579582
.help("Prints the version, and exits"),
@@ -1106,5 +1109,9 @@ where
11061109
}
11071110
}
11081111

1112+
if matches.is_present("disable-whole-symbol-regex") {
1113+
builder = builder.whole_symbol_regex(false);
1114+
}
1115+
11091116
Ok((builder, output, verbose))
11101117
}

bindgen-tests/tests/expectations/tests/disable-whole-symbol-regex.rs

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// bindgen-flags: --disable-whole-symbol-regex --allowlist-function="^foo"
2+
3+
int foo();
4+
int foo_bar();
5+
int bar_foo();
6+
int bar_foo_baz();

bindgen/lib.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,10 @@ impl Builder {
624624
output_vector.push("--merge-extern-blocks".into());
625625
}
626626

627+
if !self.options.whole_symbol_regex {
628+
output_vector.push("--disable-whole-symbol-regex".into());
629+
}
630+
627631
// Add clang arguments
628632

629633
output_vector.push("--".into());
@@ -1792,6 +1796,14 @@ impl Builder {
17921796
.insert(arg.into());
17931797
self
17941798
}
1799+
1800+
/// If true, parenthesize and surround any regex argument with `^` and `$`.
1801+
///
1802+
/// This option is enabled by default.
1803+
pub fn whole_symbol_regex(mut self, doit: bool) -> Self {
1804+
self.options.whole_symbol_regex = doit;
1805+
self
1806+
}
17951807
}
17961808

17971809
/// Configuration options for generated bindings.
@@ -2129,6 +2141,8 @@ struct BindgenOptions {
21292141
merge_extern_blocks: bool,
21302142

21312143
abi_overrides: HashMap<Abi, RegexSet>,
2144+
2145+
whole_symbol_regex: bool,
21322146
}
21332147

21342148
impl BindgenOptions {
@@ -2163,8 +2177,9 @@ impl BindgenOptions {
21632177
&mut self.must_use_types,
21642178
];
21652179
let record_matches = self.record_matches;
2180+
let whole_symbol_regex = self.whole_symbol_regex;
21662181
for regex_set in self.abi_overrides.values_mut().chain(regex_sets) {
2167-
regex_set.build(record_matches);
2182+
regex_set.build(record_matches, whole_symbol_regex);
21682183
}
21692184
}
21702185

@@ -2232,6 +2247,7 @@ impl Default for BindgenOptions {
22322247
record_matches: true,
22332248
rustfmt_bindings: true,
22342249
size_t_is_usize: true,
2250+
whole_symbol_regex: true,
22352251

22362252
--default-fields--
22372253
blocklisted_types,

bindgen/regex_set.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ impl RegexSet {
2727
S: AsRef<str>,
2828
{
2929
let string = string.as_ref().to_owned();
30-
if string == "*" {
31-
warn!("using wildcard patterns (`*`) is no longer considered valid. Use `.*` instead");
32-
}
3330
self.items.push(string);
3431
self.matched.push(Cell::new(false));
3532
self.set = None;
@@ -56,8 +53,19 @@ impl RegexSet {
5653
///
5754
/// Must be called before calling `matches()`, or it will always return
5855
/// false.
59-
pub fn build(&mut self, record_matches: bool) {
60-
let items = self.items.iter().map(|item| format!("^({})$", item));
56+
pub fn build(&mut self, record_matches: bool, whole_symbol_regex: bool) {
57+
let f = if whole_symbol_regex {
58+
(|item| {
59+
if item == "*" {
60+
warn!("using wildcard patterns (`*`) with the `--whole-symbol-regex` option enabled is not considered valid. Use `.*` instead");
61+
}
62+
format!("^({})$", item)
63+
}) as fn(&String) -> String
64+
} else {
65+
(|item| item.clone()) as fn(&String) -> String
66+
};
67+
let items = self.items.iter().map(f);
68+
6169
self.record_matches = record_matches;
6270
self.set = match RxSet::new(items) {
6371
Ok(x) => Some(x),

0 commit comments

Comments
 (0)