Browse Source

Remove unreachable return statements. Closes #174. Closes #192

pull/219/head
David Srbecký 14 years ago committed by Eusebiu Marcu
parent
commit
f9d877b25a
  1. 19
      ICSharpCode.Decompiler/ILAst/GotoRemoval.cs
  2. 4
      ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs

19
ICSharpCode.Decompiler/ILAst/GotoRemoval.cs

@ -98,10 +98,27 @@ namespace ICSharpCode.Decompiler.ILAst
} }
} }
// Remove redundant return // Remove redundant return at the end of method
if (method.Body.Count > 0 && method.Body.Last().Match(ILCode.Ret) && ((ILExpression)method.Body.Last()).Arguments.Count == 0) { if (method.Body.Count > 0 && method.Body.Last().Match(ILCode.Ret) && ((ILExpression)method.Body.Last()).Arguments.Count == 0) {
method.Body.RemoveAt(method.Body.Count - 1); method.Body.RemoveAt(method.Body.Count - 1);
} }
// Remove unreachable return statements
bool modified = false;
foreach(ILBlock block in method.GetSelfAndChildrenRecursive<ILBlock>()) {
for (int i = 0; i < block.Body.Count - 1;) {
if (block.Body[i].IsUnconditionalControlFlow() && block.Body[i+1].Match(ILCode.Ret)) {
modified = true;
block.Body.RemoveAt(i+1);
} else {
i++;
}
}
}
if (modified) {
// More removals might be possible
new GotoRemoval().RemoveGotos(method);
}
} }
IEnumerable<ILNode> GetParents(ILNode node) IEnumerable<ILNode> GetParents(ILNode node)

4
ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs

@ -56,6 +56,7 @@ namespace ICSharpCode.Decompiler.ILAst
RemoveRedundantCode2, RemoveRedundantCode2,
GotoRemoval, GotoRemoval,
DuplicateReturns, DuplicateReturns,
GotoRemoval2,
ReduceIfNesting, ReduceIfNesting,
InlineVariables3, InlineVariables3,
CachedDelegateInitialization, CachedDelegateInitialization,
@ -182,6 +183,9 @@ namespace ICSharpCode.Decompiler.ILAst
if (abortBeforeStep == ILAstOptimizationStep.DuplicateReturns) return; if (abortBeforeStep == ILAstOptimizationStep.DuplicateReturns) return;
DuplicateReturnStatements(method); DuplicateReturnStatements(method);
if (abortBeforeStep == ILAstOptimizationStep.GotoRemoval2) return;
new GotoRemoval().RemoveGotos(method);
if (abortBeforeStep == ILAstOptimizationStep.ReduceIfNesting) return; if (abortBeforeStep == ILAstOptimizationStep.ReduceIfNesting) return;
ReduceIfNesting(method); ReduceIfNesting(method);

Loading…
Cancel
Save