Browse Source

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

pull/234/merge
David Srbecký 14 years ago
parent
commit
6fd28e8b6e
  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 @@ -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) {
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)

4
ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs

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

Loading…
Cancel
Save