Skip to content

Allow to turn off the matches recording introduced in #1469. #1472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,10 @@ impl Builder {
);
}

if !self.options.record_matches {
output_vector.push("--no-record-matches".into());
}

if !self.options.rustfmt_bindings {
output_vector.push("--no-rustfmt-bindings".into());
}
Expand Down Expand Up @@ -1137,6 +1141,12 @@ impl Builder {
self
}

/// Set whether we should record matched items in our regex sets.
pub fn record_matches(mut self, doit: bool) -> Self {
self.options.record_matches = doit;
self
}

/// Set the absolute path to the rustfmt configuration file, if None, the standard rustfmt
/// options are used.
pub fn rustfmt_configuration_file(mut self, path: Option<PathBuf>) -> Self {
Expand Down Expand Up @@ -1486,6 +1496,12 @@ struct BindgenOptions {
/// Features to enable, derived from `rust_target`
rust_features: RustFeatures,

/// Whether we should record which items in the regex sets ever matched.
///
/// This may be a bit slower, but will enable reporting of unused whitelist
/// items via the `error!` log.
record_matches: bool,

/// Whether rustfmt should format the generated bindings.
rustfmt_bindings: bool,

Expand All @@ -1511,20 +1527,26 @@ impl ::std::panic::UnwindSafe for BindgenOptions {}

impl BindgenOptions {
fn build(&mut self) {
self.whitelisted_vars.build();
self.whitelisted_types.build();
self.whitelisted_functions.build();
self.blacklisted_types.build();
self.blacklisted_functions.build();
self.blacklisted_items.build();
self.opaque_types.build();
self.bitfield_enums.build();
self.constified_enums.build();
self.constified_enum_modules.build();
self.rustified_enums.build();
self.no_partialeq_types.build();
self.no_copy_types.build();
self.no_hash_types.build();
let mut regex_sets = [
&mut self.whitelisted_vars,
&mut self.whitelisted_types,
&mut self.whitelisted_functions,
&mut self.blacklisted_types,
&mut self.blacklisted_functions,
&mut self.blacklisted_items,
&mut self.opaque_types,
&mut self.bitfield_enums,
&mut self.constified_enums,
&mut self.constified_enum_modules,
&mut self.rustified_enums,
&mut self.no_partialeq_types,
&mut self.no_copy_types,
&mut self.no_hash_types,
];
let record_matches = self.record_matches;
for regex_set in &mut regex_sets {
regex_set.build(record_matches);
}
}

/// Update rust target version
Expand Down Expand Up @@ -1601,6 +1623,7 @@ impl Default for BindgenOptions {
enable_mangling: true,
prepend_enum_name: true,
time_phases: false,
record_matches: true,
rustfmt_bindings: true,
rustfmt_configuration_file: None,
no_partialeq_types: Default::default(),
Expand Down
8 changes: 8 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ where
Useful when debugging bindgen, using C-Reduce, or when \
filing issues. The resulting file will be named \
something like `__bindgen.i` or `__bindgen.ii`."),
Arg::with_name("no-record-matches")
.long("no-record-matches")
.help("Do not record matching items in the regex sets. \
This disables reporting of unused items."),
Arg::with_name("no-rustfmt-bindings")
.long("no-rustfmt-bindings")
.help("Do not format the generated bindings with rustfmt."),
Expand Down Expand Up @@ -591,6 +595,10 @@ where
builder.dump_preprocessed_input()?;
}

if matches.is_present("no-record-matches") {
builder = builder.record_matches(false);
}

let no_rustfmt_bindings = matches.is_present("no-rustfmt-bindings");
if no_rustfmt_bindings {
builder = builder.rustfmt_bindings(false);
Expand Down
58 changes: 31 additions & 27 deletions src/regex_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ use regex::RegexSet as RxSet;
use std::cell::Cell;

/// A dynamic set of regular expressions.
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct RegexSet {
items: Vec<String>,
/// Whether any of the items in the set was ever matched. The length of this
/// vector is exactly the length of `items`.
matched: Vec<Cell<bool>>,
set: Option<RxSet>,
/// Whether we should record matching items in the `matched` vector or not.
record_matches: bool,
}

impl RegexSet {
Expand All @@ -32,23 +36,25 @@ impl RegexSet {
&self.items[..]
}

/// Returns regexes in the set which didn't match any strings yet
pub fn unmatched_items(&self) -> Vec<String> {
let mut items = vec![];
for (i, item) in self.items.iter().enumerate() {
if !self.matched[i].get() {
items.push(item.clone());
/// Returns an iterator over regexes in the set which didn't match any
/// strings yet.
pub fn unmatched_items(&self) -> impl Iterator<Item = &String> {
self.items.iter().enumerate().filter_map(move |(i, item)| {
if !self.record_matches || self.matched[i].get() {
return None;
}
}
items

Some(item)
})
}

/// Construct a RegexSet from the set of entries we've accumulated.
///
/// Must be called before calling `matches()`, or it will always return
/// false.
pub fn build(&mut self) {
pub fn build(&mut self, record_matches: bool) {
let items = self.items.iter().map(|item| format!("^{}$", item));
self.record_matches = record_matches;
self.set = match RxSet::new(items) {
Ok(x) => Some(x),
Err(e) => {
Expand All @@ -64,25 +70,23 @@ impl RegexSet {
S: AsRef<str>,
{
let s = string.as_ref();
if let Some(set) = self.set.as_ref() {
let matches = set.matches(s);
if matches.matched_any() {
for i in matches.iter() {
self.matched[i].set(true);
}
return true;
}
let set = match self.set {
Some(ref set) => set,
None => return false,
};

if !self.record_matches {
return set.is_match(s);
}
false
}
}

impl Default for RegexSet {
fn default() -> Self {
RegexSet {
items: vec![],
matched: vec![],
set: None,
let matches = set.matches(s);
if !matches.matched_any() {
return false;
}
for i in matches.iter() {
self.matched[i].set(true);
}

true
}
}