From 7d4611941a7fb287e73b4acf2cbcc31a57ddff6d Mon Sep 17 00:00:00 2001 From: Erik Krogh Kristensen Date: Sun, 30 May 2021 21:42:15 +0000 Subject: [PATCH] fix printAst performance --- ql/src/codeql_ql/printAstAst.qll | 50 ++++++++++++-------------------- 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/ql/src/codeql_ql/printAstAst.qll b/ql/src/codeql_ql/printAstAst.qll index dc2ae735dc2..42603fe3752 100644 --- a/ql/src/codeql_ql/printAstAst.qll +++ b/ql/src/codeql_ql/printAstAst.qll @@ -28,7 +28,6 @@ class PrintAstConfiguration extends string { * The ordering is location based and pretty arbitary. */ AstNode getAstChild(PrintAstNode parent, int i) { - parent.shouldPrint() and result = rank[i](AstNode child, Location l | child.getParent() = parent and @@ -44,32 +43,24 @@ AstNode getAstChild(PrintAstNode parent, int i) { * A node in the output tree. */ class PrintAstNode extends AstNode { - string getProperty(string key) { - this.shouldPrint() and - ( - key = "semmle.label" and - result = "[" + concat(this.getAPrimaryQlClass(), ", ") + "] " + this.toString() - or - key = "semmle.order" and - result = - any(int i | - this = - rank[i](AstNode p, Location l, File f | - l = p.getLocation() and - f = l.getFile() - | - p order by f.getBaseName(), f.getAbsolutePath(), l.getStartLine(), l.getStartColumn() - ) - ).toString() - ) - } + PrintAstNode() { shouldPrintNode(this) } - /** - * Holds if this node should be printed in the output. By default, all nodes - * are printed, but the query can override - * `PrintAstConfiguration.shouldPrintNode` to filter the output. - */ - predicate shouldPrint() { shouldPrintNode(this) } + string getProperty(string key) { + key = "semmle.label" and + result = "[" + concat(this.getAPrimaryQlClass(), ", ") + "] " + this.toString() + or + key = "semmle.order" and + result = + any(int i | + this = + rank[i](AstNode p, Location l, File f | + l = p.getLocation() and + f = l.getFile() + | + p order by f.getBaseName(), f.getAbsolutePath(), l.getStartLine(), l.getStartColumn() + ) + ).toString() + } /** * Gets the child node that is accessed using the predicate `edgeName`. @@ -85,18 +76,13 @@ private predicate shouldPrintNode(AstNode n) { * Holds if `node` belongs to the output tree, and its property `key` has the * given `value`. */ -query predicate nodes(PrintAstNode node, string key, string value) { - node.shouldPrint() and - value = node.getProperty(key) -} +query predicate nodes(PrintAstNode node, string key, string value) { value = node.getProperty(key) } /** * Holds if `target` is a child of `source` in the AST, and property `key` of * the edge has the given `value`. */ query predicate edges(PrintAstNode source, PrintAstNode target, string key, string value) { - source.shouldPrint() and - target.shouldPrint() and target = source.getChild(_) and ( key = "semmle.label" and