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.
This commit is contained in:
Arthur Baars
2021-03-12 16:52:28 +01:00
parent 61b3aa8f27
commit 3e5ff1d042

View File

@@ -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()
}