Browse Source

Fix #538: Give higher priority to out parameters when naming variables

pull/1176/head
Siegfried Pammer 7 years ago
parent
commit
3d4e38ddce
  1. 4
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/AnonymousTypes.cs
  2. 10
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.cs
  3. 2
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.il
  4. 2
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.mcs.il
  5. 2
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.opt.il
  6. 2
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.opt.mcs.il
  7. 2
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.opt.roslyn.il
  8. 2
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.roslyn.il
  9. 4
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/PInvoke.cs
  10. 23
      ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs

4
ICSharpCode.Decompiler.Tests/TestCases/Pretty/AnonymousTypes.cs

@ -105,11 +105,11 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
#if CS70 #if CS70
private void AnonymousTypeOutVar() private void AnonymousTypeOutVar()
{ {
InlineVarDecl(out var anon, new { InlineVarDecl(out var v, new {
X = 1, X = 1,
Y = 2 Y = 2
}); });
Console.WriteLine(anon.X); Console.WriteLine(v.X);
} }
#endif #endif

10
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.cs

@ -280,7 +280,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
item = null; item = null;
} }
private static void Operation(ref int item) private static void Operation(ref int i)
{ {
} }
@ -417,11 +417,11 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
foreach (int item in items) { foreach (int item in items) {
#if ROSLYN && OPT #if ROSLYN && OPT
// The variable name differs based on whether roslyn optimizes out the 'item' variable // The variable name differs based on whether roslyn optimizes out the 'item' variable
int current = item; int i = item;
Operation(ref current); Operation(ref i);
#else #else
int num = item; int i = item;
Operation(ref num); Operation(ref i);
#endif #endif
} }
} }

2
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.il

@ -693,7 +693,7 @@
IL_0004: ret IL_0004: ret
} // end of method Loops::TryGetItem } // end of method Loops::TryGetItem
.method private hidebysig static void Operation(int32& item) cil managed .method private hidebysig static void Operation(int32& i) cil managed
{ {
// Code size 2 (0x2) // Code size 2 (0x2)
.maxstack 8 .maxstack 8

2
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.mcs.il

@ -653,7 +653,7 @@
IL_0003: ret IL_0003: ret
} // end of method Loops::TryGetItem } // end of method Loops::TryGetItem
.method private hidebysig static void Operation(int32& item) cil managed .method private hidebysig static void Operation(int32& i) cil managed
{ {
// Code size 1 (0x1) // Code size 1 (0x1)
.maxstack 8 .maxstack 8

2
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.opt.il

@ -603,7 +603,7 @@
IL_0003: ret IL_0003: ret
} // end of method Loops::TryGetItem } // end of method Loops::TryGetItem
.method private hidebysig static void Operation(int32& item) cil managed .method private hidebysig static void Operation(int32& i) cil managed
{ {
// Code size 1 (0x1) // Code size 1 (0x1)
.maxstack 8 .maxstack 8

2
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.opt.mcs.il

@ -653,7 +653,7 @@
IL_0003: ret IL_0003: ret
} // end of method Loops::TryGetItem } // end of method Loops::TryGetItem
.method private hidebysig static void Operation(int32& item) cil managed .method private hidebysig static void Operation(int32& i) cil managed
{ {
// Code size 1 (0x1) // Code size 1 (0x1)
.maxstack 8 .maxstack 8

2
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.opt.roslyn.il

@ -607,7 +607,7 @@
IL_0003: ret IL_0003: ret
} // end of method Loops::TryGetItem } // end of method Loops::TryGetItem
.method private hidebysig static void Operation(int32& item) cil managed .method private hidebysig static void Operation(int32& i) cil managed
{ {
// Code size 1 (0x1) // Code size 1 (0x1)
.maxstack 8 .maxstack 8

2
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.roslyn.il

@ -694,7 +694,7 @@
IL_0004: ret IL_0004: ret
} // end of method Loops::TryGetItem } // end of method Loops::TryGetItem
.method private hidebysig static void Operation(int32& item) cil managed .method private hidebysig static void Operation(int32& i) cil managed
{ {
// Code size 2 (0x2) // Code size 2 (0x2)
.maxstack 8 .maxstack 8

4
ICSharpCode.Decompiler.Tests/TestCases/Pretty/PInvoke.cs

@ -92,8 +92,8 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
public void CallMethodWithInOutParameter() public void CallMethodWithInOutParameter()
{ {
int num = 0; int argp = 0;
ioctlsocket(IntPtr.Zero, 0, ref num); ioctlsocket(IntPtr.Zero, 0, ref argp);
} }
} }
} }

23
ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs

@ -259,6 +259,15 @@ namespace ICSharpCode.Decompiler.IL.Transforms
} }
} }
} }
if (string.IsNullOrEmpty(proposedName)) {
var proposedNameForAddress = variable.AddressInstructions.OfType<LdLoca>()
.Select(arg => arg.Parent is CallInstruction c ? GetNameFromParameter(c.GetParameter(arg.ChildIndex)) : null)
.Where(arg => !string.IsNullOrWhiteSpace(arg))
.Except(currentFieldNames).ToList();
if (proposedNameForAddress.Count > 0) {
proposedName = proposedNameForAddress[0];
}
}
if (string.IsNullOrEmpty(proposedName)) { if (string.IsNullOrEmpty(proposedName)) {
var proposedNameForStores = variable.StoreInstructions.OfType<StLoc>() var proposedNameForStores = variable.StoreInstructions.OfType<StLoc>()
.Select(expr => GetNameFromInstruction(expr.Value)) .Select(expr => GetNameFromInstruction(expr.Value))
@ -301,6 +310,20 @@ namespace ICSharpCode.Decompiler.IL.Transforms
} }
} }
static readonly FullTypeName DllImportAttributeName = new TopLevelTypeName("System.Runtime.InteropServices", "DllImportAttribute");
static string GetNameFromParameter(IParameter param)
{
if (param == null || !(param.Owner is IMethod method))
return string.Empty;
string name = param.Name;
// Special case for DllImports:
if (name.Length > 1 && name.StartsWith("p", StringComparison.Ordinal) && method.GetAttribute(DllImportAttributeName) != null) {
return name.Substring(1);
}
return name;
}
static string GetNameFromInstruction(ILInstruction inst) static string GetNameFromInstruction(ILInstruction inst)
{ {
switch (inst) { switch (inst) {

Loading…
Cancel
Save