Skip to content

Commit 21527f6

Browse files
committed
Swift: Extract KeyPath applications and KeyPathDot expressions.
1 parent 7ca0144 commit 21527f6

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

swift/codegen/schema.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ InOutExpr:
390390

391391
KeyPathApplicationExpr:
392392
_extends: Expr
393+
base: Expr
394+
key_path: Expr
393395

394396
KeyPathDotExpr:
395397
_extends: Expr

swift/extractor/visitors/ExprVisitor.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,22 @@ class ExprVisitor : public AstVisitorBase<ExprVisitor> {
509509
dispatcher_.emit(IfExprsTrap{label, condLabel, thenLabel, elseLabel});
510510
}
511511

512+
void visitKeyPathDotExpr(swift::KeyPathDotExpr* expr) {
513+
auto label = dispatcher_.assignNewLabel(expr);
514+
dispatcher_.emit(KeyPathDotExprsTrap{label});
515+
}
516+
517+
void visitKeyPathApplicationExpr(swift::KeyPathApplicationExpr* expr) {
518+
auto label = dispatcher_.assignNewLabel(expr);
519+
assert(expr->getBase() && "KeyPathApplicationExpr has getBase()");
520+
assert(expr->getKeyPath() && "KeyPathApplicationExpr has getKeyPath()");
521+
522+
auto baseLabel = dispatcher_.fetchLabel(expr->getBase());
523+
auto keyPathLabel = dispatcher_.fetchLabel(expr->getKeyPath());
524+
525+
dispatcher_.emit(KeyPathApplicationExprsTrap{label, baseLabel, keyPathLabel});
526+
}
527+
512528
private:
513529
TrapLabel<ArgumentTag> emitArgument(const swift::Argument& arg) {
514530
auto argLabel = dispatcher_.createLabel<ArgumentTag>();

swift/ql/lib/codeql/swift/generated/expr/KeyPathApplicationExpr.qll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,18 @@ import codeql.swift.elements.expr.Expr
33

44
class KeyPathApplicationExprBase extends @key_path_application_expr, Expr {
55
override string getAPrimaryQlClass() { result = "KeyPathApplicationExpr" }
6+
7+
Expr getBase() {
8+
exists(Expr x |
9+
key_path_application_exprs(this, x, _) and
10+
result = x.resolve()
11+
)
12+
}
13+
14+
Expr getKeyPath() {
15+
exists(Expr x |
16+
key_path_application_exprs(this, _, x) and
17+
result = x.resolve()
18+
)
19+
}
620
}

swift/ql/lib/swift.dbscheme

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,9 @@ in_out_exprs(
888888
);
889889

890890
key_path_application_exprs(
891-
unique int id: @key_path_application_expr
891+
unique int id: @key_path_application_expr,
892+
int base: @expr ref,
893+
int key_path: @expr ref
892894
);
893895

894896
key_path_dot_exprs(

0 commit comments

Comments
 (0)