Browse Source

Inlining: remove the arg_-Variable in catch blocks.

pull/143/merge
Daniel Grunwald 14 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 @@ -279,6 +279,8 @@ namespace ICSharpCode.Decompiler.Ast
name = "array";
} else if (type.IsPointer || type.IsByReference) {
name = "ptr";
} else if (type.Name.EndsWith("Exception", StringComparison.Ordinal)) {
name = "ex";
} else if (!typeNameToVariableNameDict.TryGetValue(type.FullName, out name)) {
name = type.Name;
// remove the 'I' for interfaces

7
ICSharpCode.Decompiler/Disassembler/DisassemblerHelpers.cs

@ -38,7 +38,7 @@ namespace ICSharpCode.Decompiler.Disassembler @@ -38,7 +38,7 @@ namespace ICSharpCode.Decompiler.Disassembler
/// </summary>
TypeName,
/// <summary>
/// Name (even for built-in types)
/// Name (but built-in types use keyword syntax)
/// </summary>
ShortTypeName
}
@ -275,7 +275,10 @@ namespace ICSharpCode.Decompiler.Disassembler @@ -275,7 +275,10 @@ namespace ICSharpCode.Decompiler.Disassembler
} else {
string name = PrimitiveTypeName(type.FullName);
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) {
writer.Write(name);
} else {

4
ICSharpCode.Decompiler/ILAst/ILAstTypes.cs

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

32
ICSharpCode.Decompiler/ILAst/ILInlining.cs

@ -47,7 +47,13 @@ namespace ICSharpCode.Decompiler.ILAst @@ -47,7 +47,13 @@ namespace ICSharpCode.Decompiler.ILAst
numLdloca.Clear();
// 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;
if (locVar != null) {
if (expr.Code == ILCode.Stloc) {
@ -60,6 +66,16 @@ namespace ICSharpCode.Decompiler.ILAst @@ -60,6 +66,16 @@ namespace ICSharpCode.Decompiler.ILAst
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 @@ -76,6 +92,20 @@ namespace ICSharpCode.Decompiler.ILAst
{
bool modified = false;
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;) {
ILVariable locVar;
ILExpression expr;

4
ICSharpCode.Decompiler/Tests/ExceptionHandling.cs

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

2
ICSharpCode.Decompiler/Tests/TestRunner.cs

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

Loading…
Cancel
Save