Skip to content

Commit b3b7817

Browse files
authored
Merge pull request #18603 from github/cklin/restrict-alerts-to-exact
AlertFiltering: add restrictAlertsToExactLocation
2 parents 8edcad0 + 96caa68 commit b3b7817

File tree

2 files changed

+77
-17
lines changed

2 files changed

+77
-17
lines changed

shared/util/codeql/util/AlertFiltering.qll

+71-17
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,92 @@
66
private import codeql.util.Location
77

88
/**
9-
* Restricts alerts to a specific location in specific files.
9+
* Holds if the query should produce alerts that match the given line ranges.
1010
*
11-
* If this predicate is empty, accept all alerts. Otherwise, accept alerts only at the specified
12-
* locations. Note that alert restrictions apply only to the start line of an alert (even if the
13-
* alert location spans multiple lines) because alerts are displayed on their start lines.
11+
* This predicate is active if and only if it is nonempty. If this predicate is inactive, it has no
12+
* effect. If it is active, it accepts any alert that has at least one matching location.
1413
*
15-
* - filePath: Absolute path of the file to restrict alerts to.
16-
* - startLine: Start line number (starting with 1, inclusive) to restrict alerts to.
17-
* - endLine: End line number (starting with 1, inclusive) to restrict alerts to.
14+
* Note that an alert that is not accepted by this filtering predicate may still be included in the
15+
* query results if it is accepted by another active filtering predicate in this module. An alert is
16+
* excluded from the query results if only if (1) there is at least one active filtering predicate,
17+
* and (2) it is not accepted by any active filtering predicate.
1818
*
19-
* If startLine and endLine are both 0, accept alerts anywhere in the file.
19+
* An alert location is a match if it matches a row in this predicate. If `startLineStart` and
20+
* `startLineEnd` are both 0, the row specifies a whole-file match, and a location is a match if
21+
* its file path matches `filePath`. Otherwise, the row specifies a line-range match, and a
22+
* location is a match if its file path matches `filePath`, and its start line is between
23+
* `startLineStart` and `startLineEnd`, inclusive. (Note that only start line of the location is
24+
* used for matching because an alert is displayed on the first line of its location.)
2025
*
21-
* A query should either completely ignore this predicate (i.e., perform no filtering whatsoever),
22-
* or only return alerts that meet the filtering criteria as specified above.
26+
* - filePath: alert location file path (absolute).
27+
* - startLineStart: inclusive start of the range for alert location start line number (1-based).
28+
* - startLineEnd: inclusive end of the range for alert location start line number (1-based).
29+
*
30+
* A query should either perform no alert filtering, or adhere to all the filtering rules in this
31+
* module and return all and only the accepted alerts.
32+
*
33+
* This predicate is suitable for situations where we want to filter alerts at line granularity,
34+
* such as based on the pull request diff.
35+
*
36+
* See also: `restrictAlertsToExactLocation`.
37+
*/
38+
extensible predicate restrictAlertsTo(string filePath, int startLineStart, int startLineEnd);
39+
40+
/**
41+
* Holds if the query should produce alerts that match the given locations.
42+
*
43+
* This predicate is active if and only if it is nonempty. If this predicate is inactive, it has no
44+
* effect. If it is active, it accepts any alert that has at least one matching location.
45+
*
46+
* Note that an alert that is not accepted by this filtering predicate may still be included in the
47+
* query results if it is accepted by another active filtering predicate in this module. An alert is
48+
* excluded from the query results if only if (1) there is at least one active filtering predicate,
49+
* and (2) it is not accepted by any active filtering predicate.
50+
*
51+
* An alert location is a match if it matches a row in this predicate. Each row specifies an exact
52+
* location: an alert location is a match if its file path matches `filePath`, its start line and
53+
* column match `startLine` and `startColumn`, and its end line and column match `endLine` and
54+
* `endColumn`.
55+
*
56+
* - filePath: alert location file path (absolute).
57+
* - startLine: alert location start line number (1-based).
58+
* - startColumn: alert location start column number (1-based).
59+
* - endLine: alert location end line number (1-based).
60+
* - endColumn: alert location end column number (1-based).
61+
*
62+
* A query should either perform no alert filtering, or adhere to all the filtering rules in this
63+
* module and return all and only the accepted alerts.
64+
*
65+
* This predicate is suitable for situations where we want to filter by the exact alert location,
66+
* distinguishing between alerts on the same line.
67+
*
68+
* See also: `restrictAlertsTo`.
2369
*/
24-
extensible predicate restrictAlertsTo(string filePath, int startLine, int endLine);
70+
extensible predicate restrictAlertsToExactLocation(
71+
string filePath, int startLine, int startColumn, int endLine, int endColumn
72+
);
2573

2674
/** Module for applying alert location filtering. */
2775
module AlertFilteringImpl<LocationSig Location> {
2876
/** Applies alert filtering to the given location. */
2977
bindingset[location]
3078
predicate filterByLocation(Location location) {
31-
not restrictAlertsTo(_, _, _)
79+
not restrictAlertsTo(_, _, _) and not restrictAlertsToExactLocation(_, _, _, _, _)
3280
or
33-
exists(string filePath, int startLine, int endLine |
34-
restrictAlertsTo(filePath, startLine, endLine)
81+
exists(string filePath, int startLineStart, int startLineEnd |
82+
restrictAlertsTo(filePath, startLineStart, startLineEnd)
3583
|
36-
startLine = 0 and
37-
endLine = 0 and
84+
startLineStart = 0 and
85+
startLineEnd = 0 and
3886
location.hasLocationInfo(filePath, _, _, _, _)
3987
or
40-
location.hasLocationInfo(filePath, [startLine .. endLine], _, _, _)
88+
location.hasLocationInfo(filePath, [startLineStart .. startLineEnd], _, _, _)
89+
)
90+
or
91+
exists(string filePath, int startLine, int startColumn, int endLine, int endColumn |
92+
restrictAlertsToExactLocation(filePath, startLine, startColumn, endLine, endColumn)
93+
|
94+
location.hasLocationInfo(filePath, startLine, startColumn, endLine, endColumn)
4195
)
4296
}
4397
}

shared/util/ext/default-alert-filter.yml

+6
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ extensions:
55
extensible: restrictAlertsTo
66
# Empty predicate means no restrictions on alert locations
77
data: []
8+
9+
- addsTo:
10+
pack: codeql/util
11+
extensible: restrictAlertsToExactLocation
12+
# Empty predicate means no restrictions on alert locations
13+
data: []

0 commit comments

Comments
 (0)