Skip to content

Commit 092ad92

Browse files
committed
Add codeql.util.AlertFiltering
1 parent 4de0d10 commit 092ad92

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Provides the `restrictAlertsTo` extensible predicate to restrict alerts to specific source
3+
* locations, and the `AlertFilteringImpl` parameterized module to apply the filtering.
4+
*/
5+
6+
private import codeql.util.Location
7+
8+
/**
9+
* Restricts alerts to a specific location in specific files.
10+
*
11+
* If this predicate is empty, accept all alerts. Otherwise, accept alerts only at the specified
12+
* locations.
13+
*
14+
* - filePath: Absolute path of the file to restrict alerts to.
15+
* - line: Line number (starting with 1) to restrict alerts to. If 0, accept alerts anywhere in the
16+
* file.
17+
*/
18+
extensible predicate restrictAlertsTo(string filePath, int line);
19+
20+
/** Module for applying alert location filtering. */
21+
module AlertFilteringImpl<LocationSig Location> {
22+
/** Applies alert filtering to the given location. */
23+
bindingset[location]
24+
predicate filterByLocation(Location location) {
25+
not exists( | restrictAlertsTo(_, _))
26+
or
27+
exists(string filePath, int startLine, int endLine |
28+
location.hasLocationInfo(filePath, startLine, _, endLine, _) and
29+
(
30+
restrictAlertsTo(filePath, [startLine .. endLine]) or
31+
restrictAlertsTo(filePath, 0)
32+
)
33+
)
34+
}
35+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extensions:
2+
3+
- addsTo:
4+
pack: codeql/util
5+
extensible: restrictAlertsTo
6+
# Empty predicate means no restrictions on alert locations
7+
data: []

shared/util/qlpack.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ version: 1.0.6-dev
33
groups: shared
44
library: true
55
dependencies: null
6+
dataExtensions:
7+
- ext/*.yml
68
warnOnImplicitThis: true

0 commit comments

Comments
 (0)