]> granicus.if.org Git - clang/commitdiff
Add fixit notes for -Wconstant-logical-operand.
authorMatt Beaumont-Gay <matthewbg@google.com>
Mon, 15 Aug 2011 17:50:06 +0000 (17:50 +0000)
committerMatt Beaumont-Gay <matthewbg@google.com>
Mon, 15 Aug 2011 17:50:06 +0000 (17:50 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@137620 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaExpr.cpp
test/Sema/exprs.c
test/Sema/i-c-e.c
test/SemaCXX/bool.cpp
test/SemaCXX/expressions.cpp

index 88d6e784c6399e2926f2f0274b38a78fff7c2ba6..2430372c3df4b280bb4c44f8e76dd2ee37b9870b 100644 (file)
@@ -2940,8 +2940,12 @@ def note_precedence_conditional_silence : Note<
   "place parentheses around the '%0' expression to silence this warning">;
 
 def warn_logical_instead_of_bitwise : Warning<
-  "use of logical %0 with constant operand; switch to bitwise %1 or "
-  "remove constant">, InGroup<DiagGroup<"constant-logical-operand">>;
+  "use of logical '%0' with constant operand">,
+  InGroup<DiagGroup<"constant-logical-operand">>;
+def note_logical_instead_of_bitwise_change_operator : Note<
+  "use '%0' for a bitwise operation">;
+def note_logical_instead_of_bitwise_remove_constant : Note<
+  "remove constant to silence this warning">;
 
 def warn_bitwise_and_in_bitwise_or : Warning<
   "'&' within '|'">, InGroup<BitwiseOpParentheses>;
index efc05de90ecf68dcdb8db49dcb8cdec1b692e4d2..20d129da99908b2dcc1066edb057c57b48f6469b 100644 (file)
@@ -6719,9 +6719,24 @@ inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
           (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 ? "&" : "|");
-    }
+          << (Opc == BO_LAnd ? "&&" : "||");
+        // Suggest replacing the logical operator with the bitwise version
+        Diag(Loc, diag::note_logical_instead_of_bitwise_change_operator)
+            << (Opc == BO_LAnd ? "&" : "|")
+            << FixItHint::CreateReplacement(SourceRange(
+                Loc, Lexer::getLocForEndOfToken(Loc, 0, getSourceManager(),
+                                                getLangOptions())),
+                                            Opc == BO_LAnd ? "&" : "|");
+        if (Opc == BO_LAnd)
+          // Suggest replacing "Foo() && kNonZero" with "Foo()"
+          Diag(Loc, diag::note_logical_instead_of_bitwise_remove_constant)
+              << FixItHint::CreateRemoval(
+                  SourceRange(
+                      Lexer::getLocForEndOfToken(lex.get()->getLocEnd(),
+                                                 0, getSourceManager(),
+                                                 getLangOptions()),
+                      rex.get()->getLocEnd()));
+      }
   }
   
   if (!Context.getLangOptions().CPlusPlus) {
index 460f72cb17422543bde1668f3733f9c7298bd31b..72cff65f4837ca94f2de49a3737a1a5561c4be0b 100644 (file)
@@ -183,7 +183,9 @@ void test19() {
 }
 
 int test20(int x) {
-  return x && 4; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
+  return x && 4; // expected-warning {{use of logical '&&' with constant operand}} \
+                 // expected-note {{use '&' for a bitwise operation}} \
+                 // expected-note {{remove constant to silence this warning}}
 
   return x && sizeof(int) == 4;  // no warning, RHS is logical op.
   
@@ -192,20 +194,32 @@ int test20(int x) {
 
   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 || -1; // expected-warning {{use of logical '||' with constant operand}} \
+                  // expected-note {{use '|' for a bitwise operation}}
+  return x || 5; // expected-warning {{use of logical '||' with constant operand}} \
+                 // expected-note {{use '|' for a bitwise operation}}
   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 && -1; // expected-warning {{use of logical '&&' with constant operand}} \
+                  // expected-note {{use '&' for a bitwise operation}} \
+                  // expected-note {{remove constant to silence this warning}}
+  return x && 5; // expected-warning {{use of logical '&&' with constant operand}} \
+                 // expected-note {{use '&' for a bitwise operation}} \
+                 // expected-note {{remove constant to silence this warning}}
   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 || (-1); // expected-warning {{use of logical '||' with constant operand}} \
+                    // expected-note {{use '|' for a bitwise operation}}
+  return x || (5); // expected-warning {{use of logical '||' with constant operand}} \
+                   // expected-note {{use '|' for a bitwise operation}}
   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 && (-1); // expected-warning {{use of logical '&&' with constant operand}} \
+                    // expected-note {{use '&' for a bitwise operation}} \
+                    // expected-note {{remove constant to silence this warning}}
+  return x && (5); // expected-warning {{use of logical '&&' with constant operand}} \
+                   // expected-note {{use '&' for a bitwise operation}} \
+                   // expected-note {{remove constant to silence this warning}}
 
 }
 
index 1aac51e204f150f18a8929733b7a8ae9db5c7b88..9b50916f85ffb80094531a1c1d3ab5fb70c686a0 100644 (file)
@@ -53,7 +53,8 @@ char z[__builtin_constant_p(4) ? 1 : -1];
 // Comma tests
 int comma1[0?1,2:3];  // expected-warning {{expression result unused}}
 int comma2[1||(1,2)]; // expected-warning {{expression result unused}} \
-                      // expected-warning {{use of logical || with constant operand}}
+                      // expected-warning {{use of logical '||' with constant operand}} \
+                      // expected-note {{use '|' for a bitwise operation}}
 int comma3[(1,2)]; // expected-warning {{size of static array must be an integer constant expression}} \
                                        // expected-warning {{expression result unused}}
 
index 726fa6cb60fc13cb2cb0e4a1cf4b559b112e72d9..2b3ab68848ebf072f78f196725030dcd91a714b2 100644 (file)
@@ -25,6 +25,9 @@ void static_assert_arg_is_bool(T x) {
 
 void test2() {
   int n = 2;
-  static_assert_arg_is_bool(n && 4);  // expected-warning {{use of logical && with constant operand}}
-  static_assert_arg_is_bool(n || 5);  // expected-warning {{use of logical || with constant operand}}
+  static_assert_arg_is_bool(n && 4);  // expected-warning {{use of logical '&&' with constant operand}} \
+                                      // expected-note {{use '&' for a bitwise operation}} \
+                                      // expected-note {{remove constant to silence this warning}}
+  static_assert_arg_is_bool(n || 5);  // expected-warning {{use of logical '||' with constant operand}} \
+                                      // expected-note {{use '|' for a bitwise operation}}
 }
index 8a294face3806ba843300560581f9ee2cd44d554..355833e693fa2c4eee800b553e3fd7ba5f33dbe5 100644 (file)
@@ -34,7 +34,9 @@ namespace test1 {
 }
 
 int test2(int x) {
-  return x && 4; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
+  return x && 4; // expected-warning {{use of logical '&&' with constant operand}} \
+                   // expected-note {{use '&' for a bitwise operation}} \
+                   // expected-note {{remove constant to silence this warning}}
 
   return x && sizeof(int) == 4;  // no warning, RHS is logical op.
   return x && true;
@@ -42,38 +44,69 @@ int test2(int x) {
   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)0; // expected-warning {{use of logical '&&' with constant operand}} \
+                   // expected-note {{use '&' for a bitwise operation}} \
+                   // expected-note {{remove constant to silence this warning}}
 
-  return x || (unsigned)1; // 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}} \
+                   // expected-note {{use '|' for a bitwise operation}}
 
-  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}}
+  return x || 0; // expected-warning {{use of logical '||' with constant operand}} \
+                   // expected-note {{use '|' for a bitwise operation}}
+  return x || 1; // expected-warning {{use of logical '||' with constant operand}} \
+                   // expected-note {{use '|' for a bitwise operation}}
+  return x || -1; // expected-warning {{use of logical '||' with constant operand}} \
+                   // expected-note {{use '|' for a bitwise operation}}
+  return x || 5; // expected-warning {{use of logical '||' with constant operand}} \
+                   // expected-note {{use '|' for a bitwise operation}}
+  return x && 0; // expected-warning {{use of logical '&&' with constant operand}} \
+                   // expected-note {{use '&' for a bitwise operation}} \
+                   // expected-note {{remove constant to silence this warning}}
+  return x && 1; // expected-warning {{use of logical '&&' with constant operand}} \
+                   // expected-note {{use '&' for a bitwise operation}} \
+                   // expected-note {{remove constant to silence this warning}}
+  return x && -1; // expected-warning {{use of logical '&&' with constant operand}} \
+                   // expected-note {{use '&' for a bitwise operation}} \
+                   // expected-note {{remove constant to silence this warning}}
+  return x && 5; // expected-warning {{use of logical '&&' with constant operand}} \
+                   // expected-note {{use '&' for a bitwise operation}} \
+                   // expected-note {{remove constant to silence this warning}}
+  return x || (0); // expected-warning {{use of logical '||' with constant operand}} \
+                   // expected-note {{use '|' for a bitwise operation}}
+  return x || (1); // expected-warning {{use of logical '||' with constant operand}} \
+                   // expected-note {{use '|' for a bitwise operation}}
+  return x || (-1); // expected-warning {{use of logical '||' with constant operand}} \
+                   // expected-note {{use '|' for a bitwise operation}}
+  return x || (5); // expected-warning {{use of logical '||' with constant operand}} \
+                   // expected-note {{use '|' for a bitwise operation}}
+  return x && (0); // expected-warning {{use of logical '&&' with constant operand}} \
+                   // expected-note {{use '&' for a bitwise operation}} \
+                   // expected-note {{remove constant to silence this warning}}
+  return x && (1); // expected-warning {{use of logical '&&' with constant operand}} \
+                   // expected-note {{use '&' for a bitwise operation}} \
+                   // expected-note {{remove constant to silence this warning}}
+  return x && (-1); // expected-warning {{use of logical '&&' with constant operand}} \
+                   // expected-note {{use '&' for a bitwise operation}} \
+                   // expected-note {{remove constant to silence this warning}}
+  return x && (5); // expected-warning {{use of logical '&&' with constant operand}} \
+                   // expected-note {{use '&' for a bitwise operation}} \
+                   // expected-note {{remove constant to silence this warning}}
 }
 
 template<unsigned int A, unsigned int B> struct S
 {
   enum {
     e1 = A && B,
-    e2 = A && 7      // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
+    e2 = A && 7      // expected-warning {{use of logical '&&' with constant operand}} \
+                     // expected-note {{use '&' for a bitwise operation}} \
+                     // expected-note {{remove constant to silence this warning}}
   };
 
   int foo() {
     int x = A && B;
-    int y = B && 3;  // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
+    int y = B && 3;  // expected-warning {{use of logical '&&' with constant operand}} \
+                     // expected-note {{use '&' for a bitwise operation}} \
+                     // expected-note {{remove constant to silence this warning}}
 
     return x + y;
   }