Skip to content

Commit d7dc508

Browse files
authored
Merge pull request #1469 from sanxiyn/warn-unused-option. r=emilio
Warn about unused whitelist options
2 parents 45d4134 + 256f41e commit d7dc508

File tree

3 files changed

+55
-80
lines changed

3 files changed

+55
-80
lines changed

src/ir/context.rs

+12
Original file line numberDiff line numberDiff line change
@@ -2385,6 +2385,18 @@ If you encounter an error missing from this list, please file an issue or a PR!"
23852385

23862386
self.whitelisted = Some(whitelisted);
23872387
self.codegen_items = Some(codegen_items);
2388+
2389+
for item in self.options().whitelisted_functions.unmatched_items() {
2390+
error!("unused option: --whitelist-function {}", item);
2391+
}
2392+
2393+
for item in self.options().whitelisted_vars.unmatched_items() {
2394+
error!("unused option: --whitelist-var {}", item);
2395+
}
2396+
2397+
for item in self.options().whitelisted_types.unmatched_items() {
2398+
error!("unused option: --whitelist-type {}", item);
2399+
}
23882400
}
23892401

23902402
/// Convenient method for getting the prefix to use for most traits in

src/lib.rs

+15-75
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,7 @@ impl Builder {
233233
.iter()
234234
.map(|item| {
235235
output_vector.push("--bitfield-enum".into());
236-
output_vector.push(
237-
item.trim_left_matches("^")
238-
.trim_right_matches("$")
239-
.into(),
240-
);
236+
output_vector.push(item.to_owned());
241237
})
242238
.count();
243239

@@ -247,11 +243,7 @@ impl Builder {
247243
.iter()
248244
.map(|item| {
249245
output_vector.push("--rustified-enum".into());
250-
output_vector.push(
251-
item.trim_left_matches("^")
252-
.trim_right_matches("$")
253-
.into(),
254-
);
246+
output_vector.push(item.to_owned());
255247
})
256248
.count();
257249

@@ -261,11 +253,7 @@ impl Builder {
261253
.iter()
262254
.map(|item| {
263255
output_vector.push("--constified-enum-module".into());
264-
output_vector.push(
265-
item.trim_left_matches("^")
266-
.trim_right_matches("$")
267-
.into(),
268-
);
256+
output_vector.push(item.to_owned());
269257
})
270258
.count();
271259

@@ -275,11 +263,7 @@ impl Builder {
275263
.iter()
276264
.map(|item| {
277265
output_vector.push("--constified-enum".into());
278-
output_vector.push(
279-
item.trim_left_matches("^")
280-
.trim_right_matches("$")
281-
.into(),
282-
);
266+
output_vector.push(item.to_owned());
283267
})
284268
.count();
285269

@@ -289,11 +273,7 @@ impl Builder {
289273
.iter()
290274
.map(|item| {
291275
output_vector.push("--blacklist-type".into());
292-
output_vector.push(
293-
item.trim_left_matches("^")
294-
.trim_right_matches("$")
295-
.into(),
296-
);
276+
output_vector.push(item.to_owned());
297277
})
298278
.count();
299279

@@ -303,11 +283,7 @@ impl Builder {
303283
.iter()
304284
.map(|item| {
305285
output_vector.push("--blacklist-function".into());
306-
output_vector.push(
307-
item.trim_left_matches("^")
308-
.trim_right_matches("$")
309-
.into(),
310-
);
286+
output_vector.push(item.to_owned());
311287
})
312288
.count();
313289

@@ -317,11 +293,7 @@ impl Builder {
317293
.iter()
318294
.map(|item| {
319295
output_vector.push("--blacklist-item".into());
320-
output_vector.push(
321-
item.trim_left_matches("^")
322-
.trim_right_matches("$")
323-
.into(),
324-
);
296+
output_vector.push(item.to_owned());
325297
})
326298
.count();
327299

@@ -472,11 +444,7 @@ impl Builder {
472444
.iter()
473445
.map(|item| {
474446
output_vector.push("--opaque-type".into());
475-
output_vector.push(
476-
item.trim_left_matches("^")
477-
.trim_right_matches("$")
478-
.into(),
479-
);
447+
output_vector.push(item.to_owned());
480448
})
481449
.count();
482450

@@ -485,11 +453,7 @@ impl Builder {
485453
.iter()
486454
.map(|item| {
487455
output_vector.push("--raw-line".into());
488-
output_vector.push(
489-
item.trim_left_matches("^")
490-
.trim_right_matches("$")
491-
.into(),
492-
);
456+
output_vector.push(item.to_owned());
493457
})
494458
.count();
495459

@@ -507,11 +471,7 @@ impl Builder {
507471
.iter()
508472
.map(|item| {
509473
output_vector.push("--whitelist-function".into());
510-
output_vector.push(
511-
item.trim_left_matches("^")
512-
.trim_right_matches("$")
513-
.into(),
514-
);
474+
output_vector.push(item.to_owned());
515475
})
516476
.count();
517477

@@ -521,11 +481,7 @@ impl Builder {
521481
.iter()
522482
.map(|item| {
523483
output_vector.push("--whitelist-type".into());
524-
output_vector.push(
525-
item.trim_left_matches("^")
526-
.trim_right_matches("$")
527-
.into(),
528-
);
484+
output_vector.push(item.to_owned());
529485
})
530486
.count();
531487

@@ -535,11 +491,7 @@ impl Builder {
535491
.iter()
536492
.map(|item| {
537493
output_vector.push("--whitelist-var".into());
538-
output_vector.push(
539-
item.trim_left_matches("^")
540-
.trim_right_matches("$")
541-
.into(),
542-
);
494+
output_vector.push(item.to_owned());
543495
})
544496
.count();
545497

@@ -576,11 +528,7 @@ impl Builder {
576528
.iter()
577529
.map(|item| {
578530
output_vector.push("--no-partialeq".into());
579-
output_vector.push(
580-
item.trim_left_matches("^")
581-
.trim_right_matches("$")
582-
.into(),
583-
);
531+
output_vector.push(item.to_owned());
584532
})
585533
.count();
586534

@@ -590,11 +538,7 @@ impl Builder {
590538
.iter()
591539
.map(|item| {
592540
output_vector.push("--no-copy".into());
593-
output_vector.push(
594-
item.trim_left_matches("^")
595-
.trim_right_matches("$")
596-
.into(),
597-
);
541+
output_vector.push(item.to_owned());
598542
})
599543
.count();
600544

@@ -604,11 +548,7 @@ impl Builder {
604548
.iter()
605549
.map(|item| {
606550
output_vector.push("--no-hash".into());
607-
output_vector.push(
608-
item.trim_left_matches("^")
609-
.trim_right_matches("$")
610-
.into(),
611-
);
551+
output_vector.push(item.to_owned());
612552
})
613553
.count();
614554

src/regex_set.rs

+28-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
//! A type that represents the union of a set of regular expressions.
22
33
use regex::RegexSet as RxSet;
4+
use std::cell::Cell;
45

56
/// A dynamic set of regular expressions.
67
#[derive(Debug)]
78
pub struct RegexSet {
89
items: Vec<String>,
10+
matched: Vec<Cell<bool>>,
911
set: Option<RxSet>,
1012
}
1113

@@ -20,7 +22,8 @@ impl RegexSet {
2022
where
2123
S: AsRef<str>,
2224
{
23-
self.items.push(format!("^{}$", string.as_ref()));
25+
self.items.push(string.as_ref().to_owned());
26+
self.matched.push(Cell::new(false));
2427
self.set = None;
2528
}
2629

@@ -29,12 +32,24 @@ impl RegexSet {
2932
&self.items[..]
3033
}
3134

35+
/// Returns regexes in the set which didn't match any strings yet
36+
pub fn unmatched_items(&self) -> Vec<String> {
37+
let mut items = vec![];
38+
for (i, item) in self.items.iter().enumerate() {
39+
if !self.matched[i].get() {
40+
items.push(item.clone());
41+
}
42+
}
43+
items
44+
}
45+
3246
/// Construct a RegexSet from the set of entries we've accumulated.
3347
///
3448
/// Must be called before calling `matches()`, or it will always return
3549
/// false.
3650
pub fn build(&mut self) {
37-
self.set = match RxSet::new(&self.items) {
51+
let items = self.items.iter().map(|item| format!("^{}$", item));
52+
self.set = match RxSet::new(items) {
3853
Ok(x) => Some(x),
3954
Err(e) => {
4055
error!("Invalid regex in {:?}: {:?}", self.items, e);
@@ -49,16 +64,24 @@ impl RegexSet {
4964
S: AsRef<str>,
5065
{
5166
let s = string.as_ref();
52-
self.set.as_ref().map(|set| set.is_match(s)).unwrap_or(
53-
false,
54-
)
67+
if let Some(set) = self.set.as_ref() {
68+
let matches = set.matches(s);
69+
if matches.matched_any() {
70+
for i in matches.iter() {
71+
self.matched[i].set(true);
72+
}
73+
return true;
74+
}
75+
}
76+
false
5577
}
5678
}
5779

5880
impl Default for RegexSet {
5981
fn default() -> Self {
6082
RegexSet {
6183
items: vec![],
84+
matched: vec![],
6285
set: None,
6386
}
6487
}

0 commit comments

Comments
 (0)