]> granicus.if.org Git - clang/commitdiff
Emit dead store warnings for ++ and -- operators.
authorTed Kremenek <kremenek@apple.com>
Mon, 5 May 2008 23:12:21 +0000 (23:12 +0000)
committerTed Kremenek <kremenek@apple.com>
Mon, 5 May 2008 23:12:21 +0000 (23:12 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@50679 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/DeadStores.cpp
test/Analysis/dead-stores.c

index 6858e3ab370c40d3edd9d8730a72908899eeb9cf..f7523e508f08ddb3cd3243e82bad6adfc9e61e87 100644 (file)
@@ -35,6 +35,19 @@ public:
   
   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 @@ public:
       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()) {        
index 58960605f0840be02be6e1ab4d4d88833edec077..2ec9b48f0065b25c75c3d691ffb3f8d76bb5a4d2 100644 (file)
@@ -35,4 +35,11 @@ void f5() {
   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;
+}