]> granicus.if.org Git - clang/commitdiff
Add new checker-specific attribute 'objc_ownership_cfretain'. This is the same
authorTed Kremenek <kremenek@apple.com>
Mon, 27 Apr 2009 18:27:22 +0000 (18:27 +0000)
committerTed Kremenek <kremenek@apple.com>
Mon, 27 Apr 2009 18:27:22 +0000 (18:27 +0000)
as 'objc_ownership_cfretain' except that the method acts like a CFRetain instead
of a [... retain] (important in GC modes). Checker support is wired up, but
currently only for Objective-C message expressions (not function calls).

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

include/clang/AST/Attr.h
include/clang/Parse/AttributeList.h
lib/Analysis/CFRefCount.cpp
lib/Frontend/PCHReaderDecl.cpp
lib/Frontend/PCHWriter.cpp
lib/Parse/AttributeList.cpp
lib/Sema/SemaDeclAttr.cpp
test/Analysis/retain-release-gc-only.m
test/Analysis/retain-release.m

index 549ba632a4d6705d063dd7bee7504d7ea352a7a1..bde0c9b705cca2d32b903337fdf70b98c8c5cfee 100644 (file)
@@ -51,8 +51,9 @@ public:
     NonNull,
     ObjCException,
     ObjCNSObject,
-    ObjCOwnershipRetain, // Clang/Checker-specific.
-    ObjCOwnershipReturns, // Clang/Checker-specific.
+    ObjCOwnershipRetain,   // Clang/Checker-specific.
+    ObjCOwnershipCFRetain, // Clang/Checker-specific.
+    ObjCOwnershipReturns,  // Clang/Checker-specific.
     Overloadable, // Clang-specific
     Packed,
     Pure,
@@ -600,6 +601,7 @@ public:\
 };
 
 // Checker-specific attributes.
+DEF_SIMPLE_ATTR(ObjCOwnershipCFRetain)
 DEF_SIMPLE_ATTR(ObjCOwnershipRetain)
 DEF_SIMPLE_ATTR(ObjCOwnershipReturns)
 
index 6f4e2f2a08c44ff2fa1c8cf742b726f5f1dd125f..616d293fd69d2a33a29a5a061a628892224c305f 100644 (file)
@@ -76,10 +76,11 @@ public:
     AT_nothrow,
     AT_nsobject,
     AT_objc_exception,
-    AT_objc_ownership_retain,  // Clang-specific.
-    AT_objc_ownership_returns, // Clang-specific.
+    AT_objc_ownership_cfretain, // Clang-specific.
+    AT_objc_ownership_retain,   // Clang-specific.
+    AT_objc_ownership_returns,  // Clang-specific.
     AT_objc_gc,
-    AT_overloadable,           // Clang-specific.
+    AT_overloadable,            // Clang-specific.
     AT_packed,
     AT_pure,
     AT_regparm,
index c2369abb2cf6bb35a28b78869110bfbb677f935d..6223e7cdba5412cee4951e96a82c3a1ca1d335c1 100644 (file)
@@ -1104,7 +1104,11 @@ RetainSummaryManager::getMethodSummaryFromAnnotations(ObjCMethodDecl *MD) {
       ScratchArgs.push_back(std::make_pair(i, IncRefMsg));
       hasArgEffect = true;
     }
-}
+    else if ((*I)->getAttr<ObjCOwnershipCFRetainAttr>()) {
+      ScratchArgs.push_back(std::make_pair(i, IncRef));
+      hasArgEffect = true;
+    }    
+  }
   
   if (!hasRetEffect && !hasArgEffect)
     return 0;
index 508d2ef170ccd17b8b38fa5ed8163300d9e64fb0..50cc031225510f3edb29aa611fea9640f44ed13b 100644 (file)
@@ -475,6 +475,7 @@ Attr *PCHReader::ReadAttributes() {
 
     SIMPLE_ATTR(ObjCException);
     SIMPLE_ATTR(ObjCNSObject);
+    SIMPLE_ATTR(ObjCOwnershipCFRetain);
     SIMPLE_ATTR(ObjCOwnershipRetain);
     SIMPLE_ATTR(ObjCOwnershipReturns);
     SIMPLE_ATTR(Overloadable);
index 82eb5dffe4358dc0ae4d2983b8c7a95eaf7ca31a..391a1f91659010d52186ce73beeb3a0bf26b11f8 100644 (file)
@@ -1420,6 +1420,7 @@ void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
 
     case Attr::ObjCException:
     case Attr::ObjCNSObject:
+    case Attr::ObjCOwnershipCFRetain:
     case Attr::ObjCOwnershipRetain:
     case Attr::ObjCOwnershipReturns:
     case Attr::Overloadable:
index a9c552ba179c66200fc4d9a450f716a27da056d9..9e46159153cbcf92f9d9daab59e31b70af981b57 100644 (file)
@@ -136,12 +136,17 @@ AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name) {
   case 21:
     if (!memcmp(Str, "objc_ownership_retain", 21))
       return AT_objc_ownership_retain;      
+    break;
   case 22:
     if (!memcmp(Str, "objc_ownership_returns", 22))
       return AT_objc_ownership_returns;
     if (!memcmp(Str, "no_instrument_function", 22))
       return AT_no_instrument_function;
     break;
+  case 23:
+    if (!memcmp(Str, "objc_ownership_cfretain", 23))
+      return AT_objc_ownership_cfretain;
+    break;
   }
   return UnknownAttribute;
 }
index c0a3b44c43ef9d3abc0290a85a19f16def654758..7bbfb2695413f468f59e0bdcdb0c56c74e7865ad 100644 (file)
@@ -1545,6 +1545,18 @@ static void HandleObjCOwnershipRetainAttr(Decl *d, const AttributeList &Attr,
   d->addAttr(::new (S.Context) ObjCOwnershipRetainAttr());
 }
 
+static void HandleObjCOwnershipCFRetainAttr(Decl *d, const AttributeList &Attr,
+                                            Sema &S) {
+  
+  if (!isa<ParmVarDecl>(d)) {
+    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
+    "objc_ownership_cfretain" << 4 /* parameter */;
+    return;
+  }
+  
+  d->addAttr(::new (S.Context) ObjCOwnershipCFRetainAttr());
+}
+
 //===----------------------------------------------------------------------===//
 // Top Level Sema Entry Points
 //===----------------------------------------------------------------------===//
@@ -1587,6 +1599,8 @@ static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
     HandleObjCOwnershipRetainAttr(D, Attr, S); break;
   case AttributeList::AT_objc_ownership_returns:
     HandleObjCOwnershipReturnsAttr(D, Attr, S); break;
+  case AttributeList::AT_objc_ownership_cfretain:
+    HandleObjCOwnershipCFRetainAttr(D, Attr, S); break;
 
   case AttributeList::AT_packed:      HandlePackedAttr    (D, Attr, S); break;
   case AttributeList::AT_section:     HandleSectionAttr   (D, Attr, S); break;
index b378b048ce4b27f0fb8f9bf360dba80131ea7c3a..5bf0702cf5b31fbfb73475bfebd22bf0fc33aeaf 100644 (file)
@@ -93,3 +93,31 @@ void f3() {
   CFMakeCollectable(A);
   CFRetain(A);
 }
+
+//===----------------------------------------------------------------------===//
+// Tests of ownership attributes.
+//===----------------------------------------------------------------------===//
+
+@interface TestOwnershipAttr : NSObject
+- (NSString*) returnsAnOwnedString __attribute__((objc_ownership_returns));
+- (void) myRetain:(id)__attribute__((objc_ownership_retain))obj;
+- (void) myCFRetain:(id)__attribute__((objc_ownership_cfretain))obj;
+@end
+
+void test_attr_1(TestOwnershipAttr *X) {
+  NSString *str = [X returnsAnOwnedString]; // no-warning
+}
+
+void test_attr_2(TestOwnershipAttr *X) {
+  NSString *str = [X returnsAnOwnedString]; // no-warning
+  [X myRetain:str];
+  [str release];
+}
+
+void test_attr_3(TestOwnershipAttr *X) {
+  // FIXME: This should be a leak.  Need to change the analyzer to
+  // to track Objective-C objects retain counts even in GC mode.
+  NSString *str = [X returnsAnOwnedString]; // no-warning
+  [X myCFRetain:str];
+  [str release];
+}
index ae1b75c2162df16e7c045286b43c367acbbada5d..b547aa0373ddbc333748bb6e0b30877b97c77417 100644 (file)
@@ -412,6 +412,7 @@ void rdar6704930(unsigned char *s, unsigned int length) {
 @interface TestOwnershipAttr : NSObject
 - (NSString*) returnsAnOwnedString __attribute__((objc_ownership_returns));
 - (void) myRetain:(id)__attribute__((objc_ownership_retain))obj;
+- (void) myCFRetain:(id)__attribute__((objc_ownership_cfretain))obj;
 @end
 
 void test_attr_1(TestOwnershipAttr *X) {
@@ -424,4 +425,9 @@ void test_attr_2(TestOwnershipAttr *X) {
   [str release];
 }
 
+void test_attr_3(TestOwnershipAttr *X) {
+  NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}}
+  [X myCFRetain:str];
+  [str release];
+}