Skip to content

Commit 5db2f0a

Browse files
committed
mock classes, and add a Mock T class to QlBuiltins
1 parent b7e0d1f commit 5db2f0a

File tree

10 files changed

+68
-18
lines changed

10 files changed

+68
-18
lines changed

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -877,22 +877,26 @@ class TypeDeclaration extends TTypeDeclaration, Declaration { }
877877
* A QL class.
878878
*/
879879
class Class extends TClass, TypeDeclaration, ModuleDeclaration {
880-
QL::Dataclass cls;
880+
Mocks::ClassOrMock cls;
881881

882882
Class() { this = TClass(cls) }
883883

884-
override Location getLocation() { result = cls.getName().getLocation() }
884+
override Location getLocation() { result = cls.asLeft().getName().getLocation() }
885885

886886
override string getAPrimaryQlClass() { result = "Class" }
887887

888-
override string getName() { result = cls.getName().getValue() }
888+
override string getName() {
889+
result = cls.asLeft().getName().getValue()
890+
or
891+
result = cls.asRight().getName()
892+
}
889893

890894
/**
891895
* Gets the characteristic predicate for this class.
892896
*/
893-
CharPred getCharPred() { toQL(result) = cls.getChild(_).(QL::ClassMember).getChild(_) }
897+
CharPred getCharPred() { toQL(result) = cls.asLeft().getChild(_).(QL::ClassMember).getChild(_) }
894898

895-
AstNode getMember(int i) { toQL(result) = cls.getChild(i).(QL::ClassMember).getChild(_) }
899+
AstNode getMember(int i) { toQL(result) = cls.asLeft().getChild(i).(QL::ClassMember).getChild(_) }
896900

897901
QLDoc getQLDocFor(AstNode m) {
898902
exists(int i | result = this.getMember(i) and m = this.getMember(i + 1))
@@ -902,7 +906,7 @@ class Class extends TClass, TypeDeclaration, ModuleDeclaration {
902906
* Gets a predicate in this class.
903907
*/
904908
ClassPredicate getAClassPredicate() {
905-
toQL(result) = cls.getChild(_).(QL::ClassMember).getChild(_)
909+
toQL(result) = cls.asLeft().getChild(_).(QL::ClassMember).getChild(_)
906910
}
907911

908912
/**
@@ -921,18 +925,20 @@ class Class extends TClass, TypeDeclaration, ModuleDeclaration {
921925
/**
922926
* Gets a super-type referenced in the `extends` part of the class declaration.
923927
*/
924-
TypeExpr getASuperType() { toQL(result) = cls.getExtends(_) }
928+
TypeExpr getASuperType() { toQL(result) = cls.asLeft().getExtends(_) }
925929

926930
/**
927931
* Gets a type referenced in the `instanceof` part of the class declaration.
928932
*/
929-
TypeExpr getAnInstanceofType() { toQL(result) = cls.getInstanceof(_) }
933+
TypeExpr getAnInstanceofType() { toQL(result) = cls.asLeft().getInstanceof(_) }
930934

931935
/** Gets the type that this class is defined to be an alias of. */
932-
TypeExpr getAliasType() { toQL(result) = cls.getChild(_).(QL::TypeAliasBody).getChild() }
936+
TypeExpr getAliasType() { toQL(result) = cls.asLeft().getChild(_).(QL::TypeAliasBody).getChild() }
933937

934938
/** Gets the type of one of the members that this class is defined to be a union of. */
935-
TypeExpr getUnionMember() { toQL(result) = cls.getChild(_).(QL::TypeUnionBody).getChild(_) }
939+
TypeExpr getUnionMember() {
940+
toQL(result) = cls.asLeft().getChild(_).(QL::TypeUnionBody).getChild(_)
941+
}
936942

937943
/** Gets the class type defined by this class declaration. */
938944
Type getType() { result.getDeclaration() = this }

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ private import codeql.util.Either
1414
// 1: Better type checking with distinct types.
1515
// 2: We don't get all the methods defined on strings, to confuse us.
1616
// 3: The Either type gets a type without bindingset on the charpred/toString.
17-
newtype TMockAst = TMockModule(string id) { id instanceof MockModule::Range }
17+
newtype TMockAst =
18+
TMockModule(string id) { id instanceof MockModule::Range } or
19+
TMockClass(string id) { id instanceof MockClass::Range }
1820

1921
/** Gets a mocked Ast node from the string ID that represents it. */
2022
MockAst fromId(string id) {
2123
result = TMockModule(id)
24+
or
25+
result = TMockClass(id)
2226
// TODO: Other nodes.
2327
}
2428

@@ -67,3 +71,27 @@ module MockModule {
6771
}
6872

6973
class ModuleOrMock = Either<QL::Module, MockModule>::Either;
74+
75+
/**
76+
* A mocked class.
77+
* Extend `MockClass::Range` to add new mocked classes.
78+
*/
79+
class MockClass extends MockAst, TMockClass {
80+
MockClass::Range range;
81+
82+
MockClass() { this = TMockClass(range) }
83+
84+
final string getName() { result = range.getName() }
85+
}
86+
87+
module MockClass {
88+
abstract class Range extends string {
89+
bindingset[this]
90+
Range() { this = this }
91+
92+
/** Gets the name of this mocked class. */
93+
abstract string getName();
94+
}
95+
}
96+
97+
class ClassOrMock = Either<QL::Dataclass, MockClass>::Either;

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ newtype TAstNode =
1212
TClasslessPredicate(QL::ClasslessPredicate pred) or
1313
TVarDecl(QL::VarDecl decl) or
1414
TFieldDecl(QL::Field field) or
15-
TClass(QL::Dataclass dc) or
15+
TClass(Mocks::ClassOrMock cls) or
1616
TCharPred(QL::Charpred pred) or
1717
TClassPredicate(QL::MemberPredicate pred) or
1818
TDBRelation(Dbscheme::Table table) or
@@ -166,7 +166,7 @@ QL::AstNode toQL(AST::AstNode n) {
166166
or
167167
n = TFieldDecl(result)
168168
or
169-
n = TClass(result)
169+
n = TClass(any(Mocks::ClassOrMock m | m.asLeft() = result))
170170
or
171171
n = TCharPred(result)
172172
or
@@ -210,6 +210,8 @@ QL::AstNode toQL(AST::AstNode n) {
210210
Mocks::MockAst toMock(AST::AstNode n) {
211211
n = TModule(any(Mocks::ModuleOrMock m | m.asRight() = result))
212212
or
213+
n = TClass(any(Mocks::ClassOrMock m | m.asRight() = result))
214+
or
213215
none() // TODO: Remove, this is to loosen the type of `toMock` to avoid type-errors in WIP code.
214216
}
215217

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,11 @@ module QlBuiltinsMocks {
106106

107107
override string getName() { result = "QlBuiltins" }
108108

109-
override MockModule::Range getMember(int i) {
110-
// TODO: T, InstSig
109+
override string getMember(int i) {
110+
// TODO: InstSig
111+
i = 0 and
112+
result instanceof EquivalenceRelation::SigClass
113+
or
111114
i = 2 and
112115
result instanceof EquivalenceRelation::EquivalenceRelationModule
113116
}
@@ -118,7 +121,7 @@ module QlBuiltinsMocks {
118121
* The equivalent to the following is implemented: (TODO: WIP, MISSING THE LINES WITH //)
119122
* ```CodeQL
120123
* module QlBuiltins {
121-
* signature class T; //
124+
* signature class T;
122125
* module InstSig<T MyT> { //
123126
* signature predicate edgeSig(MyT a, MyT b); //
124127
* }
@@ -129,6 +132,12 @@ module QlBuiltinsMocks {
129132
*}
130133
*/
131134
module EquivalenceRelation {
135+
class SigClass extends MockClass::Range {
136+
SigClass() { this = "Mock: QlBuiltins::T" }
137+
138+
override string getName() { result = "T" }
139+
}
140+
132141
class EquivalenceRelationModule extends MockModule::Range {
133142
EquivalenceRelationModule() { this = "Mock: QlBuiltins::EquivalenceRelation" }
134143

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
| testcases/BadNoSecurity.ql:1:1:15:9 | TopLevel | This query file is missing a `@tag security`. |
2-
| testcases/BadNoSeverity.ql:1:1:15:9 | TopLevel | This query file is missing a `@security-severity` tag. |
1+
| testcases/BadNoSecurity.ql:1:1:16:9 | TopLevel | This query file is missing a `@tag security`. |
2+
| testcases/BadNoSeverity.ql:1:1:16:9 | TopLevel | This query file is missing a `@security-severity` tag. |

ql/ql/test/queries/style/MissingSecurityMetadata/testcases/BadNoSecurity.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
import ql
1313

1414
from Class c
15+
where none()
1516
select c

ql/ql/test/queries/style/MissingSecurityMetadata/testcases/BadNoSeverity.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
import ql
1313

1414
from Class c
15+
where none()
1516
select c

ql/ql/test/queries/style/MissingSecurityMetadata/testcases/Good.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
import ql
1414

1515
from Class c
16+
where none()
1617
select c

ql/ql/test/queries/style/MissingSecurityMetadata/testcases/GoodNoPrecision.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
import ql
1212

1313
from Class c
14+
where none()
1415
select c

ql/ql/test/queries/style/MissingSecurityMetadata/testcases/GoodNoSecurity.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
import ql
1212

1313
from Class c
14+
where none()
1415
select c

0 commit comments

Comments
 (0)