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

Ted Kremenek kremenek at apple.com
Sun Nov 22 12:26:21 PST 2009


Author: kremenek
Date: Sun Nov 22 14:26:21 2009
New Revision: 89621

URL: http://llvm.org/viewvc/llvm-project?rev=89621&view=rev
Log:
Change CheckDeadStores to use Expr::isNullPointerConstant, which will correctly determine whether an expression is a null pointer constant.

Patch by Kovarththanan Rajaratnam!


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=89621&r1=89620&r2=89621&view=diff

==============================================================================
--- cfe/trunk/lib/Analysis/CheckDeadStores.cpp (original)
+++ cfe/trunk/lib/Analysis/CheckDeadStores.cpp Sun Nov 22 14:26:21 2009
@@ -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))

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

==============================================================================
--- cfe/trunk/test/Analysis/dead-stores.c (original)
+++ cfe/trunk/test/Analysis/dead-stores.c Sun Nov 22 14:26:21 2009
@@ -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}}





More information about the cfe-commits mailing list