Skip to content

Commit 7111d95

Browse files
committed
Swift: add AlertSuppression.ql
1 parent dceb142 commit 7111d95

File tree

7 files changed

+231
-0
lines changed

7 files changed

+231
-0
lines changed

swift/ql/src/AlertSuppression.ql

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @name Alert suppression
3+
* @description Generates information about alert suppressions.
4+
* @kind alert-suppression
5+
* @id swift/alert-suppression
6+
*/
7+
8+
private import codeql.util.suppression.AlertSuppression as AS
9+
private import codeql.swift.elements.Locatable as L
10+
private import codeql.swift.elements.Comment as C
11+
12+
class AstNode extends L::Locatable {
13+
predicate hasLocationInfo(string path, int startLine, int startColumn, int endLine, int endColumn) {
14+
this.getLocation().hasLocationInfo(path, startLine, startColumn, endLine, endColumn)
15+
}
16+
}
17+
18+
class SingleLineComment extends AstNode instanceof C::Comment {
19+
private string text;
20+
21+
SingleLineComment() {
22+
this instanceof C::SingleLineComment and
23+
text = super.getText().regexpCapture("//([^\\r\\n]*)[\\r\\n]?", 1)
24+
or
25+
this instanceof C::MultiLineComment and
26+
// suppression comments must be single-line
27+
text = super.getText().regexpCapture("/\\*([^\\r\\n]*)\\*/", 1)
28+
}
29+
30+
override predicate hasLocationInfo(
31+
string path, int startLine, int startColumn, int endLine, int endColumn
32+
) {
33+
this.(C::SingleLineComment).getLocation().hasLocationInfo(path, startLine, startColumn, _, _) and
34+
endLine = startLine and
35+
endColumn = startColumn + text.length() + 1
36+
or
37+
this.(C::MultiLineComment)
38+
.getLocation()
39+
.hasLocationInfo(path, startLine, startColumn, endLine, endColumn + 1)
40+
}
41+
42+
string getText() { result = text }
43+
}
44+
45+
import AS::Make<AstNode, SingleLineComment>

swift/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/swift-code-scanning.qls
99
dependencies:
1010
codeql/swift-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.swift eol=crlf
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
| tst.swift:7:8:7:14 | // lgtm\n | lgtm | lgtm | tst.swift:7:1:7:14 | suppression range |
2+
| tst.swift:8:8:8:42 | // lgtm[swift/redundant-assignment]\n | lgtm[swift/redundant-assignment] | lgtm[swift/redundant-assignment] | tst.swift:8:1:8:42 | suppression range |
3+
| tst.swift:9:8:9:42 | // lgtm[swift/redundant-assignment]\n | lgtm[swift/redundant-assignment] | lgtm[swift/redundant-assignment] | tst.swift:9:1:9:42 | suppression range |
4+
| tst.swift:10:8:10:69 | // lgtm[swift/redundant-assignment, swift/redundant-operation]\n | lgtm[swift/redundant-assignment, swift/redundant-operation] | lgtm[swift/redundant-assignment, swift/redundant-operation] | tst.swift:10:1:10:69 | suppression range |
5+
| tst.swift:11:8:11:29 | // lgtm[@tag:nullness]\n | lgtm[@tag:nullness] | lgtm[@tag:nullness] | tst.swift:11:1:11:29 | suppression range |
6+
| tst.swift:12:8:12:56 | // lgtm[@tag:nullness,swift/redundant-assignment]\n | lgtm[@tag:nullness,swift/redundant-assignment] | lgtm[@tag:nullness,swift/redundant-assignment] | tst.swift:12:1:12:56 | suppression range |
7+
| tst.swift:13:8:13:35 | // lgtm[@expires:2017-06-11]\n | lgtm[@expires:2017-06-11] | lgtm[@expires:2017-06-11] | tst.swift:13:1:13:35 | suppression range |
8+
| tst.swift:14:8:14:73 | // lgtm[swift/redundant-operation] because I know better than lgtm\n | lgtm[swift/redundant-operation] because I know better than lgtm | lgtm[swift/redundant-operation] | tst.swift:14:1:14:73 | suppression range |
9+
| tst.swift:15:8:15:25 | // lgtm: blah blah\n | lgtm: blah blah | lgtm | tst.swift:15:1:15:25 | suppression range |
10+
| tst.swift:16:8:16:39 | // lgtm blah blah #falsepositive\n | lgtm blah blah #falsepositive | lgtm | tst.swift:16:1:16:39 | suppression range |
11+
| tst.swift:17:8:17:42 | //lgtm [swift/redundant-operation]\n | lgtm [swift/redundant-operation] | lgtm [swift/redundant-operation] | tst.swift:17:1:17:42 | suppression range |
12+
| tst.swift:18:8:18:17 | /* lgtm */ | lgtm | lgtm | tst.swift:18:1:18:17 | suppression range |
13+
| tst.swift:19:8:19:16 | // lgtm[]\n | lgtm[] | lgtm[] | tst.swift:19:1:19:16 | suppression range |
14+
| tst.swift:21:8:21:13 | //lgtm\n | lgtm | lgtm | tst.swift:21:1:21:13 | suppression range |
15+
| tst.swift:22:8:22:14 | //\tlgtm\n | \tlgtm | lgtm | tst.swift:22:1:22:14 | suppression range |
16+
| tst.swift:23:8:23:43 | // lgtm\t[swift/redundant-assignment]\n | lgtm\t[swift/redundant-assignment] | lgtm\t[swift/redundant-assignment] | tst.swift:23:1:23:43 | suppression range |
17+
| tst.swift:26:8:26:19 | // foo; lgtm\n | foo; lgtm | lgtm | tst.swift:26:1:26:19 | suppression range |
18+
| tst.swift:27:8:27:47 | // foo; lgtm[swift/redundant-assignment]\n | foo; lgtm[swift/redundant-assignment] | lgtm[swift/redundant-assignment] | tst.swift:27:1:27:47 | suppression range |
19+
| tst.swift:29:8:29:46 | // foo lgtm[swift/redundant-assignment]\n | foo lgtm[swift/redundant-assignment] | lgtm[swift/redundant-assignment] | tst.swift:29:1:29:46 | suppression range |
20+
| tst.swift:31:8:31:50 | // foo lgtm[swift/redundant-assignment] bar\n | foo lgtm[swift/redundant-assignment] bar | lgtm[swift/redundant-assignment] | tst.swift:31:1:31:50 | suppression range |
21+
| tst.swift:32:8:32:15 | // LGTM!\n | LGTM! | LGTM | tst.swift:32:1:32:15 | suppression range |
22+
| tst.swift:33:8:33:42 | // LGTM[swift/redundant-assignment]\n | LGTM[swift/redundant-assignment] | LGTM[swift/redundant-assignment] | tst.swift:33:1:33:42 | suppression range |
23+
| tst.swift:34:8:34:78 | // lgtm[swift/redundant-assignment] and lgtm[swift/redundant-operation]\n | lgtm[swift/redundant-assignment] and lgtm[swift/redundant-operation] | lgtm[swift/redundant-assignment] | tst.swift:34:1:34:78 | suppression range |
24+
| tst.swift:34:8:34:78 | // lgtm[swift/redundant-assignment] and lgtm[swift/redundant-operation]\n | lgtm[swift/redundant-assignment] and lgtm[swift/redundant-operation] | lgtm[swift/redundant-operation] | tst.swift:34:1:34:78 | suppression range |
25+
| tst.swift:35:8:35:48 | // lgtm[swift/redundant-assignment]; lgtm\n | lgtm[swift/redundant-assignment]; lgtm | lgtm | tst.swift:35:1:35:48 | suppression range |
26+
| tst.swift:35:8:35:48 | // lgtm[swift/redundant-assignment]; lgtm\n | lgtm[swift/redundant-assignment]; lgtm | lgtm[swift/redundant-assignment] | tst.swift:35:1:35:48 | suppression range |
27+
| tst.swift:36:8:36:19 | /* lgtm[] */ | lgtm[] | lgtm[] | tst.swift:36:1:36:19 | suppression range |
28+
| tst.swift:37:8:37:45 | /* lgtm[swift/redundant-assignment] */ | lgtm[swift/redundant-assignment] | lgtm[swift/redundant-assignment] | tst.swift:37:1:37:45 | suppression range |
29+
| tst.swift:43:8:43:59 | /* lgtm[@tag:nullness,swift/redundant-assignment] */ | lgtm[@tag:nullness,swift/redundant-assignment] | lgtm[@tag:nullness,swift/redundant-assignment] | tst.swift:43:1:43:59 | suppression range |
30+
| tst.swift:44:8:44:32 | /* lgtm[@tag:nullness] */ | lgtm[@tag:nullness] | lgtm[@tag:nullness] | tst.swift:44:1:44:32 | suppression range |
31+
| tst.swift:45:2:45:38 | // codeql[swift/redundant-assignment]\n | codeql[swift/redundant-assignment] | lgtm[swift/redundant-assignment] | tst.swift:46:0:46:0 | suppression range |
32+
| tst.swift:47:2:47:38 | // CODEQL[swift/redundant-assignment]\n | CODEQL[swift/redundant-assignment] | lgtm[swift/redundant-assignment] | tst.swift:48:0:48:0 | suppression range |
33+
| tst.swift:49:2:49:75 | // codeql[swift/redundant-assignment] -- because I know better than codeql\n | codeql[swift/redundant-assignment] -- because I know better than codeql | lgtm[swift/redundant-assignment] | tst.swift:50:0:50:0 | suppression range |
34+
| tst.swift:51:2:51:41 | /* codeql[swift/redundant-assignment] */ | codeql[swift/redundant-assignment] | lgtm[swift/redundant-assignment] | tst.swift:52:0:52:0 | suppression range |
35+
| tstWindows.swift:7:8:7:14 | // lgtm\r | lgtm | lgtm | tstWindows.swift:7:1:7:14 | suppression range |
36+
| tstWindows.swift:8:8:8:42 | // lgtm[swift/redundant-assignment]\r | lgtm[swift/redundant-assignment] | lgtm[swift/redundant-assignment] | tstWindows.swift:8:1:8:42 | suppression range |
37+
| tstWindows.swift:9:8:9:42 | // lgtm[swift/redundant-assignment]\r | lgtm[swift/redundant-assignment] | lgtm[swift/redundant-assignment] | tstWindows.swift:9:1:9:42 | suppression range |
38+
| tstWindows.swift:10:8:10:69 | // lgtm[swift/redundant-assignment, swift/redundant-operation]\r | lgtm[swift/redundant-assignment, swift/redundant-operation] | lgtm[swift/redundant-assignment, swift/redundant-operation] | tstWindows.swift:10:1:10:69 | suppression range |
39+
| tstWindows.swift:11:8:11:29 | // lgtm[@tag:nullness]\r | lgtm[@tag:nullness] | lgtm[@tag:nullness] | tstWindows.swift:11:1:11:29 | suppression range |
40+
| tstWindows.swift:12:8:12:56 | // lgtm[@tag:nullness,swift/redundant-assignment]\r | lgtm[@tag:nullness,swift/redundant-assignment] | lgtm[@tag:nullness,swift/redundant-assignment] | tstWindows.swift:12:1:12:56 | suppression range |
41+
| tstWindows.swift:13:8:13:35 | // lgtm[@expires:2017-06-11]\r | lgtm[@expires:2017-06-11] | lgtm[@expires:2017-06-11] | tstWindows.swift:13:1:13:35 | suppression range |
42+
| tstWindows.swift:14:8:14:73 | // lgtm[swift/redundant-operation] because I know better than lgtm\r | lgtm[swift/redundant-operation] because I know better than lgtm | lgtm[swift/redundant-operation] | tstWindows.swift:14:1:14:73 | suppression range |
43+
| tstWindows.swift:15:8:15:25 | // lgtm: blah blah\r | lgtm: blah blah | lgtm | tstWindows.swift:15:1:15:25 | suppression range |
44+
| tstWindows.swift:16:8:16:39 | // lgtm blah blah #falsepositive\r | lgtm blah blah #falsepositive | lgtm | tstWindows.swift:16:1:16:39 | suppression range |
45+
| tstWindows.swift:17:8:17:42 | //lgtm [swift/redundant-operation]\r | lgtm [swift/redundant-operation] | lgtm [swift/redundant-operation] | tstWindows.swift:17:1:17:42 | suppression range |
46+
| tstWindows.swift:18:8:18:17 | /* lgtm */ | lgtm | lgtm | tstWindows.swift:18:1:18:17 | suppression range |
47+
| tstWindows.swift:19:8:19:16 | // lgtm[]\r | lgtm[] | lgtm[] | tstWindows.swift:19:1:19:16 | suppression range |
48+
| tstWindows.swift:21:8:21:13 | //lgtm\r | lgtm | lgtm | tstWindows.swift:21:1:21:13 | suppression range |
49+
| tstWindows.swift:22:8:22:14 | //\tlgtm\r | \tlgtm | lgtm | tstWindows.swift:22:1:22:14 | suppression range |
50+
| tstWindows.swift:23:8:23:43 | // lgtm\t[swift/redundant-assignment]\r | lgtm\t[swift/redundant-assignment] | lgtm\t[swift/redundant-assignment] | tstWindows.swift:23:1:23:43 | suppression range |
51+
| tstWindows.swift:26:8:26:19 | // foo; lgtm\r | foo; lgtm | lgtm | tstWindows.swift:26:1:26:19 | suppression range |
52+
| tstWindows.swift:27:8:27:47 | // foo; lgtm[swift/redundant-assignment]\r | foo; lgtm[swift/redundant-assignment] | lgtm[swift/redundant-assignment] | tstWindows.swift:27:1:27:47 | suppression range |
53+
| tstWindows.swift:29:8:29:46 | // foo lgtm[swift/redundant-assignment]\r | foo lgtm[swift/redundant-assignment] | lgtm[swift/redundant-assignment] | tstWindows.swift:29:1:29:46 | suppression range |
54+
| tstWindows.swift:31:8:31:50 | // foo lgtm[swift/redundant-assignment] bar\r | foo lgtm[swift/redundant-assignment] bar | lgtm[swift/redundant-assignment] | tstWindows.swift:31:1:31:50 | suppression range |
55+
| tstWindows.swift:32:8:32:15 | // LGTM!\r | LGTM! | LGTM | tstWindows.swift:32:1:32:15 | suppression range |
56+
| tstWindows.swift:33:8:33:42 | // LGTM[swift/redundant-assignment]\r | LGTM[swift/redundant-assignment] | LGTM[swift/redundant-assignment] | tstWindows.swift:33:1:33:42 | suppression range |
57+
| tstWindows.swift:34:8:34:78 | // lgtm[swift/redundant-assignment] and lgtm[swift/redundant-operation]\r | lgtm[swift/redundant-assignment] and lgtm[swift/redundant-operation] | lgtm[swift/redundant-assignment] | tstWindows.swift:34:1:34:78 | suppression range |
58+
| tstWindows.swift:34:8:34:78 | // lgtm[swift/redundant-assignment] and lgtm[swift/redundant-operation]\r | lgtm[swift/redundant-assignment] and lgtm[swift/redundant-operation] | lgtm[swift/redundant-operation] | tstWindows.swift:34:1:34:78 | suppression range |
59+
| tstWindows.swift:35:8:35:48 | // lgtm[swift/redundant-assignment]; lgtm\r | lgtm[swift/redundant-assignment]; lgtm | lgtm | tstWindows.swift:35:1:35:48 | suppression range |
60+
| tstWindows.swift:35:8:35:48 | // lgtm[swift/redundant-assignment]; lgtm\r | lgtm[swift/redundant-assignment]; lgtm | lgtm[swift/redundant-assignment] | tstWindows.swift:35:1:35:48 | suppression range |
61+
| tstWindows.swift:36:8:36:19 | /* lgtm[] */ | lgtm[] | lgtm[] | tstWindows.swift:36:1:36:19 | suppression range |
62+
| tstWindows.swift:37:8:37:45 | /* lgtm[swift/redundant-assignment] */ | lgtm[swift/redundant-assignment] | lgtm[swift/redundant-assignment] | tstWindows.swift:37:1:37:45 | suppression range |
63+
| tstWindows.swift:43:8:43:59 | /* lgtm[@tag:nullness,swift/redundant-assignment] */ | lgtm[@tag:nullness,swift/redundant-assignment] | lgtm[@tag:nullness,swift/redundant-assignment] | tstWindows.swift:43:1:43:59 | suppression range |
64+
| tstWindows.swift:44:8:44:32 | /* lgtm[@tag:nullness] */ | lgtm[@tag:nullness] | lgtm[@tag:nullness] | tstWindows.swift:44:1:44:32 | suppression range |
65+
| tstWindows.swift:45:2:45:38 | // codeql[swift/redundant-assignment]\r | codeql[swift/redundant-assignment] | lgtm[swift/redundant-assignment] | tstWindows.swift:46:0:46:0 | suppression range |
66+
| tstWindows.swift:47:2:47:38 | // CODEQL[swift/redundant-assignment]\r | CODEQL[swift/redundant-assignment] | lgtm[swift/redundant-assignment] | tstWindows.swift:48:0:48:0 | suppression range |
67+
| tstWindows.swift:49:2:49:75 | // codeql[swift/redundant-assignment] -- because I know better than codeql\r | codeql[swift/redundant-assignment] -- because I know better than codeql | lgtm[swift/redundant-assignment] | tstWindows.swift:50:0:50:0 | suppression range |
68+
| tstWindows.swift:51:2:51:41 | /* codeql[swift/redundant-assignment] */ | codeql[swift/redundant-assignment] | lgtm[swift/redundant-assignment] | tstWindows.swift:52:0:52:0 | suppression range |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AlertSuppression.ql
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
3+
4+
5+
func main() {
6+
var x = 42
7+
x = 1 // lgtm
8+
x = 1 // lgtm[swift/redundant-assignment]
9+
x = 1 // lgtm[swift/redundant-assignment]
10+
x = 1 // lgtm[swift/redundant-assignment, swift/redundant-operation]
11+
x = 1 // lgtm[@tag:nullness]
12+
x = 1 // lgtm[@tag:nullness,swift/redundant-assignment]
13+
x = 1 // lgtm[@expires:2017-06-11]
14+
x = 1 // lgtm[swift/redundant-operation] because I know better than lgtm
15+
x = 1 // lgtm: blah blah
16+
x = 1 // lgtm blah blah #falsepositive
17+
x = 1 //lgtm [swift/redundant-operation]
18+
x = 1 /* lgtm */
19+
x = 1 // lgtm[]
20+
x = 1 // lgtmfoo
21+
x = 1 //lgtm
22+
x = 1 // lgtm
23+
x = 1 // lgtm [swift/redundant-assignment]
24+
x = 1 // foolgtm[swift/redundant-assignment]
25+
x = 1 // foolgtm
26+
x = 1 // foo; lgtm
27+
x = 1 // foo; lgtm[swift/redundant-assignment]
28+
x = 1 // foo lgtm
29+
x = 1 // foo lgtm[swift/redundant-assignment]
30+
x = 1 // foo lgtm bar
31+
x = 1 // foo lgtm[swift/redundant-assignment] bar
32+
x = 1 // LGTM!
33+
x = 1 // LGTM[swift/redundant-assignment]
34+
x = 1 // lgtm[swift/redundant-assignment] and lgtm[swift/redundant-operation]
35+
x = 1 // lgtm[swift/redundant-assignment]; lgtm
36+
x = 1 /* lgtm[] */
37+
x = 1 /* lgtm[swift/redundant-assignment] */
38+
x = 1 /* lgtm
39+
*/
40+
x = 1 /* lgtm
41+
42+
*/
43+
x = 1 /* lgtm[@tag:nullness,swift/redundant-assignment] */
44+
x = 1 /* lgtm[@tag:nullness] */
45+
// codeql[swift/redundant-assignment]
46+
x = 1
47+
// CODEQL[swift/redundant-assignment]
48+
x = 1
49+
// codeql[swift/redundant-assignment] -- because I know better than codeql
50+
x = 1
51+
/* codeql[swift/redundant-assignment] */
52+
x = 1
53+
/* codeql[swift/redundant-assignment]
54+
*/
55+
x = 1
56+
x = 1 // codeql[swift/redundant-assignment]
57+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
3+
4+
5+
func winMain() {
6+
var x = 42
7+
x = 1 // lgtm
8+
x = 1 // lgtm[swift/redundant-assignment]
9+
x = 1 // lgtm[swift/redundant-assignment]
10+
x = 1 // lgtm[swift/redundant-assignment, swift/redundant-operation]
11+
x = 1 // lgtm[@tag:nullness]
12+
x = 1 // lgtm[@tag:nullness,swift/redundant-assignment]
13+
x = 1 // lgtm[@expires:2017-06-11]
14+
x = 1 // lgtm[swift/redundant-operation] because I know better than lgtm
15+
x = 1 // lgtm: blah blah
16+
x = 1 // lgtm blah blah #falsepositive
17+
x = 1 //lgtm [swift/redundant-operation]
18+
x = 1 /* lgtm */
19+
x = 1 // lgtm[]
20+
x = 1 // lgtmfoo
21+
x = 1 //lgtm
22+
x = 1 // lgtm
23+
x = 1 // lgtm [swift/redundant-assignment]
24+
x = 1 // foolgtm[swift/redundant-assignment]
25+
x = 1 // foolgtm
26+
x = 1 // foo; lgtm
27+
x = 1 // foo; lgtm[swift/redundant-assignment]
28+
x = 1 // foo lgtm
29+
x = 1 // foo lgtm[swift/redundant-assignment]
30+
x = 1 // foo lgtm bar
31+
x = 1 // foo lgtm[swift/redundant-assignment] bar
32+
x = 1 // LGTM!
33+
x = 1 // LGTM[swift/redundant-assignment]
34+
x = 1 // lgtm[swift/redundant-assignment] and lgtm[swift/redundant-operation]
35+
x = 1 // lgtm[swift/redundant-assignment]; lgtm
36+
x = 1 /* lgtm[] */
37+
x = 1 /* lgtm[swift/redundant-assignment] */
38+
x = 1 /* lgtm
39+
*/
40+
x = 1 /* lgtm
41+
42+
*/
43+
x = 1 /* lgtm[@tag:nullness,swift/redundant-assignment] */
44+
x = 1 /* lgtm[@tag:nullness] */
45+
// codeql[swift/redundant-assignment]
46+
x = 1
47+
// CODEQL[swift/redundant-assignment]
48+
x = 1
49+
// codeql[swift/redundant-assignment] -- because I know better than codeql
50+
x = 1
51+
/* codeql[swift/redundant-assignment] */
52+
x = 1
53+
/* codeql[swift/redundant-assignment]
54+
*/
55+
x = 1
56+
x = 1 // codeql[swift/redundant-assignment]
57+
}
58+

0 commit comments

Comments
 (0)