Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend EliminateRedundantTryFinally in ReduceNestingTransform #2959

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions ICSharpCode.Decompiler/IL/Transforms/ReduceNestingTransform.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018 Siegfried Pammer
// Copyright (c) 2018 Siegfried Pammer
//
// 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
Expand Down Expand Up @@ -637,10 +637,25 @@ leave IL_003e (nop)
// Finally is empty and redundant. But we'll delete the block only if there's a PinnedRegion.
if (!(tryFinally.TryBlock is BlockContainer tryContainer))
return;
if (tryContainer.SingleInstruction() is PinnedRegion pinnedRegion)
if (tryContainer.Blocks.Count != 1)
return;
var tryBlock = tryContainer.Blocks[0];
if (tryBlock.Instructions.Count == 1)
{
context.Step("Removing try-finally around PinnedRegion", pinnedRegion);
tryFinally.ReplaceWith(pinnedRegion);
if (tryBlock.Instructions[0] is PinnedRegion pinnedRegion)
{
context.Step("Removing try-finally around PinnedRegion", pinnedRegion);
tryFinally.ReplaceWith(pinnedRegion);
}
}
else if (tryBlock.Instructions.Count == 2)
{
if (tryBlock.Instructions[0] is PinnedRegion pinnedRegion &&
tryBlock.Instructions[1].MatchLeave(tryContainer))
{
context.Step("Removing try-finally around PinnedRegion", pinnedRegion);
tryFinally.ReplaceWith(pinnedRegion);
}
}
}
}
Expand Down