Skip to content

Commit a620658

Browse files
committed
Swift: fix PrintAst order, double parents, orphan decl refs
The main problem is that a lot of the old DotSyntaxApplyExpr->MethodRefExpr synth-constructor hacks were not fully generalized to SelfApplyExpr and OtherConstructorDeclRefExpr. Also: - Gave a index-in-parent-based ordering to PrintAst nodes(), to stabilize it more. - Use a slightly more general SelfApplyExpr->Decl conversion
1 parent b5bb814 commit a620658

File tree

7 files changed

+39
-25
lines changed

7 files changed

+39
-25
lines changed

swift/ql/.generated.list

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ ql/lib/codeql/swift/elements/expr/OpenExistentialExprConstructor.qll c56e5e6f7ae
173173
ql/lib/codeql/swift/elements/expr/OptionalEvaluationExpr.qll 2f46c15d17a50b14e91552be8ac5b72dbdc9f39b8fac9fa068e519ae5c8aa99b 559902efedbf4c5ef24697267c7b48162129b4ab463b41d89bdfb8b94742fa9f
174174
ql/lib/codeql/swift/elements/expr/OptionalEvaluationExprConstructor.qll 4ba0af8f8b4b7920bc1106d069455eb754b7404d9a4bfc361d2ea22e8763f4fe 6d07e7838339290d1a2aec88addd511f01224d7e1d485b08ef4793e01f4b4421
175175
ql/lib/codeql/swift/elements/expr/OptionalTryExprConstructor.qll 60d2f88e2c6fc843353cc52ce1e1c9f7b80978750d0e780361f817b1b2fea895 4eabd9f03dc5c1f956e50e2a7af0535292484acc69692d7c7f771e213609fd04
176-
ql/lib/codeql/swift/elements/expr/OtherConstructorDeclRefExprConstructor.qll cf726ed7ed830e17aaedf1acddf1edc4efc7d72ab9f9580bc89cc8eefbd54d8a 4ef3010dc5500bd503db8aa531d5455a9c80bc30172fb005abc6459b6f66ea00
177176
ql/lib/codeql/swift/elements/expr/OverloadedDeclRefExpr.qll adb49e25cdd87d2e6259399a7ce3a1fbe6eb345f9b8f4e34eb23cb39eb3555da 47b1c6df5397de490f62e96edc0656b1f97c0be73c6b99ecd78b62d46106ce61
178177
ql/lib/codeql/swift/elements/expr/OverloadedDeclRefExprConstructor.qll 2cf79b483f942fbf8aaf9956429b92bf9536e212bb7f7940c2bc1d30e8e8dfd5 f4c16a90e3ab944dded491887779f960e3077f0a8823f17f50f82cf5b9803737
179178
ql/lib/codeql/swift/elements/expr/ParenExprConstructor.qll 6baaa592db57870f5ecd9be632bd3f653c44d72581efd41e8a837916e1590f9e 6f28988d04b2cb69ddcb63fba9ae3166b527803a61c250f97e48ff39a28379f6
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
private import codeql.swift.generated.Raw
2+
private import codeql.swift.elements.expr.MethodLookupExpr
23

34
predicate constructArgument(Raw::Argument id) {
4-
// exclude an argument that will be part of a DotSyntaxCallExpr
5-
// that will be transformed into a MethodRefCallExpr
6-
not exists(Raw::DotSyntaxCallExpr c |
7-
c.getFunction() instanceof Raw::DeclRefExpr and id = c.getArgument(0)
8-
)
5+
// exclude an argument that will be part of a SelfApplyExpr
6+
// that will be transformed into a MethodLookupExpr
7+
not exists(Raw::SelfApplyExpr e | id.getExpr() = e.getBase())
98
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
private import codeql.swift.generated.Raw
2+
private import codeql.swift.elements.expr.MethodLookupExprConstructor
23

34
predicate constructDeclRefExpr(Raw::DeclRefExpr id) {
4-
// exclude an argument that will be part of a DotSyntaxCallExpr
5-
// that will be transformed into a MethodRefCallExpr
6-
not exists(Raw::DotSyntaxCallExpr c | id = c.getFunction())
5+
// exclude an argument that will be part of a SelfApplyExpr
6+
// that will be transformed into a MethodLookupExpr
7+
not exists(Raw::SelfApplyExpr e | id.getDecl() = extractDeclFromSelfApplyExpr(e))
78
}

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
private import codeql.swift.generated.expr.MethodLookupExpr
2+
private import codeql.swift.elements.expr.MethodLookupExprConstructor
23
private import codeql.swift.elements.expr.Expr
34
private import codeql.swift.elements.decl.Decl
45
private import codeql.swift.elements.decl.MethodDecl
@@ -13,17 +14,7 @@ class MethodLookupExpr extends Generated::MethodLookupExpr {
1314
}
1415

1516
override Decl getImmediateMember() {
16-
result =
17-
Synth::convertDeclFromRaw([
18-
this.getUnderlying().getFunction().(Raw::DeclRefExpr).getDecl(),
19-
this.getUnderlying()
20-
.getFunction()
21-
.(Raw::FunctionConversionExpr)
22-
.getSubExpr()
23-
.(Raw::DeclRefExpr)
24-
.getDecl(),
25-
this.getUnderlying().getFunction().(Raw::OtherConstructorDeclRefExpr).getConstructorDecl()
26-
])
17+
result = Synth::convertDeclFromRaw(extractDeclFromSelfApplyExpr(this.getUnderlying()))
2718
}
2819

2920
MethodDecl getMethod() { result = this.getMember() }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
private import codeql.swift.generated.Raw
22

33
predicate constructMethodLookupExpr(Raw::SelfApplyExpr id) { any() }
4+
5+
Raw::Decl extractDeclFromSelfApplyExpr(Raw::SelfApplyExpr e) {
6+
exists(Raw::Expr unwrappedFunction | unwrappedFunction = unwrapConversion*(e.getFunction()) |
7+
result =
8+
[
9+
unwrappedFunction.(Raw::DeclRefExpr).getDecl(),
10+
unwrappedFunction.(Raw::OtherConstructorDeclRefExpr).getConstructorDecl()
11+
]
12+
)
13+
}
14+
15+
private Raw::Expr unwrapConversion(Raw::Expr e) {
16+
e.(Raw::ImplicitConversionExpr).getSubExpr() = result
17+
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
// generated by codegen/codegen.py, remove this comment if you wish to edit this file
21
private import codeql.swift.generated.Raw
2+
private import codeql.swift.elements.expr.MethodLookupExprConstructor
33

4-
predicate constructOtherConstructorDeclRefExpr(Raw::OtherConstructorDeclRefExpr id) { any() }
4+
predicate constructOtherConstructorDeclRefExpr(Raw::OtherConstructorDeclRefExpr id) {
5+
// exclude an argument that will be part of a SelfApplyExpr
6+
// that will be transformed into a MethodLookupExpr
7+
not exists(Raw::SelfApplyExpr e | id.getConstructorDecl() = extractDeclFromSelfApplyExpr(e))
8+
}

swift/ql/lib/codeql/swift/printast/PrintAst.qll

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ import PrintAstNode
77
cached
88
private int getOrder(PrintAstNode node) {
99
node =
10-
rank[result](PrintAstNode n, Location loc |
11-
loc = n.getLocation()
10+
rank[result](PrintAstNode n, Location loc, int index, string accessor |
11+
loc = n.getLocation() and
12+
if any(PrintAstNode p).hasChild(n, index, accessor)
13+
then any()
14+
else (
15+
index = -1 and
16+
accessor = ""
17+
)
1218
|
1319
n
1420
order by
1521
loc.getFile().getName(), loc.getStartLine(), loc.getStartColumn(), loc.getEndLine(),
16-
loc.getEndColumn()
22+
loc.getEndColumn(), index, accessor
1723
)
1824
}
1925

0 commit comments

Comments
 (0)