Skip to content

Commit 72caadb

Browse files
authored
Merge pull request github#16869 from hvitved/shared/cfg/mermaid-output
Shared: Generate mermaid output in `View CFG` query
2 parents 40b7534 + e1c1314 commit 72caadb

File tree

1 file changed

+36
-0
lines changed
  • shared/controlflow/codeql/controlflow

1 file changed

+36
-0
lines changed

shared/controlflow/codeql/controlflow/Cfg.qll

+36
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,41 @@ module Make<LocationSig Location, InputSig<Location> Input> {
11911191
)
11921192
).toString()
11931193
}
1194+
1195+
module Mermaid {
1196+
private string nodeId(RelevantNode n) { nodes(n, "semmle.order", result) }
1197+
1198+
private string nodes() {
1199+
result =
1200+
concat(RelevantNode n, string id, string text |
1201+
id = nodeId(n) and
1202+
text = n.toString()
1203+
|
1204+
id + "[\"" + text + "\"]", "\n" order by id
1205+
)
1206+
}
1207+
1208+
private string edge(RelevantNode pred, RelevantNode succ, string ord) {
1209+
edges(pred, succ, "semmle.order", ord) and
1210+
exists(string label |
1211+
edges(pred, succ, "semmle.label", label) and
1212+
if label = ""
1213+
then result = nodeId(pred) + " --> " + nodeId(succ)
1214+
else result = nodeId(pred) + " -- " + label + " --> " + nodeId(succ)
1215+
)
1216+
}
1217+
1218+
private string edges() {
1219+
result =
1220+
concat(RelevantNode pred, RelevantNode succ, string edge, string ord |
1221+
edge = edge(pred, succ, ord)
1222+
|
1223+
edge, "\n" order by ord
1224+
)
1225+
}
1226+
1227+
query predicate mermaid(string s) { s = "flowchart TD\n" + nodes() + "\n\n" + edges() }
1228+
}
11941229
}
11951230

11961231
/** Provides the input to `ViewCfgQuery`. */
@@ -1263,6 +1298,7 @@ module Make<LocationSig Location, InputSig<Location> Input> {
12631298
}
12641299

12651300
import TestOutput<RelevantNode>
1301+
import Mermaid
12661302
}
12671303

12681304
/** Provides a set of consistency queries. */

0 commit comments

Comments
 (0)