From 8d9076365c5fb8c686f638a7560bd12f7c29e5ed Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Mon, 21 Feb 2011 00:14:32 +0100 Subject: [PATCH] Make using statement work if there are variables declared between the initializer and the try block. --- .../Ast/Transforms/UsingStatementTransform.cs | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/ICSharpCode.Decompiler/Ast/Transforms/UsingStatementTransform.cs b/ICSharpCode.Decompiler/Ast/Transforms/UsingStatementTransform.cs index 780a2ed18..7dbd55717 100644 --- a/ICSharpCode.Decompiler/Ast/Transforms/UsingStatementTransform.cs +++ b/ICSharpCode.Decompiler/Ast/Transforms/UsingStatementTransform.cs @@ -26,6 +26,12 @@ namespace Decompiler.Transforms ).ToVariable() } }; + static readonly AstNode simpleVariableDefinition = new VariableDeclarationStatement { + Type = new AnyNode().ToType(), + Variables = { + new VariableInitializer() // any name but no initializer + } + }; static readonly AstNode usingTryCatchPattern = new TryCatchStatement { TryBlock = new AnyNode("body").ToBlock(), FinallyBlock = new BlockStatement { @@ -58,7 +64,10 @@ namespace Decompiler.Transforms foreach (AstNode node in compilationUnit.Descendants.ToArray()) { Match m1 = usingVarDeclPattern.Match(node); if (m1 == null) continue; - Match m2 = usingTryCatchPattern.Match(node.NextSibling); + AstNode tryCatch = node.NextSibling; + while (simpleVariableDefinition.Match(tryCatch) != null) + tryCatch = tryCatch.NextSibling; + Match m2 = usingTryCatchPattern.Match(tryCatch); if (m2 == null) continue; if (m1.Get("variable").Single().Name == m2.Get("ident").Single().Identifier) { if (m2.Has("valueType")) { @@ -68,12 +77,10 @@ namespace Decompiler.Transforms continue; } BlockStatement body = m2.Get("body").Single(); - body.Remove(); - node.NextSibling.Remove(); - node.ReplaceWith( - varDecl => new UsingStatement { - ResourceAcquisition = varDecl, - EmbeddedStatement = body + tryCatch.ReplaceWith( + new UsingStatement { + ResourceAcquisition = node.Detach(), + EmbeddedStatement = body.Detach() }); } }