From 3e5ff1d042672f47deee7d4ba377f478fe86f47e Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Fri, 12 Mar 2021 16:52:28 +0100 Subject: [PATCH] AST: order edges by target node When printing a tree CodeQL iterates over the nodes and for each node prints the successor edges as children. If the the successor edges are ordered by target node then the children printe in the right order in the expected output. --- ql/test/library-tests/ast/Ast.ql | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/ql/test/library-tests/ast/Ast.ql b/ql/test/library-tests/ast/Ast.ql index f851015a1e2..9784cdc7d05 100644 --- a/ql/test/library-tests/ast/Ast.ql +++ b/ql/test/library-tests/ast/Ast.ql @@ -2,9 +2,21 @@ * @kind graph */ -import codeql_ruby.printAst +import codeql_ruby.printAst as P -class OrderedAstNode extends PrintAstNode { +query predicate nodes = P::nodes/3; + +query predicate graphProperties = P::graphProperties/2; + +query predicate edges(P::PrintAstNode source, P::PrintAstNode target, string key, string value) { + P::edges(source, target, key, value) + or + P::edges(source, target, _, _) and + key = "semmle.order" and + value = target.(OrderedAstNode).getProperty("semmle.order") +} + +class OrderedAstNode extends P::PrintAstNode { override string getProperty(string key) { result = super.getProperty(key) or @@ -12,12 +24,12 @@ class OrderedAstNode extends PrintAstNode { result = any(int i | this = - rank[i](AstNode p | + rank[i](P::AstNode p | | p order by p.getLocation().getFile().getBaseName(), p.getLocation().getFile().getAbsolutePath(), - p.getLocation().getStartLine() + p.getLocation().getStartLine(), p.getLocation().getStartColumn() ) ).toString() }