From c7effe1b10c60c26b5a5387ad850e404f291f088 Mon Sep 17 00:00:00 2001 From: Allister Beharry Date: Tue, 25 Jul 2023 20:45:51 -0400 Subject: [PATCH] Fix bug in GraphAnalyzer. Load mermaidjs in head. --- .../Stratis.CodeAnalysis.Cs/GraphAnalyzer.cs | 42 +++++++++++++++++-- src/Stratis.DevEx.Drawing/Html.cs | 8 ++-- src/Stratis.DevEx.Gui/MainForm.cs | 2 +- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/Stratis.CodeAnalysis.Cs/Stratis.CodeAnalysis.Cs/GraphAnalyzer.cs b/src/Stratis.CodeAnalysis.Cs/Stratis.CodeAnalysis.Cs/GraphAnalyzer.cs index 87eebb9..f64e074 100644 --- a/src/Stratis.CodeAnalysis.Cs/Stratis.CodeAnalysis.Cs/GraphAnalyzer.cs +++ b/src/Stratis.CodeAnalysis.Cs/Stratis.CodeAnalysis.Cs/GraphAnalyzer.cs @@ -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++) { @@ -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++) @@ -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(); diff --git a/src/Stratis.DevEx.Drawing/Html.cs b/src/Stratis.DevEx.Drawing/Html.cs index b694677..b9601b4 100644 --- a/src/Stratis.DevEx.Drawing/Html.cs +++ b/src/Stratis.DevEx.Drawing/Html.cs @@ -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(""); stringBuilder.AppendLine(""); @@ -38,7 +39,7 @@ public static string DrawCallGraph(Graph graph) var stringBuilder = new StringBuilder(); var divId = Guid.NewGuid().ToString("N"); stringBuilder.AppendLine("Title"); - stringBuilder.AppendLine($"
"); + stringBuilder.AppendLine($"
"); stringBuilder.AppendLine(""); stringBuilder.AppendLine("Title"); + stringBuilder.AppendLine($"
");
             stringBuilder.AppendLine(summary);
             stringBuilder.AppendLine("
"); - stringBuilder.AppendLine(""); stringBuilder.AppendLine(""); diff --git a/src/Stratis.DevEx.Gui/MainForm.cs b/src/Stratis.DevEx.Gui/MainForm.cs index 8375e79..54a18f2 100644 --- a/src/Stratis.DevEx.Gui/MainForm.cs +++ b/src/Stratis.DevEx.Gui/MainForm.cs @@ -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)) {