[cfe-commits] r67390 - in /cfe/trunk: lib/Sema/SemaExpr.cpp test/Sema/self-comparison.c

Ted Kremenek kremenek at apple.com
Fri Mar 20 11:35:54 PDT 2009


Author: kremenek
Date: Fri Mar 20 13:35:45 2009
New Revision: 67390

URL: http://llvm.org/viewvc/llvm-project?rev=67390&view=rev
Log:
Fix <rdar://problem/6703892> by not warning about self-comparisons of enum
constants.

Modified:
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/Sema/self-comparison.c

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=67390&r1=67389&r2=67390&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Fri Mar 20 13:35:45 2009
@@ -3276,11 +3276,15 @@
     // For non-floating point types, check for self-comparisons of the form
     // x == x, x != x, x < x, etc.  These always evaluate to a constant, and
     // often indicate logic errors in the program.
+    // NOTE: Per <rdar://problem/6703892>, don't warn about comparisons of enum
+    //  constants.  These can arise from macro expansions, and are usually quite
+    //  deliberate.
     Expr *LHSStripped = lex->IgnoreParens();
     Expr *RHSStripped = rex->IgnoreParens();
     if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped))
       if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped))
-        if (DRL->getDecl() == DRR->getDecl())
+        if (DRL->getDecl() == DRR->getDecl() &&
+            !isa<EnumConstantDecl>(DRL->getDecl()))
           Diag(Loc, diag::warn_selfcomparison);
     
     if (isa<CastExpr>(LHSStripped))

Modified: cfe/trunk/test/Sema/self-comparison.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/self-comparison.c?rev=67390&r1=67389&r2=67390&view=diff

==============================================================================
--- cfe/trunk/test/Sema/self-comparison.c (original)
+++ cfe/trunk/test/Sema/self-comparison.c Fri Mar 20 13:35:45 2009
@@ -23,3 +23,11 @@
 int bar2(float x) {
   return x != x; // no-warning
 }
+
+// Motivated by <rdar://problem/6703892>, self-comparisons of enum constants
+// should not be warned about.  These can be expanded from macros, and thus
+// are usually deliberate.
+int compare_enum() {
+  enum { A };
+  return A == A; // no-warning
+}





More information about the cfe-commits mailing list