Add new checker-specific attribute 'objc_ownership_retain'. This isn't hooked up

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.

llvm-svn: 70005
This commit is contained in:
Ted Kremenek 2009-04-25 00:17:17 +00:00
parent 12bdebbbf7
commit 2cfd264636
8 changed files with 32 additions and 2 deletions

View File

@ -51,6 +51,7 @@ public:
NonNull, NonNull,
ObjCException, ObjCException,
ObjCNSObject, ObjCNSObject,
ObjCOwnershipRetain, // Clang/Checker-specific.
ObjCOwnershipReturns, // Clang/Checker-specific. ObjCOwnershipReturns, // Clang/Checker-specific.
Overloadable, // Clang-specific Overloadable, // Clang-specific
Packed, Packed,
@ -599,6 +600,7 @@ public:\
}; };
// Checker-specific attributes. // Checker-specific attributes.
DEF_SIMPLE_ATTR(ObjCOwnershipRetain)
DEF_SIMPLE_ATTR(ObjCOwnershipReturns) DEF_SIMPLE_ATTR(ObjCOwnershipReturns)
#undef DEF_SIMPLE_ATTR #undef DEF_SIMPLE_ATTR

View File

@ -410,7 +410,7 @@ def warn_attribute_weak_import_invalid_on_definition : Warning<
"'weak_import' attribute cannot be specified on a definition">; "'weak_import' attribute cannot be specified on a definition">;
def warn_attribute_wrong_decl_type : Warning< def warn_attribute_wrong_decl_type : Warning<
"'%0' attribute only applies to %select{function|union|" "'%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< def warn_gnu_inline_attribute_requires_inline : Warning<
"'gnu_inline' attribute requires function to be marked 'inline'," "'gnu_inline' attribute requires function to be marked 'inline',"
" attribute ignored">; " attribute ignored">;

View File

@ -75,9 +75,10 @@ public:
AT_nothrow, AT_nothrow,
AT_nsobject, AT_nsobject,
AT_objc_exception, AT_objc_exception,
AT_objc_ownership_retain, // Clang-specific.
AT_objc_ownership_returns, // Clang-specific. AT_objc_ownership_returns, // Clang-specific.
AT_objc_gc, AT_objc_gc,
AT_overloadable, // Clang-specific. AT_overloadable, // Clang-specific.
AT_packed, AT_packed,
AT_pure, AT_pure,
AT_regparm, AT_regparm,

View File

@ -3048,6 +3048,7 @@ Attr *PCHReader::ReadAttributes() {
SIMPLE_ATTR(ObjCException); SIMPLE_ATTR(ObjCException);
SIMPLE_ATTR(ObjCNSObject); SIMPLE_ATTR(ObjCNSObject);
SIMPLE_ATTR(ObjCOwnershipRetain);
SIMPLE_ATTR(ObjCOwnershipReturns); SIMPLE_ATTR(ObjCOwnershipReturns);
SIMPLE_ATTR(Overloadable); SIMPLE_ATTR(Overloadable);
UNSIGNED_ATTR(Packed); UNSIGNED_ATTR(Packed);

View File

@ -2181,6 +2181,7 @@ void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
case Attr::ObjCException: case Attr::ObjCException:
case Attr::ObjCNSObject: case Attr::ObjCNSObject:
case Attr::ObjCOwnershipRetain:
case Attr::ObjCOwnershipReturns: case Attr::ObjCOwnershipReturns:
case Attr::Overloadable: case Attr::Overloadable:
break; break;

View File

@ -133,6 +133,9 @@ AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name) {
case 18: case 18:
if (!memcmp(Str, "warn_unused_result", 18)) return AT_warn_unused_result; if (!memcmp(Str, "warn_unused_result", 18)) return AT_warn_unused_result;
break; break;
case 21:
if (!memcmp(Str, "objc_ownership_retain", 21))
return AT_objc_ownership_returns;
case 22: case 22:
if (!memcmp(Str, "objc_ownership_returns", 22)) if (!memcmp(Str, "objc_ownership_returns", 22))
return AT_objc_ownership_returns; return AT_objc_ownership_returns;

View File

@ -1533,6 +1533,18 @@ static void HandleObjCOwnershipReturnsAttr(Decl *d, const AttributeList &Attr,
d->addAttr(::new (S.Context) ObjCOwnershipReturnsAttr()); 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 // 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; case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
// Checker-specific. // Checker-specific.
case AttributeList::AT_objc_ownership_retain:
HandleObjCOwnershipRetainAttr(D, Attr, S); break;
case AttributeList::AT_objc_ownership_returns: case AttributeList::AT_objc_ownership_returns:
HandleObjCOwnershipReturnsAttr(D, Attr, S); break; HandleObjCOwnershipReturnsAttr(D, Attr, S); break;

View File

@ -411,9 +411,17 @@ void rdar6704930(unsigned char *s, unsigned int length) {
@interface TestOwnershipAttr : NSObject @interface TestOwnershipAttr : NSObject
- (NSString*) returnsAnOwnedString __attribute__((objc_ownership_returns)); - (NSString*) returnsAnOwnedString __attribute__((objc_ownership_returns));
- (void) myRetain:(id __attribute__((objc_ownership_retain)))obj;
@end @end
void test_attr_1(TestOwnershipAttr *X) { void test_attr_1(TestOwnershipAttr *X) {
NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}} 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];
}