Skip to content

Commit 799b4c9

Browse files
committed
Swift: DotSyntaxBaseIgnored calls now have static target
This relies on getStaticTarget() returning a Callable... Not sure how I feel about that, since often we want to say ``` exists(Call c | c.getStaticTarget().hasName("...") ) ``` and Callable has a sparse interface. Maybe some AbstractFunctionDecl methods can be moved to Callable.
1 parent 89bfad0 commit 799b4c9

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

swift/ql/lib/codeql/swift/elements/expr/ApplyExpr.qll

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,12 @@ private import codeql.swift.generated.expr.ApplyExpr
22
private import codeql.swift.elements.Callable
33
private import codeql.swift.elements.expr.DeclRefExpr
44
private import codeql.swift.elements.expr.MethodLookupExpr
5-
private import codeql.swift.elements.expr.ConstructorRefCallExpr
5+
private import codeql.swift.elements.expr.DotSyntaxBaseIgnoredExpr
6+
private import codeql.swift.elements.expr.AutoClosureExpr
67
private import codeql.swift.elements.decl.MethodDecl
78

89
class ApplyExpr extends Generated::ApplyExpr {
9-
Callable getStaticTarget() {
10-
exists(Expr f |
11-
f = this.getFunction() and
12-
(
13-
result = f.(DeclRefExpr).getDecl()
14-
or
15-
result = f.(ConstructorRefCallExpr).getFunction().(DeclRefExpr).getDecl() // TODO: fix this
16-
)
17-
)
18-
}
10+
Callable getStaticTarget() { result = this.getFunction().(DeclRefExpr).getDecl() }
1911

2012
/** Gets the method qualifier, if this is applying a method */
2113
Expr getQualifier() { none() }
@@ -45,3 +37,25 @@ class MethodApplyExpr extends ApplyExpr {
4537

4638
override Expr getQualifier() { result = method.getBase() }
4739
}
40+
41+
private class PartialDotSyntaxBaseIgnoredApplyExpr extends ApplyExpr {
42+
private DotSyntaxBaseIgnoredExpr expr;
43+
44+
PartialDotSyntaxBaseIgnoredApplyExpr() { expr = this.getFunction() }
45+
46+
override AutoClosureExpr getStaticTarget() { result = expr.getSubExpr() }
47+
48+
override Expr getQualifier() { result = expr.getQualifier() }
49+
50+
override string toString() { result = "call to " + expr }
51+
}
52+
53+
private class FullDotSyntaxBaseIgnoredApplyExpr extends ApplyExpr {
54+
private PartialDotSyntaxBaseIgnoredApplyExpr expr;
55+
56+
FullDotSyntaxBaseIgnoredApplyExpr() { expr = this.getFunction() }
57+
58+
override AutoClosureExpr getStaticTarget() { result = expr.getStaticTarget().getExpr() }
59+
60+
override string toString() { result = "call to " + expr }
61+
}
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
private import codeql.swift.generated.expr.DotSyntaxBaseIgnoredExpr
2+
private import codeql.swift.elements.expr.AutoClosureExpr
3+
private import codeql.swift.elements.expr.CallExpr
4+
private import codeql.swift.elements.expr.TypeExpr
5+
private import codeql.swift.elements.decl.MethodDecl
26

37
class DotSyntaxBaseIgnoredExpr extends Generated::DotSyntaxBaseIgnoredExpr {
4-
override string toString() { result = "." + this.getSubExpr().toString() }
8+
override string toString() {
9+
result =
10+
concat(this.getQualifier().(TypeExpr).getTypeRepr().toString()) + "." + this.getMethod()
11+
}
12+
13+
/**
14+
* Gets the underlying instance method that is called when the result of this
15+
* expression is fully applied.
16+
*/
17+
MethodDecl getMethod() {
18+
result =
19+
this.getSubExpr()
20+
.(AutoClosureExpr)
21+
.getExpr()
22+
.(AutoClosureExpr)
23+
.getExpr()
24+
.(CallExpr)
25+
.getStaticTarget()
26+
}
527
}

0 commit comments

Comments
 (0)