Browse Source

Add UnwrapNestedContainerIfPossible to reduce block container nesting in foreach pattern.

pull/870/merge
Siegfried Pammer 8 years ago
parent
commit
86d3101b22
  1. 16
      ICSharpCode.Decompiler/IL/Transforms/UsingTransform.cs

16
ICSharpCode.Decompiler/IL/Transforms/UsingTransform.cs

@ -92,10 +92,24 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -92,10 +92,24 @@ namespace ICSharpCode.Decompiler.IL.Transforms
}
context.Step("UsingTransform", body);
block.Instructions.RemoveAt(i);
block.Instructions[i - 1] = new UsingInstruction(resourceExpression, body.TryBlock);
block.Instructions[i - 1] = new UsingInstruction(resourceExpression, UnwrapNestedContainerIfPossible(body.TryBlock));
return true;
}
ILInstruction UnwrapNestedContainerIfPossible(ILInstruction tryBlock)
{
if (!(tryBlock is BlockContainer container))
return tryBlock;
if (container.Blocks.Count != 1)
return tryBlock;
var nestedBlock = container.Blocks[0];
if (nestedBlock.Instructions.Count != 2 ||
!(nestedBlock.Instructions[0] is BlockContainer nestedContainer) ||
!nestedBlock.Instructions[1].MatchLeave(container))
return tryBlock;
return nestedContainer;
}
bool MatchDisposeBlock(BlockContainer container, ILVariable objVar, bool usingNull)
{
var entryPoint = container.EntryPoint;

Loading…
Cancel
Save