Skip to content

Commit ad80822

Browse files
committed
C/C++: use shared AlertSuppression.qll
1 parent b0e8085 commit ad80822

File tree

3 files changed

+82
-124
lines changed

3 files changed

+82
-124
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

0 commit comments

Comments
 (0)