Skip to content

Commit bc646d4

Browse files
committed
Java: use shared AlertSuppression.qll
1 parent 072a180 commit bc646d4

File tree

2 files changed

+10
-69
lines changed

2 files changed

+10
-69
lines changed

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>

java/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/java-code-scanning.qls
99
dependencies:
1010
codeql/java-all: ${workspace}
1111
codeql/suite-helpers: ${workspace}
12+
codeql/util: ${workspace}

0 commit comments

Comments
 (0)