Skip to content

Commit e3e9774

Browse files
committed
Swift: print conversions similarly to C++
1 parent 1224330 commit e3e9774

File tree

2 files changed

+69
-129
lines changed

2 files changed

+69
-129
lines changed

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

Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,7 @@ private predicate shouldPrint(Locatable e) { any(PrintAstConfiguration config).s
2828
/**
2929
* An AST node that should be printed.
3030
*/
31-
private newtype TPrintAstNode =
32-
TLocatable(Locatable ast) {
33-
// Only consider resolved nodes (that is not within the hidden conversion AST)
34-
ast = ast.resolve()
35-
} or
36-
TConversion(Expr conv) { conv.isConversion() } or
37-
TConversionContainer(Expr e) { e = e.resolve() and e.hasConversions() }
31+
private newtype TPrintAstNode = TLocatable(Locatable ast)
3832

3933
/**
4034
* A node in the output tree.
@@ -49,7 +43,7 @@ class PrintAstNode extends TPrintAstNode {
4943
* Gets the child node at index `index`. Child indices must be unique,
5044
* but need not be contiguous.
5145
*/
52-
abstract predicate hasChild(PrintAstNode child, int index, string accessor);
46+
abstract predicate hasChild(PrintAstNode child, int index, string label);
5347

5448
/**
5549
* Holds if this node should be printed in the output.
@@ -72,6 +66,10 @@ private string prettyPrint(Locatable e) {
7266
result = "[" + concat(e.getPrimaryQlClasses(), ", ") + "] " + e
7367
}
7468

69+
private class Unresolved extends Locatable {
70+
Unresolved() { this != this.resolve() }
71+
}
72+
7573
/**
7674
* A graph node representing a real Locatable node.
7775
*/
@@ -84,61 +82,37 @@ class PrintLocatable extends PrintAstNode, TLocatable {
8482

8583
final override predicate shouldBePrinted() { shouldPrint(ast) }
8684

87-
override predicate hasChild(PrintAstNode child, int index, string accessor) {
88-
child = TLocatable(getChildAndAccessor(ast, index, accessor))
85+
override predicate hasChild(PrintAstNode child, int index, string label) {
86+
exists(Locatable c, int i, string accessor |
87+
c = getChildAndAccessor(ast, i, accessor) and
88+
(
89+
// use even indexes for normal children, leaving odd slots for conversions if any
90+
child = TLocatable(c) and index = 2 * i and label = accessor
91+
or
92+
child = TLocatable(c.getFullyUnresolved().(Unresolved)) and
93+
index = 2 * i + 1 and
94+
(
95+
if c instanceof Expr
96+
then label = accessor + ".getFullyUncoverted()"
97+
else label = accessor + ".getFullyUnresolved()"
98+
)
99+
)
100+
)
89101
}
90102

91103
final override Location getLocation() { result = ast.getLocation() }
92104
}
93105

94106
/**
95-
* A graph node representing a conversion.
96-
*/
97-
class PrintConversion extends PrintAstNode, TConversion {
98-
Expr conv;
99-
100-
PrintConversion() { this = TConversion(conv) }
101-
102-
override string toString() { result = prettyPrint(conv) }
103-
104-
final override predicate shouldBePrinted() { shouldPrint(conv.resolve()) }
105-
106-
override predicate hasChild(PrintAstNode child, int index, string accessor) { none() }
107-
108-
final override Location getLocation() { result = conv.getLocation() }
109-
}
110-
111-
/**
112-
* A graph node representing a virtual container for conversions.
107+
* A specialization of graph node for "unresolved" children, that is nodes in
108+
* the parallel conversion AST.
113109
*/
114-
class PrintConversionContainer extends PrintAstNode, TConversionContainer {
115-
Expr convertee;
116-
117-
PrintConversionContainer() { this = TConversionContainer(convertee) }
118-
119-
override string toString() { result = "" }
120-
121-
final override predicate shouldBePrinted() { shouldPrint(convertee) }
122-
123-
override predicate hasChild(PrintAstNode child, int index, string accessor) {
124-
child = TConversion(convertee.getConversion(index)) and
125-
accessor = "getConversion(" + index + ")"
126-
}
127-
128-
final override Location getLocation() { result = convertee.getFullyConverted().getLocation() }
129-
}
110+
class PrintUnresolved extends PrintLocatable {
111+
override Unresolved ast;
130112

131-
/** A graph node specialization for expressions to show conversions. */
132-
class PrintExpr extends PrintLocatable {
133-
override Expr ast;
134-
135-
override predicate hasChild(PrintAstNode child, int index, string accessor) {
136-
super.hasChild(child, index, accessor)
137-
or
138-
ast.hasConversions() and
139-
index = -1 and
140-
accessor = "conversions" and
141-
child = TConversionContainer(ast)
113+
override predicate hasChild(PrintAstNode child, int index, string label) {
114+
// only print immediate unresolved children from the "parallel" AST
115+
child = TLocatable(getImmediateChildAndAccessor(ast, index, label).(Unresolved))
142116
}
143117
}
144118

0 commit comments

Comments
 (0)