Skip to content

Commit 43f1b84

Browse files
committed
Prepare to share PrintGraph with DFG viewer
1 parent f0e665d commit 43f1b84

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

shared/controlflow/codeql/controlflow/Cfg.qll

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,13 +1315,18 @@ module MakeWithSplitting<
13151315

13161316
private import PrintGraph as Pp
13171317

1318+
final private class FinalNode = Node;
1319+
13181320
private module PrintGraphInput implements Pp::InputSig<Location> {
13191321
class Callable = CfgScope;
13201322

1321-
class ControlFlowNode = Node;
1323+
class Node = FinalNode;
13221324

1323-
ControlFlowNode getASuccessor(ControlFlowNode n, SuccessorType t) {
1324-
result = n.getASuccessor(t)
1325+
Node getASuccessor(Node n, string s) {
1326+
exists(SuccessorType t |
1327+
result = n.getASuccessor(t) and
1328+
if t instanceof DirectSuccessor then s = "" else s = t.toString()
1329+
)
13251330
}
13261331
}
13271332

shared/controlflow/codeql/controlflow/PrintGraph.qll

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,47 @@
11
/**
2-
* Provides modules for printing control flow graphs in VSCode via the "View
3-
* CFG" query. Also provides modules for printing control flow graphs in tests
2+
* Provides modules for printing control flow and data flow graphs in VSCode via the
3+
* "View CFG/DFG" queries. Also provides modules for printing such graphs in tests
44
* and as Mermaid diagrams.
55
*/
66
overlay[local?]
77
module;
88

99
private import codeql.util.FileSystem
1010
private import codeql.util.Location
11-
private import SuccessorType
1211

1312
signature module InputSig<LocationSig Location> {
1413
class Callable;
1514

16-
class ControlFlowNode {
15+
class Node {
1716
Callable getEnclosingCallable();
1817

1918
Location getLocation();
2019

2120
string toString();
2221
}
2322

24-
ControlFlowNode getASuccessor(ControlFlowNode n, SuccessorType t);
23+
Node getASuccessor(Node n, string label);
2524
}
2625

27-
/** Provides modules for printing control flow graphs. */
26+
/** Provides modules for printing flow graphs. */
2827
module PrintGraph<LocationSig Location, InputSig<Location> Input> {
2928
private import Input
3029

3130
/** A node to be included in the output of `TestOutput`. */
32-
signature class RelevantNodeSig extends ControlFlowNode;
31+
signature class RelevantNodeSig extends Node;
3332

3433
/**
35-
* Import this module into a `.ql` file to output a CFG. The
34+
* Import this module into a `.ql` file to output a graph. The
3635
* graph is restricted to nodes from `RelevantNode`.
3736
*/
3837
module TestOutput<RelevantNodeSig RelevantNode> {
39-
/** Holds if `pred -> succ` is an edge in the CFG. */
38+
/** Holds if `pred -> succ` is an edge in the graph. */
4039
query predicate edges(RelevantNode pred, RelevantNode succ, string label) {
41-
label =
42-
strictconcat(SuccessorType t, string s |
43-
succ = getASuccessor(pred, t) and
44-
if t instanceof DirectSuccessor then s = "" else s = t.toString()
45-
|
46-
s, ", " order by s
47-
)
40+
label = strictconcat(string s | succ = getASuccessor(pred, s) | s, ", " order by s)
4841
}
4942

5043
/**
51-
* Provides logic for representing a CFG as a [Mermaid diagram](https://mermaid.js.org/).
44+
* Provides logic for representing a graph as a [Mermaid diagram](https://mermaid.js.org/).
5245
*/
5346
module Mermaid {
5447
private string nodeId(RelevantNode n) {
@@ -103,8 +96,8 @@ module PrintGraph<LocationSig Location, InputSig<Location> Input> {
10396
}
10497
}
10598

106-
/** Provides the input to `ViewCfgQuery`. */
107-
signature module ViewCfgQueryInputSig<FileSig File> {
99+
/** Provides the input to `ViewGraphQuery`. */
100+
signature module ViewGraphQueryInputSig<FileSig File> {
108101
/** Gets the source file selected in the IDE. Should be an `external` predicate. */
109102
string selectedSourceFile();
110103

@@ -118,15 +111,15 @@ module PrintGraph<LocationSig Location, InputSig<Location> Input> {
118111
* Holds if `callable` spans column `startColumn` of line `startLine` to
119112
* column `endColumn` of line `endLine` in `file`.
120113
*/
121-
predicate cfgScopeSpan(
114+
predicate callableSpan(
122115
Callable callable, File file, int startLine, int startColumn, int endLine, int endColumn
123116
);
124117
}
125118

126119
/**
127-
* Provides an implementation for a `View CFG` query.
120+
* Provides an implementation for a `View CFG` or `View DFG` query.
128121
*
129-
* Import this module into a `.ql` that looks like
122+
* Import this module into a `.ql` that looks like (for the `View CFG` query):
130123
*
131124
* ```ql
132125
* @name Print CFG
@@ -136,15 +129,17 @@ module PrintGraph<LocationSig Location, InputSig<Location> Input> {
136129
* @kind graph
137130
* @tags ide-contextual-queries/print-cfg
138131
* ```
132+
*
133+
* For the `View DFG` query replace "cfg" with "dfg" above and "control flow" with "data flow".
139134
*/
140-
module ViewCfgQuery<FileSig File, ViewCfgQueryInputSig<File> ViewCfgQueryInput> {
141-
private import ViewCfgQueryInput
135+
module ViewGraphQuery<FileSig File, ViewGraphQueryInputSig<File> ViewGraphQueryInput> {
136+
private import ViewGraphQueryInput
142137

143138
bindingset[file, line, column]
144139
private Callable smallestEnclosingScope(File file, int line, int column) {
145140
result =
146141
min(Callable callable, int startLine, int startColumn, int endLine, int endColumn |
147-
cfgScopeSpan(callable, file, startLine, startColumn, endLine, endColumn) and
142+
callableSpan(callable, file, startLine, startColumn, endLine, endColumn) and
148143
(
149144
startLine < line
150145
or
@@ -162,9 +157,9 @@ module PrintGraph<LocationSig Location, InputSig<Location> Input> {
162157

163158
private import IdeContextual<File>
164159

165-
final private class FinalControlFlowNode = ControlFlowNode;
160+
final private class FinalNode = Node;
166161

167-
private class RelevantNode extends FinalControlFlowNode {
162+
private class RelevantNode extends FinalNode {
168163
RelevantNode() {
169164
this.getEnclosingCallable() =
170165
smallestEnclosingScope(getFileBySourceArchiveName(selectedSourceFile()),
@@ -178,7 +173,7 @@ module PrintGraph<LocationSig Location, InputSig<Location> Input> {
178173

179174
import Output::Mermaid
180175

181-
/** Holds if `pred` -> `succ` is an edge in the CFG. */
176+
/** Holds if `pred` -> `succ` is an edge in the graph. */
182177
query predicate edges(RelevantNode pred, RelevantNode succ, string attr, string val) {
183178
attr = "semmle.label" and
184179
Output::edges(pred, succ, val)

0 commit comments

Comments
 (0)