]> granicus.if.org Git - clang/commitdiff
Add new checker-specific attribute 'objc_ownership_retain'. This isn't hooked up
authorTed Kremenek <kremenek@apple.com>
Sat, 25 Apr 2009 00:17:17 +0000 (00:17 +0000)
committerTed Kremenek <kremenek@apple.com>
Sat, 25 Apr 2009 00:17:17 +0000 (00:17 +0000)
to the checker yet, but essentially it allows a user to specify that an
Objective-C method or C function increments the reference count of a passed
object.

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

include/clang/AST/Attr.h
include/clang/Basic/DiagnosticSemaKinds.td
include/clang/Parse/AttributeList.h
lib/Frontend/PCHReader.cpp
lib/Frontend/PCHWriter.cpp
lib/Parse/AttributeList.cpp
lib/Sema/SemaDeclAttr.cpp
test/Analysis/retain-release.m

index 93a9edb8077bf85463a78827be498338be9db62f..549ba632a4d6705d063dd7bee7504d7ea352a7a1 100644 (file)
@@ -51,6 +51,7 @@ public:
     NonNull,
     ObjCException,
     ObjCNSObject,
+    ObjCOwnershipRetain, // Clang/Checker-specific.
     ObjCOwnershipReturns, // Clang/Checker-specific.
     Overloadable, // Clang-specific
     Packed,
@@ -599,6 +600,7 @@ public:\
 };
 
 // Checker-specific attributes.
+DEF_SIMPLE_ATTR(ObjCOwnershipRetain)
 DEF_SIMPLE_ATTR(ObjCOwnershipReturns)
 
 #undef DEF_SIMPLE_ATTR
index 071fcab437abcf0762d2ef99f87c69cb0cc238f5..b9310db762815b71dd4458e97928bd2ac2330060 100644 (file)
@@ -410,7 +410,7 @@ def warn_attribute_weak_import_invalid_on_definition : Warning<
   "'weak_import' attribute cannot be specified on a definition">;
 def warn_attribute_wrong_decl_type : Warning<
   "'%0' attribute only applies to %select{function|union|"
-  "variable and function|function or method}1 types">;
+  "variable and function|function or method|parameter}1 types">;
 def warn_gnu_inline_attribute_requires_inline : Warning<
   "'gnu_inline' attribute requires function to be marked 'inline',"
   " attribute ignored">;
index 9712cc896edac593aab58d52ff73f9b97db207f8..b585838df8399236b1ef4dd4ed1659edbeeb16da 100644 (file)
@@ -75,9 +75,10 @@ public:
     AT_nothrow,
     AT_nsobject,
     AT_objc_exception,
+    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 66f65dbb0bd0968bc8362eeb94f1122a0992c1fc..b10778a1e1dfcac18c784dcab612a471c775214d 100644 (file)
@@ -3048,6 +3048,7 @@ Attr *PCHReader::ReadAttributes() {
 
     SIMPLE_ATTR(ObjCException);
     SIMPLE_ATTR(ObjCNSObject);
+    SIMPLE_ATTR(ObjCOwnershipRetain);
     SIMPLE_ATTR(ObjCOwnershipReturns);
     SIMPLE_ATTR(Overloadable);
     UNSIGNED_ATTR(Packed);
index 4d2f4092ef721f371e350b5e7feee67c4f440ced..421c345e78040c03ae281c0320422338b569efff 100644 (file)
@@ -2181,6 +2181,7 @@ void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
 
     case Attr::ObjCException:
     case Attr::ObjCNSObject:
+    case Attr::ObjCOwnershipRetain:
     case Attr::ObjCOwnershipReturns:
     case Attr::Overloadable:
       break;
index 33715899c5ccb2d4cd2d733a6b961fe4016e8de4..a205c0468a04b1bfeca7a87b63fe509e3f969736 100644 (file)
@@ -133,6 +133,9 @@ AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name) {
   case 18:
     if (!memcmp(Str, "warn_unused_result", 18)) return AT_warn_unused_result;
     break;
+  case 21:
+    if (!memcmp(Str, "objc_ownership_retain", 21))
+      return AT_objc_ownership_returns;      
   case 22:
     if (!memcmp(Str, "objc_ownership_returns", 22))
       return AT_objc_ownership_returns;
index 5a99fdccd16da6835bba27167b5752c75bfe90e3..add12e53c26f29412943a588387b0e56228e672e 100644 (file)
@@ -1533,6 +1533,18 @@ static void HandleObjCOwnershipReturnsAttr(Decl *d, const AttributeList &Attr,
   d->addAttr(::new (S.Context) ObjCOwnershipReturnsAttr());
 }
 
+static void HandleObjCOwnershipRetainAttr(Decl *d, const AttributeList &Attr,
+                                           Sema &S) {
+  
+  if (!isa<ParmVarDecl>(d)) {
+    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
+    "objc_ownership_retain" << 4 /* parameter */;
+    return;
+  }
+  
+  d->addAttr(::new (S.Context) ObjCOwnershipRetainAttr());
+}
+
 //===----------------------------------------------------------------------===//
 // Top Level Sema Entry Points
 //===----------------------------------------------------------------------===//
@@ -1571,6 +1583,8 @@ static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
   case AttributeList::AT_nothrow:     HandleNothrowAttr   (D, Attr, S); break;
 
   // Checker-specific.
+  case AttributeList::AT_objc_ownership_retain:
+    HandleObjCOwnershipRetainAttr(D, Attr, S); break;
   case AttributeList::AT_objc_ownership_returns:
     HandleObjCOwnershipReturnsAttr(D, Attr, S); break;
 
index f5f3142cfee6a4739de6226329e117c8b4a0ddac..0de2025398d393b7b8c4fd4d442b9a912e0f1486 100644 (file)
@@ -411,9 +411,17 @@ 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;
 @end
 
 void test_attr_1(TestOwnershipAttr *X) {
   NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}}
 }
 
+void test_attr_2(TestOwnershipAttr *X) {
+  NSString *str = [X returnsAnOwnedString]; // no-warning (yet)
+  [X myRetain:str];
+  [str release];
+}
+
+