]> granicus.if.org Git - clang/commitdiff
Per discussions with Ken Ferry and Paul Marks (<rdar://problem/6815234>) greatly
authorTed Kremenek <kremenek@apple.com>
Thu, 23 Apr 2009 19:11:35 +0000 (19:11 +0000)
committerTed Kremenek <kremenek@apple.com>
Thu, 23 Apr 2009 19:11:35 +0000 (19:11 +0000)
extend the number of objects tracked by the retain/release checker by assuming
that all class and instance methods should follow Cocoa object "getter" and
"alloc/new" conventions.

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

lib/Analysis/CFRefCount.cpp
test/Analysis/NSString.m
test/Analysis/retain-release.m

index 0e8d67ca292aca8d6b74705e14dc949984975bc8..2dfacfd42e185324e8a368c4e5c933b713826ed4 100644 (file)
@@ -1074,15 +1074,18 @@ RetainSummaryManager::getMethodSummary(ObjCMessageExpr* ME,
   if (!isTrackedObjectType(Ctx.getCanonicalType(ME->getType())))
     return 0;
 
-  if (followsFundamentalRule(s)) {    
-    RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
-                                : RetEffect::MakeOwned(RetEffect::ObjC, true);
-    RetainSummary* Summ = getPersistentSummary(E);
-    ObjCMethodSummaries[ME] = Summ;
-    return Summ;
-  }
+  // EXPERIMENTAL: Assume the Cocoa conventions for all objects returned
+  //  by instance methods.
+
+  RetEffect E =
+    followsFundamentalRule(s)
+    ? (isGCEnabled() ? RetEffect::MakeNotOwned(RetEffect::ObjC)
+                     : RetEffect::MakeOwned(RetEffect::ObjC, true))
+    : RetEffect::MakeNotOwned(RetEffect::ObjC);
   
-  return 0;
+  RetainSummary* Summ = getPersistentSummary(E);
+  ObjCMethodSummaries[ME] = Summ;
+  return Summ;
 }
 
 RetainSummary*
@@ -1099,7 +1102,17 @@ RetainSummaryManager::getClassMethodSummary(IdentifierInfo* ClsName,
   if (I != ObjCClassMethodSummaries.end())
     return I->second;
   
-  return 0;
+  // EXPERIMENTAL: Assume the Cocoa conventions for all objects returned
+  //  by class methods.
+  const char* s = S.getIdentifierInfoForSlot(0)->getName();
+  RetEffect E = followsFundamentalRule(s)
+    ? (isGCEnabled() ? RetEffect::MakeNotOwned(RetEffect::ObjC)
+       : RetEffect::MakeOwned(RetEffect::ObjC, true))
+    : RetEffect::MakeNotOwned(RetEffect::ObjC);
+  
+  RetainSummary* Summ = getPersistentSummary(E);
+  ObjCClassMethodSummaries[ObjCSummaryKey(ClsName, S)] = Summ;
+  return Summ;
 }
 
 void RetainSummaryManager::InitializeClassMethodSummaries() {
@@ -2351,9 +2364,8 @@ namespace {
     BadRelease(CFRefCount* tf) : CFRefBug(tf, "bad release") {}
 
     const char* getDescription() const {
-      return "Incorrect decrement of the reference count of a "
-      "Core Foundation object ("
-      "the object is not owned at this point by the caller)";
+      return "Incorrect decrement of the reference count of an "
+             "object is not owned at this point by the caller";
     }
   };
   
index 6e644d5fbc814411c2110d50171249d3fa37648a..a95e8868c3c9e0e0c621b943933b9a685ad4ec00 100644 (file)
@@ -73,6 +73,7 @@ typedef NSUInteger NSStringCompareOptions;
 - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(id)locale;
 - (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;
 - (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator;
++ (id)stringWithFormat:(NSString *)format, ... __attribute__((format(__NSString__, 1, 2)));
 @end
 @interface NSSimpleCString : NSString {} @end
 @interface NSConstantString : NSSimpleCString @end
@@ -243,3 +244,10 @@ void test_objc_atomicCompareAndSwap() {
     [old release];
 }
 
+// Test stringWithFormat (<rdar://problem/6815234>)
+void test_stringWithFormat() {  
+  NSString *string = [[NSString stringWithFormat:@"%ld", (long) 100] retain];
+  [string release];
+  [string release]; // expected-warning{{Incorrect decrement of the reference count}}
+}
+
index 5a0471ae88e2df0bc231e8f2504681e4933db908..012e340f563e80c3819eef968dfb50ff6575a7bc 100644 (file)
@@ -335,7 +335,7 @@ static void rdar_6659160(char *inkind, char *inname)
     return;
 
   [kind release];
-  [name release];
+  [name release]; // expected-warning{{Incorrect decrement of the reference count}}
 }
 
 // PR 3677 - 'allocWithZone' should be treated as following the Cocoa naming