[ObjCARC] Add an new attribute, objc_externally_retained
This attribute, called "objc_externally_retained", exposes clang's
notion of pseudo-__strong variables in ARC. Pseudo-strong variables
"borrow" their initializer, meaning that they don't retain/release
it, instead assuming that someone else is keeping their value alive.
If a function is annotated with this attribute, implicitly strong
parameters of that function aren't implicitly retained/released in
the function body, and are implicitly const. This is useful to expose
for performance reasons, most functions don't need the extra safety
of the retain/release, so programmers can opt out as needed.
This attribute can also apply to declarations of local variables,
with similar effect.
Differential revision: https://reviews.llvm.org/D55865
llvm-svn: 350422
2019-01-05 02:33:06 +08:00
|
|
|
// RUN: %clang_cc1 -triple x86_64-apple-macosx10.13.0 -fobjc-runtime=macosx-10.13.0 -fblocks -fobjc-arc %s -verify
|
|
|
|
// RUN: %clang_cc1 -triple x86_64-apple-macosx10.13.0 -fobjc-runtime=macosx-10.13.0 -fblocks -fobjc-arc -xobjective-c++ %s -verify
|
|
|
|
|
|
|
|
#define EXT_RET __attribute__((objc_externally_retained))
|
|
|
|
|
|
|
|
@interface ObjCTy
|
|
|
|
@end
|
|
|
|
|
|
|
|
void test1() {
|
|
|
|
EXT_RET int a; // expected-warning{{'objc_externally_retained' can only be applied to}}
|
|
|
|
EXT_RET __weak ObjCTy *b; // expected-warning{{'objc_externally_retained' can only be applied to}}
|
|
|
|
EXT_RET __weak int (^c)(); // expected-warning{{'objc_externally_retained' can only be applied to}}
|
|
|
|
|
|
|
|
EXT_RET int (^d)() = ^{return 0;};
|
|
|
|
EXT_RET ObjCTy *e = 0;
|
|
|
|
EXT_RET __strong ObjCTy *f = 0;
|
|
|
|
|
|
|
|
e = 0; // expected-error{{variable declared with 'objc_externally_retained' cannot be modified in ARC}}
|
|
|
|
f = 0; // expected-error{{variable declared with 'objc_externally_retained' cannot be modified in ARC}}
|
|
|
|
d = ^{ return 0; }; // expected-error{{variable declared with 'objc_externally_retained' cannot be modified in ARC}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void test2(ObjCTy *a);
|
|
|
|
|
|
|
|
void test2(ObjCTy *a) EXT_RET {
|
|
|
|
a = 0; // expected-error{{variable declared with 'objc_externally_retained' cannot be modified in ARC}}
|
|
|
|
}
|
|
|
|
|
|
|
|
EXT_RET ObjCTy *test3; // expected-warning{{'objc_externally_retained' can only be applied to}}
|
|
|
|
|
|
|
|
@interface X // expected-warning{{defined without specifying a base class}} expected-note{{add a super class}}
|
|
|
|
-(void)m: (ObjCTy *) p;
|
|
|
|
@end
|
|
|
|
@implementation X
|
|
|
|
-(void)m: (ObjCTy *) p EXT_RET {
|
|
|
|
p = 0; // expected-error{{variable declared with 'objc_externally_retained' cannot be modified in ARC}}
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
void test4() {
|
|
|
|
__attribute__((objc_externally_retained(0))) ObjCTy *a; // expected-error{{'objc_externally_retained' attribute takes no arguments}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void test5(ObjCTy *first, __strong ObjCTy *second) EXT_RET {
|
|
|
|
first = 0; // expected-error{{variable declared with 'objc_externally_retained' cannot be modified in ARC}}
|
|
|
|
second = 0; // fine
|
|
|
|
}
|
|
|
|
|
|
|
|
void test6(ObjCTy *first,
|
|
|
|
__strong ObjCTy *second) EXT_RET {
|
|
|
|
first = 0; // expected-error{{variable declared with 'objc_externally_retained' cannot be modified in ARC}}
|
|
|
|
second = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((objc_root_class)) @interface Y @end
|
|
|
|
|
|
|
|
@implementation Y
|
|
|
|
- (void)test7:(__strong ObjCTy *)first
|
|
|
|
withThird:(ObjCTy *)second EXT_RET {
|
|
|
|
first = 0;
|
|
|
|
second = 0; // expected-error{{variable declared with 'objc_externally_retained' cannot be modified in ARC}}
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
void (^blk)(ObjCTy *, ObjCTy *) =
|
|
|
|
^(__strong ObjCTy *first, ObjCTy *second) EXT_RET {
|
|
|
|
first = 0;
|
|
|
|
second = 0; // expected-error{{variable declared with 'objc_externally_retained' cannot be modified in ARC}}
|
|
|
|
};
|
|
|
|
|
2019-05-07 11:20:17 +08:00
|
|
|
void (^blk2)(ObjCTy *, ObjCTy *) =
|
|
|
|
^(__strong ObjCTy *first, ObjCTy *second) __attribute__((objc_externally_retained)) {
|
|
|
|
first = 0;
|
|
|
|
second = 0; // expected-error{{variable declared with 'objc_externally_retained' cannot be modified in ARC}}
|
|
|
|
};
|
|
|
|
|
[ObjCARC] Add an new attribute, objc_externally_retained
This attribute, called "objc_externally_retained", exposes clang's
notion of pseudo-__strong variables in ARC. Pseudo-strong variables
"borrow" their initializer, meaning that they don't retain/release
it, instead assuming that someone else is keeping their value alive.
If a function is annotated with this attribute, implicitly strong
parameters of that function aren't implicitly retained/released in
the function body, and are implicitly const. This is useful to expose
for performance reasons, most functions don't need the extra safety
of the retain/release, so programmers can opt out as needed.
This attribute can also apply to declarations of local variables,
with similar effect.
Differential revision: https://reviews.llvm.org/D55865
llvm-svn: 350422
2019-01-05 02:33:06 +08:00
|
|
|
void test8(EXT_RET ObjCTy *x) {} // expected-warning{{'objc_externally_retained' attribute only applies to variables}}
|
|
|
|
|
|
|
|
#pragma clang attribute ext_ret.push(__attribute__((objc_externally_retained)), apply_to=any(function, block, objc_method))
|
|
|
|
void test9(ObjCTy *first, __strong ObjCTy *second) {
|
|
|
|
first = 0; // expected-error{{variable declared with 'objc_externally_retained' cannot be modified in ARC}}
|
|
|
|
second = 0;
|
|
|
|
}
|
|
|
|
void (^test10)(ObjCTy *first, ObjCTy *second) = ^(ObjCTy *first, __strong ObjCTy *second) {
|
|
|
|
first = 0; // expected-error{{variable declared with 'objc_externally_retained' cannot be modified in ARC}}
|
|
|
|
second = 0;
|
|
|
|
};
|
|
|
|
__attribute__((objc_root_class)) @interface Test11 @end
|
|
|
|
@implementation Test11
|
|
|
|
-(void)meth: (ObjCTy *)first withSecond:(__strong ObjCTy *)second {
|
|
|
|
first = 0; // expected-error{{variable declared with 'objc_externally_retained' cannot be modified in ARC}}
|
|
|
|
second = 0;
|
|
|
|
}
|
|
|
|
+(void)othermeth: (ObjCTy *)first withSecond:(__strong ObjCTy *)second {
|
|
|
|
first = 0; // expected-error{{variable declared with 'objc_externally_retained' cannot be modified in ARC}}
|
|
|
|
second = 0;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
#if __cplusplus
|
|
|
|
class Test12 {
|
|
|
|
void inline_member(ObjCTy *first, __strong ObjCTy *second) {
|
|
|
|
first = 0; // expected-error{{variable declared with 'objc_externally_retained' cannot be modified in ARC}}
|
|
|
|
second = 0;
|
|
|
|
}
|
|
|
|
static void static_inline_member(ObjCTy *first, __strong ObjCTy *second) {
|
|
|
|
first = 0; // expected-error{{variable declared with 'objc_externally_retained' cannot be modified in ARC}}
|
|
|
|
second = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void test13(ObjCTy *first, __weak ObjCTy *second, __unsafe_unretained ObjCTy *third, __strong ObjCTy *fourth) {
|
|
|
|
first = 0; // expected-error{{variable declared with 'objc_externally_retained' cannot be modified in ARC}}
|
|
|
|
second = 0;
|
|
|
|
third = 0;
|
|
|
|
fourth = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma clang attribute ext_ret.pop
|