Browse Source

Fix #1148: Do not use hexadecimal display in enums if the value is negative.

pull/1420/head
Siegfried Pammer 7 years ago
parent
commit
f1f70d0ded
  1. 1
      ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj
  2. 6
      ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
  3. 35
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeTests.cs
  4. 65
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeTests.il
  5. 65
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeTests.opt.il
  6. 69
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeTests.opt.roslyn.il
  7. 70
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeTests.roslyn.il
  8. 2
      ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs

1
ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj

@ -68,6 +68,7 @@
<Compile Include="Semantics\OverloadResolutionTests.cs" /> <Compile Include="Semantics\OverloadResolutionTests.cs" />
<Compile Include="DataFlowTest.cs" /> <Compile Include="DataFlowTest.cs" />
<Compile Include="TestCases\Correctness\RefLocalsAndReturns.cs" /> <Compile Include="TestCases\Correctness\RefLocalsAndReturns.cs" />
<Compile Include="TestCases\Pretty\TypeTests.cs" />
<None Include="TestCases\Disassembler\Pretty\SecurityDeclarations.il" /> <None Include="TestCases\Disassembler\Pretty\SecurityDeclarations.il" />
<Compile Include="TestCases\Pretty\CustomAttributeConflicts.cs" /> <Compile Include="TestCases\Pretty\CustomAttributeConflicts.cs" />
<Compile Include="TestCases\Pretty\DynamicTests.cs" /> <Compile Include="TestCases\Pretty\DynamicTests.cs" />

6
ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs

@ -338,6 +338,12 @@ namespace ICSharpCode.Decompiler.Tests
RunForLibrary(cscOptions: cscOptions); RunForLibrary(cscOptions: cscOptions);
} }
[Test]
public void TypeTests([ValueSource("defaultOptions")] CSharpCompilerOptions cscOptions)
{
RunForLibrary(cscOptions: cscOptions);
}
void RunForLibrary([CallerMemberName] string testName = null, AssemblerOptions asmOptions = AssemblerOptions.None, CSharpCompilerOptions cscOptions = CSharpCompilerOptions.None, DecompilerSettings decompilerSettings = null) void RunForLibrary([CallerMemberName] string testName = null, AssemblerOptions asmOptions = AssemblerOptions.None, CSharpCompilerOptions cscOptions = CSharpCompilerOptions.None, DecompilerSettings decompilerSettings = null)
{ {
Run(testName, asmOptions | AssemblerOptions.Library, cscOptions | CSharpCompilerOptions.Library, decompilerSettings); Run(testName, asmOptions | AssemblerOptions.Library, cscOptions | CSharpCompilerOptions.Library, decompilerSettings);

35
ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeTests.cs

@ -0,0 +1,35 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
internal class TypeTests
{
[Flags]
public enum NegativeValueWithFlags
{
Value = -2147483647
}
public enum NegativeValueWithoutFlags
{
Value = -2147483647
}
}
}

65
ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeTests.il

@ -0,0 +1,65 @@
// Metadata version: v4.0.30319
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0
}
.assembly TypeTests
{
.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
63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows.
.permissionset reqmin
= {[mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {property bool 'SkipVerification' = bool(true)}}
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module TypeTests.dll
.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
// =============== CLASS MEMBERS DECLARATION ===================
.class private auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.TypeTests
extends [mscorlib]System.Object
{
.class auto ansi sealed nested public NegativeValueWithFlags
extends [mscorlib]System.Enum
{
.custom instance void [mscorlib]System.FlagsAttribute::.ctor() = ( 01 00 00 00 )
.field public specialname rtspecialname int32 value__
.field public static literal valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.TypeTests/NegativeValueWithFlags Value = int32(0x80000001)
} // end of class NegativeValueWithFlags
.class auto ansi sealed nested public NegativeValueWithoutFlags
extends [mscorlib]System.Enum
{
.field public specialname rtspecialname int32 value__
.field public static literal valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.TypeTests/NegativeValueWithoutFlags Value = int32(0x80000001)
} // end of class NegativeValueWithoutFlags
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method TypeTests::.ctor
} // end of class ICSharpCode.Decompiler.Tests.TestCases.Pretty.TypeTests
// =============================================================
// *********** DISASSEMBLY COMPLETE ***********************

65
ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeTests.opt.il

@ -0,0 +1,65 @@
// Metadata version: v4.0.30319
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0
}
.assembly TypeTests.opt
{
.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
63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows.
.permissionset reqmin
= {[mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {property bool 'SkipVerification' = bool(true)}}
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module TypeTests.opt.dll
.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
// =============== CLASS MEMBERS DECLARATION ===================
.class private auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.TypeTests
extends [mscorlib]System.Object
{
.class auto ansi sealed nested public NegativeValueWithFlags
extends [mscorlib]System.Enum
{
.custom instance void [mscorlib]System.FlagsAttribute::.ctor() = ( 01 00 00 00 )
.field public specialname rtspecialname int32 value__
.field public static literal valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.TypeTests/NegativeValueWithFlags Value = int32(0x80000001)
} // end of class NegativeValueWithFlags
.class auto ansi sealed nested public NegativeValueWithoutFlags
extends [mscorlib]System.Enum
{
.field public specialname rtspecialname int32 value__
.field public static literal valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.TypeTests/NegativeValueWithoutFlags Value = int32(0x80000001)
} // end of class NegativeValueWithoutFlags
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method TypeTests::.ctor
} // end of class ICSharpCode.Decompiler.Tests.TestCases.Pretty.TypeTests
// =============================================================
// *********** DISASSEMBLY COMPLETE ***********************

69
ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeTests.opt.roslyn.il

@ -0,0 +1,69 @@
// Metadata version: v4.0.30319
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0
}
.assembly TypeTests
{
.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
63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows.
// --- The following custom attribute is added automatically, do not uncomment -------
// .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 02 00 00 00 00 00 )
.permissionset reqmin
= {[mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {property bool 'SkipVerification' = bool(true)}}
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module TypeTests.dll
.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
// =============== CLASS MEMBERS DECLARATION ===================
.class private auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.TypeTests
extends [mscorlib]System.Object
{
.class auto ansi sealed nested public NegativeValueWithFlags
extends [mscorlib]System.Enum
{
.custom instance void [mscorlib]System.FlagsAttribute::.ctor() = ( 01 00 00 00 )
.field public specialname rtspecialname int32 value__
.field public static literal valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.TypeTests/NegativeValueWithFlags Value = int32(0x80000001)
} // end of class NegativeValueWithFlags
.class auto ansi sealed nested public NegativeValueWithoutFlags
extends [mscorlib]System.Enum
{
.field public specialname rtspecialname int32 value__
.field public static literal valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.TypeTests/NegativeValueWithoutFlags Value = int32(0x80000001)
} // end of class NegativeValueWithoutFlags
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method TypeTests::.ctor
} // end of class ICSharpCode.Decompiler.Tests.TestCases.Pretty.TypeTests
// =============================================================
// *********** DISASSEMBLY COMPLETE ***********************

70
ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeTests.roslyn.il

@ -0,0 +1,70 @@
// Metadata version: v4.0.30319
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0
}
.assembly TypeTests
{
.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
63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows.
// --- The following custom attribute is added automatically, do not uncomment -------
// .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 07 01 00 00 00 00 )
.permissionset reqmin
= {[mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {property bool 'SkipVerification' = bool(true)}}
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module TypeTests.dll
.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
// =============== CLASS MEMBERS DECLARATION ===================
.class private auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.TypeTests
extends [mscorlib]System.Object
{
.class auto ansi sealed nested public NegativeValueWithFlags
extends [mscorlib]System.Enum
{
.custom instance void [mscorlib]System.FlagsAttribute::.ctor() = ( 01 00 00 00 )
.field public specialname rtspecialname int32 value__
.field public static literal valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.TypeTests/NegativeValueWithFlags Value = int32(0x80000001)
} // end of class NegativeValueWithFlags
.class auto ansi sealed nested public NegativeValueWithoutFlags
extends [mscorlib]System.Enum
{
.field public specialname rtspecialname int32 value__
.field public static literal valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.TypeTests/NegativeValueWithoutFlags Value = int32(0x80000001)
} // end of class NegativeValueWithoutFlags
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: nop
IL_0007: ret
} // end of method TypeTests::.ctor
} // end of class ICSharpCode.Decompiler.Tests.TestCases.Pretty.TypeTests
// =============================================================
// *********** DISASSEMBLY COMPLETE ***********************

2
ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs

@ -1170,7 +1170,7 @@ namespace ICSharpCode.Decompiler.CSharp
long initValue = (long)CSharpPrimitiveCast.Cast(TypeCode.Int64, field.ConstantValue, false); long initValue = (long)CSharpPrimitiveCast.Cast(TypeCode.Int64, field.ConstantValue, false);
enumDec.Initializer = typeSystemAstBuilder.ConvertConstantValue(decompilationContext.CurrentTypeDefinition.EnumUnderlyingType, field.ConstantValue); enumDec.Initializer = typeSystemAstBuilder.ConvertConstantValue(decompilationContext.CurrentTypeDefinition.EnumUnderlyingType, field.ConstantValue);
if (enumDec.Initializer is PrimitiveExpression primitive if (enumDec.Initializer is PrimitiveExpression primitive
&& (decompilationContext.CurrentTypeDefinition.HasAttribute(KnownAttribute.Flags) && initValue >= 0 && (decompilationContext.CurrentTypeDefinition.HasAttribute(KnownAttribute.Flags)
|| (initValue > 9 && (unchecked(initValue & (initValue - 1)) == 0 || unchecked(initValue & (initValue + 1)) == 0)))) || (initValue > 9 && (unchecked(initValue & (initValue - 1)) == 0 || unchecked(initValue & (initValue + 1)) == 0))))
{ {
primitive.SetValue(initValue, $"0x{initValue:X}"); primitive.SetValue(initValue, $"0x{initValue:X}");

Loading…
Cancel
Save