diff --git a/ICSharpCode.Decompiler/FlowAnalysis/ControlFlowNode.cs b/ICSharpCode.Decompiler/FlowAnalysis/ControlFlowNode.cs index b84f09f69..cab507bc9 100644 --- a/ICSharpCode.Decompiler/FlowAnalysis/ControlFlowNode.cs +++ b/ICSharpCode.Decompiler/FlowAnalysis/ControlFlowNode.cs @@ -280,6 +280,10 @@ namespace ICSharpCode.Decompiler.FlowAnalysis writer.WriteLine(); Disassembler.DisassemblerHelpers.WriteTo(inst, new PlainTextOutput(writer)); } + if (UserData != null) { + writer.WriteLine(); + writer.Write(UserData.ToString()); + } return writer.ToString(); } diff --git a/ICSharpCode.Decompiler/ILAst/LoopsAndConditions.cs b/ICSharpCode.Decompiler/ILAst/LoopsAndConditions.cs index a310624c0..32095b03e 100644 --- a/ICSharpCode.Decompiler/ILAst/LoopsAndConditions.cs +++ b/ICSharpCode.Decompiler/ILAst/LoopsAndConditions.cs @@ -228,15 +228,10 @@ namespace ICSharpCode.Decompiler.ILAst // Do not modify entry data scope = new HashSet(scope); - HashSet agenda = new HashSet(); - agenda.Add(entryNode); - while(agenda.Any()) { - ControlFlowNode node = agenda.First(); - // Attempt for a good order - while(agenda.Contains(node.ImmediateDominator)) { - node = node.ImmediateDominator; - } - agenda.Remove(node); + Stack agenda = new Stack(); + agenda.Push(entryNode); + while(agenda.Count > 0) { + ControlFlowNode node = agenda.Pop(); // Find a block that represents a simple condition if (scope.Contains(node)) { @@ -384,9 +379,9 @@ namespace ICSharpCode.Decompiler.ILAst } } - // Using the dominator tree should ensure we find the the widest loop first - foreach(var child in node.DominatorTreeChildren) { - agenda.Add(child); + // depth-first traversal of dominator tree + for (int i = node.DominatorTreeChildren.Count - 1; i >= 0; i--) { + agenda.Push(node.DominatorTreeChildren[i]); } } diff --git a/ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj b/ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj index b1a4d783c..52a4f950d 100644 --- a/ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj +++ b/ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj @@ -66,6 +66,8 @@ + + @@ -78,6 +80,10 @@ + + + + @@ -118,10 +124,11 @@ ICSharpCode.Decompiler - + + + - \ No newline at end of file diff --git a/ICSharpCode.Decompiler/Tests/IL/ILTests.cs b/ICSharpCode.Decompiler/Tests/IL/ILTests.cs new file mode 100644 index 000000000..3cfed30f2 --- /dev/null +++ b/ICSharpCode.Decompiler/Tests/IL/ILTests.cs @@ -0,0 +1,51 @@ +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of this +// software and associated documentation files (the "Software"), to deal in the Software +// without restriction, including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons +// to whom the Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all copies or +// substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE +// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +using System; +using System.IO; +using ICSharpCode.Decompiler.Ast; +using ICSharpCode.Decompiler.Tests.Helpers; +using Mono.Cecil; +using NUnit.Framework; + +namespace ICSharpCode.Decompiler.Tests +{ + [TestFixture] + public class ILTests + { + const string path = "../../Tests/IL"; + + [Test] + public void SequenceOfNestedIfs() + { + Run("SequenceOfNestedIfs.dll", "SequenceOfNestedIfs.Output.cs"); + } + + void Run(string compiledFile, string expectedOutputFile) + { + string expectedOutput = File.ReadAllText(Path.Combine(path, expectedOutputFile)); + var assembly = AssemblyDefinition.ReadAssembly(Path.Combine(path, compiledFile)); + AstBuilder decompiler = new AstBuilder(new DecompilerContext(assembly.MainModule)); + decompiler.AddAssembly(assembly); + new Helpers.RemoveCompilerAttribute().Run(decompiler.CompilationUnit); + StringWriter output = new StringWriter(); + decompiler.GenerateCode(new PlainTextOutput(output)); + CodeAssert.AreEqual(expectedOutput, output.ToString()); + } + } +} diff --git a/ICSharpCode.Decompiler/Tests/IL/SequenceOfNestedIfs.Output.cs b/ICSharpCode.Decompiler/Tests/IL/SequenceOfNestedIfs.Output.cs new file mode 100644 index 000000000..754d7dafd --- /dev/null +++ b/ICSharpCode.Decompiler/Tests/IL/SequenceOfNestedIfs.Output.cs @@ -0,0 +1,53 @@ +using System; +[Serializable] +public class Material +{ + public static implicit operator bool(Material m) + { + return m == null; + } +} +[Serializable] +public class SequenceOfNestedIfs +{ + public bool _clear; + public Material _material; + public override bool CheckShader() + { + return false; + } + public override void CreateMaterials() + { + if (!this._clear) + { + if (!this.CheckShader()) + { + return; + } + this._material = new Material(); + } + if (!this._material) + { + if (!this.CheckShader()) + { + return; + } + this._material = new Material(); + } + if (!this._material) + { + if (!this.CheckShader()) + { + return; + } + this._material = new Material(); + } + if (!this._material) + { + if (this.CheckShader()) + { + this._material = new Material(); + } + } + } +} diff --git a/ICSharpCode.Decompiler/Tests/IL/SequenceOfNestedIfs.dll b/ICSharpCode.Decompiler/Tests/IL/SequenceOfNestedIfs.dll new file mode 100644 index 000000000..f517d4546 Binary files /dev/null and b/ICSharpCode.Decompiler/Tests/IL/SequenceOfNestedIfs.dll differ diff --git a/ICSharpCode.Decompiler/Tests/IL/SequenceOfNestedIfs.il b/ICSharpCode.Decompiler/Tests/IL/SequenceOfNestedIfs.il new file mode 100644 index 000000000..9c9b749ce --- /dev/null +++ b/ICSharpCode.Decompiler/Tests/IL/SequenceOfNestedIfs.il @@ -0,0 +1,140 @@ +// Metadata version: v2.0.50727 +.assembly extern mscorlib +{ + .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4.. + .ver 2:0:0:0 +} + +.assembly SequenceOfNestedIfs +{ + .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx + 63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows. + .hash algorithm 0x00008004 + .ver 0:0:0:0 +} +.module SequenceOfNestedIfs +// MVID: {DCEC8A87-5679-4EBE-89A3-51274D8B5446} +.imagebase 0x00400000 +.file alignment 0x00000200 +.stackreserve 0x00100000 +.subsystem 0x0003 // WINDOWS_CUI +.corflags 0x00000001 // ILONLY +// Image base: 0x01D60000 + + +// =============== CLASS MEMBERS DECLARATION =================== + +.class public auto ansi serializable beforefieldinit Material + extends [mscorlib]System.Object +{ + .method public hidebysig specialname rtspecialname + instance void .ctor() cil managed + { + // Code size 7 (0x7) + .maxstack 8 + IL_0000: ldarg.0 + IL_0001: call instance void [mscorlib]System.Object::.ctor() + IL_0006: ret + } // end of method Material::.ctor + + .method public hidebysig specialname static + bool op_Implicit(class Material m) cil managed + { + // Code size 11 (0xb) + .maxstack 8 + IL_0000: ldarg.0 + IL_0001: ldnull + IL_0008: ceq + IL_000a: ret + } // end of method Material::op_Implicit + +} // end of class Material + +.class public auto ansi serializable beforefieldinit SequenceOfNestedIfs + extends [mscorlib]System.Object +{ + .field public bool _clear + .field public class Material _material + .method public hidebysig specialname rtspecialname + instance void .ctor() cil managed + { + // Code size 7 (0x7) + .maxstack 8 + IL_0000: ldarg.0 + IL_0001: call instance void [mscorlib]System.Object::.ctor() + IL_0006: ret + } // end of method SequenceOfNestedIfs::.ctor + + .method public hidebysig virtual instance bool + CheckShader() cil managed + { + // Code size 2 (0x2) + .maxstack 8 + IL_0000: ldc.i4.0 + IL_0001: ret + } // end of method SequenceOfNestedIfs::CheckShader + + .method public hidebysig virtual instance void + CreateMaterials() cil managed + { + // Code size 168 (0xa8) + .maxstack 13 + IL_0000: ldarg.0 + IL_0001: ldfld bool SequenceOfNestedIfs::_clear + IL_0006: brtrue IL_0026 + + IL_000b: ldarg.0 + IL_000c: callvirt instance bool SequenceOfNestedIfs::CheckShader() + IL_0011: brtrue IL_001b + + IL_0016: br IL_00a7 + + IL_001b: ldarg.0 + IL_001c: newobj instance void Material::.ctor() + IL_0021: stfld class Material SequenceOfNestedIfs::_material + IL_0026: ldarg.0 + IL_0027: ldfld class Material SequenceOfNestedIfs::_material + IL_002c: call bool Material::op_Implicit(class Material) + IL_0031: brtrue IL_0051 + + IL_0036: ldarg.0 + IL_0037: callvirt instance bool SequenceOfNestedIfs::CheckShader() + IL_003c: brtrue IL_0046 + + IL_0041: br IL_00a7 + + IL_0046: ldarg.0 + IL_0047: newobj instance void Material::.ctor() + IL_004c: stfld class Material SequenceOfNestedIfs::_material + IL_0051: ldarg.0 + IL_0052: ldfld class Material SequenceOfNestedIfs::_material + IL_0057: call bool Material::op_Implicit(class Material) + IL_005c: brtrue IL_007c + + IL_0061: ldarg.0 + IL_0062: callvirt instance bool SequenceOfNestedIfs::CheckShader() + IL_0067: brtrue IL_0071 + + IL_006c: br IL_00a7 + + IL_0071: ldarg.0 + IL_0072: newobj instance void Material::.ctor() + IL_0077: stfld class Material SequenceOfNestedIfs::_material + IL_007c: ldarg.0 + IL_007d: ldfld class Material SequenceOfNestedIfs::_material + IL_0082: call bool Material::op_Implicit(class Material) + IL_0087: brtrue IL_00a7 + + IL_008c: ldarg.0 + IL_008d: callvirt instance bool SequenceOfNestedIfs::CheckShader() + IL_0092: brtrue IL_009c + + IL_0097: br IL_00a7 + + IL_009c: ldarg.0 + IL_009d: newobj instance void Material::.ctor() + IL_00a2: stfld class Material SequenceOfNestedIfs::_material + IL_00a7: ret + } // end of method SequenceOfNestedIfs::CreateMaterials + +} // end of class SequenceOfNestedIfs \ No newline at end of file diff --git a/ICSharpCode.Decompiler/Tests/StackTests/StackTests.exe b/ICSharpCode.Decompiler/Tests/IL/StackTests.exe similarity index 100% rename from ICSharpCode.Decompiler/Tests/StackTests/StackTests.exe rename to ICSharpCode.Decompiler/Tests/IL/StackTests.exe diff --git a/ICSharpCode.Decompiler/Tests/StackTests/StackTests.il b/ICSharpCode.Decompiler/Tests/IL/StackTests.il similarity index 100% rename from ICSharpCode.Decompiler/Tests/StackTests/StackTests.il rename to ICSharpCode.Decompiler/Tests/IL/StackTests.il diff --git a/NRefactory/ICSharpCode.NRefactory/Utils/GraphVizGraph.cs b/NRefactory/ICSharpCode.NRefactory/Utils/GraphVizGraph.cs index 880a4c2df..77953349a 100644 --- a/NRefactory/ICSharpCode.NRefactory/Utils/GraphVizGraph.cs +++ b/NRefactory/ICSharpCode.NRefactory/Utils/GraphVizGraph.cs @@ -22,6 +22,7 @@ using System.Diagnostics; using System.Globalization; using System.IO; using System.Text.RegularExpressions; +using System.Threading; namespace ICSharpCode.NRefactory.Utils { @@ -66,8 +67,11 @@ namespace ICSharpCode.NRefactory.Utils name = name.Replace(c, '-'); string fileName = name != null ? Path.Combine(Path.GetTempPath(), name) : Path.GetTempFileName(); Save(fileName + ".gv"); - Process.Start("dot", "\"" + fileName + ".gv\" -Tpng -o \"" + fileName + ".png\"").WaitForExit(); - Process.Start(fileName + ".png"); + new Thread(new ThreadStart( + delegate { + Process.Start("dot", "\"" + fileName + ".gv\" -Tpng -o \"" + fileName + ".png\"").WaitForExit(); + Process.Start(fileName + ".png"); + })).Start(); } static string Escape(string text)