From aab0e43a0804b74e21834f78a281b99db55a5626 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Srbeck=C3=BD?= Date: Sat, 5 Mar 2011 20:12:55 +0000 Subject: [PATCH] Don't just fall out of case statements. --- ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs | 2 -- ICSharpCode.Decompiler/ILAst/GotoRemoval.cs | 10 ++++++---- ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs | 8 +++----- ICSharpCode.Decompiler/ILAst/ILAstTypes.cs | 3 --- 4 files changed, 9 insertions(+), 14 deletions(-) diff --git a/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs b/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs index b25fe1cb5..a143d8e73 100644 --- a/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs +++ b/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs @@ -130,8 +130,6 @@ namespace ICSharpCode.Decompiler.Ast switchStmt.SwitchSections.Add(section); } yield return switchStmt; - if (ilSwitch.DefaultGoto != null) - yield return (Statement)TransformExpression(ilSwitch.DefaultGoto); } else if (node is ILTryCatchBlock) { ILTryCatchBlock tryCatchNode = ((ILTryCatchBlock)node); var tryCatchStmt = new Ast.TryCatchStatement(); diff --git a/ICSharpCode.Decompiler/ILAst/GotoRemoval.cs b/ICSharpCode.Decompiler/ILAst/GotoRemoval.cs index 253d01b05..4b5c09cbe 100644 --- a/ICSharpCode.Decompiler/ILAst/GotoRemoval.cs +++ b/ICSharpCode.Decompiler/ILAst/GotoRemoval.cs @@ -76,6 +76,7 @@ namespace ICSharpCode.Decompiler.ILAst return true; } + // TODO: Swich also qualifies for break; ILWhileLoop loop = null; ILNode current = gotoExpr; while(loop == null && current != null) { @@ -184,13 +185,14 @@ namespace ICSharpCode.Decompiler.ILAst } } - if (nodeParent is ILCondition || - nodeParent is ILTryCatchBlock || - nodeParent is ILSwitch) - { + if (nodeParent is ILCondition || nodeParent is ILTryCatchBlock) { return Exit(nodeParent, visitedNodes); } + if (nodeParent is ILSwitch) { + return null; // Implicit exit from switch is not allowed + } + if (nodeParent is ILWhileLoop) { return Enter(nodeParent, visitedNodes); } diff --git a/ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs b/ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs index 9ddaa6688..36c0756c4 100644 --- a/ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs +++ b/ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs @@ -586,13 +586,11 @@ namespace ICSharpCode.Decompiler.ILAst // The labels will not be used - kill them condBranch.Operand = null; - ILSwitch ilSwitch = new ILSwitch() { - Condition = condBranch, - DefaultGoto = block.FallthoughGoto - }; + ILSwitch ilSwitch = new ILSwitch() { Condition = condBranch }; result.Add(new ILBasicBlock() { EntryLabel = block.EntryLabel, // Keep the entry label - Body = { ilSwitch } + Body = { ilSwitch }, + FallthoughGoto = block.FallthoughGoto }); // Remove the item so that it is not picked up as content diff --git a/ICSharpCode.Decompiler/ILAst/ILAstTypes.cs b/ICSharpCode.Decompiler/ILAst/ILAstTypes.cs index 6227fb92e..cc7a65e04 100644 --- a/ICSharpCode.Decompiler/ILAst/ILAstTypes.cs +++ b/ICSharpCode.Decompiler/ILAst/ILAstTypes.cs @@ -443,7 +443,6 @@ namespace ICSharpCode.Decompiler.ILAst { public ILExpression Condition; public List CaseBlocks = new List(); - public ILExpression DefaultGoto; public override IEnumerable GetChildren() { @@ -452,8 +451,6 @@ namespace ICSharpCode.Decompiler.ILAst foreach (ILBlock caseBlock in this.CaseBlocks) { yield return caseBlock; } - if (this.DefaultGoto != null) - yield return this.DefaultGoto; } public override void WriteTo(ITextOutput output)