diff --git a/llvm/lib/Support/FileCheckImpl.h b/llvm/lib/Support/FileCheckImpl.h --- a/llvm/lib/Support/FileCheckImpl.h +++ b/llvm/lib/Support/FileCheckImpl.h @@ -64,8 +64,8 @@ return Value != Kind::NoFormat && Value == Other.Value; } - bool operator!=(const ExpressionFormat &other) const { - return !(*this == other); + bool operator!=(const ExpressionFormat &Other) const { + return !(*this == Other); } bool operator==(Kind OtherValue) const { return Value == OtherValue; } @@ -119,8 +119,19 @@ template explicit ExpressionValue(T Val) : Value(Val), Negative(Val < 0) {} + bool operator==(const ExpressionValue &Other) const { + return Value == Other.Value && isNegative() == Other.isNegative(); + } + + bool operator!=(const ExpressionValue &Other) const { + return !(*this == Other); + } + /// Returns true if value is signed and negative, false otherwise. - bool isNegative() const { return Negative; } + bool isNegative() const { + assert((Value != 0 || !Negative) && "Unexpected negative zero!"); + return Negative; + } /// \returns the value as a signed integer or an error if the value is out of /// range. diff --git a/llvm/unittests/Support/FileCheckTest.cpp b/llvm/unittests/Support/FileCheckTest.cpp --- a/llvm/unittests/Support/FileCheckTest.cpp +++ b/llvm/unittests/Support/FileCheckTest.cpp @@ -478,6 +478,46 @@ expectOperationValueResult(operator-, 10, 11, -1); } +TEST_F(FileCheckTest, ExpressionValueEquality) { + // Test negative and positive value. + EXPECT_FALSE(ExpressionValue(5) == ExpressionValue(-3)); + EXPECT_TRUE(ExpressionValue(5) != ExpressionValue(-3)); + EXPECT_FALSE(ExpressionValue(-2) == ExpressionValue(6)); + EXPECT_TRUE(ExpressionValue(-2) != ExpressionValue(6)); + EXPECT_FALSE(ExpressionValue(-7) == ExpressionValue(7)); + EXPECT_TRUE(ExpressionValue(-7) != ExpressionValue(7)); + EXPECT_FALSE(ExpressionValue(4) == ExpressionValue(-4)); + EXPECT_TRUE(ExpressionValue(4) != ExpressionValue(-4)); + EXPECT_FALSE(ExpressionValue(MaxUint64) == ExpressionValue(-1)); + EXPECT_TRUE(ExpressionValue(MaxUint64) != ExpressionValue(-1)); + + // Test both negative values. + EXPECT_FALSE(ExpressionValue(-2) == ExpressionValue(-7)); + EXPECT_TRUE(ExpressionValue(-2) != ExpressionValue(-7)); + EXPECT_TRUE(ExpressionValue(-3) == ExpressionValue(-3)); + EXPECT_FALSE(ExpressionValue(-3) != ExpressionValue(-3)); + EXPECT_FALSE(ExpressionValue(MinInt64) == ExpressionValue(-1)); + EXPECT_TRUE(ExpressionValue(MinInt64) != ExpressionValue(-1)); + EXPECT_FALSE(ExpressionValue(MinInt64) == ExpressionValue(-0)); + EXPECT_TRUE(ExpressionValue(MinInt64) != ExpressionValue(-0)); + + // Test both positive values. + EXPECT_FALSE(ExpressionValue(8) == ExpressionValue(9)); + EXPECT_TRUE(ExpressionValue(8) != ExpressionValue(9)); + EXPECT_TRUE(ExpressionValue(1) == ExpressionValue(1)); + EXPECT_FALSE(ExpressionValue(1) != ExpressionValue(1)); + + // Check the signedness of zero doesn't affect equality. + EXPECT_TRUE(ExpressionValue(0) == ExpressionValue(0)); + EXPECT_FALSE(ExpressionValue(0) != ExpressionValue(0)); + EXPECT_TRUE(ExpressionValue(0) == ExpressionValue(-0)); + EXPECT_FALSE(ExpressionValue(0) != ExpressionValue(-0)); + EXPECT_TRUE(ExpressionValue(-0) == ExpressionValue(0)); + EXPECT_FALSE(ExpressionValue(-0) != ExpressionValue(0)); + EXPECT_TRUE(ExpressionValue(-0) == ExpressionValue(-0)); + EXPECT_FALSE(ExpressionValue(-0) != ExpressionValue(-0)); +} + TEST_F(FileCheckTest, Literal) { SourceMgr SM;