Browse Source

Fix user-defined op_Addition 'p += 1;' being turned into 'p++;'

pull/1143/head
Daniel Grunwald 7 years ago
parent
commit
f4bbd100b9
  1. 11
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.cs
  2. 36
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.il
  3. 33
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.opt.il
  4. 33
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.opt.roslyn.il
  5. 36
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.roslyn.il
  6. 2
      ICSharpCode.Decompiler/CSharp/Transforms/PrettifyAssignments.cs

11
ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.cs

@ -134,6 +134,10 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -134,6 +134,10 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
throw new NotImplementedException();
}
public static CustomClass operator +(CustomClass lhs, int rhs)
{
throw new NotImplementedException();
}
public static CustomClass operator -(CustomClass lhs, CustomClass rhs)
{
throw new NotImplementedException();
@ -4499,6 +4503,13 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -4499,6 +4503,13 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
}
#endregion
public static void AddOneToCustomClass(ref CustomClass c)
{
// This should not be turned into post-increment:
c += 1;
c.CustomClassProp += 1;
}
private static Item GetItem(object obj)
{
return null;

36
ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.il

@ -522,6 +522,18 @@ @@ -522,6 +522,18 @@
IL_0006: throw
} // end of method CustomClass::op_Addition
.method public hidebysig specialname static
class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass
op_Addition(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass lhs,
int32 rhs) cil managed
{
// Code size 7 (0x7)
.maxstack 8
IL_0000: nop
IL_0001: newobj instance void [mscorlib]System.NotImplementedException::.ctor()
IL_0006: throw
} // end of method CustomClass::op_Addition
.method public hidebysig specialname static
class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass
op_Subtraction(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass lhs,
@ -21354,6 +21366,30 @@ @@ -21354,6 +21366,30 @@
IL_018e: ret
} // end of method CompoundAssignmentTest::CustomStructPreDecTest
.method public hidebysig static void AddOneToCustomClass(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass& c) cil managed
{
// Code size 32 (0x20)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.0
IL_0002: dup
IL_0003: ldind.ref
IL_0004: ldc.i4.1
IL_0005: call class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass::op_Addition(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass,
int32)
IL_000a: stind.ref
IL_000b: ldarg.0
IL_000c: ldind.ref
IL_000d: dup
IL_000e: callvirt instance class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass::get_CustomClassProp()
IL_0013: ldc.i4.1
IL_0014: call class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass::op_Addition(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass,
int32)
IL_0019: callvirt instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass::set_CustomClassProp(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass)
IL_001e: nop
IL_001f: ret
} // end of method CompoundAssignmentTest::AddOneToCustomClass
.method private hidebysig static class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/Item
GetItem(object obj) cil managed
{

33
ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.opt.il

@ -454,6 +454,17 @@ @@ -454,6 +454,17 @@
IL_0005: throw
} // end of method CustomClass::op_Addition
.method public hidebysig specialname static
class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass
op_Addition(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass lhs,
int32 rhs) cil managed
{
// Code size 6 (0x6)
.maxstack 8
IL_0000: newobj instance void [mscorlib]System.NotImplementedException::.ctor()
IL_0005: throw
} // end of method CustomClass::op_Addition
.method public hidebysig specialname static
class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass
op_Subtraction(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass lhs,
@ -19660,6 +19671,28 @@ @@ -19660,6 +19671,28 @@
IL_0188: ret
} // end of method CompoundAssignmentTest::CustomStructPreDecTest
.method public hidebysig static void AddOneToCustomClass(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass& c) cil managed
{
// Code size 30 (0x1e)
.maxstack 8
IL_0000: ldarg.0
IL_0001: dup
IL_0002: ldind.ref
IL_0003: ldc.i4.1
IL_0004: call class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass::op_Addition(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass,
int32)
IL_0009: stind.ref
IL_000a: ldarg.0
IL_000b: ldind.ref
IL_000c: dup
IL_000d: callvirt instance class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass::get_CustomClassProp()
IL_0012: ldc.i4.1
IL_0013: call class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass::op_Addition(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass,
int32)
IL_0018: callvirt instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass::set_CustomClassProp(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass)
IL_001d: ret
} // end of method CompoundAssignmentTest::AddOneToCustomClass
.method private hidebysig static class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/Item
GetItem(object obj) cil managed
{

33
ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.opt.roslyn.il

@ -458,6 +458,17 @@ @@ -458,6 +458,17 @@
IL_0005: throw
} // end of method CustomClass::op_Addition
.method public hidebysig specialname static
class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass
op_Addition(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass lhs,
int32 rhs) cil managed
{
// Code size 6 (0x6)
.maxstack 8
IL_0000: newobj instance void [mscorlib]System.NotImplementedException::.ctor()
IL_0005: throw
} // end of method CustomClass::op_Addition
.method public hidebysig specialname static
class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass
op_Subtraction(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass lhs,
@ -22967,6 +22978,28 @@ @@ -22967,6 +22978,28 @@
IL_01de: ret
} // end of method CompoundAssignmentTest::CustomStructPreDecTest
.method public hidebysig static void AddOneToCustomClass(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass& c) cil managed
{
// Code size 30 (0x1e)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.0
IL_0002: ldind.ref
IL_0003: ldc.i4.1
IL_0004: call class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass::op_Addition(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass,
int32)
IL_0009: stind.ref
IL_000a: ldarg.0
IL_000b: ldind.ref
IL_000c: dup
IL_000d: callvirt instance class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass::get_CustomClassProp()
IL_0012: ldc.i4.1
IL_0013: call class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass::op_Addition(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass,
int32)
IL_0018: callvirt instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass::set_CustomClassProp(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass)
IL_001d: ret
} // end of method CompoundAssignmentTest::AddOneToCustomClass
.method private hidebysig static class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/Item
GetItem(object obj) cil managed
{

36
ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.roslyn.il

@ -480,6 +480,18 @@ @@ -480,6 +480,18 @@
IL_0006: throw
} // end of method CustomClass::op_Addition
.method public hidebysig specialname static
class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass
op_Addition(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass lhs,
int32 rhs) cil managed
{
// Code size 7 (0x7)
.maxstack 8
IL_0000: nop
IL_0001: newobj instance void [mscorlib]System.NotImplementedException::.ctor()
IL_0006: throw
} // end of method CustomClass::op_Addition
.method public hidebysig specialname static
class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass
op_Subtraction(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass lhs,
@ -25308,6 +25320,30 @@ @@ -25308,6 +25320,30 @@
IL_01f8: ret
} // end of method CompoundAssignmentTest::CustomStructPreDecTest
.method public hidebysig static void AddOneToCustomClass(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass& c) cil managed
{
// Code size 32 (0x20)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldarg.0
IL_0003: ldind.ref
IL_0004: ldc.i4.1
IL_0005: call class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass::op_Addition(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass,
int32)
IL_000a: stind.ref
IL_000b: ldarg.0
IL_000c: ldind.ref
IL_000d: dup
IL_000e: callvirt instance class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass::get_CustomClassProp()
IL_0013: ldc.i4.1
IL_0014: call class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass::op_Addition(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass,
int32)
IL_0019: callvirt instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass::set_CustomClassProp(class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/CustomClass)
IL_001e: nop
IL_001f: ret
} // end of method CompoundAssignmentTest::AddOneToCustomClass
.method private hidebysig static class ICSharpCode.Decompiler.Tests.TestCases.Pretty.CompoundAssignmentTest/Item
GetItem(object obj) cil managed
{

2
ICSharpCode.Decompiler/CSharp/Transforms/PrettifyAssignments.cs

@ -58,7 +58,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -58,7 +58,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
var rr = assignment.Right.GetResolveResult();
if (rr.IsCompileTimeConstant && rr.Type.IsCSharpPrimitiveIntegerType() && CSharpPrimitiveCast.Cast(rr.Type.GetTypeCode(), 1, false).Equals(rr.ConstantValue)) {
// only if it's not a custom operator
if (assignment.Annotation<IL.CallInstruction>() == null) {
if (assignment.Annotation<IL.CallInstruction>() == null && assignment.Annotation<IL.UserDefinedCompoundAssign>() == null) {
UnaryOperatorType type;
// When the parent is an expression statement, pre- or post-increment doesn't matter;
// so we can pick post-increment which is more commonly used (for (int i = 0; i < x; i++))

Loading…
Cancel
Save