[cfe-commits] r70952 - 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 May 4 17:46:12 PDT 2009


Author: kremenek
Date: Mon May  4 19:46:09 2009
New Revision: 70952

URL: http://llvm.org/viewvc/llvm-project?rev=70952&view=rev
Log:
Implement attribute 'cf_returns_owned' (mirrors 'ns_returns_owned').

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=70952&r1=70951&r2=70952&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Attr.h (original)
+++ cfe/trunk/include/clang/AST/Attr.h Mon May  4 19:46:09 2009
@@ -61,6 +61,7 @@
     ObjCNSObject,
     CFOwnershipRelease,       // Clang/Checker-specific.
     CFOwnershipRetain,        // Clang/Checker-specific.
+    CFOwnershipReturns,       // Clang/Checker-specific.
     NSOwnershipRelease,         // Clang/Checker-specific.
     NSOwnershipRetain,          // Clang/Checker-specific.
     NSOwnershipReturns,         // Clang/Checker-specific.
@@ -135,7 +136,7 @@
 class ATTR##Attr : public Attr {                                        \
 public:                                                                 \
   ATTR##Attr() : Attr(ATTR) {}                                          \
-  virtual Attr *clone(ASTContext &C) const { return ::new (C) ATTR##Attr; }                \
+  virtual Attr *clone(ASTContext &C) const { return ::new (C) ATTR##Attr; }\
   static bool classof(const Attr *A) { return A->getKind() == ATTR; }   \
   static bool classof(const ATTR##Attr *A) { return true; }             \
 }
@@ -466,8 +467,9 @@
 
 // Checker-specific attributes.
 DEF_SIMPLE_ATTR(CFOwnershipRelease);
-DEF_SIMPLE_ATTR(NSOwnershipRelease);
 DEF_SIMPLE_ATTR(CFOwnershipRetain);
+DEF_SIMPLE_ATTR(CFOwnershipReturns);
+DEF_SIMPLE_ATTR(NSOwnershipRelease);
 DEF_SIMPLE_ATTR(NSOwnershipRetain);
 DEF_SIMPLE_ATTR(NSOwnershipReturns);
 

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

==============================================================================
--- cfe/trunk/include/clang/Parse/AttributeList.h (original)
+++ cfe/trunk/include/clang/Parse/AttributeList.h Mon May  4 19:46:09 2009
@@ -78,11 +78,12 @@
     AT_objc_exception,
     AT_cf_releases,        // Clang-specific.
     AT_cf_retains,         // Clang-specific.
-    AT_ns_releases,          // Clang-specific.
-    AT_ns_retains,           // Clang-specific.
-    AT_ns_returns_owned,          // Clang-specific.
+    AT_cf_returns_owned,   // Clang-specific.
+    AT_ns_releases,        // Clang-specific.
+    AT_ns_retains,         // Clang-specific.
+    AT_ns_returns_owned,   // 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=70952&r1=70951&r2=70952&view=diff

==============================================================================
--- cfe/trunk/lib/Analysis/CFRefCount.cpp (original)
+++ cfe/trunk/lib/Analysis/CFRefCount.cpp Mon May  4 19:46:09 2009
@@ -1128,6 +1128,9 @@
                         ? RetEffect::MakeGCNotOwned()
                         : RetEffect::MakeOwned(RetEffect::ObjC, true));
     }
+    else if (FD->getAttr<CFOwnershipReturnsAttr>()) {
+      Summ.setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true));
+    }
   }
   
   // Determine if there are any arguments with a specific ArgEffect.
@@ -1150,6 +1153,9 @@
                         ? RetEffect::MakeGCNotOwned()
                         : RetEffect::MakeOwned(RetEffect::ObjC, true));
     }
+    else if (MD->getAttr<CFOwnershipReturnsAttr>()) {
+      Summ.setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true));
+    }    
   }
   
   // Determine if there are any arguments with a specific ArgEffect.

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

==============================================================================
--- cfe/trunk/lib/Frontend/PCHReaderDecl.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReaderDecl.cpp Mon May  4 19:46:09 2009
@@ -477,6 +477,7 @@
     SIMPLE_ATTR(ObjCNSObject);
     SIMPLE_ATTR(CFOwnershipRelease);
     SIMPLE_ATTR(CFOwnershipRetain);
+    SIMPLE_ATTR(CFOwnershipReturns);
     SIMPLE_ATTR(NSOwnershipRelease);
     SIMPLE_ATTR(NSOwnershipRetain);
     SIMPLE_ATTR(NSOwnershipReturns);

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

==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriter.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriter.cpp Mon May  4 19:46:09 2009
@@ -1551,6 +1551,7 @@
     case Attr::ObjCNSObject:
     case Attr::CFOwnershipRelease:
     case Attr::CFOwnershipRetain:
+    case Attr::CFOwnershipReturns:
     case Attr::NSOwnershipRelease:
     case Attr::NSOwnershipRetain:
     case Attr::NSOwnershipReturns:

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

==============================================================================
--- cfe/trunk/lib/Parse/AttributeList.cpp (original)
+++ cfe/trunk/lib/Parse/AttributeList.cpp Mon May  4 19:46:09 2009
@@ -132,6 +132,7 @@
     break;
   case 16:
     if (!memcmp(Str, "ns_returns_owned", 16)) return AT_ns_returns_owned;
+    if (!memcmp(Str, "cf_returns_owned", 16)) return AT_cf_returns_owned;
     break;      
   case 17:
     if (!memcmp(Str, "transparent_union", 17)) return AT_transparent_union;

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Mon May  4 19:46:09 2009
@@ -1548,15 +1548,37 @@
 //===----------------------------------------------------------------------===//
 
 static void HandleNSOwnershipReturnsAttr(Decl *d, const AttributeList &Attr,
-                                           Sema &S) {
+                                         Sema &S) {
 
   if (!isa<ObjCMethodDecl>(d) && !isa<FunctionDecl>(d)) {
+    const char *name;
+    
+    switch (Attr.getKind()) {
+      default:
+        assert(0 && "invalid ownership attribute");
+        return;
+      case AttributeList::AT_cf_returns_owned:
+        name = "cf_returns_owned"; break;
+      case AttributeList::AT_ns_returns_owned:
+        name = "ns_returns_owned"; break;
+    };
+
     S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
-      "ns_returns_owned" << 3 /* function or method */;
+      name << 3 /* function or method */;
     return;
   }
   
-  d->addAttr(::new (S.Context) NSOwnershipReturnsAttr());
+  switch (Attr.getKind()) {
+    default:
+      assert(0 && "invalid ownership attribute");
+      return;
+    case AttributeList::AT_cf_returns_owned:
+      d->addAttr(::new (S.Context) CFOwnershipReturnsAttr());
+      return;
+    case AttributeList::AT_ns_returns_owned:
+      d->addAttr(::new (S.Context) NSOwnershipReturnsAttr());
+      return;
+  };
 }
 
 static void HandleNSOwnershipAttr(Decl *d, const AttributeList &Attr,
@@ -1579,9 +1601,9 @@
         name = "ns_retains"; break;
     };
 
-    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << name
-      << (attachToMethodDecl ? 5 /* parameter or method decl */ 
-                             : 4 /* parameter */);
+    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
+      name << (attachToMethodDecl ? 5 /* parameter or method decl */ 
+                                  : 4 /* parameter */);
     return;
   }
   
@@ -1590,13 +1612,17 @@
       assert(0 && "invalid ownership attribute");
       return;
     case AttributeList::AT_cf_releases:
-      d->addAttr(::new (S.Context) CFOwnershipReleaseAttr()); return;      
+      d->addAttr(::new (S.Context) CFOwnershipReleaseAttr());
+      return;
     case AttributeList::AT_cf_retains:
-      d->addAttr(::new (S.Context) CFOwnershipRetainAttr()); return;
+      d->addAttr(::new (S.Context) CFOwnershipRetainAttr());
+      return;
     case AttributeList::AT_ns_releases:
-      d->addAttr(::new (S.Context) NSOwnershipReleaseAttr());   return;
+      d->addAttr(::new (S.Context) NSOwnershipReleaseAttr());
+      return;
     case AttributeList::AT_ns_retains:
-      d->addAttr(::new (S.Context) NSOwnershipRetainAttr());   return;
+      d->addAttr(::new (S.Context) NSOwnershipRetainAttr());
+      return;
   }
 }
 
@@ -1645,6 +1671,7 @@
   case AttributeList::AT_ns_retains:
       HandleNSOwnershipAttr(D, Attr, S, true); break;
   case AttributeList::AT_ns_returns_owned:
+  case AttributeList::AT_cf_returns_owned:
     HandleNSOwnershipReturnsAttr(D, Attr, S); break;
 
   case AttributeList::AT_packed:      HandlePackedAttr    (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=70952&r1=70951&r2=70952&view=diff

==============================================================================
--- cfe/trunk/test/Analysis/retain-release-gc-only.m (original)
+++ cfe/trunk/test/Analysis/retain-release-gc-only.m Mon May  4 19:46:09 2009
@@ -130,6 +130,7 @@
 
 @interface TestOwnershipAttr : NSObject
 - (NSString*) returnsAnOwnedString __attribute__((ns_returns_owned));
+- (NSString*) returnsAnOwnedCFString  __attribute__((cf_returns_owned));
 - (void) myRetain:(id)__attribute__((ns_retains))obj;
 - (void) myCFRetain:(id)__attribute__((cf_retains))obj;
 - (void) myRelease:(id)__attribute__((ns_releases))obj;
@@ -140,12 +141,22 @@
   NSString *str = [X returnsAnOwnedString]; // no-warning
 }
 
+void test_attr_1b(TestOwnershipAttr *X) {
+  NSString *str = [X returnsAnOwnedCFString]; // expected-warning{{leak}}
+}
+
 void test_attr_2(TestOwnershipAttr *X) {
   NSString *str = [X returnsAnOwnedString]; // no-warning
   [X myRetain:str];
   [str release];
 }
 
+void test_attr_2b(TestOwnershipAttr *X) {
+  NSString *str = [X returnsAnOwnedCFString]; // expected-warning{{leak}}
+  [X myRetain:str];
+  [str release];
+}
+
 void test_attr_3(TestOwnershipAttr *X) {
   NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}}
   [X myCFRetain:str];

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

==============================================================================
--- cfe/trunk/test/Analysis/retain-release.m (original)
+++ cfe/trunk/test/Analysis/retain-release.m Mon May  4 19:46:09 2009
@@ -432,6 +432,7 @@
 
 @interface TestOwnershipAttr : NSObject
 - (NSString*) returnsAnOwnedString  __attribute__((ns_returns_owned));
+- (NSString*) returnsAnOwnedCFString  __attribute__((cf_returns_owned));
 - (void) myRetain:(id)__attribute__((ns_retains))obj;
 - (void) myCFRetain:(id)__attribute__((cf_retains))obj;
 - (void) myRelease:(id)__attribute__((ns_releases))obj;
@@ -458,12 +459,22 @@
   NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}}
 }
 
+void test_attr_1b(TestOwnershipAttr *X) {
+  NSString *str = [X returnsAnOwnedCFString]; // expected-warning{{leak}}
+}
+
 void test_attr_2(TestOwnershipAttr *X) {
   NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}}
   [X myRetain:str];
   [str release];
 }
 
+void test_attr_2b(TestOwnershipAttr *X) {
+  NSString *str = [X returnsAnOwnedCFString]; // expected-warning{{leak}}
+  [X myRetain:str];
+  [str release];
+}
+
 void test_attr_3(TestOwnershipAttr *X) {
   NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}}
   [X myCFRetain:str];





More information about the cfe-commits mailing list