Skip to content

Commit

Permalink
Fix bug in GraphAnalyzer. Load mermaidjs in head.
Browse files Browse the repository at this point in the history
  • Loading branch information
allisterb committed Jul 26, 2023
1 parent cfce4fc commit c7effe1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,17 @@ public static void AnalyzeControlFlow(string cfgFile, Configuration config, Sema
};

Info("Analyzing control-flow of method: {ident}...", method.Identifier);
/*
var op = model.GetOperation(m);
if (op is null || op is not IMethodBodyOperation)
{
Error("Could not get semantic model operation for method {ident}.", method.Identifier);
continue;
}
var mbop = (IMethodBodyOperation)op;
var cfg = ControlFlowGraph.Create(mbop);
var mbop = op;
*/
var cfg = ControlFlowGraph.Create(m, model);
Debug("{ident} has {len} basic block(s).", method.Identifier, cfg.Blocks.Where(bb => bb.Kind == BasicBlockKind.Block).Count());
for (int j = 0; j < cfg.Blocks.Length; j++)
{
Expand All @@ -72,8 +75,9 @@ public static void AnalyzeControlFlow(string cfgFile, Configuration config, Sema
if (node is null)
{
node = new Node(nid);
var src = bb.Operations.Select(o => o?.Syntax.ToString() ?? "").JoinWith(Environment.NewLine);
node.LabelText =
bb.Kind == BasicBlockKind.Entry ? method.Identifier + "::" + method.Type : bb.Operations.Select(o => o?.Syntax.ToString() ?? "").JoinWith(Environment.NewLine);
bb.Kind == BasicBlockKind.Entry ? method.Identifier + "::" + method.Type + Environment.NewLine + src : src;
graph.AddNode(node);
}
for (int k = 0; k < bb.Predecessors.Length; k++)
Expand All @@ -82,7 +86,39 @@ public static void AnalyzeControlFlow(string cfgFile, Configuration config, Sema
var pid = method.Identifier + "::" + method.Type + "_" + pb.Source.Ordinal.ToString();
var pred = graph.FindNode(pid);
graph.AddEdge(pred.Id, node.Id);

}
if (bb.ConditionalSuccessor is not null)
{
nid = method.Identifier + "::" + method.Type + "_" + bb.ConditionalSuccessor.Destination.Ordinal.ToString();
var succ = graph.FindNode(nid);
if (succ is null)
{
succ = new Node(nid);
var src = bb.ConditionalSuccessor.Destination.Operations.Select(o => o?.Syntax.ToString() ?? "").JoinWith(Environment.NewLine);
succ.LabelText =
bb.ConditionalSuccessor.Destination.Kind == BasicBlockKind.Entry ? method.Identifier + "::" + method.Type + Environment.NewLine + src : src;
graph.AddNode(succ);
}
graph.AddEdge(node.Id, succ.Id);
}

/*
if (bb.FallThroughSuccessor is not null && bb.FallThroughSuccessor.Destination != bb.ConditionalSuccessor.Destination)
{
nid = method.Identifier + "::" + method.Type + "_" + bb.FallThroughSuccessor.Destination.Ordinal.ToString();
var succ = graph.FindNode(nid);
if (succ is null)
{
succ = new Node(nid);
var src = bb.FallThroughSuccessor.Destination.Operations.Select(o => o?.Syntax.ToString() ?? "").JoinWith(Environment.NewLine);
succ.LabelText =
bb.FallThroughSuccessor.Destination.Kind == BasicBlockKind.Entry ? method.Identifier + "::" + method.Type + Environment.NewLine + src : src;
graph.AddNode(succ);
}
graph.AddEdge(node.Id, succ.Id);
}
*/
}
}
top.Complete();
Expand Down
8 changes: 4 additions & 4 deletions src/Stratis.DevEx.Drawing/Html.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static string DrawControlFlowGraph(Graph graph)
}};
let options = {network.SerializeOptions()};
let network = new vis.Network(container, data, options);
network.fit();
");
stringBuilder.AppendLine("</script>");
stringBuilder.AppendLine("</body></html>");
Expand All @@ -38,7 +39,7 @@ public static string DrawCallGraph(Graph graph)
var stringBuilder = new StringBuilder();
var divId = Guid.NewGuid().ToString("N");
stringBuilder.AppendLine("<html lang=\"en\"><head><script type=\"text/javascript\" src=\"https://unpkg.com/vis-network/standalone/umd/vis-network.min.js\"></script><title>Title</title><body>");
stringBuilder.AppendLine($"<div id=\"{divId}\" style=\"height:1000px; width:100%\"></div>");
stringBuilder.AppendLine($"<div id=\"{divId}\" style=\"height:100%; width:100%\"></div>");
stringBuilder.AppendLine("</div>");
stringBuilder.AppendLine("<script type=\"text/javascript\">");
stringBuilder.AppendLine($@"
Expand All @@ -62,11 +63,10 @@ public static string DrawSummary(string summary)
var op = Begin("Drawing summary to HTML");
var stringBuilder = new StringBuilder();
var divId = Guid.NewGuid().ToString("N");
stringBuilder.AppendLine("<html lang=\"en\"><head><title>Title</title></head><body>");
stringBuilder.AppendLine($"<pre id=\"{divId}\" class=\"mermaid\" style=\"height:1000px; width:100%\">");
stringBuilder.AppendLine("<html lang=\"en\"><head><script src=\"https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js\"></script><title>Title</title></head><body>");
stringBuilder.AppendLine($"<pre id=\"{divId}\" class=\"mermaid\" style=\"height:100%; width:100%\">");
stringBuilder.AppendLine(summary);
stringBuilder.AppendLine("</pre>");
stringBuilder.AppendLine("<script src=\"https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js\"></script>");
stringBuilder.AppendLine("<script type=\"text/JavaScript\">");
stringBuilder.AppendLine("mermaid.initialize({ startOnLoad: true });");
stringBuilder.AppendLine("</script>");
Expand Down
2 changes: 1 addition & 1 deletion src/Stratis.DevEx.Gui/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public void ReadMessage(SummaryMessage m)
var docid = projectid + "_" + m.Document;
Graph cg = Program.CreateCallGraph(m.Implements, m.Invocations);

File.WriteAllText(projectDir.CombinePath(DateTime.Now.Millisecond.ToString() + ".html"), Html.DrawCallGraph(cg));
//File.WriteAllText(projectDir.CombinePath(DateTime.Now.Millisecond.ToString() + ".html"), Html.DrawCallGraph(cg));
var projects = (TreeItem)navigation.DataStore[1];
if (projects.Children.Any(c => c.Key == projectid))
{
Expand Down

0 comments on commit c7effe1

Please sign in to comment.