Skip to content

Commit f738567

Browse files
committed
refactor some code out into a helper class QueryDoc
1 parent 5e53124 commit f738567

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

ql/ql/src/codeql_ql/ast/Ast.qll

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,29 @@ class QLDoc extends TQLDoc, Comment {
176176
override AstNode getParent() { result.getQLDoc() = this }
177177
}
178178

179+
/** The QLDoc for a query (i.e. the top comment in a .ql file). */
180+
class QueryDoc extends QLDoc {
181+
QueryDoc() {
182+
this.getLocation().getFile().getExtension() = "ql" and
183+
this = any(TopLevel t).getQLDoc()
184+
}
185+
186+
override string getAPrimaryQlClass() { result = "QueryDoc" }
187+
188+
/** Gets the @kind for the query */
189+
string getQueryKind() { result = this.getContents().regexpCapture("(?s).*@kind (\\w+)\\s.*", 1) }
190+
191+
/** Gets the id part (without language) of the @id */
192+
string getQueryId() {
193+
result = this.getContents().regexpCapture("(?s).*@id (\\w+)/([\\w\\-]+)\\s.*", 2)
194+
}
195+
196+
/** Gets the language of the @id */
197+
string getQueryLanguage() {
198+
result = this.getContents().regexpCapture("(?s).*@id (\\w+)/([\\w\\-]+)\\s.*", 1)
199+
}
200+
}
201+
179202
class BlockComment extends TBlockComment, Comment {
180203
QL::BlockComment comment;
181204

@@ -237,6 +260,8 @@ class Select extends TSelect, AstNode {
237260
}
238261

239262
override string getAPrimaryQlClass() { result = "Select" }
263+
264+
QueryDoc getQueryDoc() { result.getLocation().getFile() = this.getLocation().getFile() }
240265
}
241266

242267
class PredicateOrBuiltin extends TPredOrBuiltin, AstNode {

ql/ql/src/queries/style/ConsistentAlertMessage.ql

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,10 @@ string getMessage(Select sel) {
3131
* This fingerprint avoid false positives where two queries with the same ID behave differently (which is OK).
3232
*/
3333
string getSelectFingerPrint(Select sel) {
34-
exists(File file, QLDoc doc |
35-
sel.getLocation().getFile() = file and
36-
any(TopLevel top | top.getLocation().getFile() = file).getQLDoc() = doc
37-
|
34+
exists(QueryDoc doc | doc = sel.getQueryDoc() |
3835
result =
39-
doc.getContents().regexpCapture("(?s).*@id (\\w+)/([\\w\\-]+)\\s.*", 2) // query ID (without lang)
40-
+ "-" + doc.getContents().regexpCapture("(?s).*@kind (\\w+)\\s.*", 1) // @kind
36+
doc.getQueryId() // query ID (without lang)
37+
+ "-" + doc.getQueryKind() // @kind
4138
+ "-" +
4239
strictcount(String e | e.getParent*() = sel.getExpr(any(int i | i % 2 = 1))) // the number of string constants in the select
4340
+ "-" + count(sel.getExpr(_)) // and the total number of expressions in the select
@@ -49,10 +46,10 @@ string getSelectFingerPrint(Select sel) {
4946
* The query-id (without language), the language, the message from the select, and a language agnostic fingerprint.
5047
*/
5148
Select parseSelect(string id, string lang, string msg, string fingerPrint) {
52-
exists(File file, QLDoc doc | result.getLocation().getFile() = file |
53-
any(TopLevel top | top.getLocation().getFile() = file).getQLDoc() = doc and
54-
id = doc.getContents().regexpCapture("(?s).*@id (\\w+)/([\\w\\-]+)\\s.*", 2) and
55-
lang = doc.getContents().regexpCapture("(?s).*@id (\\w+)/([\\w\\-]+)\\s.*", 1) and
49+
exists(QueryDoc doc |
50+
doc = result.getQueryDoc() and
51+
id = doc.getQueryId() and
52+
lang = doc.getQueryLanguage() and
5653
fingerPrint = getSelectFingerPrint(result) and
5754
msg = getMessage(result).toLowerCase() // case normalize, because some languages upper-case methods.
5855
) and

0 commit comments

Comments
 (0)