[cfe-commits] r71397 - in /cfe/trunk: lib/Analysis/CFRefCount.cpp test/Analysis/retain-release-gc-only.m

Ted Kremenek kremenek at apple.com
Sun May 10 09:52:15 PDT 2009


Author: kremenek
Date: Sun May 10 11:52:15 2009
New Revision: 71397

URL: http://llvm.org/viewvc/llvm-project?rev=71397&view=rev
Log:
Add special warning about returning a retained object where a GC'ed object is expected.

Modified:
    cfe/trunk/lib/Analysis/CFRefCount.cpp
    cfe/trunk/test/Analysis/retain-release-gc-only.m

Modified: cfe/trunk/lib/Analysis/CFRefCount.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/CFRefCount.cpp?rev=71397&r1=71396&r2=71397&view=diff

==============================================================================
--- cfe/trunk/lib/Analysis/CFRefCount.cpp (original)
+++ cfe/trunk/lib/Analysis/CFRefCount.cpp Sun May 10 11:52:15 2009
@@ -2517,9 +2517,10 @@
   else if (RV->getKind() == RefVal::ErrorGCLeakReturned) {
     ObjCMethodDecl& MD = cast<ObjCMethodDecl>(BRC.getCodeDecl());
     os << " and returned from method '" << MD.getSelector().getAsString()
-       << "' is potentially leaked when using garbage collection.  Callers"
-          " of this method do not expect a +1 retain count since the return"
-          " type is an Objective-C object reference";
+       << "' is potentially leaked when using garbage collection.  Callers "
+          "of this method do not expect a returned object with a +1 retain "
+          "count since they expect the object to be managed by the garbage "
+          "collector";
   }
   else
     os << " is no longer referenced after this point and has a retain count of"
@@ -3073,11 +3074,12 @@
       if (isGCEnabled() && RE.getObjKind() == RetEffect::ObjC) {
         // Things are more complicated with garbage collection.  If the
         // returned object is suppose to be an Objective-C object, we have
-        // a leak (as the caller expects a GC'ed object).        
+        // a leak (as the caller expects a GC'ed object) because no
+        // method should return ownership unless it returns a CF object.
         X = X ^ RefVal::ErrorGCLeakReturned;
         
         // Keep this false until this is properly tested.
-        hasError = false;
+        hasError = true;
       }
       else if (!RE.isOwned()) {
         // Either we are using GC and the returned object is a CF type

Modified: cfe/trunk/test/Analysis/retain-release-gc-only.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/retain-release-gc-only.m?rev=71397&r1=71396&r2=71397&view=diff

==============================================================================
--- cfe/trunk/test/Analysis/retain-release-gc-only.m (original)
+++ cfe/trunk/test/Analysis/retain-release-gc-only.m Sun May 10 11:52:15 2009
@@ -128,13 +128,17 @@
 // is expected.
 @interface TestReturnNotOwnedWhenExpectedOwned
 - (NSString*)newString;
+- (CFMutableArrayRef)newArray;
 @end
 
 @implementation TestReturnNotOwnedWhenExpectedOwned
 - (NSString*)newString {
-  NSString *s = [NSString stringWithUTF8String:"hello"];  
-  // FIXME: Should this be an error anyway?
-  return s; // no-warning
+  NSString *s = [NSString stringWithUTF8String:"hello"]; // expected-warning{{Potential leak (when using garbage collection) of an object allocated on line 136 and stored into 's'}}
+  CFRetain(s);
+  return s;
+}
+- (CFMutableArrayRef)newArray{
+   return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
 }
 @end
 





More information about the cfe-commits mailing list