Browse Source

Fix foreach variable naming in case of parameter in-expression. Add more tests.

pull/946/head
Siegfried Pammer 8 years ago
parent
commit
11a9d901e2
  1. 9
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.cs
  2. 62
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.il
  3. 49
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.opt.il
  4. 58
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.opt.roslyn.il
  5. 68
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.roslyn.il
  6. 5
      ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs

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

@ -252,6 +252,8 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -252,6 +252,8 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
}
}
private IEnumerable<string> alternatives;
private static void Operation(ref int item)
{
}
@ -260,6 +262,13 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -260,6 +262,13 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
}
public void ForEachOnField()
{
foreach (string alternative in this.alternatives) {
alternative.ToLower();
}
}
public void ForEach(IEnumerable<string> alternatives)
{
foreach (string alternative in alternatives) {

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

@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0
}
.assembly y1gx1hfa
.assembly ic2bztjj
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx
@ -20,15 +20,15 @@ @@ -20,15 +20,15 @@
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module y1gx1hfa.dll
// MVID: {21EB73BD-6DA1-4305-9961-7F543EB0E1F3}
.module ic2bztjj.dll
// MVID: {821665CB-F67F-4600-8C6C-27617D873D36}
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x02A00000
// Image base: 0x01550000
// =============== CLASS MEMBERS DECLARATION ===================
@ -618,6 +618,7 @@ @@ -618,6 +618,7 @@
} // end of class '<>c__DisplayClass1'
.field private class [mscorlib]System.Collections.Generic.IEnumerable`1<string> alternatives
.method private hidebysig static void Operation(int32& item) cil managed
{
// Code size 2 (0x2)
@ -634,6 +635,59 @@ @@ -634,6 +635,59 @@
IL_0001: ret
} // end of method Loops::Operation
.method public hidebysig instance void
ForEachOnField() cil managed
{
// Code size 62 (0x3e)
.maxstack 2
.locals init (string V_0,
class [mscorlib]System.Collections.Generic.IEnumerator`1<string> V_1,
bool V_2)
IL_0000: nop
IL_0001: nop
IL_0002: ldarg.0
IL_0003: ldfld class [mscorlib]System.Collections.Generic.IEnumerable`1<string> ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops::alternatives
IL_0008: callvirt instance class [mscorlib]System.Collections.Generic.IEnumerator`1<!0> class [mscorlib]System.Collections.Generic.IEnumerable`1<string>::GetEnumerator()
IL_000d: stloc.1
.try
{
IL_000e: br.s IL_0020
IL_0010: ldloc.1
IL_0011: callvirt instance !0 class [mscorlib]System.Collections.Generic.IEnumerator`1<string>::get_Current()
IL_0016: stloc.0
IL_0017: nop
IL_0018: ldloc.0
IL_0019: callvirt instance string [mscorlib]System.String::ToLower()
IL_001e: pop
IL_001f: nop
IL_0020: ldloc.1
IL_0021: callvirt instance bool [mscorlib]System.Collections.IEnumerator::MoveNext()
IL_0026: stloc.2
IL_0027: ldloc.2
IL_0028: brtrue.s IL_0010
IL_002a: leave.s IL_003c
} // end .try
finally
{
IL_002c: ldloc.1
IL_002d: ldnull
IL_002e: ceq
IL_0030: stloc.2
IL_0031: ldloc.2
IL_0032: brtrue.s IL_003b
IL_0034: ldloc.1
IL_0035: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_003a: nop
IL_003b: endfinally
} // end handler
IL_003c: nop
IL_003d: ret
} // end of method Loops::ForEachOnField
.method public hidebysig instance void
ForEach(class [mscorlib]System.Collections.Generic.IEnumerable`1<string> alternatives) cil managed
{

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

@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0
}
.assembly de0u0jkz
.assembly fabjeeha
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx
@ -20,15 +20,15 @@ @@ -20,15 +20,15 @@
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module de0u0jkz.dll
// MVID: {F3F9EFD3-F9A8-4885-B2D2-AD75367C0339}
.module fabjeeha.dll
// MVID: {4FB3AE1F-ECE0-4E7D-84DC-E693282CA0AF}
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x01A60000
// Image base: 0x00D90000
// =============== CLASS MEMBERS DECLARATION ===================
@ -535,6 +535,7 @@ @@ -535,6 +535,7 @@
} // end of class '<>c__DisplayClass1'
.field private class [mscorlib]System.Collections.Generic.IEnumerable`1<string> alternatives
.method private hidebysig static void Operation(int32& item) cil managed
{
// Code size 1 (0x1)
@ -549,6 +550,46 @@ @@ -549,6 +550,46 @@
IL_0000: ret
} // end of method Loops::Operation
.method public hidebysig instance void
ForEachOnField() cil managed
{
// Code size 49 (0x31)
.maxstack 1
.locals init (string V_0,
class [mscorlib]System.Collections.Generic.IEnumerator`1<string> V_1)
IL_0000: ldarg.0
IL_0001: ldfld class [mscorlib]System.Collections.Generic.IEnumerable`1<string> ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops::alternatives
IL_0006: callvirt instance class [mscorlib]System.Collections.Generic.IEnumerator`1<!0> class [mscorlib]System.Collections.Generic.IEnumerable`1<string>::GetEnumerator()
IL_000b: stloc.1
.try
{
IL_000c: br.s IL_001c
IL_000e: ldloc.1
IL_000f: callvirt instance !0 class [mscorlib]System.Collections.Generic.IEnumerator`1<string>::get_Current()
IL_0014: stloc.0
IL_0015: ldloc.0
IL_0016: callvirt instance string [mscorlib]System.String::ToLower()
IL_001b: pop
IL_001c: ldloc.1
IL_001d: callvirt instance bool [mscorlib]System.Collections.IEnumerator::MoveNext()
IL_0022: brtrue.s IL_000e
IL_0024: leave.s IL_0030
} // end .try
finally
{
IL_0026: ldloc.1
IL_0027: brfalse.s IL_002f
IL_0029: ldloc.1
IL_002a: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_002f: endfinally
} // end handler
IL_0030: ret
} // end of method Loops::ForEachOnField
.method public hidebysig instance void
ForEach(class [mscorlib]System.Collections.Generic.IEnumerable`1<string> alternatives) cil managed
{

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

@ -25,14 +25,14 @@ @@ -25,14 +25,14 @@
.ver 0:0:0:0
}
.module Loops.dll
// MVID: {13449EE0-18A5-44A4-B482-40B5C738DFBB}
// MVID: {F03B196B-9B00-49BE-A335-2D460586E39A}
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x00D70000
// Image base: 0x02D60000
// =============== CLASS MEMBERS DECLARATION ===================
@ -510,7 +510,7 @@ @@ -510,7 +510,7 @@
} // end of property CustomStructEnumeratorWithIDisposable`1::Current
} // end of class CustomStructEnumeratorWithIDisposable`1
.class auto ansi sealed nested private beforefieldinit '<>c__DisplayClass23_0'
.class auto ansi sealed nested private beforefieldinit '<>c__DisplayClass25_0'
extends [mscorlib]System.Object
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
@ -523,7 +523,7 @@ @@ -523,7 +523,7 @@
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method '<>c__DisplayClass23_0'::.ctor
} // end of method '<>c__DisplayClass25_0'::.ctor
.method assembly hidebysig instance bool
'<ForeachWithCapturedVariable>b__0'() cil managed
@ -531,14 +531,15 @@ @@ -531,14 +531,15 @@
// Code size 10 (0xa)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass23_0'::c
IL_0001: ldfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass25_0'::c
IL_0006: ldc.i4.5
IL_0007: ceq
IL_0009: ret
} // end of method '<>c__DisplayClass23_0'::'<ForeachWithCapturedVariable>b__0'
} // end of method '<>c__DisplayClass25_0'::'<ForeachWithCapturedVariable>b__0'
} // end of class '<>c__DisplayClass23_0'
} // end of class '<>c__DisplayClass25_0'
.field private class [mscorlib]System.Collections.Generic.IEnumerable`1<string> alternatives
.method private hidebysig static void Operation(int32& item) cil managed
{
// Code size 1 (0x1)
@ -553,6 +554,43 @@ @@ -553,6 +554,43 @@
IL_0000: ret
} // end of method Loops::Operation
.method public hidebysig instance void
ForEachOnField() cil managed
{
// Code size 47 (0x2f)
.maxstack 1
.locals init (class [mscorlib]System.Collections.Generic.IEnumerator`1<string> V_0)
IL_0000: ldarg.0
IL_0001: ldfld class [mscorlib]System.Collections.Generic.IEnumerable`1<string> ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops::alternatives
IL_0006: callvirt instance class [mscorlib]System.Collections.Generic.IEnumerator`1<!0> class [mscorlib]System.Collections.Generic.IEnumerable`1<string>::GetEnumerator()
IL_000b: stloc.0
.try
{
IL_000c: br.s IL_001a
IL_000e: ldloc.0
IL_000f: callvirt instance !0 class [mscorlib]System.Collections.Generic.IEnumerator`1<string>::get_Current()
IL_0014: callvirt instance string [mscorlib]System.String::ToLower()
IL_0019: pop
IL_001a: ldloc.0
IL_001b: callvirt instance bool [mscorlib]System.Collections.IEnumerator::MoveNext()
IL_0020: brtrue.s IL_000e
IL_0022: leave.s IL_002e
} // end .try
finally
{
IL_0024: ldloc.0
IL_0025: brfalse.s IL_002d
IL_0027: ldloc.0
IL_0028: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_002d: endfinally
} // end handler
IL_002e: ret
} // end of method Loops::ForEachOnField
.method public hidebysig instance void
ForEach(class [mscorlib]System.Collections.Generic.IEnumerable`1<string> alternatives) cil managed
{
@ -1063,11 +1101,11 @@ @@ -1063,11 +1101,11 @@
IL_0009: ldloca.s V_0
IL_000b: call instance !0 valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<int32>::get_Current()
IL_0010: stloc.1
IL_0011: newobj instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass23_0'::.ctor()
IL_0011: newobj instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass25_0'::.ctor()
IL_0016: dup
IL_0017: ldloc.1
IL_0018: stfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass23_0'::c
IL_001d: ldftn instance bool ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass23_0'::'<ForeachWithCapturedVariable>b__0'()
IL_0018: stfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass25_0'::c
IL_001d: ldftn instance bool ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass25_0'::'<ForeachWithCapturedVariable>b__0'()
IL_0023: newobj instance void class [mscorlib]System.Func`1<bool>::.ctor(object,
native int)
IL_0028: call void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops::Operation(class [mscorlib]System.Func`1<bool>)

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

@ -25,14 +25,14 @@ @@ -25,14 +25,14 @@
.ver 0:0:0:0
}
.module Loops.dll
// MVID: {747E198F-8825-4B5B-9C27-DF069E6F35AE}
// MVID: {F66BA703-8DC0-4A56-81BC-D288A1D83D30}
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x027A0000
// Image base: 0x02D60000
// =============== CLASS MEMBERS DECLARATION ===================
@ -592,7 +592,7 @@ @@ -592,7 +592,7 @@
} // end of property CustomStructEnumeratorWithIDisposable`1::Current
} // end of class CustomStructEnumeratorWithIDisposable`1
.class auto ansi sealed nested private beforefieldinit '<>c__DisplayClass23_0'
.class auto ansi sealed nested private beforefieldinit '<>c__DisplayClass25_0'
extends [mscorlib]System.Object
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
@ -606,7 +606,7 @@ @@ -606,7 +606,7 @@
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: nop
IL_0007: ret
} // end of method '<>c__DisplayClass23_0'::.ctor
} // end of method '<>c__DisplayClass25_0'::.ctor
.method assembly hidebysig instance bool
'<ForeachWithCapturedVariable>b__0'() cil managed
@ -614,14 +614,15 @@ @@ -614,14 +614,15 @@
// Code size 10 (0xa)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass23_0'::c
IL_0001: ldfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass25_0'::c
IL_0006: ldc.i4.5
IL_0007: ceq
IL_0009: ret
} // end of method '<>c__DisplayClass23_0'::'<ForeachWithCapturedVariable>b__0'
} // end of method '<>c__DisplayClass25_0'::'<ForeachWithCapturedVariable>b__0'
} // end of class '<>c__DisplayClass23_0'
} // end of class '<>c__DisplayClass25_0'
.field private class [mscorlib]System.Collections.Generic.IEnumerable`1<string> alternatives
.method private hidebysig static void Operation(int32& item) cil managed
{
// Code size 2 (0x2)
@ -638,6 +639,51 @@ @@ -638,6 +639,51 @@
IL_0001: ret
} // end of method Loops::Operation
.method public hidebysig instance void
ForEachOnField() cil managed
{
// Code size 54 (0x36)
.maxstack 1
.locals init (class [mscorlib]System.Collections.Generic.IEnumerator`1<string> V_0,
string V_1)
IL_0000: nop
IL_0001: nop
IL_0002: ldarg.0
IL_0003: ldfld class [mscorlib]System.Collections.Generic.IEnumerable`1<string> ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops::alternatives
IL_0008: callvirt instance class [mscorlib]System.Collections.Generic.IEnumerator`1<!0> class [mscorlib]System.Collections.Generic.IEnumerable`1<string>::GetEnumerator()
IL_000d: stloc.0
.try
{
IL_000e: br.s IL_0020
IL_0010: ldloc.0
IL_0011: callvirt instance !0 class [mscorlib]System.Collections.Generic.IEnumerator`1<string>::get_Current()
IL_0016: stloc.1
IL_0017: nop
IL_0018: ldloc.1
IL_0019: callvirt instance string [mscorlib]System.String::ToLower()
IL_001e: pop
IL_001f: nop
IL_0020: ldloc.0
IL_0021: callvirt instance bool [mscorlib]System.Collections.IEnumerator::MoveNext()
IL_0026: brtrue.s IL_0010
IL_0028: leave.s IL_0035
} // end .try
finally
{
IL_002a: ldloc.0
IL_002b: brfalse.s IL_0034
IL_002d: ldloc.0
IL_002e: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_0033: nop
IL_0034: endfinally
} // end handler
IL_0035: ret
} // end of method Loops::ForEachOnField
.method public hidebysig instance void
ForEach(class [mscorlib]System.Collections.Generic.IEnumerable`1<string> alternatives) cil managed
{
@ -1259,7 +1305,7 @@ @@ -1259,7 +1305,7 @@
.maxstack 2
.locals init (valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<int32> V_0,
int32 V_1,
class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass23_0' V_2)
class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass25_0' V_2)
IL_0000: nop
IL_0001: nop
IL_0002: ldarg.0
@ -1272,14 +1318,14 @@ @@ -1272,14 +1318,14 @@
IL_000b: ldloca.s V_0
IL_000d: call instance !0 valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<int32>::get_Current()
IL_0012: stloc.1
IL_0013: newobj instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass23_0'::.ctor()
IL_0013: newobj instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass25_0'::.ctor()
IL_0018: stloc.2
IL_0019: nop
IL_001a: ldloc.2
IL_001b: ldloc.1
IL_001c: stfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass23_0'::c
IL_001c: stfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass25_0'::c
IL_0021: ldloc.2
IL_0022: ldftn instance bool ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass23_0'::'<ForeachWithCapturedVariable>b__0'()
IL_0022: ldftn instance bool ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops/'<>c__DisplayClass25_0'::'<ForeachWithCapturedVariable>b__0'()
IL_0028: newobj instance void class [mscorlib]System.Func`1<bool>::.ctor(object,
native int)
IL_002d: call void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Loops::Operation(class [mscorlib]System.Func`1<bool>)

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

@ -362,6 +362,11 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -362,6 +362,11 @@ namespace ICSharpCode.Decompiler.IL.Transforms
AddExistingName(reservedVariableNames, f);
string baseName = GetNameFromInstruction(valueContext);
if (string.IsNullOrEmpty(baseName)) {
if (valueContext is LdLoc ldloc && ldloc.Variable.Kind == VariableKind.Parameter) {
baseName = ldloc.Variable.Name;
}
}
string proposedName = "item";
if (!string.IsNullOrEmpty(baseName)) {

Loading…
Cancel
Save