From 8f50b24c8b17368f68c2e71240d487dde53f6da8 Mon Sep 17 00:00:00 2001 From: Richard Trieu Date: Fri, 16 Nov 2012 01:32:40 +0000 Subject: [PATCH] Take into account the zero sign bit for positive numbers when computing the bit width of an enum with negative values in IntRange. Include a test for -Wtautological-constant-out-of-range-compare where this had manifested. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@168126 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaChecking.cpp | 6 +++++- test/SemaCXX/compare.cpp | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index 05fa2a0663..8e0a983d12 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -3922,7 +3922,11 @@ struct IntRange { unsigned NumPositive = Enum->getNumPositiveBits(); unsigned NumNegative = Enum->getNumNegativeBits(); - return IntRange(std::max(NumPositive, NumNegative), NumNegative == 0); + if (NumNegative == 0) + return IntRange(NumPositive, true/*NonNegative*/); + else + return IntRange(std::max(NumPositive + 1, NumNegative), + false/*NonNegative*/); } const BuiltinType *BT = cast(T); diff --git a/test/SemaCXX/compare.cpp b/test/SemaCXX/compare.cpp index 05980baf6d..c76efe92ba 100644 --- a/test/SemaCXX/compare.cpp +++ b/test/SemaCXX/compare.cpp @@ -338,3 +338,13 @@ void test7(unsigned long other) { (void)((unsigned char)other != (unsigned short)(0x100)); // expected-warning{{true}} (void)((unsigned short)other != (unsigned char)(0xff)); } + +void test8(int x) { + enum E { + Negative = -1, + Positive = 1 + }; + + (void)((E)x == 1); + (void)((E)x == -1); +} -- 2.50.1