[cfe-commits] r137820 - in /cfe/trunk: lib/Sema/SemaStmt.cpp test/SemaCXX/warn-top-level-comparison.cpp

Chandler Carruth chandlerc at gmail.com
Wed Aug 17 01:38:11 PDT 2011


Author: chandlerc
Date: Wed Aug 17 03:38:11 2011
New Revision: 137820

URL: http://llvm.org/viewvc/llvm-project?rev=137820&view=rev
Log:
Don't suggest assignment in implausible situation. We still warn, as the
code is very likely to be buggy, but its going to require more
significant changes on the part of the user to correct it in this case.

Modified:
    cfe/trunk/lib/Sema/SemaStmt.cpp
    cfe/trunk/test/SemaCXX/warn-top-level-comparison.cpp

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=137820&r1=137819&r2=137820&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Wed Aug 17 03:38:11 2011
@@ -147,21 +147,23 @@
   }
 
   SourceLocation Loc;
-  bool IsNotEqual = false;
+  bool IsNotEqual, CanAssign;
 
   if (const BinaryOperator *Op = dyn_cast<BinaryOperator>(E)) {
     if (Op->getOpcode() != BO_EQ && Op->getOpcode() != BO_NE)
       return;
 
-    IsNotEqual = Op->getOpcode() == BO_NE;
     Loc = Op->getOperatorLoc();
+    IsNotEqual = Op->getOpcode() == BO_NE;
+    CanAssign = Op->getLHS()->IgnoreParenImpCasts()->isLValue();
   } else if (const CXXOperatorCallExpr *Op = dyn_cast<CXXOperatorCallExpr>(E)) {
     if (Op->getOperator() != OO_EqualEqual &&
         Op->getOperator() != OO_ExclaimEqual)
       return;
 
-    IsNotEqual = Op->getOperator() == OO_ExclaimEqual;
     Loc = Op->getOperatorLoc();
+    IsNotEqual = Op->getOperator() == OO_ExclaimEqual;
+    CanAssign = Op->getArg(0)->IgnoreParenImpCasts()->isLValue();
   } else {
     // Not a typo-prone comparison.
     return;
@@ -181,12 +183,16 @@
         << FixItHint::CreateInsertion(Open, "(void)(")
         << FixItHint::CreateInsertion(Close, ")");
 
-  if (IsNotEqual)
-    S.Diag(Loc, diag::note_inequality_comparison_to_or_assign)
-      << FixItHint::CreateReplacement(Loc, "|=");
-  else
-    S.Diag(Loc, diag::note_equality_comparison_to_assign)
-      << FixItHint::CreateReplacement(Loc, "=");
+  // If the LHS is a plausible entity to assign to, provide a fixit hint to
+  // correct common typos.
+  if (CanAssign) {
+    if (IsNotEqual)
+      S.Diag(Loc, diag::note_inequality_comparison_to_or_assign)
+        << FixItHint::CreateReplacement(Loc, "|=");
+    else
+      S.Diag(Loc, diag::note_equality_comparison_to_assign)
+        << FixItHint::CreateReplacement(Loc, "=");
+  }
 }
 
 void Sema::DiagnoseUnusedExprResult(const Stmt *S) {

Modified: cfe/trunk/test/SemaCXX/warn-top-level-comparison.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-top-level-comparison.cpp?rev=137820&r1=137819&r2=137820&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-top-level-comparison.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-top-level-comparison.cpp Wed Aug 17 03:38:11 2011
@@ -17,6 +17,8 @@
   x != 7; // expected-warning {{inequality comparison as an unused top-level statement}} \
           // expected-note {{cast this comparison to void to silence this warning}} \
           // expected-note {{use '|=' to turn this inequality comparison into an or-assignment}}
+  7 == x; // expected-warning {{equality comparison as an unused top-level statement}} \
+          // expected-note {{cast this comparison to void to silence this warning}}
   p == p; // expected-warning {{equality comparison as an unused top-level statement}} \
           // expected-note {{cast this comparison to void to silence this warning}} \
           // expected-note {{use '=' to turn this equality comparison into an assignment}} \
@@ -30,6 +32,8 @@
   a != b; // expected-warning {{inequality comparison as an unused top-level statement}} \
           // expected-note {{cast this comparison to void to silence this warning}} \
           // expected-note {{use '|=' to turn this inequality comparison into an or-assignment}}
+  A() == b; // expected-warning {{equality comparison as an unused top-level statement}} \
+            // expected-note {{cast this comparison to void to silence this warning}}
   if (42) x == 7; // expected-warning {{equality comparison as an unused top-level statement}} \
                   // expected-note {{cast this comparison to void to silence this warning}} \
                   // expected-note {{use '=' to turn this equality comparison into an assignment}}





More information about the cfe-commits mailing list