Skip to content

Commit c9739b2

Browse files
committed
AlertSuppression: add support for //codeql comments
1 parent c176606 commit c9739b2

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

java/ql/src/AlertSuppressionAnnotations.ql

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import Metrics.Internal.Extents
1212
/** Gets the LGTM suppression annotation text in the string `s`, if any. */
1313
bindingset[s]
1414
string getAnnotationText(string s) {
15-
// match `lgtm[...]` anywhere in the comment
16-
result = s.regexpFind("(?i)\\blgtm\\s*\\[[^\\]]*\\]", _, _)
15+
// match `lgtm[...]` or `codeql[...]` anywhere in the comment
16+
result = s.regexpFind("(?i)\\b(lgtm|codeql)\\s*\\[[^\\]]*\\]", _, _).trim()
1717
}
1818

1919
/**
@@ -96,5 +96,5 @@ where
9696
annotationText = getAnnotationText(text)
9797
select c, // suppression entity
9898
text, // full text of suppression string
99-
annotationText, // LGTM suppression annotation text
99+
annotationText.regexpReplaceAll("(?i)^codeql", "lgtm"), // LGTM suppression annotation text
100100
c.getScope() // scope of suppression

python/ql/test/query-tests/analysis/suppression/AlertSuppression.expected

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
| testWindows.py:39:3:39:7 | Comment #noqa | noqa | lgtm | testWindows.py:39:1:39:7 | suppression range |
6666
| testWindows.py:40:4:40:9 | Comment # noqa | noqa | lgtm | testWindows.py:40:1:40:9 | suppression range |
6767
| testWindows.py:45:1:45:28 | Comment # noqa -- Some extra detail. | noqa -- Some extra detail. | lgtm | testWindows.py:45:1:45:28 | suppression range |
68-
| testWindows.py:45:1:45:28 | Comment # noqa -- Some extra detail. | noqa -- Some extra detail. | lgtm | testWindows.py:46:0:46:0 | suppression range |
6968
| testWindows.py:48:4:48:60 | Comment # lgtm[py/line-too-long] and lgtm[py/non-callable-called] | lgtm[py/line-too-long] and lgtm[py/non-callable-called] | lgtm[py/line-too-long] | testWindows.py:48:1:48:60 | suppression range |
7069
| testWindows.py:48:4:48:60 | Comment # lgtm[py/line-too-long] and lgtm[py/non-callable-called] | lgtm[py/line-too-long] and lgtm[py/non-callable-called] | lgtm[py/non-callable-called] | testWindows.py:48:1:48:60 | suppression range |
7170
| testWindows.py:49:4:49:33 | Comment # lgtm[py/line-too-long]; lgtm | lgtm[py/line-too-long]; lgtm | lgtm | testWindows.py:49:1:49:33 | suppression range |

shared/util/codeql/suppression/AlertSuppression.qll

+28
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,34 @@ module Make<AstNode Node, SingleLineComment Comment> {
9494
}
9595
}
9696

97+
private class CodeQlSuppressionComment extends SuppressionComment {
98+
private string annotation;
99+
100+
CodeQlSuppressionComment() {
101+
// match `codeql[...]` anywhere in the comment
102+
annotation = this.(Comment).getText().regexpFind("(?i)\\bcodeql\\s*\\[[^\\]]*\\]", _, _) and
103+
exists(string filepath, int cStartLine, int cStartColumn |
104+
this.(Comment).hasLocationInfo(filepath, cStartLine, cStartColumn, _, _) and
105+
not exists(int c, Node n | c < cStartColumn |
106+
n.hasLocationInfo(filepath, _, _, cStartLine, c) or
107+
n.hasLocationInfo(filepath, cStartLine, c, _, _)
108+
)
109+
)
110+
}
111+
112+
override string getAnnotation() { result = "lgtm" + annotation.suffix(6) }
113+
114+
override predicate covers(
115+
string filepath, int startline, int startcolumn, int endline, int endcolumn
116+
) {
117+
this.hasLocationInfo(filepath, _, _, startline - 1, _) and
118+
// when there is no column information, a location spans the whole line
119+
startcolumn = 0 and
120+
endcolumn = 0 and
121+
endline = startline
122+
}
123+
}
124+
97125
/**
98126
* The scope of an alert suppression comment.
99127
*/

0 commit comments

Comments
 (0)