|
6 | 6 | private import codeql.util.Location
|
7 | 7 |
|
8 | 8 | /**
|
9 |
| - * Restricts alerts to a specific location in specific files. |
| 9 | + * Holds if the query should produce alerts that match the given line ranges. |
10 | 10 | *
|
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. |
14 | 13 | *
|
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. |
18 | 18 | *
|
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.) |
20 | 25 | *
|
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`. |
23 | 69 | */
|
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 | +); |
25 | 73 |
|
26 | 74 | /** Module for applying alert location filtering. */
|
27 | 75 | module AlertFilteringImpl<LocationSig Location> {
|
28 | 76 | /** Applies alert filtering to the given location. */
|
29 | 77 | bindingset[location]
|
30 | 78 | predicate filterByLocation(Location location) {
|
31 |
| - not restrictAlertsTo(_, _, _) |
| 79 | + not restrictAlertsTo(_, _, _) and not restrictAlertsToExactLocation(_, _, _, _, _) |
32 | 80 | 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) |
35 | 83 | |
|
36 |
| - startLine = 0 and |
37 |
| - endLine = 0 and |
| 84 | + startLineStart = 0 and |
| 85 | + startLineEnd = 0 and |
38 | 86 | location.hasLocationInfo(filePath, _, _, _, _)
|
39 | 87 | 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) |
41 | 95 | )
|
42 | 96 | }
|
43 | 97 | }
|
0 commit comments