]> granicus.if.org Git - clang/commitdiff
Don't warn about extraneous '()' around a comparison if it occurs within a macro.
authorTed Kremenek <kremenek@apple.com>
Tue, 1 Feb 2011 22:36:09 +0000 (22:36 +0000)
committerTed Kremenek <kremenek@apple.com>
Tue, 1 Feb 2011 22:36:09 +0000 (22:36 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@124695 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaExpr.cpp
test/SemaCXX/warn-assignment-condition.cpp

index 6fe111fac73c2815c147e37844ea4825c95e5b6f..ab190276af3abd67d67fc85b9bb8c668000fe9c4 100644 (file)
@@ -9240,15 +9240,18 @@ void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *parenE) {
         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());
+      }
     }
 }
 
index 7596bb26aeaa4facc7ba108f72f35e0bb6d4d859..ab9d2ad4a5757ea02ce7bca707ea0f87601ef829 100644 (file)
@@ -124,3 +124,13 @@ void test2() {
                           // 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;
+}
+