Skip to content

Commit 2d61fc5

Browse files
committed
Java: Add support for "View CFG".
1 parent 389cd5d commit 2d61fc5

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

java/ql/lib/printCfg.ql

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @name Print CFG
3+
* @description Produces a representation of a file's Control Flow Graph.
4+
* This query is used by the VS Code extension.
5+
* @id java/print-cfg
6+
* @kind graph
7+
* @tags ide-contextual-queries/print-cfg
8+
*/
9+
10+
import java
11+
12+
external string selectedSourceFile();
13+
14+
private predicate selectedSourceFileAlias = selectedSourceFile/0;
15+
16+
external int selectedSourceLine();
17+
18+
private predicate selectedSourceLineAlias = selectedSourceLine/0;
19+
20+
external int selectedSourceColumn();
21+
22+
private predicate selectedSourceColumnAlias = selectedSourceColumn/0;
23+
24+
module ViewCfgQueryInput implements ViewCfgQueryInputSig<File> {
25+
predicate selectedSourceFile = selectedSourceFileAlias/0;
26+
27+
predicate selectedSourceLine = selectedSourceLineAlias/0;
28+
29+
predicate selectedSourceColumn = selectedSourceColumnAlias/0;
30+
31+
predicate cfgScopeSpan(
32+
Callable callable, File file, int startLine, int startColumn, int endLine, int endColumn
33+
) {
34+
file = callable.getFile() and
35+
callable.getLocation().getStartLine() = startLine and
36+
callable.getLocation().getStartColumn() = startColumn and
37+
exists(Location loc |
38+
loc.getEndLine() = endLine and
39+
loc.getEndColumn() = endColumn and
40+
loc = callable.getBody().getLocation()
41+
)
42+
}
43+
}
44+
45+
import ViewCfgQuery<File, ViewCfgQueryInput>

java/ql/lib/semmle/code/java/ControlFlowGraph.qll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,3 +1775,17 @@ class ConditionNode extends ControlFlow::Node {
17751775
/** Gets the condition of this `ConditionNode`. */
17761776
ExprParent getCondition() { result = this.asExpr() or result = this.asStmt() }
17771777
}
1778+
1779+
private import codeql.controlflow.PrintGraph as PrintGraph
1780+
1781+
private module PrintGraphInput implements PrintGraph::InputSig<Location> {
1782+
private import java as J
1783+
1784+
class Callable = J::Callable;
1785+
1786+
class ControlFlowNode = J::ControlFlowNode;
1787+
1788+
ControlFlowNode getASuccessor(ControlFlowNode n, SuccessorType t) { result = n.getASuccessor(t) }
1789+
}
1790+
1791+
import PrintGraph::PrintGraph<Location, PrintGraphInput>

0 commit comments

Comments
 (0)