diff --git a/ICSharpCode.Decompiler.Tests/CustomAttributes/S_CustomAttributes.cs b/ICSharpCode.Decompiler.Tests/CustomAttributes/S_CustomAttributes.cs index bec4928b9..415d106dc 100644 --- a/ICSharpCode.Decompiler.Tests/CustomAttributes/S_CustomAttributes.cs +++ b/ICSharpCode.Decompiler.Tests/CustomAttributes/S_CustomAttributes.cs @@ -26,7 +26,7 @@ namespace CustomAttributes [Flags] public enum EnumWithFlag { - All = 15, + All = 0xF, None = 0, Item1 = 1, Item2 = 2, diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.cs index 1ba1b67f8..bff6c260f 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.cs @@ -34,9 +34,9 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty public enum ShortEnum : short { - None = 0, - One = 1, - Two = 2, + None, + One, + Two, Four = 4 } diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.cs index 6f095f0fb..2ed1d3953 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.cs @@ -48,14 +48,14 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty private enum MyEnum { - a = 0, - b = 1 + a, + b } private enum MyEnum2 { - c = 0, - d = 1 + c, + d } private class Data diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs index a80f12a2f..ccadff623 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs @@ -41,9 +41,9 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty public enum State { - False = 0, - True = 1, - Null = 2 + False, + True, + Null } public static State SwitchOverNullableBool(bool? value) diff --git a/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs b/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs index 55de49fad..ed89e3409 100644 --- a/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs +++ b/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs @@ -820,10 +820,20 @@ namespace ICSharpCode.Decompiler.CSharp Debug.Assert(decompilationContext.CurrentMember == field); var typeSystemAstBuilder = CreateAstBuilder(decompilationContext); if (decompilationContext.CurrentTypeDefinition.Kind == TypeKind.Enum) { - var enumDec = new EnumMemberDeclaration { - Name = field.Name, - Initializer = typeSystemAstBuilder.ConvertConstantValue(decompilationContext.CurrentTypeDefinition.EnumUnderlyingType, field.ConstantValue), - }; + var index = decompilationContext.CurrentTypeDefinition.Members.IndexOf(field); + long previousValue = -1; + if (index > 0) { + var previousMember = (IField)decompilationContext.CurrentTypeDefinition.Members[index - 1]; + previousValue = (long)CSharpPrimitiveCast.Cast(TypeCode.Int64, previousMember.ConstantValue, false); + } + long initValue = (long)CSharpPrimitiveCast.Cast(TypeCode.Int64, field.ConstantValue, false); + var enumDec = new EnumMemberDeclaration { Name = field.Name }; + if (previousValue + 1 != initValue || decompilationContext.CurrentTypeDefinition.Attributes.Any(a => a.AttributeType.FullName == "System.FlagsAttribute")) { + enumDec.Initializer = typeSystemAstBuilder.ConvertConstantValue(decompilationContext.CurrentTypeDefinition.EnumUnderlyingType, field.ConstantValue); + if (enumDec.Initializer is PrimitiveExpression primitive && initValue > 9 && ((initValue & (initValue - 1)) == 0 || (initValue & (initValue + 1)) == 0)) { + primitive.SetValue(initValue, $"0x{initValue:X}"); + } + } enumDec.Attributes.AddRange(field.Attributes.Select(a => new AttributeSection(typeSystemAstBuilder.ConvertAttribute(a)))); enumDec.AddAnnotation(new Semantics.MemberResolveResult(null, field)); return enumDec;