]> granicus.if.org Git - clang/commitdiff
Make -Wtautological-constant-out-of-range-compare behave sanely for enums with a...
authorEli Friedman <eli.friedman@gmail.com>
Fri, 30 Nov 2012 23:09:29 +0000 (23:09 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Fri, 30 Nov 2012 23:09:29 +0000 (23:09 +0000)
<rdar://problem/12780159>.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@169051 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 33dcb3a6cb40c63790b3833023705d5d3efef19f..235383fdd19ec41ef4bd305de6188befad60c8a9 100644 (file)
@@ -4346,7 +4346,6 @@ static void DiagnoseOutOfRangeComparison(Sema &S, BinaryOperator *E,
          && "comparison with non-integer type");
 
   bool ConstantSigned = ConstantT->isSignedIntegerType();
-  bool OtherSigned = OtherT->isSignedIntegerType();
   bool CommonSigned = CommonT->isSignedIntegerType();
 
   bool EqualityOnly = false;
@@ -4358,7 +4357,7 @@ static void DiagnoseOutOfRangeComparison(Sema &S, BinaryOperator *E,
   
   if (CommonSigned) {
     // The common type is signed, therefore no signed to unsigned conversion.
-    if (OtherSigned) {
+    if (!OtherRange.NonNegative) {
       // Check that the constant is representable in type OtherT.
       if (ConstantSigned) {
         if (OtherWidth >= Value.getMinSignedBits())
@@ -4379,10 +4378,10 @@ static void DiagnoseOutOfRangeComparison(Sema &S, BinaryOperator *E,
       }
     }
   } else {  // !CommonSigned
-    if (!OtherSigned) {
+    if (OtherRange.NonNegative) {
       if (OtherWidth >= Value.getActiveBits())
         return;
-    } else if (OtherSigned && !ConstantSigned) {
+    } else if (!OtherRange.NonNegative && !ConstantSigned) {
       // Check to see if the constant is representable in OtherT.
       if (OtherWidth > Value.getActiveBits())
         return;
index c76efe92baf3f71659bca7c0f4752263ab12f09f..5771912590c01a70ef27d8178c784b1dfd114234 100644 (file)
@@ -1,7 +1,7 @@
 // Force x86-64 because some of our heuristics are actually based
 // on integer sizes.
 
-// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -pedantic -verify -Wsign-compare %s
+// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -pedantic -verify -Wsign-compare -std=c++11 %s
 
 int test0(long a, unsigned long b) {
   enum EnumA {A};
@@ -348,3 +348,10 @@ void test8(int x) {
   (void)((E)x == 1);
   (void)((E)x == -1);
 }
+
+void test9(int x) {
+  enum E : int {
+    Positive = 1
+  };
+  (void)((E)x == 1);
+}