Skip to content

Commit c9b9751

Browse files
committed
QL: Add NodeNumbering library
1 parent 60a22b7 commit c9b9751

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
private import codeql_ql.ast.Ast
2+
3+
pragma[inline]
4+
private predicate isNumberedNode(AstNode node) {
5+
// these are not nested in the location of the parent node, so we can't use their location to order them
6+
not node instanceof Annotation and
7+
not node instanceof QLDoc
8+
}
9+
10+
private int getNodeDepth(AstNode node) {
11+
node instanceof TopLevel and
12+
result = 0
13+
or
14+
isNumberedNode(node) and
15+
result = 1 + getNodeDepth(node.getParent())
16+
}
17+
18+
/**
19+
* Gets the pre-order ID for the given `node`, that is, its visit position
20+
* in a pre-order traversal of all nodes.
21+
*
22+
* The children of a node are ordered left-to-right as they appear in the source code.
23+
*
24+
* The ID is globally unique for this AST node, also across files.
25+
*
26+
* At the moment this predicate is only defined for node which are:
27+
* - reachable via `getAChild` edges from a `TopLevel`, and
28+
* - is not a comment or annotation
29+
*/
30+
cached
31+
int getPreOrderId(AstNode node) {
32+
node =
33+
rank[result](AstNode n, Location loc, int depth |
34+
depth = getNodeDepth(n) and loc = n.getLocation()
35+
|
36+
n order by loc.getFile().getAbsolutePath(), loc.getStartLine(), loc.getStartColumn(), depth
37+
)
38+
}

0 commit comments

Comments
 (0)