Browse Source

Fix #1247: Empty destructors are not decompiled correctly

pull/1253/head
Siegfried Pammer 7 years ago
parent
commit
2bcafb8c4f
  1. 29
      ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs
  2. 2
      ICSharpCode.Decompiler/TypeSystem/Implementation/MetadataMethod.cs

29
ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs

@ -122,6 +122,11 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -122,6 +122,11 @@ 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,12 +817,14 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -812,12 +817,14 @@ 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"))
}
}
};
@ -837,6 +844,16 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -837,6 +844,16 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
}
return null;
}
DestructorDeclaration TransformDestructorBody(DestructorDeclaration dtorDef)
{
Match m = destructorBodyPattern.Match(dtorDef.Body);
if (m.Success) {
dtorDef.Body = m.Get<BlockStatement>("body").Single().Detach();
return dtorDef;
}
return null;
}
#endregion
#region Try-Catch-Finally

2
ICSharpCode.Decompiler/TypeSystem/Implementation/MetadataMethod.cs

@ -72,7 +72,7 @@ namespace ICSharpCode.Decompiler.TypeSystem.Implementation @@ -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;
}
}

Loading…
Cancel
Save