Browse Source

Transform 'try { try {} catch {} } finally {}' to 'try {} catch {} finally {}'. Closes #77.

pull/100/head
Daniel Grunwald 15 years ago
parent
commit
bf6e4bbfd9
  1. 29
      ICSharpCode.Decompiler/Ast/Transforms/PatternStatementTransform.cs
  2. 24
      ICSharpCode.Decompiler/Tests/ExceptionHandling.cs

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

@ -40,6 +40,8 @@ namespace ICSharpCode.Decompiler.Ast.Transforms @@ -40,6 +40,8 @@ namespace ICSharpCode.Decompiler.Ast.Transforms
TransformAutomaticProperties(compilationUnit);
if (context.Settings.AutomaticEvents)
TransformAutomaticEvents(compilationUnit);
TransformTryCatchFinally(compilationUnit);
}
/// <summary>
@ -652,6 +654,33 @@ namespace ICSharpCode.Decompiler.Ast.Transforms @@ -652,6 +654,33 @@ namespace ICSharpCode.Decompiler.Ast.Transforms
}
#endregion
#region Try-Catch-Finally
static readonly TryCatchStatement tryCatchFinallyPattern = new TryCatchStatement {
TryBlock = new BlockStatement {
new TryCatchStatement {
TryBlock = new AnyNode(),
CatchClauses = { new Repeat(new AnyNode()) }
}
},
FinallyBlock = new AnyNode()
};
/// <summary>
/// Simplify nested 'try { try {} catch {} } finally {}'.
/// This transformation must run after the using/lock tranformations.
/// </summary>
void TransformTryCatchFinally(AstNode compilationUnit)
{
foreach (var tryFinally in compilationUnit.Descendants.OfType<TryCatchStatement>()) {
if (tryCatchFinallyPattern.Match(tryFinally) != null) {
TryCatchStatement tryCatch = (TryCatchStatement)tryFinally.TryBlock.Statements.Single();
tryFinally.TryBlock = tryCatch.TryBlock.Detach();
tryCatch.CatchClauses.MoveTo(tryFinally.CatchClauses);
}
}
}
#endregion
#region Pattern Matching Helpers
sealed class TypePattern : Pattern
{

24
ICSharpCode.Decompiler/Tests/ExceptionHandling.cs

@ -22,4 +22,28 @@ public class ExceptionHandling @@ -22,4 +22,28 @@ public class ExceptionHandling
throw;
}
}
public void TryCatchFinally()
{
try {
Console.WriteLine("Try");
} catch (Exception ex) {
Console.WriteLine(ex.Message);
} finally {
Console.WriteLine("Finally");
}
}
public void TryCatchMultipleHandlers()
{
try {
Console.WriteLine("Try");
} catch (InvalidOperationException ex) {
Console.WriteLine(ex.Message);
} catch (Exception ex) {
Console.WriteLine(ex.Message);
} catch {
Console.WriteLine("other");
}
}
}

Loading…
Cancel
Save