You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Autoharness: change pattern options to accept regexes (#4144)
Change `--include-pattern` and `--exclude-pattern` to accept regular
expressions.
Resolves#4046
By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
Copy file name to clipboardExpand all lines: docs/src/reference/experimental/autoharness.md
+19-27Lines changed: 19 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -43,45 +43,37 @@ autoharness feature means that you are also opting into the function contracts a
43
43
Kani generates and runs these harnesses internally—the user only sees the verification results.
44
44
45
45
### Options
46
-
The `autoharness` subcommand has options `--include-pattern` and `--exclude-pattern` to include and exclude particular functions.
47
-
These flags look for partial matches against the fully qualified name of a function.
48
-
49
-
For example, if a module `my_module` has many functions, but we are only interested in `my_module::foo` and `my_module::bar`, we can run:
50
-
```
51
-
cargo run autoharness -Z autoharness --include-pattern my_module::foo --include-pattern my_module::bar
52
-
```
53
-
To exclude `my_module` entirely, run:
54
-
```
55
-
cargo run autoharness -Z autoharness --exclude-pattern my_module
56
-
```
46
+
The `autoharness` subcommand has options `--include-pattern [REGEX]` and `--exclude-pattern [REGEX]` to include and exclude particular functions using regular expressions.
47
+
When matching, Kani prefixes the function's path with the crate name. For example, a function `foo` in the `my_crate` crate will be matched as `my_crate::foo`.
57
48
58
49
The selection algorithm is as follows:
59
50
- If only `--include-pattern`s are provided, include a function if it matches any of the provided patterns.
60
51
- If only `--exclude-pattern`s are provided, include a function if it does not match any of the provided patterns.
61
52
- If both are provided, include a function if it matches an include pattern *and* does not match any of the exclude patterns. Note that this implies that the exclude pattern takes precedence, i.e., if a function matches both an include pattern and an exclude pattern, it will be excluded.
62
53
63
-
Here are some more examples:
54
+
Here are some examples:
64
55
65
-
```
66
-
# Include functions whose paths contain the substring foo or baz, but not foo::bar
Currently, the only supported "patterns" are substrings of the fully qualified path of the function.
83
-
However, if more sophisticated patterns (e.g., regular expressions) would be useful for you,
84
-
please let us know in a comment on [this GitHub issue](https://github.com/model-checking/kani/issues/3832).
74
+
Note that because Kani prefixes function paths with the crate name, some patterns might match more than you expect.
75
+
For example, given a function `foo_top_level` inside crate `my_crate`, the regex `.*::foo_.*` will match `foo_top_level`, since Kani interprets it as `my_crate::foo_top_level`.
76
+
To match only `foo_` functions inside modules, use a more specific pattern, e.g. `.*::[^:]+::foo_.*`.
85
77
86
78
## Example
87
79
Using the `estimate_size` example from [First Steps](../../tutorial-first-steps.md) again:
panic!("Invalid regexes should have been caught during argument validation: {e}")
366
+
}))
367
+
}
368
+
}
369
+
370
+
/// A function is filtered out if 1) none of the include patterns match it or 2) one of the exclude patterns matches it.
371
+
fnautoharness_filtered_out(
372
+
name:&str,
373
+
included_set:&Option<RegexSet>,
374
+
excluded_set:&Option<RegexSet>,
375
+
) -> bool{
376
+
// A function is included if `--include-pattern` is not provided or if at least one of its regexes matches `name`
377
+
let included = included_set.as_ref().is_none_or(|set| set.is_match(name));
378
+
// A function is excluded if `--exclude-pattern` is provided and at least one of its regexes matches `name`
379
+
let excluded = excluded_set.as_ref().is_some_and(|set| set.is_match(name));
380
+
!included || excluded
381
+
}
382
+
359
383
/// Partition every function in the crate into (chosen, skipped), where `chosen` is a vector of the Instances for which we'll generate automatic harnesses,
360
384
/// and `skipped` is a map of function names to the reason why we skipped them.
// Note that filtering closures out here is a UX choice: we could instead call skip_reason() on closures,
478
-
// but the limitations in the linked issue would cause the user to be flooded with reports of "skipping" Kani instrumentation functions.
479
-
// Instead, we just pretend closures don't exist in our reporting of what we did and did not verify, which has the downside of also ignoring the user's top-level closures, but those are rare.
0 commit comments