Skip to content

Commit 8f09485

Browse files
committed
Swift: enhance PrintAst testing
The `ParentChild` tests have been generalized to test all `PrintAst` by factoring out `PrintAstNode` into a separate file. The `child.ql` and `parent.ql` tests have been removed as they are subsumed by `PrintAst.ql`. Also, a new `no_parent_child_loops` is added to detect back edges to a root node (back edges to a non-root node are already detected by `no_double_parents.ql`).
1 parent cd632dc commit 8f09485

File tree

11 files changed

+109
-2961
lines changed

11 files changed

+109
-2961
lines changed

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

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,87 +2,7 @@
22
* Provides queries to pretty-print a Go AST as a graph.
33
*/
44

5-
import swift
6-
import codeql.swift.generated.ParentChild
7-
8-
private newtype TPrintAstConfiguration = TMakePrintAstConfiguration()
9-
10-
/**
11-
* The hook to customize the files and functions printed by this module.
12-
*/
13-
class PrintAstConfiguration extends TPrintAstConfiguration {
14-
/**
15-
* Gets the string representation of this singleton
16-
*/
17-
string toString() { result = "PrintAstConfiguration" }
18-
19-
/**
20-
* Holds if the AST for `e` should be printed. By default, holds for all.
21-
*/
22-
predicate shouldPrint(Locatable e) { any() }
23-
}
24-
25-
private predicate shouldPrint(Locatable e) { any(PrintAstConfiguration config).shouldPrint(e) }
26-
27-
/**
28-
* An AST node that should be printed.
29-
*/
30-
private newtype TPrintAstNode =
31-
TLocatable(Locatable ast) {
32-
// Only consider resolved nodes (that is not within the hidden conversion AST)
33-
ast = ast.resolve()
34-
}
35-
36-
/**
37-
* A node in the output tree.
38-
*/
39-
class PrintAstNode extends TPrintAstNode {
40-
/**
41-
* Gets a textual representation of this node.
42-
*/
43-
abstract string toString();
44-
45-
/**
46-
* Gets the child node at index `index`. Child indices must be unique,
47-
* but need not be contiguous.
48-
*/
49-
abstract predicate hasChild(PrintAstNode child, int index, string accessor);
50-
51-
/**
52-
* Holds if this node should be printed in the output.
53-
*/
54-
abstract predicate shouldBePrinted();
55-
56-
/**
57-
* Gets the location of this node in the source code.
58-
*/
59-
abstract Location getLocation();
60-
61-
/**
62-
* Gets the value of an additional property of this node, where the name of
63-
* the property is `key`.
64-
*/
65-
string getProperty(string key) { none() }
66-
}
67-
68-
/**
69-
* A graph node representing a real Locatable node.
70-
*/
71-
class PrintLocatable extends PrintAstNode, TLocatable {
72-
Locatable ast;
73-
74-
PrintLocatable() { this = TLocatable(ast) }
75-
76-
final override predicate shouldBePrinted() { shouldPrint(ast) }
77-
78-
override predicate hasChild(PrintAstNode child, int index, string accessor) {
79-
child = TLocatable(getChildAndAccessor(ast, index, accessor))
80-
}
81-
82-
override string toString() { result = "[" + concat(ast.getPrimaryQlClasses(), ", ") + "] " + ast }
83-
84-
final override Location getLocation() { result = ast.getLocation() }
85-
}
5+
import PrintAstNode
866

877
cached
888
private int getOrder(PrintAstNode node) {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* Provides classes used to pretty-print a Go AST as a graph.
3+
* This is factored out of `PrintAst.qll` for testing purposes.
4+
*/
5+
6+
import swift
7+
import codeql.swift.generated.ParentChild
8+
9+
private newtype TPrintAstConfiguration = TMakePrintAstConfiguration()
10+
11+
/**
12+
* The hook to customize the files and functions printed by this module.
13+
*/
14+
class PrintAstConfiguration extends TPrintAstConfiguration {
15+
/**
16+
* Gets the string representation of this singleton
17+
*/
18+
string toString() { result = "PrintAstConfiguration" }
19+
20+
/**
21+
* Holds if the AST for `e` should be printed. By default, holds for all.
22+
*/
23+
predicate shouldPrint(Locatable e) { any() }
24+
}
25+
26+
private predicate shouldPrint(Locatable e) { any(PrintAstConfiguration config).shouldPrint(e) }
27+
28+
/**
29+
* An AST node that should be printed.
30+
*/
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+
}
36+
37+
/**
38+
* A node in the output tree.
39+
*/
40+
class PrintAstNode extends TPrintAstNode {
41+
/**
42+
* Gets a textual representation of this node.
43+
*/
44+
abstract string toString();
45+
46+
/**
47+
* Gets the child node at index `index`. Child indices must be unique,
48+
* but need not be contiguous.
49+
*/
50+
abstract predicate hasChild(PrintAstNode child, int index, string accessor);
51+
52+
/**
53+
* Holds if this node should be printed in the output.
54+
*/
55+
abstract predicate shouldBePrinted();
56+
57+
/**
58+
* Gets the location of this node in the source code.
59+
*/
60+
abstract Location getLocation();
61+
62+
/**
63+
* Gets the value of an additional property of this node, where the name of
64+
* the property is `key`.
65+
*/
66+
string getProperty(string key) { none() }
67+
}
68+
69+
/**
70+
* A graph node representing a real Locatable node.
71+
*/
72+
class PrintLocatable extends PrintAstNode, TLocatable {
73+
Locatable ast;
74+
75+
PrintLocatable() { this = TLocatable(ast) }
76+
77+
final override predicate shouldBePrinted() { shouldPrint(ast) }
78+
79+
override predicate hasChild(PrintAstNode child, int index, string accessor) {
80+
child = TLocatable(getChildAndAccessor(ast, index, accessor))
81+
}
82+
83+
override string toString() { result = "[" + concat(ast.getPrimaryQlClasses(), ", ") + "] " + ast }
84+
85+
final override Location getLocation() { result = ast.getLocation() }
86+
}

0 commit comments

Comments
 (0)