From bcc574a6c0e6ca6a905b396ead2c62b2cffed399 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sun, 5 Nov 2017 23:54:53 +0100 Subject: [PATCH] Fix aggressive for-loop detection --- .../IL/Transforms/HighLevelLoopTransform.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ICSharpCode.Decompiler/IL/Transforms/HighLevelLoopTransform.cs b/ICSharpCode.Decompiler/IL/Transforms/HighLevelLoopTransform.cs index bd330cdef..28dc9a597 100644 --- a/ICSharpCode.Decompiler/IL/Transforms/HighLevelLoopTransform.cs +++ b/ICSharpCode.Decompiler/IL/Transforms/HighLevelLoopTransform.cs @@ -134,8 +134,9 @@ namespace ICSharpCode.Decompiler.IL.Transforms if (loop.EntryPoint.IncomingEdgeCount != 2) return false; var incrementBlock = loop.Blocks.SingleOrDefault( - b => b.Instructions.Last().MatchBranch(loop.EntryPoint) - && b.Instructions.SkipLast(1).All(inst => MatchIncrement(inst, out _))); + b => b != whileLoopBody + && b.Instructions.Last().MatchBranch(loop.EntryPoint) + && b.Instructions.SkipLast(1).All(IsSimpleStatement)); if (incrementBlock != null) { if (incrementBlock.Instructions.Count <= 1 || loop.Blocks.Count < 3) return false; @@ -149,7 +150,9 @@ namespace ICSharpCode.Decompiler.IL.Transforms return false; if (!last.MatchBranch(loop.EntryPoint)) return false; - if (!MatchIncrement(secondToLast, out _)) + if (!MatchIncrement(secondToLast, out var incrementVariable)) + return false; + if (!condition.Descendants.Any(inst => inst.MatchLdLoc(incrementVariable))) return false; context.Step("Transform to for loop", loop); int secondToLastIndex = secondToLast.ChildIndex;