Browse Source

Fix #1095: C# decompilation, for flags enums always use hex prefix

pull/1108/head
Siegfried Pammer 8 years ago
parent
commit
62770cf94c
  1. 4
      ICSharpCode.Decompiler.Tests/CustomAttributes/CustomAttributeTests.cs
  2. 2
      ICSharpCode.Decompiler.Tests/CustomAttributes/S_CustomAttributeSamples.cs
  3. 10
      ICSharpCode.Decompiler.Tests/CustomAttributes/S_CustomAttributes.cs
  4. 8
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.cs
  5. 2
      ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs

4
ICSharpCode.Decompiler.Tests/CustomAttributes/CustomAttributeTests.cs

@ -12,13 +12,13 @@ namespace ICSharpCode.Decompiler.Tests.CustomAttributes
} }
[Test] [Test]
public void CustomAttributesMultiTest() public void CustomAttributes()
{ {
ValidateFileRoundtrip(@"CustomAttributes/S_CustomAttributes.cs"); ValidateFileRoundtrip(@"CustomAttributes/S_CustomAttributes.cs");
} }
[Test] [Test]
public void AssemblyCustomAttributesMultiTest() public void AssemblyCustomAttribute()
{ {
ValidateFileRoundtrip(@"CustomAttributes/S_AssemblyCustomAttribute.cs"); ValidateFileRoundtrip(@"CustomAttributes/S_AssemblyCustomAttribute.cs");
} }

2
ICSharpCode.Decompiler.Tests/CustomAttributes/S_CustomAttributeSamples.cs

@ -28,7 +28,7 @@ namespace ParameterLessAttributeUsage
[Flags] [Flags]
public enum EnumWithFlagsAttribute public enum EnumWithFlagsAttribute
{ {
None = 0 None = 0x0
} }
} }
//$$ AttributeWithEnumArgument //$$ AttributeWithEnumArgument

10
ICSharpCode.Decompiler.Tests/CustomAttributes/S_CustomAttributes.cs

@ -27,11 +27,11 @@ namespace CustomAttributes
public enum EnumWithFlag public enum EnumWithFlag
{ {
All = 0xF, All = 0xF,
None = 0, None = 0x0,
Item1 = 1, Item1 = 0x1,
Item2 = 2, Item2 = 0x2,
Item3 = 4, Item3 = 0x4,
Item4 = 8 Item4 = 0x8
} }
[AttributeUsage(AttributeTargets.All)] [AttributeUsage(AttributeTargets.All)]
public class MyAttribute : Attribute public class MyAttribute : Attribute

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

@ -26,10 +26,10 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
[Flags] [Flags]
private enum MyEnum private enum MyEnum
{ {
None = 0, None = 0x0,
One = 1, One = 0x1,
Two = 2, Two = 0x2,
Four = 4 Four = 0x4
} }
public enum ShortEnum : short public enum ShortEnum : short

2
ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs

@ -1174,7 +1174,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);
if (decompilationContext.CurrentTypeDefinition.Attributes.Any(a => a.AttributeType.FullName == "System.FlagsAttribute")) { if (decompilationContext.CurrentTypeDefinition.Attributes.Any(a => a.AttributeType.FullName == "System.FlagsAttribute")) {
enumDec.Initializer = typeSystemAstBuilder.ConvertConstantValue(decompilationContext.CurrentTypeDefinition.EnumUnderlyingType, field.ConstantValue); enumDec.Initializer = typeSystemAstBuilder.ConvertConstantValue(decompilationContext.CurrentTypeDefinition.EnumUnderlyingType, field.ConstantValue);
if (enumDec.Initializer is PrimitiveExpression primitive && initValue > 9) if (enumDec.Initializer is PrimitiveExpression primitive)
primitive.SetValue(initValue, $"0x{initValue:X}"); primitive.SetValue(initValue, $"0x{initValue:X}");
} else if (previousValue + 1 != initValue) { } else if (previousValue + 1 != initValue) {
enumDec.Initializer = typeSystemAstBuilder.ConvertConstantValue(decompilationContext.CurrentTypeDefinition.EnumUnderlyingType, field.ConstantValue); enumDec.Initializer = typeSystemAstBuilder.ConvertConstantValue(decompilationContext.CurrentTypeDefinition.EnumUnderlyingType, field.ConstantValue);

Loading…
Cancel
Save