From 3c1400d605f4c3e5165aab41d0ab8bf2ca1ba235 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Wed, 9 Mar 2011 18:36:31 +0100 Subject: [PATCH] Fixed bug in DeclareVariableInSmallestScope --- ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs | 5 ++++- .../Ast/DeclareVariableInSmallestScope.cs | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs b/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs index 802cb2040..d1972056d 100644 --- a/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs +++ b/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs @@ -22,6 +22,7 @@ namespace ICSharpCode.Decompiler.Ast TypeSystem typeSystem; DecompilerContext context; HashSet localVariablesToDefine = new HashSet(); // local variables that are missing a definition + HashSet implicitlyDefinedVariables = new HashSet(); // local variables that are implicitly defined (e.g. catch handler) public static BlockStatement CreateMethodBody(MethodDefinition methodDef, DecompilerContext context) { @@ -69,7 +70,7 @@ namespace ICSharpCode.Decompiler.Ast context.CancellationToken.ThrowIfCancellationRequested(); Ast.BlockStatement astBlock = TransformBlock(ilMethod); CommentStatement.ReplaceAll(astBlock); // convert CommentStatements to Comments - foreach (ILVariable v in localVariablesToDefine) { + foreach (ILVariable v in localVariablesToDefine.Except(implicitlyDefinedVariables)) { DeclareVariableInSmallestScope.DeclareVariable(astBlock, AstBuilder.ConvertType(v.Type), v.Name); } @@ -138,6 +139,8 @@ namespace ICSharpCode.Decompiler.Ast var tryCatchStmt = new Ast.TryCatchStatement(); tryCatchStmt.TryBlock = TransformBlock(tryCatchNode.TryBlock); foreach (var catchClause in tryCatchNode.CatchBlocks) { + if (catchClause.ExceptionVariable != null) + implicitlyDefinedVariables.Add(catchClause.ExceptionVariable); tryCatchStmt.CatchClauses.Add( new Ast.CatchClause { Type = AstBuilder.ConvertType(catchClause.ExceptionType), diff --git a/ICSharpCode.Decompiler/Ast/DeclareVariableInSmallestScope.cs b/ICSharpCode.Decompiler/Ast/DeclareVariableInSmallestScope.cs index 1bc37a624..36d319fa8 100644 --- a/ICSharpCode.Decompiler/Ast/DeclareVariableInSmallestScope.cs +++ b/ICSharpCode.Decompiler/Ast/DeclareVariableInSmallestScope.cs @@ -47,13 +47,13 @@ namespace ICSharpCode.Decompiler.Ast static AstNode FindInsertPos(AstNode node, string name, bool allowPassIntoLoops) { - IdentifierExpression ident = node as IdentifierExpression; - if (ident != null && ident.Identifier == name && ident.TypeArguments.Count == 0) - return node; - AstNode pos = null; AstNode withinPos = null; while (node != null) { + IdentifierExpression ident = node as IdentifierExpression; + if (ident != null && ident.Identifier == name && ident.TypeArguments.Count == 0) + return node; + AstNode withinCurrent = FindInsertPos(node.FirstChild, name, allowPassIntoLoops); if (withinCurrent != null) { if (pos == null) {