From 464acf02eaf1f7fc57f89300e07af9a53eecf87a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20Zgodzi=C5=84ski?= Date: Thu, 24 Feb 2011 20:35:28 +0100 Subject: [PATCH] fixed printing of enum values that cannot be mapped to enum named constants. --- ICSharpCode.Decompiler/Ast/AstBuilder.cs | 17 ++++++-- .../Tests/ICSharpCode.Decompiler.Tests.csproj | 2 + .../Tests/Types/EnumSamples.cs | 39 +++++++++++++++++++ .../Tests/Types/EnumTests.cs | 17 ++++++++ 4 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 ICSharpCode.Decompiler/Tests/Types/EnumSamples.cs create mode 100644 ICSharpCode.Decompiler/Tests/Types/EnumTests.cs diff --git a/ICSharpCode.Decompiler/Ast/AstBuilder.cs b/ICSharpCode.Decompiler/Ast/AstBuilder.cs index 5bdefecc4..4ba5042bb 100644 --- a/ICSharpCode.Decompiler/Ast/AstBuilder.cs +++ b/ICSharpCode.Decompiler/Ast/AstBuilder.cs @@ -515,6 +515,7 @@ namespace Decompiler Body = AstMethodBodyBuilder.CreateMethodBody(propDef.SetMethod, context) }.WithAnnotation(propDef.SetMethod); } + ConvertCustomAtributes(astProp, propDef); return astProp; } @@ -552,6 +553,7 @@ namespace Decompiler else initializer.Initializer = new PrimitiveExpression(fieldDef.Constant); } + ConvertCustomAtributes(astField, fieldDef); return astField; } @@ -619,12 +621,13 @@ namespace Decompiler foreach (FieldDefinition field in enumDefinition.Fields) { if (field.IsStatic && object.Equals(CSharpPrimitiveCast.Cast(TypeCode.Int64, field.Constant, false), val)) - return AstBuilder.ConvertType(enumDefinition).Member(field.Name).WithAnnotation(field); + return ConvertType(enumDefinition).Member(field.Name).WithAnnotation(field); else if (!field.IsStatic && field.IsRuntimeSpecialName) type = field.FieldType; // use primitive type of the enum } if (IsFlagsEnum(enumDefinition)) { + long enumValue = val; Expression expr = null; foreach (FieldDefinition field in enumDefinition.Fields.Where(fld => fld.IsStatic)) { @@ -632,18 +635,24 @@ namespace Decompiler if (fieldValue == 0) continue; // skip None enum value - if ((fieldValue & val) == fieldValue) + if ((fieldValue & enumValue) == fieldValue) { - var fieldExpression = AstBuilder.ConvertType(enumDefinition).Member(field.Name).WithAnnotation(field); + var fieldExpression = ConvertType(enumDefinition).Member(field.Name).WithAnnotation(field); if (expr == null) expr = fieldExpression; else expr = new BinaryOperatorExpression(expr, BinaryOperatorType.BitwiseOr, fieldExpression); + + enumValue &= ~fieldValue; + if (enumValue == 0) + break; } } - if (expr != null) + if(enumValue == 0 && expr != null) return expr; } + TypeCode enumBaseTypeCode = TypeAnalysis.GetTypeCode(type); + return new Ast.PrimitiveExpression(CSharpPrimitiveCast.Cast(enumBaseTypeCode, val, false)).CastTo(ConvertType(enumDefinition)); } } TypeCode code = TypeAnalysis.GetTypeCode(type); diff --git a/ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj b/ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj index 1e82a0137..bae7fefe6 100644 --- a/ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj +++ b/ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj @@ -56,6 +56,8 @@ + + diff --git a/ICSharpCode.Decompiler/Tests/Types/EnumSamples.cs b/ICSharpCode.Decompiler/Tests/Types/EnumSamples.cs new file mode 100644 index 000000000..b1cc54128 --- /dev/null +++ b/ICSharpCode.Decompiler/Tests/Types/EnumSamples.cs @@ -0,0 +1,39 @@ +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under MIT X11 license (for details please see \doc\license.txt) + +//$CS +using System; +//$CE + +//$$ SingleValue +public class TS_SingleValue +{ + public AttributeTargets Method() + { + return AttributeTargets.Class; + } +} +//$$ TwoValuesOr +public class TS_TwoValuesOr +{ + public AttributeTargets Method() + { + return AttributeTargets.Class | AttributeTargets.Method; + } +} +//$$ ThreeValuesOr +public class TS_ThreeValuesOr +{ + public AttributeTargets Method() + { + return AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Parameter; + } +} +//$$ UnknownNumericValue +public class TS_UnknownNumericValue +{ + public AttributeTargets Method() + { + return (AttributeTargets)1000000; + } +} diff --git a/ICSharpCode.Decompiler/Tests/Types/EnumTests.cs b/ICSharpCode.Decompiler/Tests/Types/EnumTests.cs new file mode 100644 index 000000000..b4234f623 --- /dev/null +++ b/ICSharpCode.Decompiler/Tests/Types/EnumTests.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using MbUnit.Framework; + +namespace ICSharpCode.Decompiler.Tests.Types +{ + public class EnumTests : DecompilerTestBase + { + [StaticTestFactory] + public static IEnumerable EnumSamples() + { + return GenerateSectionTests(@"Types\EnumSamples.cs"); + } + } +}