Skip to content

Commit 016c7a8

Browse files
authored
Merge pull request github#11719 from aibaars/alert-suppression-shared
Shared AlertSuppression library
2 parents ca1c463 + 8be882f commit 016c7a8

File tree

23 files changed

+291
-542
lines changed

23 files changed

+291
-542
lines changed

cpp/ql/src/AlertSuppression.ql

Lines changed: 23 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,78 +5,35 @@
55
* @id cpp/alert-suppression
66
*/
77

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)
3124
) 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%")
3927
}
4028

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-
*/
7129
predicate hasLocationInfo(
7230
string filepath, int startline, int startcolumn, int endline, int endcolumn
7331
) {
74-
super.covers(filepath, startline, startcolumn, endline, endcolumn)
32+
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
7533
}
34+
35+
/** Gets the text in this comment, excluding the leading //. */
36+
string getText() { result = text }
7637
}
7738

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>

cpp/ql/src/qlpack.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ groups:
66
dependencies:
77
codeql/cpp-all: ${workspace}
88
codeql/suite-helpers: ${workspace}
9+
codeql/util: ${workspace}
910
suites: codeql-suites
1011
extractor: cpp
1112
defaultSuiteFile: codeql-suites/cpp-code-scanning.qls

cpp/ql/test/query-tests/AlertSuppression/AlertSuppression.expected

Lines changed: 58 additions & 58 deletions
Large diffs are not rendered by default.

csharp/ql/src/AlertSuppression.ql

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,68 +5,20 @@
55
* @id cs/alert-suppression
66
*/
77

8-
import csharp
8+
private import codeql.suppression.AlertSuppression as AS
9+
private import semmle.code.csharp.Comments
910

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() {
1713
// 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("//.*|/\\*.*\\*/")
3815
}
3916

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-
*/
5817
predicate hasLocationInfo(
5918
string filepath, int startline, int startcolumn, int endline, int endcolumn
6019
) {
61-
super.covers(filepath, startline, startcolumn, endline, endcolumn)
20+
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
6221
}
63-
64-
/** Gets a textual representation of this element. */
65-
string toString() { result = "suppression range" }
6622
}
6723

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>

csharp/ql/src/qlpack.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ defaultSuiteFile: codeql-suites/csharp-code-scanning.qls
99
dependencies:
1010
codeql/csharp-all: ${workspace}
1111
codeql/suite-helpers: ${workspace}
12+
codeql/util: ${workspace}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AlertSuppressionWindows.cs eol=crlf

go/ql/src/AlertSuppression.ql

Lines changed: 6 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,73 +5,14 @@
55
* @id go/alert-suppression
66
*/
77

8-
import go
8+
private import codeql.suppression.AlertSuppression as AS
9+
private import semmle.go.Comments as G
910

10-
/**
11-
* An alert suppression comment.
12-
*/
13-
class SuppressionComment extends Locatable {
14-
string text;
15-
string annotation;
16-
17-
SuppressionComment() {
18-
text = this.(Comment).getText() and
11+
class SingleLineComment extends G::Comment {
12+
SingleLineComment() {
1913
// suppression comments must be single-line
20-
not text.matches("%\n%") and
21-
(
22-
// match `lgtm[...]` anywhere in the comment
23-
annotation = text.regexpFind("(?i)\\blgtm\\s*\\[[^\\]]*\\]", _, _)
24-
or
25-
// match `lgtm` at the start of the comment and after semicolon
26-
annotation = text.regexpFind("(?i)(?<=^|;)\\s*lgtm(?!\\B|\\s*\\[)", _, _).trim()
27-
)
28-
}
29-
30-
/** Gets the text of this suppression comment, not including delimiters. */
31-
string getText() { result = text }
32-
33-
/** Gets the suppression annotation in this comment. */
34-
string getAnnotation() { result = annotation }
35-
36-
/**
37-
* Holds if this comment applies to the range from column `startcolumn` of line `startline`
38-
* to column `endcolumn` of line `endline` in file `filepath`.
39-
*/
40-
predicate covers(string filepath, int startline, int startcolumn, int endline, int endcolumn) {
41-
this.getLocation().hasLocationInfo(filepath, startline, _, endline, endcolumn) and
42-
startcolumn = 1
14+
not this.getText().matches("%\n%")
4315
}
44-
45-
/** Gets the scope of this suppression. */
46-
SuppressionScope getScope() { this = result.getSuppressionComment() }
47-
}
48-
49-
/**
50-
* The scope of an alert suppression comment.
51-
*/
52-
class SuppressionScope extends @locatable instanceof SuppressionComment {
53-
/** Gets a suppression comment with this scope. */
54-
SuppressionComment getSuppressionComment() { result = this }
55-
56-
/**
57-
* Holds if this element is at the specified location.
58-
* The location spans column `startcolumn` of line `startline` to
59-
* column `endcolumn` of line `endline` in file `filepath`.
60-
* For more information, see
61-
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
62-
*/
63-
predicate hasLocationInfo(
64-
string filepath, int startline, int startcolumn, int endline, int endcolumn
65-
) {
66-
super.covers(filepath, startline, startcolumn, endline, endcolumn)
67-
}
68-
69-
/** Gets a textual representation of this element. */
70-
string toString() { result = "suppression range" }
7116
}
7217

73-
from SuppressionComment c
74-
select c, // suppression comment
75-
c.getText(), // text of suppression comment (excluding delimiters)
76-
c.getAnnotation(), // text of suppression annotation
77-
c.getScope() // scope of suppression
18+
import AS::Make<SingleLineComment>

go/ql/src/qlpack.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ defaultSuiteFile: codeql-suites/go-code-scanning.qls
99
dependencies:
1010
codeql/go-all: ${workspace}
1111
codeql/suite-helpers: ${workspace}
12+
codeql/util: ${workspace}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tstWindows.go eol=crlf

java/ql/src/AlertSuppression.ql

Lines changed: 9 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,77 +5,17 @@
55
* @id java/alert-suppression
66
*/
77

8-
import java
9-
10-
/**
11-
* An alert suppression comment.
12-
*/
13-
class SuppressionComment extends Javadoc {
14-
string annotation;
15-
16-
SuppressionComment() {
17-
// suppression comments must be single-line
18-
(
19-
isEolComment(this)
20-
or
21-
isNormalComment(this) and exists(int line | this.hasLocationInfo(_, line, _, line, _))
22-
) and
23-
exists(string text | text = this.getChild(0).getText() |
24-
// match `lgtm[...]` anywhere in the comment
25-
annotation = text.regexpFind("(?i)\\blgtm\\s*\\[[^\\]]*\\]", _, _)
26-
or
27-
// match `lgtm` at the start of the comment and after semicolon
28-
annotation = text.regexpFind("(?i)(?<=^|;)\\s*lgtm(?!\\B|\\s*\\[)", _, _).trim()
29-
)
8+
private import codeql.suppression.AlertSuppression as AS
9+
private import semmle.code.java.Javadoc
10+
11+
class SingleLineComment extends Javadoc {
12+
SingleLineComment() {
13+
isEolComment(this)
14+
or
15+
isNormalComment(this) and exists(int line | this.hasLocationInfo(_, line, _, line, _))
3016
}
3117

32-
/**
33-
* Gets the text of this suppression comment.
34-
*/
3518
string getText() { result = this.getChild(0).getText() }
36-
37-
/** Gets the suppression annotation in this comment. */
38-
string getAnnotation() { result = annotation }
39-
40-
/**
41-
* Holds if this comment applies to the range from column `startcolumn` of line `startline`
42-
* to column `endcolumn` of line `endline` in file `filepath`.
43-
*/
44-
predicate covers(string filepath, int startline, int startcolumn, int endline, int endcolumn) {
45-
this.getLocation().hasLocationInfo(filepath, startline, _, endline, endcolumn) and
46-
startcolumn = 1
47-
}
48-
49-
/** Gets the scope of this suppression. */
50-
SuppressionScope getScope() { this = result.getSuppressionComment() }
51-
}
52-
53-
/**
54-
* The scope of an alert suppression comment.
55-
*/
56-
class SuppressionScope extends @javadoc instanceof SuppressionComment {
57-
/** Gets a suppression comment with this scope. */
58-
SuppressionComment getSuppressionComment() { result = this }
59-
60-
/**
61-
* Holds if this element is at the specified location.
62-
* The location spans column `startcolumn` of line `startline` to
63-
* column `endcolumn` of line `endline` in file `filepath`.
64-
* For more information, see
65-
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
66-
*/
67-
predicate hasLocationInfo(
68-
string filepath, int startline, int startcolumn, int endline, int endcolumn
69-
) {
70-
super.covers(filepath, startline, startcolumn, endline, endcolumn)
71-
}
72-
73-
/** Gets a textual representation of this element. */
74-
string toString() { result = "suppression range" }
7519
}
7620

77-
from SuppressionComment c
78-
select c, // suppression comment
79-
c.getText(), // text of suppression comment (excluding delimiters)
80-
c.getAnnotation(), // text of suppression annotation
81-
c.getScope() // scope of suppression
21+
import AS::Make<SingleLineComment>

0 commit comments

Comments
 (0)