Browse Source

Inlining: remove the arg_-Variable in catch blocks.

pull/143/merge
Daniel Grunwald 15 years ago
parent
commit
229218174f
  1. 2
      ICSharpCode.Decompiler/Ast/NameVariables.cs
  2. 7
      ICSharpCode.Decompiler/Disassembler/DisassemblerHelpers.cs
  3. 4
      ICSharpCode.Decompiler/ILAst/ILAstTypes.cs
  4. 32
      ICSharpCode.Decompiler/ILAst/ILInlining.cs
  5. 4
      ICSharpCode.Decompiler/Tests/ExceptionHandling.cs
  6. 2
      ICSharpCode.Decompiler/Tests/TestRunner.cs

2
ICSharpCode.Decompiler/Ast/NameVariables.cs

@ -279,6 +279,8 @@ namespace ICSharpCode.Decompiler.Ast
name = "array"; name = "array";
} else if (type.IsPointer || type.IsByReference) { } else if (type.IsPointer || type.IsByReference) {
name = "ptr"; name = "ptr";
} else if (type.Name.EndsWith("Exception", StringComparison.Ordinal)) {
name = "ex";
} else if (!typeNameToVariableNameDict.TryGetValue(type.FullName, out name)) { } else if (!typeNameToVariableNameDict.TryGetValue(type.FullName, out name)) {
name = type.Name; name = type.Name;
// remove the 'I' for interfaces // remove the 'I' for interfaces

7
ICSharpCode.Decompiler/Disassembler/DisassemblerHelpers.cs

@ -38,7 +38,7 @@ namespace ICSharpCode.Decompiler.Disassembler
/// </summary> /// </summary>
TypeName, TypeName,
/// <summary> /// <summary>
/// Name (even for built-in types) /// Name (but built-in types use keyword syntax)
/// </summary> /// </summary>
ShortTypeName ShortTypeName
} }
@ -275,7 +275,10 @@ namespace ICSharpCode.Decompiler.Disassembler
} else { } else {
string name = PrimitiveTypeName(type.FullName); string name = PrimitiveTypeName(type.FullName);
if (syntax == ILNameSyntax.ShortTypeName) { if (syntax == ILNameSyntax.ShortTypeName) {
writer.WriteReference(Escape(type.Name), type); if (name != null)
writer.Write(name);
else
writer.WriteReference(Escape(type.Name), type);
} else if ((syntax == ILNameSyntax.Signature || syntax == ILNameSyntax.SignatureNoNamedTypeParameters) && name != null) { } else if ((syntax == ILNameSyntax.Signature || syntax == ILNameSyntax.SignatureNoNamedTypeParameters) && name != null) {
writer.Write(name); writer.Write(name);
} else { } else {

4
ICSharpCode.Decompiler/ILAst/ILAstTypes.cs

@ -141,6 +141,10 @@ namespace ICSharpCode.Decompiler.ILAst
{ {
output.Write("catch "); output.Write("catch ");
output.WriteReference(ExceptionType.FullName, ExceptionType); output.WriteReference(ExceptionType.FullName, ExceptionType);
if (ExceptionVariable != null) {
output.Write(' ');
output.Write(ExceptionVariable.Name);
}
output.WriteLine(" {"); output.WriteLine(" {");
output.Indent(); output.Indent();
base.WriteTo(output); base.WriteTo(output);

32
ICSharpCode.Decompiler/ILAst/ILInlining.cs

@ -47,7 +47,13 @@ namespace ICSharpCode.Decompiler.ILAst
numLdloca.Clear(); numLdloca.Clear();
// Analyse the whole method // Analyse the whole method
foreach(ILExpression expr in method.GetSelfAndChildrenRecursive<ILExpression>()) { AnalyzeNode(method);
}
void AnalyzeNode(ILNode node)
{
ILExpression expr = node as ILExpression;
if (expr != null) {
ILVariable locVar = expr.Operand as ILVariable; ILVariable locVar = expr.Operand as ILVariable;
if (locVar != null) { if (locVar != null) {
if (expr.Code == ILCode.Stloc) { if (expr.Code == ILCode.Stloc) {
@ -60,6 +66,16 @@ namespace ICSharpCode.Decompiler.ILAst
throw new NotSupportedException(expr.Code.ToString()); throw new NotSupportedException(expr.Code.ToString());
} }
} }
foreach (ILExpression child in expr.Arguments)
AnalyzeNode(child);
} else {
var catchBlock = node as ILTryCatchBlock.CatchBlock;
if (catchBlock != null && catchBlock.ExceptionVariable != null) {
numStloc[catchBlock.ExceptionVariable] = numStloc.GetOrDefault(catchBlock.ExceptionVariable) + 1;
}
foreach (ILNode child in node.GetChildren())
AnalyzeNode(child);
} }
} }
@ -76,6 +92,20 @@ namespace ICSharpCode.Decompiler.ILAst
{ {
bool modified = false; bool modified = false;
List<ILNode> body = block.Body; List<ILNode> body = block.Body;
if (block is ILTryCatchBlock.CatchBlock && body.Count > 1) {
ILVariable v = ((ILTryCatchBlock.CatchBlock)block).ExceptionVariable;
if (v != null && v.IsGenerated) {
if (numLdloca.GetOrDefault(v) == 0 && numStloc.GetOrDefault(v) == 1 && numLdloc.GetOrDefault(v) == 1) {
ILVariable v2;
ILExpression ldException;
if (body[0].Match(ILCode.Stloc, out v2, out ldException) && ldException.MatchLdloc(v)) {
body.RemoveAt(0);
((ILTryCatchBlock.CatchBlock)block).ExceptionVariable = v2;
modified = true;
}
}
}
}
for(int i = 0; i < body.Count - 1;) { for(int i = 0; i < body.Count - 1;) {
ILVariable locVar; ILVariable locVar;
ILExpression expr; ILExpression expr;

4
ICSharpCode.Decompiler/Tests/ExceptionHandling.cs

@ -56,9 +56,9 @@ public class ExceptionHandling
{ {
Console.WriteLine(ex.Message); Console.WriteLine(ex.Message);
} }
catch (Exception ex) catch (Exception ex2)
{ {
Console.WriteLine(ex.Message); Console.WriteLine(ex2.Message);
} }
catch catch
{ {

2
ICSharpCode.Decompiler/Tests/TestRunner.cs

@ -37,7 +37,7 @@ namespace ICSharpCode.Decompiler.Tests
TestFile(@"..\..\Tests\DelegateConstruction.cs"); TestFile(@"..\..\Tests\DelegateConstruction.cs");
} }
[Test, Ignore("arg-Variables in catch clauses")] [Test]
public void ExceptionHandling() public void ExceptionHandling()
{ {
TestFile(@"..\..\Tests\ExceptionHandling.cs"); TestFile(@"..\..\Tests\ExceptionHandling.cs");

Loading…
Cancel
Save