Browse Source

Fix aggressive for-loop detection

pull/976/head
Siegfried Pammer 8 years ago
parent
commit
bcc574a6c0
  1. 9
      ICSharpCode.Decompiler/IL/Transforms/HighLevelLoopTransform.cs

9
ICSharpCode.Decompiler/IL/Transforms/HighLevelLoopTransform.cs

@ -134,8 +134,9 @@ namespace ICSharpCode.Decompiler.IL.Transforms
if (loop.EntryPoint.IncomingEdgeCount != 2) if (loop.EntryPoint.IncomingEdgeCount != 2)
return false; return false;
var incrementBlock = loop.Blocks.SingleOrDefault( var incrementBlock = loop.Blocks.SingleOrDefault(
b => b.Instructions.Last().MatchBranch(loop.EntryPoint) b => b != whileLoopBody
&& b.Instructions.SkipLast(1).All(inst => MatchIncrement(inst, out _))); && b.Instructions.Last().MatchBranch(loop.EntryPoint)
&& b.Instructions.SkipLast(1).All(IsSimpleStatement));
if (incrementBlock != null) { if (incrementBlock != null) {
if (incrementBlock.Instructions.Count <= 1 || loop.Blocks.Count < 3) if (incrementBlock.Instructions.Count <= 1 || loop.Blocks.Count < 3)
return false; return false;
@ -149,7 +150,9 @@ namespace ICSharpCode.Decompiler.IL.Transforms
return false; return false;
if (!last.MatchBranch(loop.EntryPoint)) if (!last.MatchBranch(loop.EntryPoint))
return false; return false;
if (!MatchIncrement(secondToLast, out _)) if (!MatchIncrement(secondToLast, out var incrementVariable))
return false;
if (!condition.Descendants.Any(inst => inst.MatchLdLoc(incrementVariable)))
return false; return false;
context.Step("Transform to for loop", loop); context.Step("Transform to for loop", loop);
int secondToLastIndex = secondToLast.ChildIndex; int secondToLastIndex = secondToLast.ChildIndex;

Loading…
Cancel
Save