Take into account the zero sign bit for positive numbers when computing the bit
authorRichard Trieu <rtrieu@google.com>
Fri, 16 Nov 2012 01:32:40 +0000 (01:32 +0000)
committerRichard Trieu <rtrieu@google.com>
Fri, 16 Nov 2012 01:32:40 +0000 (01:32 +0000)
width of an enum with negative values in IntRange.  Include a test for
-Wtautological-constant-out-of-range-compare where this had manifested.

llvm-svn: 168126

clang/lib/Sema/SemaChecking.cpp
clang/test/SemaCXX/compare.cpp

index 05fa2a0..8e0a983 100644 (file)
@@ -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<BuiltinType>(T);
index 05980ba..c76efe9 100644 (file)
@@ -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);
+}