Skip to content

Commit

Permalink
Fix #3258: Move GraphVizGraph and friends to ILSpy and remove Interna…
Browse files Browse the repository at this point in the history
…lsVisibleTo.
  • Loading branch information
siegfriedpammer committed Aug 13, 2024
1 parent fa0ab07 commit 6cee0cd
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 49 deletions.
37 changes: 0 additions & 37 deletions ICSharpCode.Decompiler/FlowAnalysis/ControlFlowNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,42 +129,5 @@ public bool Dominates(ControlFlowNode node)
}
return false;
}

#if DEBUG
internal static GraphVizGraph ExportGraph(IReadOnlyList<ControlFlowNode> nodes, Func<ControlFlowNode, string> 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
}
}
1 change: 0 additions & 1 deletion ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,6 @@
<Compile Include="TypeSystem\TupleType.cs" />
<Compile Include="TypeSystem\TypeProvider.cs" />
<Compile Include="Util\FileUtility.cs" />
<Compile Include="Util\GraphVizGraph.cs" />
<Compile Include="Util\KeyComparer.cs" />
<Compile Include="Util\LongDict.cs" />
<Compile Include="Util\ResourcesFile.cs" />
Expand Down
9 changes: 4 additions & 5 deletions ICSharpCode.Decompiler/IL/ControlFlow/ControlFlowGraph.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -36,6 +32,9 @@ public class ControlFlowGraph
/// </summary>
internal readonly ControlFlowNode[] cfg;

/// <inheritdoc cref="cfg"/>
public IReadOnlyList<ControlFlowNode> Nodes => cfg;

/// <summary>
/// Dictionary from Block to ControlFlowNode.
/// Unlike the cfg array, this can be used to discover control flow nodes even after
Expand Down
4 changes: 0 additions & 4 deletions ICSharpCode.Decompiler/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@

[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")]

39 changes: 38 additions & 1 deletion ILSpy/Commands/ShowCFGContextMenuEntry.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -17,7 +19,7 @@ public void Execute(TextViewContext context)
{
var container = (BlockContainer)context.Reference.Reference;
var cfg = new ControlFlowGraph(container);
ControlFlowNode.ExportGraph(cfg.cfg).Show();
ExportGraph(cfg.Nodes).Show();
}
catch (Exception ex)
{
Expand All @@ -34,6 +36,41 @@ public bool IsVisible(TextViewContext context)
{
return context.Reference?.Reference is BlockContainer;
}

internal static GraphVizGraph ExportGraph(IReadOnlyList<ControlFlowNode> nodes, Func<ControlFlowNode, string> 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
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
using System.IO;
using System.Text.RegularExpressions;

namespace ICSharpCode.Decompiler.Util
namespace ICSharpCode.ILSpy.Util
{
#if DEBUG
/// <summary>
Expand Down

0 comments on commit 6cee0cd

Please sign in to comment.