[cfe-commits] r100595 - in /cfe/trunk: lib/Sema/SemaChecking.cpp test/Sema/compare.c

John McCall rjmccall at apple.com
Tue Apr 6 18:14:35 PDT 2010


Author: rjmccall
Date: Tue Apr  6 20:14:35 2010
New Revision: 100595

URL: http://llvm.org/viewvc/llvm-project?rev=100595&view=rev
Log:
Teach -Wsign-compare to treat 1 << blah as "idiomatically non-negative".
Fixes a spurious warning in LLVM.


Modified:
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/Sema/compare.c

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=100595&r1=100594&r2=100595&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Tue Apr  6 20:14:35 2010
@@ -1885,6 +1885,17 @@
 
     // Left shift gets black-listed based on a judgement call.
     case BinaryOperator::Shl:
+      // ...except that we want to treat '1 << (blah)' as logically
+      // positive.  It's an important idiom.
+      if (IntegerLiteral *I
+            = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) {
+        if (I->getValue() == 1) {
+          IntRange R = IntRange::forType(C, E->getType());
+          return IntRange(R.Width, /*NonNegative*/ true);
+        }
+      }
+      // fallthrough
+
     case BinaryOperator::ShlAssign:
       return IntRange::forType(C, E->getType());
 

Modified: cfe/trunk/test/Sema/compare.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/compare.c?rev=100595&r1=100594&r2=100595&view=diff
==============================================================================
--- cfe/trunk/test/Sema/compare.c (original)
+++ cfe/trunk/test/Sema/compare.c Tue Apr  6 20:14:35 2010
@@ -282,3 +282,8 @@
     && (x >= 0)  // expected-warning {{comparison of unsigned expression >= 0 is always true}}
     && (0 <= x); // expected-warning {{comparison of 0 <= unsigned expression is always true}}
 }
+
+int test6(unsigned i, unsigned power) {
+  unsigned x = (i < (1 << power) ? i : 0);
+  return x != 3 ? 1 << power : i;
+}





More information about the cfe-commits mailing list