From 57c5e5feeab5dd0f7dfcdeef9b60b71c595c4e2e Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Wed, 9 Nov 2022 16:44:08 -0500 Subject: [PATCH] Add `--disable-whole-symbol-regex` option When this option is enabled, bindgen won't parentesize and surround any regex with ^ and $. Fixes #1783 --- CHANGELOG.md | 2 ++ bindgen-cli/options.rs | 7 +++++++ .../tests/disable-whole-symbol-regex.rs | 13 +++++++++++++ .../tests/headers/disable-whole-symbol-regex.h | 6 ++++++ bindgen/lib.rs | 18 +++++++++++++++++- bindgen/regex_set.rs | 18 +++++++++++++----- 6 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 bindgen-tests/tests/expectations/tests/disable-whole-symbol-regex.rs create mode 100644 bindgen-tests/tests/headers/disable-whole-symbol-regex.h diff --git a/CHANGELOG.md b/CHANGELOG.md index 506878c408..fb7731ddce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/bindgen-cli/options.rs b/bindgen-cli/options.rs index 482f9a7d98..375a1890d6 100644 --- a/bindgen-cli/options.rs +++ b/bindgen-cli/options.rs @@ -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"), @@ -1106,5 +1109,9 @@ where } } + if matches.is_present("disable-whole-symbol-regex") { + builder = builder.whole_symbol_regex(false); + } + Ok((builder, output, verbose)) } diff --git a/bindgen-tests/tests/expectations/tests/disable-whole-symbol-regex.rs b/bindgen-tests/tests/expectations/tests/disable-whole-symbol-regex.rs new file mode 100644 index 0000000000..cebb52f4c8 --- /dev/null +++ b/bindgen-tests/tests/expectations/tests/disable-whole-symbol-regex.rs @@ -0,0 +1,13 @@ +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] + +extern "C" { + pub fn foo() -> ::std::os::raw::c_int; +} +extern "C" { + pub fn foo_bar() -> ::std::os::raw::c_int; +} diff --git a/bindgen-tests/tests/headers/disable-whole-symbol-regex.h b/bindgen-tests/tests/headers/disable-whole-symbol-regex.h new file mode 100644 index 0000000000..78d84bae08 --- /dev/null +++ b/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(); diff --git a/bindgen/lib.rs b/bindgen/lib.rs index 3696c499c5..d1460b7e5d 100644 --- a/bindgen/lib.rs +++ b/bindgen/lib.rs @@ -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()); @@ -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. @@ -2129,6 +2141,8 @@ struct BindgenOptions { merge_extern_blocks: bool, abi_overrides: HashMap, + + whole_symbol_regex: bool, } impl BindgenOptions { @@ -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); } } @@ -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, diff --git a/bindgen/regex_set.rs b/bindgen/regex_set.rs index 9f1e2251cd..6c9ab9da1e 100644 --- a/bindgen/regex_set.rs +++ b/bindgen/regex_set.rs @@ -27,9 +27,6 @@ impl RegexSet { S: AsRef, { 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; @@ -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),