]> granicus.if.org Git - clang/commitdiff
Add special warning about returning a retained object where a GC'ed object is expected.
authorTed Kremenek <kremenek@apple.com>
Sun, 10 May 2009 16:52:15 +0000 (16:52 +0000)
committerTed Kremenek <kremenek@apple.com>
Sun, 10 May 2009 16:52:15 +0000 (16:52 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@71397 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/CFRefCount.cpp
test/Analysis/retain-release-gc-only.m

index 2715a9633f23ea97fe0c1af9945a01dce68f8887..4b254e07d59585565152a3f608f56e62feb30b87 100644 (file)
@@ -2517,9 +2517,10 @@ CFRefLeakReport::getEndPath(BugReporterContext& BRC,
   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 @@ void CFRefCount::EvalReturn(ExplodedNodeSet<GRState>& Dst,
       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
index 5aa39ec0a21e44f1fc7ebe90fb1c0c22ac19474c..70ad54f8aa4a82ecd7327b81cd69b5a905d84847 100644 (file)
@@ -128,13 +128,17 @@ void f3() {
 // 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