Skip to content

Commit 4feb48c

Browse files
committed
classless predicate, used to model the edgeSig predicate
1 parent 4dc5237 commit 4feb48c

File tree

4 files changed

+65
-17
lines changed

4 files changed

+65
-17
lines changed

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -500,11 +500,11 @@ class PredicateExpr extends TPredicateExpr, AstNode {
500500
* A classless predicate.
501501
*/
502502
class ClasslessPredicate extends TClasslessPredicate, Predicate, ModuleDeclaration {
503-
QL::ClasslessPredicate pred;
503+
Mocks::ClasslessPredicateOrMock pred;
504504

505505
ClasslessPredicate() { this = TClasslessPredicate(pred) }
506506

507-
override Location getLocation() { result = pred.getName().getLocation() }
507+
override Location getLocation() { result = pred.asLeft().getName().getLocation() }
508508

509509
/**
510510
* Gets the aliased value if this predicate is an alias
@@ -513,25 +513,33 @@ class ClasslessPredicate extends TClasslessPredicate, Predicate, ModuleDeclarati
513513
*/
514514
final AstNode getAlias() {
515515
exists(QL::PredicateAliasBody alias |
516-
alias.getParent() = pred and
516+
alias.getParent() = pred.asLeft() and
517517
toQL(result).getParent() = alias
518518
)
519519
or
520-
toQL(result) = pred.getChild(_).(QL::HigherOrderTerm)
520+
toQL(result) = pred.asLeft().getChild(_).(QL::HigherOrderTerm)
521521
}
522522

523523
override string getAPrimaryQlClass() { result = "ClasslessPredicate" }
524524

525-
override Formula getBody() { toQL(result) = pred.getChild(_).(QL::Body).getChild() }
525+
override Formula getBody() { toQL(result) = pred.asLeft().getChild(_).(QL::Body).getChild() }
526526

527-
override string getName() { result = pred.getName().getValue() }
527+
override string getName() {
528+
result = pred.asLeft().getName().getValue()
529+
or
530+
result = pred.asRight().getName()
531+
}
528532

529533
override VarDecl getParameter(int i) {
530534
toQL(result) =
531-
rank[i + 1](QL::VarDecl decl, int index | decl = pred.getChild(index) | decl order by index)
535+
rank[i + 1](QL::VarDecl decl, int index |
536+
decl = pred.asLeft().getChild(index)
537+
|
538+
decl order by index
539+
)
532540
}
533541

534-
override TypeExpr getReturnTypeExpr() { toQL(result) = pred.getReturnType() }
542+
override TypeExpr getReturnTypeExpr() { toQL(result) = pred.asLeft().getReturnType() }
535543

536544
override AstNode getAChild(string pred_name) {
537545
result = Predicate.super.getAChild(pred_name)

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ private import codeql.util.Either
1717
newtype TMockAst =
1818
TMockModule(string id) { id instanceof MockModule::Range } or
1919
TMockClass(string id) { id instanceof MockClass::Range } or
20-
TMockTypeExpr(string id) { id instanceof MockTypeExpr::Range }
20+
TMockTypeExpr(string id) { id instanceof MockTypeExpr::Range } or
21+
TMockClasslessPredicate(string id) { id instanceof MockClasslessPredicate::Range }
2122

2223
/** Gets a mocked Ast node from the string ID that represents it. */
2324
MockAst fromId(string id) {
@@ -26,6 +27,8 @@ MockAst fromId(string id) {
2627
result = TMockClass(id)
2728
or
2829
result = TMockTypeExpr(id)
30+
or
31+
result = TMockClasslessPredicate(id)
2932
// TODO: Other nodes.
3033
}
3134

@@ -51,7 +54,7 @@ class MockModule extends MockAst, TMockModule {
5154
final MockAst getMember(int i) { result = fromId(range.getMember(i)) }
5255

5356
final predicate hasTypeParam(int i, MockAst type, string name) {
54-
range.hasTypeParam(i, type.toString(), name)
57+
range.hasTypeParam(i, type.getId(), name)
5558
}
5659
}
5760

@@ -139,3 +142,32 @@ module MockTypeExpr {
139142
class TypeExprOrMock = Either<QL::TypeExpr, MockTypeExpr>::Either;
140143

141144
class SignatureExprOrMock = Either<QL::SignatureExpr, MockSignatureExpr>::Either;
145+
146+
/**
147+
* A mocked `ClasslessPredicate`.
148+
*/
149+
class MockClasslessPredicate extends MockAst {
150+
MockClasslessPredicate::Range range;
151+
152+
MockClasslessPredicate() { this = TMockClasslessPredicate(range) }
153+
154+
final string getName() { result = range.getName() }
155+
156+
// TODO: VarDecl.
157+
final MockAst getParameter(int i) { result.getId() = range.getParameter(i) }
158+
}
159+
160+
module MockClasslessPredicate {
161+
abstract class Range extends string {
162+
bindingset[this]
163+
Range() { this = this }
164+
165+
/** Gets the name of the predicate. */
166+
abstract string getName();
167+
168+
/** Gets the `i`th parameter of the predicate. */
169+
abstract string getParameter(int i);
170+
}
171+
}
172+
173+
class ClasslessPredicateOrMock = Either<QL::ClasslessPredicate, MockClasslessPredicate>::Either;

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ newtype TAstNode =
99
TQLDoc(QL::Qldoc qldoc) or
1010
TBlockComment(QL::BlockComment comment) or
1111
TLineComment(QL::LineComment comment) or
12-
TClasslessPredicate(QL::ClasslessPredicate pred) or
12+
TClasslessPredicate(Mocks::ClasslessPredicateOrMock pred) or
1313
TVarDecl(QL::VarDecl decl) or
1414
TFieldDecl(QL::Field field) or
1515
TClass(Mocks::ClassOrMock cls) or
@@ -160,7 +160,7 @@ QL::AstNode toQL(AST::AstNode n) {
160160
or
161161
n = TLineComment(result)
162162
or
163-
n = TClasslessPredicate(result)
163+
n = TClasslessPredicate(any(Mocks::ClasslessPredicateOrMock m | m.asLeft() = result))
164164
or
165165
n = TVarDecl(result)
166166
or
@@ -213,6 +213,8 @@ Mocks::MockAst toMock(AST::AstNode n) {
213213
n = TClass(any(Mocks::ClassOrMock m | m.asRight() = result))
214214
or
215215
n = TType(any(Mocks::TypeExprOrMock m | m.asRight() = result))
216+
or
217+
n = TClasslessPredicate(any(Mocks::ClasslessPredicateOrMock m | m.asRight() = result))
216218
}
217219

218220
class TPredicate =

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,24 @@ module QlBuiltinsMocks {
150150
i = 0 and name = "MyT" and type instanceof EdgeSigType
151151
}
152152

153-
override string getMember(int i) {
154-
// i = 0 and
155-
// result instanceof EdgeSig::EdgeSigPredicate
156-
none() // TODO:
157-
}
153+
override string getMember(int i) { i = 0 and result instanceof EdgeSigPred }
158154
}
159155

160156
class EdgeSigType extends MockTypeExpr::Range {
161157
EdgeSigType() { this = "Mock: QlBuiltins::EdgeSig::MyT" }
162158

163159
override string getClassName() { result = "MyT" }
164160
}
161+
162+
class EdgeSigPred extends MockClasslessPredicate::Range {
163+
EdgeSigPred() { this = "Mock: QlBuiltins::EdgeSig::edgeSig" }
164+
165+
override string getName() { result = "edgeSig" }
166+
167+
override string getParameter(int i) {
168+
none() // TODO:
169+
}
170+
}
165171
}
166172

167173
class EquivalenceRelationModule extends MockModule::Range {

0 commit comments

Comments
 (0)