[cfe-commits] r62010 - in /cfe/trunk: lib/Analysis/CheckDeadStores.cpp test/Analysis/dead-stores.c

Ted Kremenek kremenek at apple.com
Fri Jan 9 14:15:01 PST 2009


Author: kremenek
Date: Fri Jan  9 16:15:01 2009
New Revision: 62010

URL: http://llvm.org/viewvc/llvm-project?rev=62010&view=rev
Log:
Dead stores checker: Don't flag dead stores for self-assignments (common escape hatch for 'unused variable' warnings).

Modified:
    cfe/trunk/lib/Analysis/CheckDeadStores.cpp
    cfe/trunk/test/Analysis/dead-stores.c

Modified: cfe/trunk/lib/Analysis/CheckDeadStores.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/CheckDeadStores.cpp?rev=62010&r1=62009&r2=62010&view=diff

==============================================================================
--- cfe/trunk/lib/Analysis/CheckDeadStores.cpp (original)
+++ cfe/trunk/lib/Analysis/CheckDeadStores.cpp Fri Jan  9 16:15:01 2009
@@ -128,16 +128,23 @@
       
       if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()))
         if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
+          Expr* RHS = B->getRHS()->IgnoreParenCasts();
+          
           // Special case: check for assigning null to a pointer.
           //  This is a common form of defensive programming.          
           if (VD->getType()->isPointerType()) {
-            if (IntegerLiteral* L =
-                  dyn_cast<IntegerLiteral>(B->getRHS()->IgnoreParenCasts()))
+            if (IntegerLiteral* L = dyn_cast<IntegerLiteral>(RHS))
               // FIXME: Probably should have an Expr::isNullPointerConstant.              
               if (L->getValue() == 0)
                 return;
           }
-
+          // Special case: self-assignments.  These are often used to shut up
+          //  "unused variable" compiler warnings.
+          if (DeclRefExpr* RhsDR = dyn_cast<DeclRefExpr>(RHS))
+            if (VD == dyn_cast<VarDecl>(RhsDR->getDecl()))
+              return;
+            
+          // Otherwise, issue a warning.
           DeadStoreKind dsk = 
             Parents.isSubExpr(B)
             ? Enclosing 

Modified: cfe/trunk/test/Analysis/dead-stores.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/dead-stores.c?rev=62010&r1=62009&r2=62010&view=diff

==============================================================================
--- cfe/trunk/test/Analysis/dead-stores.c (original)
+++ cfe/trunk/test/Analysis/dead-stores.c Fri Jan  9 16:15:01 2009
@@ -127,3 +127,8 @@
   return x;
 }
 
+// Self-assignments should not be flagged as dead stores.
+int f17() {
+  int x = 1;
+  x = x; // no-warning
+}





More information about the cfe-commits mailing list