From 2bcafb8c4f3d7d7ae4b8bcb53123332c139a3a71 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Tue, 7 Aug 2018 17:46:41 +0200 Subject: [PATCH] Fix #1247: Empty destructors are not decompiled correctly --- .../Transforms/PatternStatementTransform.cs | 35 ++++++++++++++----- .../Implementation/MetadataMethod.cs | 2 +- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs b/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs index f2efa6d3c..e7f0bfe3c 100644 --- a/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs +++ b/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs @@ -121,7 +121,12 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms { return TransformDestructor(methodDeclaration) ?? base.VisitMethodDeclaration(methodDeclaration); } - + + public override AstNode VisitDestructorDeclaration(DestructorDeclaration destructorDeclaration) + { + return TransformDestructorBody(destructorDeclaration) ?? base.VisitDestructorDeclaration(destructorDeclaration); + } + public override AstNode VisitTryCatchStatement(TryCatchStatement tryCatchStatement) { return TransformTryCatchFinally(tryCatchStatement) ?? base.VisitTryCatchStatement(tryCatchStatement); @@ -812,16 +817,18 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms Modifiers = Modifiers.Any, ReturnType = new PrimitiveType("void"), Name = "Finalize", - Body = new BlockStatement { - new TryCatchStatement { - TryBlock = new AnyNode("body"), - FinallyBlock = new BlockStatement { - new InvocationExpression(new MemberReferenceExpression(new BaseReferenceExpression(), "Finalize")) - } + Body = destructorBodyPattern + }; + + static readonly BlockStatement destructorBodyPattern = new BlockStatement { + new TryCatchStatement { + TryBlock = new AnyNode("body"), + FinallyBlock = new BlockStatement { + new InvocationExpression(new MemberReferenceExpression(new BaseReferenceExpression(), "Finalize")) } } }; - + DestructorDeclaration TransformDestructor(MethodDeclaration methodDef) { Match m = destructorPattern.Match(methodDef); @@ -837,8 +844,18 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms } return null; } + + DestructorDeclaration TransformDestructorBody(DestructorDeclaration dtorDef) + { + Match m = destructorBodyPattern.Match(dtorDef.Body); + if (m.Success) { + dtorDef.Body = m.Get("body").Single().Detach(); + return dtorDef; + } + return null; + } #endregion - + #region Try-Catch-Finally static readonly TryCatchStatement tryCatchFinallyPattern = new TryCatchStatement { TryBlock = new BlockStatement { diff --git a/ICSharpCode.Decompiler/TypeSystem/Implementation/MetadataMethod.cs b/ICSharpCode.Decompiler/TypeSystem/Implementation/MetadataMethod.cs index 1fa81dbdd..3b5571a10 100644 --- a/ICSharpCode.Decompiler/TypeSystem/Implementation/MetadataMethod.cs +++ b/ICSharpCode.Decompiler/TypeSystem/Implementation/MetadataMethod.cs @@ -72,7 +72,7 @@ namespace ICSharpCode.Decompiler.TypeSystem.Implementation this.symbolKind = SymbolKind.Operator; } else if ((attributes & finalizerAttributes) == finalizerAttributes) { string name = this.Name; - if (name == "Finalize") { + if (name == "Finalize" && Parameters.Count == 0) { this.symbolKind = SymbolKind.Destructor; } }