[cfe-commits] r132327 - in /cfe/trunk: lib/Sema/SemaExpr.cpp test/CodeGenCXX/static-init-2.cpp test/Sema/exprs.c test/SemaCXX/expressions.cpp test/SemaCXX/switch.cpp

Chandler Carruth chandlerc at gmail.com
Mon May 30 22:41:42 PDT 2011


Author: chandlerc
Date: Tue May 31 00:41:42 2011
New Revision: 132327

URL: http://llvm.org/viewvc/llvm-project?rev=132327&view=rev
Log:
Expand the coverage of the warning for constants on the RHS of logical operands:

  return f() || -1;

where the user meant to write '|'.

This bootstraps without any additional warnings.

Patch by Richard Trieu.

Modified:
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/CodeGenCXX/static-init-2.cpp
    cfe/trunk/test/Sema/exprs.c
    cfe/trunk/test/SemaCXX/expressions.cpp
    cfe/trunk/test/SemaCXX/switch.cpp

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=132327&r1=132326&r2=132327&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Tue May 31 00:41:42 2011
@@ -7797,13 +7797,15 @@
     // If the RHS can be constant folded, and if it constant folds to something
     // that isn't 0 or 1 (which indicate a potential logical operation that
     // happened to fold to true/false) then warn.
+    // Parens on the RHS are ignored.
     Expr::EvalResult Result;
-    if (rex.get()->Evaluate(Result, Context) && !Result.HasSideEffects &&
-        Result.Val.getInt() != 0 && Result.Val.getInt() != 1) {
-      Diag(Loc, diag::warn_logical_instead_of_bitwise)
-       << rex.get()->getSourceRange()
-        << (Opc == BO_LAnd ? "&&" : "||")
-        << (Opc == BO_LAnd ? "&" : "|");
+    if (rex.get()->Evaluate(Result, Context) && !Result.HasSideEffects)
+      if ((getLangOptions().Bool && !rex.get()->getType()->isBooleanType()) ||
+          (Result.Val.getInt() != 0 && Result.Val.getInt() != 1)) {
+        Diag(Loc, diag::warn_logical_instead_of_bitwise)
+          << rex.get()->getSourceRange()
+          << (Opc == BO_LAnd ? "&&" : "||")
+          << (Opc == BO_LAnd ? "&" : "|");
     }
   }
   

Modified: cfe/trunk/test/CodeGenCXX/static-init-2.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/static-init-2.cpp?rev=132327&r1=132326&r2=132327&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/static-init-2.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/static-init-2.cpp Tue May 31 00:41:42 2011
@@ -3,4 +3,4 @@
 // Make sure we don't crash generating y; its value is constant, but the
 // initializer has side effects, so EmitConstantExpr should fail.
 int x();
-int y = x() && 0;
+int y = x() & 0;

Modified: cfe/trunk/test/Sema/exprs.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/exprs.c?rev=132327&r1=132326&r2=132327&view=diff
==============================================================================
--- cfe/trunk/test/Sema/exprs.c (original)
+++ cfe/trunk/test/Sema/exprs.c Tue May 31 00:41:42 2011
@@ -189,6 +189,24 @@
   
   // no warning, this is an idiom for "true" in old C style.
   return x && (signed char)1;
+
+  return x || 0;
+  return x || 1;
+  return x || -1; // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
+  return x || 5; // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
+  return x && 0;
+  return x && 1;
+  return x && -1; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
+  return x && 5; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
+  return x || (0);
+  return x || (1);
+  return x || (-1); // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
+  return x || (5); // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
+  return x && (0);
+  return x && (1);
+  return x && (-1); // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
+  return x && (5); // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
+
 }
 
 struct Test21; // expected-note 2 {{forward declaration}}

Modified: cfe/trunk/test/SemaCXX/expressions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/expressions.cpp?rev=132327&r1=132326&r2=132327&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/expressions.cpp (original)
+++ cfe/trunk/test/SemaCXX/expressions.cpp Tue May 31 00:41:42 2011
@@ -32,3 +32,34 @@
     bar(x += E_zero); // expected-error {{incompatible type}}
   }
 }
+
+int test2(int x) {
+  return x && 4; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
+
+  return x && sizeof(int) == 4;  // no warning, RHS is logical op.
+  return x && true;
+  return x && false;
+  return x || true;
+  return x || false;
+
+  return x && (unsigned)0; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
+
+  return x || (unsigned)1; // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
+
+  return x || 0; // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
+  return x || 1; // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
+  return x || -1; // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
+  return x || 5; // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
+  return x && 0; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
+  return x && 1; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
+  return x && -1; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
+  return x && 5; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
+  return x || (0); // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
+  return x || (1); // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
+  return x || (-1); // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
+  return x || (5); // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
+  return x && (0); // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
+  return x && (1); // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
+  return x && (-1); // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
+  return x && (5); // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
+}

Modified: cfe/trunk/test/SemaCXX/switch.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/switch.cpp?rev=132327&r1=132326&r2=132327&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/switch.cpp (original)
+++ cfe/trunk/test/SemaCXX/switch.cpp Tue May 31 00:41:42 2011
@@ -8,7 +8,7 @@
   }
 
   int n = 3;
-  switch (n && 1) { // expected-warning {{bool}}
+  switch (n && true) { // expected-warning {{bool}}
     case 1:
       break;
   }





More information about the cfe-commits mailing list