diff --git a/src/AstBuilder.cs b/src/AstBuilder.cs index a67b9af8b..6cb83d525 100644 --- a/src/AstBuilder.cs +++ b/src/AstBuilder.cs @@ -30,6 +30,7 @@ namespace Decompiler astCompileUnit.AcceptVisitor(new Transforms.Ast.RemoveEmptyElseBody(), null); astCompileUnit.AcceptVisitor(new Transforms.Ast.RestoreLoop(), null); astCompileUnit.AcceptVisitor(new Transforms.Ast.RemoveDeadLabels(), null); + astCompileUnit.AcceptVisitor(new Transforms.Ast.RemoveEmptyElseBody(), null); astCompileUnit.AcceptVisitor(csOutVisitor, null); diff --git a/src/Transforms/Ast/RemoveEmptyElseBody.cs b/src/Transforms/Ast/RemoveEmptyElseBody.cs index 9463c5afc..6321ff0e3 100644 --- a/src/Transforms/Ast/RemoveEmptyElseBody.cs +++ b/src/Transforms/Ast/RemoveEmptyElseBody.cs @@ -8,6 +8,19 @@ namespace Decompiler.Transforms.Ast { public class RemoveEmptyElseBody: AbstractAstTransformer { + public override object VisitBlockStatement(BlockStatement blockStatement, object data) + { + for(int i = 0; i < blockStatement.Children.Count; i++) { + if (blockStatement.Children[i] is Statement && + ((Statement)blockStatement.Children[i]).IsNull) + { + blockStatement.Children.RemoveAt(i); + i--; + } + } + return base.VisitBlockStatement(blockStatement, data); + } + public override object VisitIfElseStatement(IfElseStatement ifElseStatement, object data) { base.VisitIfElseStatement(ifElseStatement, data); diff --git a/src/Transforms/Ast/RestoreLoop.cs b/src/Transforms/Ast/RestoreLoop.cs index a9ef14c36..2bc9c7115 100644 --- a/src/Transforms/Ast/RestoreLoop.cs +++ b/src/Transforms/Ast/RestoreLoop.cs @@ -12,6 +12,18 @@ namespace Decompiler.Transforms.Ast { base.VisitForStatement(forStatement, data); + // Restore loop initializer + if (forStatement.Initializers.Count == 0) { + int myIndex = forStatement.Parent.Children.IndexOf(forStatement); + if (myIndex - 1 >= 0) { + LocalVariableDeclaration varDeclr = forStatement.Parent.Children[myIndex - 1] as LocalVariableDeclaration; + if (varDeclr != null) { + forStatement.Parent.Children[myIndex - 1] = Statement.Null; + forStatement.Initializers.Add(varDeclr); + } + } + } + // Restore loop condition if (forStatement.Condition.IsNull && forStatement.EmbeddedStatement.Children.Count >= 3)