|
5 | 5 | * @id cs/alert-suppression |
6 | 6 | */ |
7 | 7 |
|
8 | | -import csharp |
| 8 | +private import codeql.suppression.AlertSuppression as AS |
| 9 | +private import semmle.code.csharp.Comments |
9 | 10 |
|
10 | | -/** |
11 | | - * An alert suppression comment. |
12 | | - */ |
13 | | -class SuppressionComment extends CommentLine { |
14 | | - string annotation; |
15 | | - |
16 | | - SuppressionComment() { |
| 11 | +class SingleLineComment extends CommentLine { |
| 12 | + SingleLineComment() { |
17 | 13 | // Must be either `// ...` or `/* ... */` on a single line. |
18 | | - this.getRawText().regexpMatch("//.*|/\\*.*\\*/") and |
19 | | - exists(string text | text = this.getText() | |
20 | | - // match `lgtm[...]` anywhere in the comment |
21 | | - annotation = text.regexpFind("(?i)\\blgtm\\s*\\[[^\\]]*\\]", _, _) |
22 | | - or |
23 | | - // match `lgtm` at the start of the comment and after semicolon |
24 | | - annotation = text.regexpFind("(?i)(?<=^|;)\\s*lgtm(?!\\B|\\s*\\[)", _, _).trim() |
25 | | - ) |
26 | | - } |
27 | | - |
28 | | - /** Gets the suppression annotation in this comment. */ |
29 | | - string getAnnotation() { result = annotation } |
30 | | - |
31 | | - /** |
32 | | - * Holds if this comment applies to the range from column `startcolumn` of line `startline` |
33 | | - * to column `endcolumn` of line `endline` in file `filepath`. |
34 | | - */ |
35 | | - predicate covers(string filepath, int startline, int startcolumn, int endline, int endcolumn) { |
36 | | - this.getLocation().hasLocationInfo(filepath, startline, _, endline, endcolumn) and |
37 | | - startcolumn = 1 |
| 14 | + this.getRawText().regexpMatch("//.*|/\\*.*\\*/") |
38 | 15 | } |
39 | 16 |
|
40 | | - /** Gets the scope of this suppression. */ |
41 | | - SuppressionScope getScope() { this = result.getSuppressionComment() } |
42 | | -} |
43 | | - |
44 | | -/** |
45 | | - * The scope of an alert suppression comment. |
46 | | - */ |
47 | | -class SuppressionScope extends @commentline instanceof SuppressionComment { |
48 | | - /** Gets a suppression comment with this scope. */ |
49 | | - SuppressionComment getSuppressionComment() { result = this } |
50 | | - |
51 | | - /** |
52 | | - * Holds if this element is at the specified location. |
53 | | - * The location spans column `startcolumn` of line `startline` to |
54 | | - * column `endcolumn` of line `endline` in file `filepath`. |
55 | | - * For more information, see |
56 | | - * [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/). |
57 | | - */ |
58 | 17 | predicate hasLocationInfo( |
59 | 18 | string filepath, int startline, int startcolumn, int endline, int endcolumn |
60 | 19 | ) { |
61 | | - super.covers(filepath, startline, startcolumn, endline, endcolumn) |
| 20 | + this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) |
62 | 21 | } |
63 | | - |
64 | | - /** Gets a textual representation of this element. */ |
65 | | - string toString() { result = "suppression range" } |
66 | 22 | } |
67 | 23 |
|
68 | | -from SuppressionComment c |
69 | | -select c, // suppression comment |
70 | | - c.getText(), // text of suppression comment (excluding delimiters) |
71 | | - c.getAnnotation(), // text of suppression annotation |
72 | | - c.getScope() // scope of suppression |
| 24 | +import AS::Make<SingleLineComment> |
0 commit comments