The old warning that checked for logical operators with constants did not
treat 1 and 0 as it would for other constants.  Changed the warning so that
if a bool type exists, 1 and 0 will trigger the warning.

Patch is attached and available through Code Review:
http://codereview.appspot.com/4444068/
Index: test/CodeGenCXX/static-init-2.cpp
===================================================================
--- test/CodeGenCXX/static-init-2.cpp	(revision 130248)
+++ test/CodeGenCXX/static-init-2.cpp	(working copy)
@@ -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;
Index: test/SemaCXX/expressions.cpp
===================================================================
--- test/SemaCXX/expressions.cpp	(revision 130248)
+++ test/SemaCXX/expressions.cpp	(working copy)
@@ -32,3 +32,26 @@
     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}}
+}
Index: test/SemaCXX/switch.cpp
===================================================================
--- test/SemaCXX/switch.cpp	(revision 130248)
+++ test/SemaCXX/switch.cpp	(working copy)
@@ -8,7 +8,8 @@
   }
 
   int n = 3;
-  switch (n && 1) { // expected-warning {{bool}}
+  int m = 1;
+  switch (n && m) { // expected-warning {{bool}}
     case 1:
       break;
   }
Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp	(revision 130248)
+++ lib/Sema/SemaExpr.cpp	(working copy)
@@ -7738,12 +7738,13 @@
     // that isn't 0 or 1 (which indicate a potential logical operation that
     // happened to fold to true/false) then warn.
     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 ? "&" : "|");
     }
   }
   
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to