Skip to content

Commit a11e618

Browse files
committed
mock parameters
1 parent 4feb48c commit a11e618

File tree

4 files changed

+67
-10
lines changed

4 files changed

+67
-10
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -658,11 +658,15 @@ class VarDef extends TVarDef, AstNode {
658658
* A variable declaration, with a type and a name.
659659
*/
660660
class VarDecl extends TVarDecl, VarDef, Declaration {
661-
QL::VarDecl var;
661+
Mocks::VarDeclOrMock var;
662662

663663
VarDecl() { this = TVarDecl(var) }
664664

665-
override string getName() { result = var.getChild(1).(QL::VarName).getChild().getValue() }
665+
override string getName() {
666+
result = var.asLeft().getChild(1).(QL::VarName).getChild().getValue()
667+
or
668+
result = var.asRight().getName()
669+
}
666670

667671
override Type getType() { result = this.getTypeExpr().getResolvedType() }
668672

@@ -671,14 +675,18 @@ class VarDecl extends TVarDecl, VarDef, Declaration {
671675
/**
672676
* Gets the type part of this variable declaration.
673677
*/
674-
TypeExpr getTypeExpr() { toQL(result) = var.getChild(0) }
678+
TypeExpr getTypeExpr() {
679+
toQL(result) = var.asLeft().getChild(0)
680+
or
681+
toMock(result) = var.asRight().getType()
682+
}
675683

676684
/**
677685
* Holds if this variable declaration is a private field on a class.
678686
*/
679687
predicate isPrivate() {
680688
exists(QL::ClassMember member |
681-
var = member.getChild(_).(QL::Field).getChild() and
689+
var.asLeft() = member.getChild(_).(QL::Field).getChild() and
682690
member.getAFieldOrChild().(QL::Annotation).getName().getValue() = "private"
683691
)
684692
}

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ newtype TMockAst =
1818
TMockModule(string id) { id instanceof MockModule::Range } or
1919
TMockClass(string id) { id instanceof MockClass::Range } or
2020
TMockTypeExpr(string id) { id instanceof MockTypeExpr::Range } or
21-
TMockClasslessPredicate(string id) { id instanceof MockClasslessPredicate::Range }
21+
TMockClasslessPredicate(string id) { id instanceof MockClasslessPredicate::Range } or
22+
TMockVarDecl(string id) { id instanceof MockVarDecl::Range }
2223

2324
/** Gets a mocked Ast node from the string ID that represents it. */
2425
MockAst fromId(string id) {
@@ -29,6 +30,8 @@ MockAst fromId(string id) {
2930
result = TMockTypeExpr(id)
3031
or
3132
result = TMockClasslessPredicate(id)
33+
or
34+
result = TMockVarDecl(id)
3235
// TODO: Other nodes.
3336
}
3437

@@ -171,3 +174,31 @@ module MockClasslessPredicate {
171174
}
172175

173176
class ClasslessPredicateOrMock = Either<QL::ClasslessPredicate, MockClasslessPredicate>::Either;
177+
178+
/**
179+
* A mocked `VarDecl`.
180+
*/
181+
class MockVarDecl extends MockAst, TMockVarDecl {
182+
MockVarDecl::Range range;
183+
184+
MockVarDecl() { this = TMockVarDecl(range) }
185+
186+
final string getName() { result = range.getName() }
187+
188+
final MockTypeExpr getType() { result.getId() = range.getType() }
189+
}
190+
191+
module MockVarDecl {
192+
abstract class Range extends string {
193+
bindingset[this]
194+
Range() { this = this }
195+
196+
/** Gets the name of the variable. */
197+
abstract string getName();
198+
199+
/** Gets the type of the variable. */
200+
abstract MockTypeExpr::Range getType();
201+
}
202+
}
203+
204+
class VarDeclOrMock = Either<QL::VarDecl, MockVarDecl>::Either;

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ newtype TAstNode =
1010
TBlockComment(QL::BlockComment comment) or
1111
TLineComment(QL::LineComment comment) or
1212
TClasslessPredicate(Mocks::ClasslessPredicateOrMock pred) or
13-
TVarDecl(QL::VarDecl decl) or
13+
TVarDecl(Mocks::VarDeclOrMock decl) or
1414
TFieldDecl(QL::Field field) or
1515
TClass(Mocks::ClassOrMock cls) or
1616
TCharPred(QL::Charpred pred) or
@@ -162,7 +162,7 @@ QL::AstNode toQL(AST::AstNode n) {
162162
or
163163
n = TClasslessPredicate(any(Mocks::ClasslessPredicateOrMock m | m.asLeft() = result))
164164
or
165-
n = TVarDecl(result)
165+
n = TVarDecl(any(Mocks::VarDeclOrMock m | m.asLeft() = result))
166166
or
167167
n = TFieldDecl(result)
168168
or
@@ -215,6 +215,8 @@ Mocks::MockAst toMock(AST::AstNode n) {
215215
n = TType(any(Mocks::TypeExprOrMock m | m.asRight() = result))
216216
or
217217
n = TClasslessPredicate(any(Mocks::ClasslessPredicateOrMock m | m.asRight() = result))
218+
or
219+
n = TVarDecl(any(Mocks::VarDeclOrMock m | m.asRight() = result))
218220
}
219221

220222
class TPredicate =

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ module QlBuiltinsMocks {
125125
* module QlBuiltins {
126126
* signature class T;
127127
* module EdgeSig<T MyT> {
128-
* signature predicate edgeSig(MyT a, MyT b); //
128+
* signature predicate edgeSig(MyT a, MyT b);
129129
* }
130130
* module EquivalenceRelation<T MyT, EdgeSig<MyT>::edgeSig/2 edge> { //
131131
* class EquivalenceClass; //
@@ -164,10 +164,26 @@ module QlBuiltinsMocks {
164164

165165
override string getName() { result = "edgeSig" }
166166

167-
override string getParameter(int i) {
168-
none() // TODO:
167+
override EdgeSigPredParam getParameter(int i) {
168+
i = 0 and
169+
result.getName() = "a"
170+
or
171+
i = 1 and
172+
result.getName() = "b"
169173
}
170174
}
175+
176+
class EdgeSigPredParam extends MockVarDecl::Range {
177+
string name;
178+
179+
EdgeSigPredParam() {
180+
this = "Mock: QlBuiltins::EdgeSig::edgeSig::" + name and name = ["a", "b"]
181+
}
182+
183+
override string getName() { result = name }
184+
185+
override MockTypeExpr::Range getType() { result instanceof EdgeSigType } // TODO: I'm just using one typeexpr for everything, that might break the parent relation.
186+
}
171187
}
172188

173189
class EquivalenceRelationModule extends MockModule::Range {

0 commit comments

Comments
 (0)