Skip to content

Warn about unused whitelist options #1469

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 2 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
12 changes: 12 additions & 0 deletions src/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2385,6 +2385,18 @@ If you encounter an error missing from this list, please file an issue or a PR!"

self.whitelisted = Some(whitelisted);
self.codegen_items = Some(codegen_items);

for item in self.options().whitelisted_functions.unmatched_items() {
error!("unused option: --whitelist-function {}", item);
}

for item in self.options().whitelisted_vars.unmatched_items() {
error!("unused option: --whitelist-var {}", item);
}

for item in self.options().whitelisted_types.unmatched_items() {
error!("unused option: --whitelist-type {}", item);
}
}

/// Convenient method for getting the prefix to use for most traits in
Expand Down
90 changes: 15 additions & 75 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,7 @@ impl Builder {
.iter()
.map(|item| {
output_vector.push("--bitfield-enum".into());
output_vector.push(
item.trim_left_matches("^")
.trim_right_matches("$")
.into(),
);
output_vector.push(item.to_owned());
})
.count();

Expand All @@ -247,11 +243,7 @@ impl Builder {
.iter()
.map(|item| {
output_vector.push("--rustified-enum".into());
output_vector.push(
item.trim_left_matches("^")
.trim_right_matches("$")
.into(),
);
output_vector.push(item.to_owned());
})
.count();

Expand All @@ -261,11 +253,7 @@ impl Builder {
.iter()
.map(|item| {
output_vector.push("--constified-enum-module".into());
output_vector.push(
item.trim_left_matches("^")
.trim_right_matches("$")
.into(),
);
output_vector.push(item.to_owned());
})
.count();

Expand All @@ -275,11 +263,7 @@ impl Builder {
.iter()
.map(|item| {
output_vector.push("--constified-enum".into());
output_vector.push(
item.trim_left_matches("^")
.trim_right_matches("$")
.into(),
);
output_vector.push(item.to_owned());
})
.count();

Expand All @@ -289,11 +273,7 @@ impl Builder {
.iter()
.map(|item| {
output_vector.push("--blacklist-type".into());
output_vector.push(
item.trim_left_matches("^")
.trim_right_matches("$")
.into(),
);
output_vector.push(item.to_owned());
})
.count();

Expand All @@ -303,11 +283,7 @@ impl Builder {
.iter()
.map(|item| {
output_vector.push("--blacklist-function".into());
output_vector.push(
item.trim_left_matches("^")
.trim_right_matches("$")
.into(),
);
output_vector.push(item.to_owned());
})
.count();

Expand All @@ -317,11 +293,7 @@ impl Builder {
.iter()
.map(|item| {
output_vector.push("--blacklist-item".into());
output_vector.push(
item.trim_left_matches("^")
.trim_right_matches("$")
.into(),
);
output_vector.push(item.to_owned());
})
.count();

Expand Down Expand Up @@ -472,11 +444,7 @@ impl Builder {
.iter()
.map(|item| {
output_vector.push("--opaque-type".into());
output_vector.push(
item.trim_left_matches("^")
.trim_right_matches("$")
.into(),
);
output_vector.push(item.to_owned());
})
.count();

Expand All @@ -485,11 +453,7 @@ impl Builder {
.iter()
.map(|item| {
output_vector.push("--raw-line".into());
output_vector.push(
item.trim_left_matches("^")
.trim_right_matches("$")
.into(),
);
output_vector.push(item.to_owned());
})
.count();

Expand All @@ -507,11 +471,7 @@ impl Builder {
.iter()
.map(|item| {
output_vector.push("--whitelist-function".into());
output_vector.push(
item.trim_left_matches("^")
.trim_right_matches("$")
.into(),
);
output_vector.push(item.to_owned());
})
.count();

Expand All @@ -521,11 +481,7 @@ impl Builder {
.iter()
.map(|item| {
output_vector.push("--whitelist-type".into());
output_vector.push(
item.trim_left_matches("^")
.trim_right_matches("$")
.into(),
);
output_vector.push(item.to_owned());
})
.count();

Expand All @@ -535,11 +491,7 @@ impl Builder {
.iter()
.map(|item| {
output_vector.push("--whitelist-var".into());
output_vector.push(
item.trim_left_matches("^")
.trim_right_matches("$")
.into(),
);
output_vector.push(item.to_owned());
})
.count();

Expand Down Expand Up @@ -576,11 +528,7 @@ impl Builder {
.iter()
.map(|item| {
output_vector.push("--no-partialeq".into());
output_vector.push(
item.trim_left_matches("^")
.trim_right_matches("$")
.into(),
);
output_vector.push(item.to_owned());
})
.count();

Expand All @@ -590,11 +538,7 @@ impl Builder {
.iter()
.map(|item| {
output_vector.push("--no-copy".into());
output_vector.push(
item.trim_left_matches("^")
.trim_right_matches("$")
.into(),
);
output_vector.push(item.to_owned());
})
.count();

Expand All @@ -604,11 +548,7 @@ impl Builder {
.iter()
.map(|item| {
output_vector.push("--no-hash".into());
output_vector.push(
item.trim_left_matches("^")
.trim_right_matches("$")
.into(),
);
output_vector.push(item.to_owned());
})
.count();

Expand Down
33 changes: 28 additions & 5 deletions src/regex_set.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! A type that represents the union of a set of regular expressions.

use regex::RegexSet as RxSet;
use std::cell::Cell;

/// A dynamic set of regular expressions.
#[derive(Debug)]
pub struct RegexSet {
items: Vec<String>,
matched: Vec<Cell<bool>>,
set: Option<RxSet>,
}

Expand All @@ -20,7 +22,8 @@ impl RegexSet {
where
S: AsRef<str>,
{
self.items.push(format!("^{}$", string.as_ref()));
self.items.push(string.as_ref().to_owned());
self.matched.push(Cell::new(false));
self.set = None;
}

Expand All @@ -29,12 +32,24 @@ 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());
}
}
items
}

/// 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) {
self.set = match RxSet::new(&self.items) {
let items = self.items.iter().map(|item| format!("^{}$", item));
self.set = match RxSet::new(items) {
Ok(x) => Some(x),
Err(e) => {
error!("Invalid regex in {:?}: {:?}", self.items, e);
Expand All @@ -49,16 +64,24 @@ impl RegexSet {
S: AsRef<str>,
{
let s = string.as_ref();
self.set.as_ref().map(|set| set.is_match(s)).unwrap_or(
false,
)
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;
}
}
false
}
}

impl Default for RegexSet {
fn default() -> Self {
RegexSet {
items: vec![],
matched: vec![],
set: None,
}
}
Expand Down