[cfe-commits] r70218 - in /cfe/trunk: 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

Ted Kremenek kremenek at apple.com
Mon Apr 27 11:27:22 PDT 2009


Author: kremenek
Date: Mon Apr 27 13:27:22 2009
New Revision: 70218

URL: http://llvm.org/viewvc/llvm-project?rev=70218&view=rev
Log:
Add new checker-specific attribute 'objc_ownership_cfretain'. This is the same
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).

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

Modified: cfe/trunk/include/clang/AST/Attr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Attr.h?rev=70218&r1=70217&r2=70218&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Attr.h (original)
+++ cfe/trunk/include/clang/AST/Attr.h Mon Apr 27 13:27:22 2009
@@ -51,8 +51,9 @@
     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 @@
 };
 
 // Checker-specific attributes.
+DEF_SIMPLE_ATTR(ObjCOwnershipCFRetain)
 DEF_SIMPLE_ATTR(ObjCOwnershipRetain)
 DEF_SIMPLE_ATTR(ObjCOwnershipReturns)
 

Modified: cfe/trunk/include/clang/Parse/AttributeList.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/AttributeList.h?rev=70218&r1=70217&r2=70218&view=diff

==============================================================================
--- cfe/trunk/include/clang/Parse/AttributeList.h (original)
+++ cfe/trunk/include/clang/Parse/AttributeList.h Mon Apr 27 13:27:22 2009
@@ -76,10 +76,11 @@
     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,

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

==============================================================================
--- cfe/trunk/lib/Analysis/CFRefCount.cpp (original)
+++ cfe/trunk/lib/Analysis/CFRefCount.cpp Mon Apr 27 13:27:22 2009
@@ -1104,7 +1104,11 @@
       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;

Modified: cfe/trunk/lib/Frontend/PCHReaderDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHReaderDecl.cpp?rev=70218&r1=70217&r2=70218&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PCHReaderDecl.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReaderDecl.cpp Mon Apr 27 13:27:22 2009
@@ -475,6 +475,7 @@
 
     SIMPLE_ATTR(ObjCException);
     SIMPLE_ATTR(ObjCNSObject);
+    SIMPLE_ATTR(ObjCOwnershipCFRetain);
     SIMPLE_ATTR(ObjCOwnershipRetain);
     SIMPLE_ATTR(ObjCOwnershipReturns);
     SIMPLE_ATTR(Overloadable);

Modified: cfe/trunk/lib/Frontend/PCHWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHWriter.cpp?rev=70218&r1=70217&r2=70218&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriter.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriter.cpp Mon Apr 27 13:27:22 2009
@@ -1420,6 +1420,7 @@
 
     case Attr::ObjCException:
     case Attr::ObjCNSObject:
+    case Attr::ObjCOwnershipCFRetain:
     case Attr::ObjCOwnershipRetain:
     case Attr::ObjCOwnershipReturns:
     case Attr::Overloadable:

Modified: cfe/trunk/lib/Parse/AttributeList.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/AttributeList.cpp?rev=70218&r1=70217&r2=70218&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/AttributeList.cpp (original)
+++ cfe/trunk/lib/Parse/AttributeList.cpp Mon Apr 27 13:27:22 2009
@@ -136,12 +136,17 @@
   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;
 }

Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=70218&r1=70217&r2=70218&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Mon Apr 27 13:27:22 2009
@@ -1545,6 +1545,18 @@
   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 @@
     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;

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=70218&r1=70217&r2=70218&view=diff

==============================================================================
--- cfe/trunk/test/Analysis/retain-release-gc-only.m (original)
+++ cfe/trunk/test/Analysis/retain-release-gc-only.m Mon Apr 27 13:27:22 2009
@@ -93,3 +93,31 @@
   CFMakeCollectable(A);
   CFRetain(A);
 }
+
+//===----------------------------------------------------------------------===//
+// Tests of ownership attributes.
+//===----------------------------------------------------------------------===//
+
+ at 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;
+ at 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];
+}

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

==============================================================================
--- cfe/trunk/test/Analysis/retain-release.m (original)
+++ cfe/trunk/test/Analysis/retain-release.m Mon Apr 27 13:27:22 2009
@@ -412,6 +412,7 @@
 @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 @@
   [str release];
 }
 
+void test_attr_3(TestOwnershipAttr *X) {
+  NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}}
+  [X myCFRetain:str];
+  [str release];
+}
 





More information about the cfe-commits mailing list