Skip to content

Commit 434fd1f

Browse files
committed
Update java PrintAST query for deterministic results
Update the query implementation, documentation, and expected test results for the java tools PrintAST query in order to fix a problem where Entry nodes were non-deterministically ordered, especially between MacOS and Linux test environments.
1 parent b1dff3e commit 434fd1f

File tree

3 files changed

+12
-18
lines changed

3 files changed

+12
-18
lines changed

server/ql/java/tools/src/PrintCFG/PrintCFG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
Produces a representation of a file's Control Flow Graph (CFG) for specified source files.
44

5+
_NOTE:_ `PrintCFG` query runs may produce extremely verbose query results, even for a small (test) codebase, because the CFG includes many nodes and edges to represent relationships within the standard library.
6+
57
## Overview
68

79
The Control Flow Graph represents the order in which statements and expressions are executed in a program. Each node in the graph represents a control-flow element (statement or expression), and edges represent possible execution paths between them.
810

9-
This query outputs all CFG nodes and their successor relationships for Java code, which is useful for understanding program execution flow, debugging control flow issues, and analyzing code paths.
11+
This query outputs CFG nodes and their successor relationships for Java code, excluding Entry and Exit nodes whose ordering is non-deterministic across CodeQL CLI versions and platforms. This is useful for understanding program execution flow, debugging control flow issues, and analyzing code paths.
1012

1113
## Use Cases
1214

server/ql/java/tools/src/PrintCFG/PrintCFG.ql

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,27 @@ import java
1010
import semmle.code.java.ControlFlowGraph
1111

1212
/**
13-
* Holds if the node is an exit-related CFG node.
13+
* Holds if the node is an entry- or exit-related CFG node.
1414
* These nodes are excluded from the output because their ordering
15-
* is non-deterministic across CodeQL CLI versions.
15+
* is non-deterministic across CodeQL CLI versions and platforms.
1616
*/
17-
private predicate isExitNode(ControlFlowNode node) {
18-
node.toString().matches("%Exit")
17+
private predicate isEntryOrExitNode(ControlFlowNode node) {
18+
node.toString().matches("%Exit") or
19+
node.toString() = "Entry"
1920
}
2021

2122
/**
2223
* Configuration for PrintCFG that outputs all CFG nodes and edges,
23-
* excluding exit nodes for deterministic output.
24+
* excluding entry/exit nodes for deterministic output.
2425
*/
2526
query predicate nodes(ControlFlowNode node, string property, string value) {
2627
property = "semmle.label" and
2728
value = node.toString() and
28-
not isExitNode(node)
29+
not isEntryOrExitNode(node)
2930
}
3031

3132
query predicate edges(ControlFlowNode pred, ControlFlowNode succ) {
3233
pred.getASuccessor() = succ and
33-
not isExitNode(pred) and
34-
not isExitNode(succ)
34+
not isEntryOrExitNode(pred) and
35+
not isEntryOrExitNode(succ)
3536
}

server/ql/java/tools/test/PrintCFG/PrintCFG.expected

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
Example1.java:
2-
# 3| Entry
3-
#-----| -> { ... }
4-
5-
# 5| Entry
6-
#-----| -> { ... }
7-
8-
# 18| Entry
9-
#-----| -> { ... }
10-
112
# 3| { ... }
123
#-----| -> Before super(...)
134

0 commit comments

Comments
 (0)