Skip to content

Commit 7a4e413

Browse files
authored
QL: more improvements to printAst performance
1 parent 5e89bf9 commit 7a4e413

2 files changed

Lines changed: 18 additions & 32 deletions

File tree

ql/src/codeql_ql/printAstAst.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class PrintAstNode extends AstNode {
5353
result =
5454
any(int i |
5555
this =
56-
rank[i](AstNode p, Location l, File f |
56+
rank[i](PrintAstNode p, Location l, File f |
5757
l = p.getLocation() and
5858
f = l.getFile()
5959
|

ql/src/codeql_ql/printAstGenerated.qll

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ class PrintAstConfiguration extends string {
3131
* The ordering is location based and pretty arbitary.
3232
*/
3333
AstNode getAstChild(PrintAstNode parent, int i) {
34-
parent.shouldPrint() and
3534
result =
3635
rank[i](AstNode child, Location l |
3736
child.getParent() = parent and
@@ -47,33 +46,25 @@ AstNode getAstChild(PrintAstNode parent, int i) {
4746
* A node in the output tree.
4847
*/
4948
class PrintAstNode extends AstNode {
49+
PrintAstNode() { shouldPrintNode(this) }
50+
5051
string getProperty(string key) {
51-
this.shouldPrint() and
52-
(
53-
key = "semmle.label" and
54-
result = "[" + concat(this.getAPrimaryQlClass(), ", ") + "] " + this.toString()
55-
or
56-
key = "semmle.order" and
57-
result =
58-
any(int i |
59-
this =
60-
rank[i](AstNode p, Location l, File f |
61-
l = p.getLocation() and
62-
f = l.getFile()
63-
|
64-
p order by f.getBaseName(), f.getAbsolutePath(), l.getStartLine(), l.getStartColumn()
65-
)
66-
).toString()
67-
)
52+
key = "semmle.label" and
53+
result = "[" + concat(this.getAPrimaryQlClass(), ", ") + "] " + this.toString()
54+
or
55+
key = "semmle.order" and
56+
result =
57+
any(int i |
58+
this =
59+
rank[i](PrintAstNode p, Location l, File f |
60+
l = p.getLocation() and
61+
f = l.getFile()
62+
|
63+
p order by f.getBaseName(), f.getAbsolutePath(), l.getStartLine(), l.getStartColumn()
64+
)
65+
).toString()
6866
}
6967

70-
/**
71-
* Holds if this node should be printed in the output. By default, all nodes
72-
* are printed, but the query can override
73-
* `PrintAstConfiguration.shouldPrintNode` to filter the output.
74-
*/
75-
predicate shouldPrint() { shouldPrintNode(this) }
76-
7768
/**
7869
* Gets the child node that is accessed using the predicate `edgeName`.
7970
*/
@@ -93,18 +84,13 @@ private predicate shouldPrintNode(AstNode n) {
9384
* Holds if `node` belongs to the output tree, and its property `key` has the
9485
* given `value`.
9586
*/
96-
query predicate nodes(PrintAstNode node, string key, string value) {
97-
node.shouldPrint() and
98-
value = node.getProperty(key)
99-
}
87+
query predicate nodes(PrintAstNode node, string key, string value) { value = node.getProperty(key) }
10088

10189
/**
10290
* Holds if `target` is a child of `source` in the AST, and property `key` of
10391
* the edge has the given `value`.
10492
*/
10593
query predicate edges(PrintAstNode source, PrintAstNode target, string key, string value) {
106-
source.shouldPrint() and
107-
target.shouldPrint() and
10894
target = source.getChild(_) and
10995
(
11096
key = "semmle.label" and

0 commit comments

Comments
 (0)