Browse Source

Merge pull request #3425 from ElektroKill/fix/issue3423

Improve generation of not equals check
pull/3443/head
Siegfried Pammer 3 months ago committed by GitHub
parent
commit
4ef3581b32
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 21
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/EnumTests.cs
  2. 13
      ICSharpCode.Decompiler/CSharp/TranslatedExpression.cs

21
ICSharpCode.Decompiler.Tests/TestCases/Pretty/EnumTests.cs

@ -1,4 +1,4 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team // Copyright (c) AlphaSierraPapa for the SharpDevelop Team
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy of this // 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 // software and associated documentation files (the "Software"), to deal in the Software
@ -28,6 +28,12 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
Item2 Item2
} }
public enum NoZero
{
Item1 = 1,
Item2
}
public enum OutOfOrderMembers public enum OutOfOrderMembers
{ {
Item1 = 1, Item1 = 1,
@ -135,5 +141,18 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{ {
return AttributeTargets.Class | AttributeTargets.Delegate; return AttributeTargets.Class | AttributeTargets.Delegate;
} }
public void EnumInNotZeroCheck(SimpleEnum value, NoZero value2)
{
if (value != SimpleEnum.Item1)
{
Console.WriteLine();
}
if (value2 != 0)
{
Console.WriteLine();
}
}
} }
} }

13
ICSharpCode.Decompiler/CSharp/TranslatedExpression.cs

@ -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 // 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 // software and associated documentation files (the "Software"), to deal in the Software
@ -731,6 +731,17 @@ namespace ICSharpCode.Decompiler.CSharp
.WithRR(new OperatorResolveResult(boolType, System.Linq.Expressions.ExpressionType.NotEqual, .WithRR(new OperatorResolveResult(boolType, System.Linq.Expressions.ExpressionType.NotEqual,
this.ResolveResult, nullRef.ResolveResult)); this.ResolveResult, nullRef.ResolveResult));
} }
else if (Type.Kind == TypeKind.Enum && Type.GetDefinition() is { } typeDef &&
typeDef.Fields.Any(f => f.GetConstantValue() is { } val && (ulong)CSharpPrimitiveCast.Cast(TypeCode.UInt64, val, false) == 0L))
{
var zero = expressionBuilder
.ConvertConstantValue(new ConstantResolveResult(Type, 0), allowImplicitConversion: true);
var op = negate ? BinaryOperatorType.Equality : BinaryOperatorType.InEquality;
return new BinaryOperatorExpression(Expression, op, zero.Expression)
.WithoutILInstruction()
.WithRR(new OperatorResolveResult(boolType, System.Linq.Expressions.ExpressionType.NotEqual,
this.ResolveResult, zero.ResolveResult));
}
else else
{ {
var zero = new PrimitiveExpression(0) var zero = new PrimitiveExpression(0)

Loading…
Cancel
Save