Skip to content

Commit bf98e96

Browse files
authored
QL: Merge pull request #17 from github/first-query
Add the `ql/primary-ql-class-consistency` query
2 parents 77758e5 + 6f7cbf7 commit bf98e96

5 files changed

Lines changed: 245 additions & 164 deletions

File tree

ql/src/codeql_ql/ast/Ast.qll

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ class TypeExpr extends TType, AstNode {
280280

281281
TypeExpr() { this = TType(type) }
282282

283-
override string getAPrimaryQlClass() { result = "Type" }
283+
override string getAPrimaryQlClass() { result = "TypeExpr" }
284284

285285
/**
286286
* Gets the class name for the type.
@@ -767,7 +767,11 @@ class String extends Literal {
767767
override string getAPrimaryQlClass() { result = "String" }
768768

769769
/** Gets the string value of this literal. */
770-
string getValue() { result = lit.getChild().(Generated::String).getValue() }
770+
string getValue() {
771+
exists(string raw | raw = lit.getChild().(Generated::String).getValue() |
772+
result = raw.substring(1, raw.length() - 1)
773+
)
774+
}
771775
}
772776

773777
/** An integer literal. */

ql/src/codeql_ql/ast/internal/AstNodes.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ class TFormula =
6464
class TBinOpExpr = TAddSubExpr or TMulDivModExpr;
6565

6666
class TExpr =
67-
TBinOpExpr or TLiteral or TAggregate or TExprAggregate or TIdentifier or TInlineCast or TCall or TUnaryExpr or
68-
TExprAnnotation or TDontCare or TRange or TSet;
67+
TBinOpExpr or TLiteral or TAggregate or TExprAggregate or TIdentifier or TInlineCast or TCall or
68+
TUnaryExpr or TExprAnnotation or TDontCare or TRange or TSet;
6969

7070
class TCall = TPredicateCall or TMemberCall or TNoneCall or TAnyCall;
7171

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @name Inconsistent getAPrimaryQlClass predicate
3+
* @description A getAPrimaryQlClass predicate should result in the name of the class.
4+
* @kind problem
5+
* @problem.severity error
6+
* @id ql/primary-ql-class-consistency
7+
* @tags correctness
8+
* @precision low
9+
*/
10+
11+
import ql
12+
13+
from ClassPredicate pred, String constant
14+
where
15+
exists(string className, string constantName |
16+
pred.getParent().getName() = className and
17+
pred.getName() = "getAPrimaryQlClass" and
18+
constant = pred.getBody().(ComparisonFormula).getRightOperand() and
19+
constant.(String).getValue() = constantName and
20+
// might be "Foo::classname", detect by matching with a regexp
21+
not constantName.regexpMatch(".*\\b" + className + "$") and
22+
// ignore constants with "?" in them
23+
not constantName.regexpMatch(".*\\?.*")
24+
)
25+
select pred,
26+
"The getAPrimaryQlClass predicate $@ instead of the class name \"" + pred.getParent().getName() +
27+
"\".", constant, "results in \"" + constant.getValue() + "\""

ql/test/printAst/Foo.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ predicate calls(Foo f) {
2121
or
2222
f = any(Foo f)
2323
or
24-
((((any()))))
24+
2 = 1 + (2 + (3 + 4))
2525
}

0 commit comments

Comments
 (0)