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

Ted Kremenek kremenek at apple.com
Mon May 5 16:12:22 PDT 2008


Author: kremenek
Date: Mon May  5 18:12:21 2008
New Revision: 50679

URL: http://llvm.org/viewvc/llvm-project?rev=50679&view=rev
Log:
Emit dead store warnings for ++ and -- operators.

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

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

==============================================================================
--- cfe/trunk/lib/Analysis/DeadStores.cpp (original)
+++ cfe/trunk/lib/Analysis/DeadStores.cpp Mon May  5 18:12:21 2008
@@ -35,6 +35,19 @@
   
   virtual ~DeadStoreObs() {}
   
+  void CheckDeclRef(DeclRefExpr* DR, Expr* Val,
+                    const LiveVariables::AnalysisDataTy& AD,
+                    const LiveVariables::ValTy& Live) {
+    
+    if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
+      if (VD->hasLocalStorage() && !Live(VD, AD)) {
+        SourceRange R = Val->getSourceRange();
+        Diags.Report(&Client,
+                     Ctx.getFullLoc(DR->getSourceRange().getBegin()),
+                     diag::warn_dead_store, 0, 0, &R, 1);
+      }
+  }
+  
   virtual void ObserveStmt(Stmt* S,
                            const LiveVariables::AnalysisDataTy& AD,
                            const LiveVariables::ValTy& Live) {
@@ -47,15 +60,18 @@
       if (!B->isAssignmentOp()) return; // Skip non-assignments.
       
       if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()))
-        if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
-          if (VD->hasLocalStorage() && !Live(VD,AD)) {
-            SourceRange R = B->getRHS()->getSourceRange();
-            Diags.Report(&Client,
-                         Ctx.getFullLoc(DR->getSourceRange().getBegin()),
-                         diag::warn_dead_store, 0, 0, &R, 1);                                                                        
-        }
+        CheckDeclRef(DR, B->getRHS(), AD, Live);
     }
-    else if(DeclStmt* DS = dyn_cast<DeclStmt>(S))
+    else if (UnaryOperator* U = dyn_cast<UnaryOperator>(S)) {
+      if (!U->isIncrementOp())
+        return;
+      
+      Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
+      
+      if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(Ex))
+        CheckDeclRef(DR, U, AD, Live);
+    }    
+    else if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
       // Iterate through the decls.  Warn if any initializers are complex
       // expressions that are not live (never used).
       for (ScopedDecl* SD = DS->getDecl(); SD; SD = SD->getNextDeclarator()) {        

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

==============================================================================
--- cfe/trunk/test/Analysis/dead-stores.c (original)
+++ cfe/trunk/test/Analysis/dead-stores.c Mon May  5 18:12:21 2008
@@ -35,4 +35,11 @@
   int x = 4; // no-warning
   int *p = &x; // expected-warning{{value stored to variable is never used}}
 
-}
\ No newline at end of file
+}
+
+int f6() {
+  
+  int x = 4;
+  ++x; // expected-warning{{value stored to variable is never used}}
+  return 1;
+}





More information about the cfe-commits mailing list