Browse Source

Fixed return duplication in the case when the last return statement was already removed.

pull/70/head
David Srbecký 15 years ago
parent
commit
de6d657f3f
  1. 2
      ICSharpCode.Decompiler/ILAst/GotoRemoval.cs
  2. 27
      ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs
  3. 2
      ICSharpCode.Decompiler/ILAst/ILCodes.cs

2
ICSharpCode.Decompiler/ILAst/GotoRemoval.cs

@ -64,7 +64,7 @@ namespace ICSharpCode.Decompiler.ILAst
int count = ilCase.Body.Count; int count = ilCase.Body.Count;
if (count >= 2) { if (count >= 2) {
if (!ilCase.Body[count - 2].CanFallthough() && if (!ilCase.Body[count - 2].CanFallThough() &&
ilCase.Body[count - 1].Match(ILCode.LoopOrSwitchBreak)) { ilCase.Body[count - 1].Match(ILCode.LoopOrSwitchBreak)) {
ilCase.Body.RemoveAt(count - 1); ilCase.Body.RemoveAt(count - 1);
} }

27
ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs

@ -350,9 +350,8 @@ namespace ICSharpCode.Decompiler.ILAst
// Inline return statement // Inline return statement
ILNode target; ILNode target;
List<ILExpression> retArgs; List<ILExpression> retArgs;
if (nextSibling.TryGetValue(targetLabel, out target) && if (nextSibling.TryGetValue(targetLabel, out target)) {
target.Match(ILCode.Ret, out retArgs)) if (target.Match(ILCode.Ret, out retArgs)) {
{
ILVariable locVar; ILVariable locVar;
object constValue; object constValue;
if (retArgs.Count == 0) { if (retArgs.Count == 0) {
@ -363,6 +362,12 @@ namespace ICSharpCode.Decompiler.ILAst
block.Body[i] = new ILExpression(ILCode.Ret, null, new ILExpression(ILCode.Ldc_I4, constValue)); block.Body[i] = new ILExpression(ILCode.Ret, null, new ILExpression(ILCode.Ldc_I4, constValue));
} }
} }
} else {
if (method.Body.Count > 0 && method.Body.Last() == targetLabel) {
// It exits the main method - so it is same as return;
block.Body[i] = new ILExpression(ILCode.Ret, null);
}
}
} }
} }
} }
@ -796,8 +801,8 @@ namespace ICSharpCode.Decompiler.ILAst
for (int i = 0; i < block.Body.Count; i++) { for (int i = 0; i < block.Body.Count; i++) {
ILCondition cond = block.Body[i] as ILCondition; ILCondition cond = block.Body[i] as ILCondition;
if (cond != null) { if (cond != null) {
bool trueExits = cond.TrueBlock.Body.Count > 0 && !cond.TrueBlock.Body.Last().CanFallthough(); bool trueExits = cond.TrueBlock.Body.Count > 0 && !cond.TrueBlock.Body.Last().CanFallThough();
bool falseExits = cond.FalseBlock.Body.Count > 0 && !cond.FalseBlock.Body.Last().CanFallthough(); bool falseExits = cond.FalseBlock.Body.Count > 0 && !cond.FalseBlock.Body.Last().CanFallThough();
if (trueExits) { if (trueExits) {
// Move the false block after the condition // Move the false block after the condition
@ -899,19 +904,11 @@ namespace ICSharpCode.Decompiler.ILAst
return false; return false;
} }
public static bool CanFallthough(this ILNode node) public static bool CanFallThough(this ILNode node)
{ {
ILExpression expr = node as ILExpression; ILExpression expr = node as ILExpression;
if (expr != null) { if (expr != null) {
switch(expr.Code) { return expr.Code.CanFallThough();
case ILCode.Br:
case ILCode.Ret:
case ILCode.Throw:
case ILCode.Rethrow:
case ILCode.LoopContinue:
case ILCode.LoopOrSwitchBreak:
return false;
}
} }
return true; return true;
} }

2
ICSharpCode.Decompiler/ILAst/ILCodes.cs

@ -286,6 +286,8 @@ namespace ICSharpCode.Decompiler.ILAst
case ILCode.Endfinally: case ILCode.Endfinally:
case ILCode.Throw: case ILCode.Throw:
case ILCode.Rethrow: case ILCode.Rethrow:
case ILCode.LoopContinue:
case ILCode.LoopOrSwitchBreak:
return false; return false;
default: default:
return true; return true;

Loading…
Cancel
Save