[cfe-commits] r137620 - in /cfe/trunk: 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

Matt Beaumont-Gay matthewbg at google.com
Mon Aug 15 10:50:06 PDT 2011


Author: matthewbg
Date: Mon Aug 15 12:50:06 2011
New Revision: 137620

URL: http://llvm.org/viewvc/llvm-project?rev=137620&view=rev
Log:
Add fixit notes for -Wconstant-logical-operand.

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

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=137620&r1=137619&r2=137620&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Aug 15 12:50:06 2011
@@ -2940,8 +2940,12 @@
   "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>;

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=137620&r1=137619&r2=137620&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Mon Aug 15 12:50:06 2011
@@ -6719,9 +6719,24 @@
           (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) {

Modified: cfe/trunk/test/Sema/exprs.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/exprs.c?rev=137620&r1=137619&r2=137620&view=diff
==============================================================================
--- cfe/trunk/test/Sema/exprs.c (original)
+++ cfe/trunk/test/Sema/exprs.c Mon Aug 15 12:50:06 2011
@@ -183,7 +183,9 @@
 }
 
 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 @@
 
   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}}
 
 }
 

Modified: cfe/trunk/test/Sema/i-c-e.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/i-c-e.c?rev=137620&r1=137619&r2=137620&view=diff
==============================================================================
--- cfe/trunk/test/Sema/i-c-e.c (original)
+++ cfe/trunk/test/Sema/i-c-e.c Mon Aug 15 12:50:06 2011
@@ -53,7 +53,8 @@
 // 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}}
 

Modified: cfe/trunk/test/SemaCXX/bool.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/bool.cpp?rev=137620&r1=137619&r2=137620&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/bool.cpp (original)
+++ cfe/trunk/test/SemaCXX/bool.cpp Mon Aug 15 12:50:06 2011
@@ -25,6 +25,9 @@
 
 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}}
 }

Modified: cfe/trunk/test/SemaCXX/expressions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/expressions.cpp?rev=137620&r1=137619&r2=137620&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/expressions.cpp (original)
+++ cfe/trunk/test/SemaCXX/expressions.cpp Mon Aug 15 12:50:06 2011
@@ -34,7 +34,9 @@
 }
 
 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 @@
   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}}
+  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}} \
+                   // 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}}
+  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;
   }





More information about the cfe-commits mailing list