Change CheckDeadStores to use Expr::isNullPointerConstant, which will correctly determine whether an expression is a null pointer constant.

Patch by Kovarththanan Rajaratnam!



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@89621 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Analysis/CheckDeadStores.cpp b/lib/Analysis/CheckDeadStores.cpp
index d5cb7ca..dd70ed7 100644
--- a/lib/Analysis/CheckDeadStores.cpp
+++ b/lib/Analysis/CheckDeadStores.cpp
@@ -134,16 +134,15 @@
 
       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>(RHS))
-              // FIXME: Probably should have an Expr::isNullPointerConstant.
-              if (L->getValue() == 0)
-                return;
+            if (B->getRHS()->isNullPointerConstant(Ctx,
+                                              Expr::NPC_ValueDependentIsNull))
+              return;
           }
+
+          Expr* RHS = B->getRHS()->IgnoreParenCasts();
           // Special case: self-assignments.  These are often used to shut up
           //  "unused variable" compiler warnings.
           if (DeclRefExpr* RhsDR = dyn_cast<DeclRefExpr>(RHS))
diff --git a/test/Analysis/dead-stores.c b/test/Analysis/dead-stores.c
index 811ac81..2dfb599 100644
--- a/test/Analysis/dead-stores.c
+++ b/test/Analysis/dead-stores.c
@@ -34,7 +34,7 @@
     
   k = 2;  // expected-warning {{never read}}
 }
-
+  
 void f5() {
 
   int x = 4; // no-warning
@@ -55,6 +55,24 @@
   return 1;
 }
 
+int f7b(int *p) {  
+  // This is allowed for defensive programming.
+  p = (0); // no-warning  
+  return 1;
+}
+
+int f7c(int *p) {  
+  // This is allowed for defensive programming.
+  p = (void*) 0; // no-warning  
+  return 1;
+}
+
+int f7d(int *p) {  
+  // This is allowed for defensive programming.
+  p = (void*) (0); // no-warning  
+  return 1;
+}
+
 int f8(int *p) {
   extern int *baz();
   if ((p = baz())) // expected-warning{{Although the value}}