Go: use shared AlertSuppression.qll

This commit is contained in:
Arthur Baars
2022-12-14 15:51:32 +01:00
parent 23f595bea1
commit b0e8085765
2 changed files with 7 additions and 65 deletions

View File

@@ -5,73 +5,14 @@
* @id go/alert-suppression
*/
import go
private import codeql.suppression.AlertSuppression as AS
private import semmle.go.Comments as G
/**
* An alert suppression comment.
*/
class SuppressionComment extends Locatable {
string text;
string annotation;
SuppressionComment() {
text = this.(Comment).getText() and
class SingleLineComment extends G::Comment {
SingleLineComment() {
// suppression comments must be single-line
not text.matches("%\n%") and
(
// match `lgtm[...]` anywhere in the comment
annotation = text.regexpFind("(?i)\\blgtm\\s*\\[[^\\]]*\\]", _, _)
or
// match `lgtm` at the start of the comment and after semicolon
annotation = text.regexpFind("(?i)(?<=^|;)\\s*lgtm(?!\\B|\\s*\\[)", _, _).trim()
)
not this.getText().matches("%\n%")
}
/** Gets the text of this suppression comment, not including delimiters. */
string getText() { result = text }
/** Gets the suppression annotation in this comment. */
string getAnnotation() { result = annotation }
/**
* Holds if this comment applies to the range from column `startcolumn` of line `startline`
* to column `endcolumn` of line `endline` in file `filepath`.
*/
predicate covers(string filepath, int startline, int startcolumn, int endline, int endcolumn) {
this.getLocation().hasLocationInfo(filepath, startline, _, endline, endcolumn) and
startcolumn = 1
}
/** Gets the scope of this suppression. */
SuppressionScope getScope() { this = result.getSuppressionComment() }
}
/**
* The scope of an alert suppression comment.
*/
class SuppressionScope extends @locatable instanceof SuppressionComment {
/** Gets a suppression comment with this scope. */
SuppressionComment getSuppressionComment() { result = this }
/**
* Holds if this element is at the specified location.
* The location spans column `startcolumn` of line `startline` to
* column `endcolumn` of line `endline` in file `filepath`.
* For more information, see
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
*/
predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
super.covers(filepath, startline, startcolumn, endline, endcolumn)
}
/** Gets a textual representation of this element. */
string toString() { result = "suppression range" }
}
from SuppressionComment c
select c, // suppression comment
c.getText(), // text of suppression comment (excluding delimiters)
c.getAnnotation(), // text of suppression annotation
c.getScope() // scope of suppression
import AS::Make<SingleLineComment>

View File

@@ -9,3 +9,4 @@ defaultSuiteFile: codeql-suites/go-code-scanning.qls
dependencies:
codeql/go-all: ${workspace}
codeql/suite-helpers: ${workspace}
codeql/util: ${workspace}