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