]> granicus.if.org Git - clang/commitdiff
Dead emit dead store warnings when assigning nil to an ObjC object
authorTed Kremenek <kremenek@apple.com>
Tue, 23 Feb 2010 21:19:33 +0000 (21:19 +0000)
committerTed Kremenek <kremenek@apple.com>
Tue, 23 Feb 2010 21:19:33 +0000 (21:19 +0000)
pointer (for defensive programming).  This matches the behavior with
assigning NULL to a regular pointer.  Fixes <rdar://problem/7631278>.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@96985 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Checker/CheckDeadStores.cpp
test/Analysis/dead-stores.m

index 4a7ca705488a2674864d55880a1726c6f37f2b74..31f9390e62282d9d5eda64079c6f41ffbae989e3 100644 (file)
@@ -142,7 +142,8 @@ public:
         if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
           // Special case: check for assigning null to a pointer.
           //  This is a common form of defensive programming.
-          if (VD->getType()->isPointerType()) {
+          QualType T = VD->getType();
+          if (T->isPointerType() || T->isObjCObjectPointerType()) {
             if (B->getRHS()->isNullPointerConstant(Ctx,
                                               Expr::NPC_ValueDependentIsNull))
               return;
index 765a24a3c355f70acff33f1813d077ba41aeef08..701e5802b25e5f9cc1c1331b43d41a81f299b059 100644 (file)
@@ -34,3 +34,10 @@ void DeadStoreTest(NSObject *anObject) {
       ([keys containsObject:@"name"] && [keys containsObject:@"icon"])) {}
 }
 
+// This test case was a false positive due to how clang models
+// pointer types and ObjC object pointer types differently.  Here
+// we don't warn about a dead store because 'nil' is assigned to
+// an object pointer for the sake of defensive programming.
+void rdar_7631278(NSObject *x) {
+  x = ((void*)0);
+}