From 6cee0cdc4b18ed608be118b628cf90931d3a8204 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Tue, 13 Aug 2024 11:41:34 +0200 Subject: [PATCH] Fix #3258: Move GraphVizGraph and friends to ILSpy and remove InternalsVisibleTo. --- .../FlowAnalysis/ControlFlowNode.cs | 37 ------------------ .../ICSharpCode.Decompiler.csproj | 1 - .../IL/ControlFlow/ControlFlowGraph.cs | 9 ++--- .../Properties/AssemblyInfo.cs | 4 -- ILSpy/Commands/ShowCFGContextMenuEntry.cs | 39 ++++++++++++++++++- .../Util/GraphVizGraph.cs | 2 +- 6 files changed, 43 insertions(+), 49 deletions(-) rename {ICSharpCode.Decompiler => ILSpy}/Util/GraphVizGraph.cs (99%) diff --git a/ICSharpCode.Decompiler/FlowAnalysis/ControlFlowNode.cs b/ICSharpCode.Decompiler/FlowAnalysis/ControlFlowNode.cs index bb1365de0..713fd360f 100644 --- a/ICSharpCode.Decompiler/FlowAnalysis/ControlFlowNode.cs +++ b/ICSharpCode.Decompiler/FlowAnalysis/ControlFlowNode.cs @@ -129,42 +129,5 @@ namespace ICSharpCode.Decompiler.FlowAnalysis } return false; } - -#if DEBUG - internal static GraphVizGraph ExportGraph(IReadOnlyList nodes, Func labelFunc = null) - { - if (labelFunc == null) - { - labelFunc = node => { - var block = node.UserData as IL.Block; - return block != null ? block.Label : node.UserData?.ToString(); - }; - } - GraphVizGraph g = new GraphVizGraph(); - GraphVizNode[] n = new GraphVizNode[nodes.Count]; - for (int i = 0; i < n.Length; i++) - { - n[i] = new GraphVizNode(nodes[i].UserIndex); - n[i].shape = "box"; - n[i].label = labelFunc(nodes[i]); - g.AddNode(n[i]); - } - foreach (var source in nodes) - { - foreach (var target in source.Successors) - { - g.AddEdge(new GraphVizEdge(source.UserIndex, target.UserIndex)); - } - if (source.ImmediateDominator != null) - { - g.AddEdge( - new GraphVizEdge(source.ImmediateDominator.UserIndex, source.UserIndex) { - color = "green" - }); - } - } - return g; - } -#endif } } diff --git a/ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj b/ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj index ba3a308e8..583adedeb 100644 --- a/ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj +++ b/ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj @@ -475,7 +475,6 @@ - diff --git a/ICSharpCode.Decompiler/IL/ControlFlow/ControlFlowGraph.cs b/ICSharpCode.Decompiler/IL/ControlFlow/ControlFlowGraph.cs index a4cecb709..8eea6edd2 100644 --- a/ICSharpCode.Decompiler/IL/ControlFlow/ControlFlowGraph.cs +++ b/ICSharpCode.Decompiler/IL/ControlFlow/ControlFlowGraph.cs @@ -1,10 +1,6 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Diagnostics; -using System.Linq; -using System.Text; using System.Threading; -using System.Threading.Tasks; using ICSharpCode.Decompiler.FlowAnalysis; using ICSharpCode.Decompiler.Util; @@ -36,6 +32,9 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow /// internal readonly ControlFlowNode[] cfg; + /// + public IReadOnlyList Nodes => cfg; + /// /// Dictionary from Block to ControlFlowNode. /// Unlike the cfg array, this can be used to discover control flow nodes even after diff --git a/ICSharpCode.Decompiler/Properties/AssemblyInfo.cs b/ICSharpCode.Decompiler/Properties/AssemblyInfo.cs index 170493460..3f6d671f7 100644 --- a/ICSharpCode.Decompiler/Properties/AssemblyInfo.cs +++ b/ICSharpCode.Decompiler/Properties/AssemblyInfo.cs @@ -19,10 +19,6 @@ using System.Runtime.InteropServices; [assembly: InternalsVisibleTo("ICSharpCode.Decompiler.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001004dcf3979c4e902efa4dd2163a039701ed5822e6f1134d77737296abbb97bf0803083cfb2117b4f5446a217782f5c7c634f9fe1fc60b4c11d62c5b3d33545036706296d31903ddcf750875db38a8ac379512f51620bb948c94d0831125fbc5fe63707cbb93f48c1459c4d1749eb7ac5e681a2f0d6d7c60fa527a3c0b8f92b02bf")] -#if DEBUG -[assembly: InternalsVisibleTo("ILSpy, PublicKey=00240000048000009400000006020000002400005253413100040000010001004dcf3979c4e902efa4dd2163a039701ed5822e6f1134d77737296abbb97bf0803083cfb2117b4f5446a217782f5c7c634f9fe1fc60b4c11d62c5b3d33545036706296d31903ddcf750875db38a8ac379512f51620bb948c94d0831125fbc5fe63707cbb93f48c1459c4d1749eb7ac5e681a2f0d6d7c60fa527a3c0b8f92b02bf")] -#endif - [assembly: SuppressMessage("Microsoft.Usage", "CA2243:AttributeStringLiteralsShouldParseCorrectly", Justification = "AssemblyInformationalVersion does not need to be a parsable version")] diff --git a/ILSpy/Commands/ShowCFGContextMenuEntry.cs b/ILSpy/Commands/ShowCFGContextMenuEntry.cs index 217240e95..a3e2b4e29 100644 --- a/ILSpy/Commands/ShowCFGContextMenuEntry.cs +++ b/ILSpy/Commands/ShowCFGContextMenuEntry.cs @@ -1,9 +1,11 @@ using System; +using System.Collections.Generic; using System.Windows; using ICSharpCode.Decompiler.FlowAnalysis; using ICSharpCode.Decompiler.IL; using ICSharpCode.Decompiler.IL.ControlFlow; +using ICSharpCode.ILSpy.Util; namespace ICSharpCode.ILSpy.Commands { @@ -17,7 +19,7 @@ namespace ICSharpCode.ILSpy.Commands { var container = (BlockContainer)context.Reference.Reference; var cfg = new ControlFlowGraph(container); - ControlFlowNode.ExportGraph(cfg.cfg).Show(); + ExportGraph(cfg.Nodes).Show(); } catch (Exception ex) { @@ -34,6 +36,41 @@ namespace ICSharpCode.ILSpy.Commands { return context.Reference?.Reference is BlockContainer; } + + internal static GraphVizGraph ExportGraph(IReadOnlyList nodes, Func labelFunc = null) + { + if (labelFunc == null) + { + labelFunc = node => { + var block = node.UserData as Block; + return block != null ? block.Label : node.UserData?.ToString(); + }; + } + GraphVizGraph g = new GraphVizGraph(); + GraphVizNode[] n = new GraphVizNode[nodes.Count]; + for (int i = 0; i < n.Length; i++) + { + n[i] = new GraphVizNode(nodes[i].UserIndex); + n[i].shape = "box"; + n[i].label = labelFunc(nodes[i]); + g.AddNode(n[i]); + } + foreach (var source in nodes) + { + foreach (var target in source.Successors) + { + g.AddEdge(new GraphVizEdge(source.UserIndex, target.UserIndex)); + } + if (source.ImmediateDominator != null) + { + g.AddEdge( + new GraphVizEdge(source.ImmediateDominator.UserIndex, source.UserIndex) { + color = "green" + }); + } + } + return g; + } } #endif } diff --git a/ICSharpCode.Decompiler/Util/GraphVizGraph.cs b/ILSpy/Util/GraphVizGraph.cs similarity index 99% rename from ICSharpCode.Decompiler/Util/GraphVizGraph.cs rename to ILSpy/Util/GraphVizGraph.cs index 92069e7d9..19ce1cd06 100644 --- a/ICSharpCode.Decompiler/Util/GraphVizGraph.cs +++ b/ILSpy/Util/GraphVizGraph.cs @@ -25,7 +25,7 @@ using System.Globalization; using System.IO; using System.Text.RegularExpressions; -namespace ICSharpCode.Decompiler.Util +namespace ICSharpCode.ILSpy.Util { #if DEBUG ///