[cfe-commits] r124695 - in /cfe/trunk: lib/Sema/SemaExpr.cpp test/SemaCXX/warn-assignment-condition.cpp

Ted Kremenek kremenek at apple.com
Tue Feb 1 14:36:09 PST 2011


Author: kremenek
Date: Tue Feb  1 16:36:09 2011
New Revision: 124695

URL: http://llvm.org/viewvc/llvm-project?rev=124695&view=rev
Log:
Don't warn about extraneous '()' around a comparison if it occurs within a macro.

Macros frequently contain extra '()' to make instantiation less error prone.
This warning was flagging a ton of times on postgresql because of its use of macros.

Modified:
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/SemaCXX/warn-assignment-condition.cpp

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=124695&r1=124694&r2=124695&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Tue Feb  1 16:36:09 2011
@@ -9240,15 +9240,18 @@
         opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context)
                                                            == Expr::MLV_Valid) {
       SourceLocation Loc = opE->getOperatorLoc();
-
-      Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange();
-
-      Diag(Loc, diag::note_equality_comparison_to_assign)
-        << FixItHint::CreateReplacement(Loc, "=");
-
-      Diag(Loc, diag::note_equality_comparison_silence)
-        << FixItHint::CreateRemoval(parenE->getSourceRange().getBegin())
-        << FixItHint::CreateRemoval(parenE->getSourceRange().getEnd());
+      
+      // Don't emit a warning if the operation occurs within a macro.
+      // Sometimes extra parentheses are used within macros to make the
+      // instantiation of the macro less error prone.
+      if (!Loc.isMacroID()) {
+        Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange();
+        Diag(Loc, diag::note_equality_comparison_to_assign)
+          << FixItHint::CreateReplacement(Loc, "=");
+        Diag(Loc, diag::note_equality_comparison_silence)
+          << FixItHint::CreateRemoval(parenE->getSourceRange().getBegin())
+          << FixItHint::CreateRemoval(parenE->getSourceRange().getEnd());
+      }
     }
 }
 

Modified: cfe/trunk/test/SemaCXX/warn-assignment-condition.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-assignment-condition.cpp?rev=124695&r1=124694&r2=124695&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-assignment-condition.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-assignment-condition.cpp Tue Feb  1 16:36:09 2011
@@ -124,3 +124,13 @@
                           // expected-note {{remove extraneous parentheses around the comparison to silence this warning}}
     if ((test2 == fn)) {}
 }
+
+// Do not warn about extra '()' used within a macro.  This pattern
+// occurs frequently.
+#define COMPARE(x,y) (x == y)
+int test3(int x, int y) {
+  if (COMPARE(x, y)) // no-warning
+    return 0;
+  return 1;
+}
+





More information about the cfe-commits mailing list