]> granicus.if.org Git - clang/commitdiff
Dead stores checker: Don't flag dead stores for self-assignments (common escape hatch...
authorTed Kremenek <kremenek@apple.com>
Fri, 9 Jan 2009 22:15:01 +0000 (22:15 +0000)
committerTed Kremenek <kremenek@apple.com>
Fri, 9 Jan 2009 22:15:01 +0000 (22:15 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@62010 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 51943d50166ad6b70f176f31b070f501ff369950..cad19f4f934dcfa6afe3fdca4e9d6433b940c288 100644 (file)
@@ -128,16 +128,23 @@ public:
       
       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 
index 71c4e3b28bf0e34368c25dd91e859d7abc618abc..7f99499aabb68852b51bd07a47a84e77e03a18c7 100644 (file)
@@ -127,3 +127,8 @@ int f16(int x) {
   return x;
 }
 
+// Self-assignments should not be flagged as dead stores.
+int f17() {
+  int x = 1;
+  x = x; // no-warning
+}