diff --git a/shared/controlflow/codeql/controlflow/Cfg.qll b/shared/controlflow/codeql/controlflow/Cfg.qll index e7e033f9ae2..9ed75a6539b 100644 --- a/shared/controlflow/codeql/controlflow/Cfg.qll +++ b/shared/controlflow/codeql/controlflow/Cfg.qll @@ -1191,6 +1191,41 @@ module Make Input> { ) ).toString() } + + module Mermaid { + private string nodeId(RelevantNode n) { nodes(n, "semmle.order", result) } + + private string nodes() { + result = + concat(RelevantNode n, string id, string text | + id = nodeId(n) and + text = n.toString() + | + id + "[\"" + text + "\"]", "\n" order by id + ) + } + + private string edge(RelevantNode pred, RelevantNode succ, string ord) { + edges(pred, succ, "semmle.order", ord) and + exists(string label | + edges(pred, succ, "semmle.label", label) and + if label = "" + then result = nodeId(pred) + " --> " + nodeId(succ) + else result = nodeId(pred) + " -- " + label + " --> " + nodeId(succ) + ) + } + + private string edges() { + result = + concat(RelevantNode pred, RelevantNode succ, string edge, string ord | + edge = edge(pred, succ, ord) + | + edge, "\n" order by ord + ) + } + + query predicate mermaid(string s) { s = "flowchart TD\n" + nodes() + "\n\n" + edges() } + } } /** Provides the input to `ViewCfgQuery`. */ @@ -1263,6 +1298,7 @@ module Make Input> { } import TestOutput + import Mermaid } /** Provides a set of consistency queries. */