Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1462: Inconsistent enum flag check #1476

Merged
merged 3 commits into from
May 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion ICSharpCode.Decompiler.Tests/TestCases/Pretty/ConstantsTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
using System.Threading.Tasks;

namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
internal class ConstantsTests
{
Expand All @@ -24,6 +26,12 @@ public void SByte_BitmaskingInCondition(sbyte v)
Test((v | 0x123) == 0);
}

public void Enum_Flag_Check(TaskCreationOptions v)
{
Test((v & TaskCreationOptions.AttachedToParent) != 0);
Test((v & TaskCreationOptions.AttachedToParent) == 0);
}

private void Test(bool expr)
{
}
Expand Down
22 changes: 19 additions & 3 deletions ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2014 Daniel Grunwald
// Copyright (c) 2014 Daniel Grunwald
//
// 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
Expand Down Expand Up @@ -699,8 +699,8 @@ TranslatedExpression TranslateCeq(Comp inst, out bool negateOutput)
}

// Special case comparisons with enum and char literals
left = AdjustConstantExpressionToType(left, right.Type);
right = AdjustConstantExpressionToType(right, left.Type);
left = TryUniteEqualityOperandType(left, right);
right = TryUniteEqualityOperandType(right, left);

if (IsSpecialCasedReferenceComparisonWithNull(left, right)) {
// When comparing a string/delegate with null, the C# compiler generates a reference comparison.
Expand Down Expand Up @@ -763,6 +763,22 @@ TranslatedExpression TranslateCeq(Comp inst, out bool negateOutput)
.WithRR(rr);
}

TranslatedExpression TryUniteEqualityOperandType(TranslatedExpression left, TranslatedExpression right)
{
// Special case for enum flag check "(enum & EnumType.SomeValue) == 0"
// so that the const 0 value is printed as 0 integer and not as enum type, e.g. EnumType.None
if (left.ResolveResult.IsCompileTimeConstant &&
left.ResolveResult.Type.IsCSharpPrimitiveIntegerType() &&
(left.ResolveResult.ConstantValue as int?) == 0 &&
NullableType.GetUnderlyingType(right.Type).Kind == TypeKind.Enum &&
right.Expression is BinaryOperatorExpression binaryExpr &&
binaryExpr.Operator == BinaryOperatorType.BitwiseAnd)
{
return AdjustConstantExpressionToType(left, compilation.FindType(KnownTypeCode.Int32));
} else
return AdjustConstantExpressionToType(left, right.Type);
}

bool IsSpecialCasedReferenceComparisonWithNull(TranslatedExpression lhs, TranslatedExpression rhs)
{
if (lhs.Type.Kind == TypeKind.Null)
Expand Down