1 // RUN: %clang_cc1 -fsyntax-only -fobjc-runtime-has-weak -fobjc-arc -fblocks -verify -Wno-objc-root-class -Wno-implicit-retain-self %s
3 void *_Block_copy(const void *block);
6 - (void) setBlock: (void(^)(void)) block;
7 - (void) addBlock: (void(^)(void)) block;
10 void test0(Test0 *x) {
11 [x setBlock: // expected-note {{block will be retained by the captured object}}
12 ^{ [x actNow]; }]; // expected-warning {{capturing 'x' strongly in this block is likely to lead to a retain cycle}}
13 x.block = // expected-note {{block will be retained by the captured object}}
14 ^{ [x actNow]; }; // expected-warning {{capturing 'x' strongly in this block is likely to lead to a retain cycle}}
16 [x addBlock: // expected-note {{block will be retained by the captured object}}
17 ^{ [x actNow]; }]; // expected-warning {{capturing 'x' strongly in this block is likely to lead to a retain cycle}}
19 // These actually don't cause retain cycles.
20 __weak Test0 *weakx = x;
21 [x addBlock: ^{ [weakx actNow]; }];
22 [x setBlock: ^{ [weakx actNow]; }];
23 x.block = ^{ [weakx actNow]; };
25 // These do cause retain cycles, but we're not clever enough to figure that out.
26 [weakx addBlock: ^{ [x actNow]; }];
27 [weakx setBlock: ^{ [x actNow]; }];
28 weakx.block = ^{ [x actNow]; };
31 x.block = ^{ (void)x.actNow; }; // expected-warning {{capturing 'x' strongly in this block is likely to lead to a retain cycle}} \
32 // expected-note {{block will be retained by the captured object}}
36 @property (retain) void (^strong)(void); // expected-warning {{retain'ed block property does not copy the block - use copy attribute instead}}
43 @property (retain) BlockOwner *owner;
44 @property (assign) __strong BlockOwner *owner2; // expected-error {{unsafe_unretained property 'owner2' may not also be declared __strong}}
45 @property (assign) BlockOwner *owner3;
47 void test1(Test1 *x) {
48 x->owner.strong = ^{ (void) x; }; // expected-warning {{retain cycle}} expected-note {{block will be retained by an object strongly retained by the captured object}}
49 x.owner.strong = ^{ (void) x; }; // expected-warning {{retain cycle}} expected-note {{block will be retained by an object strongly retained by the captured object}}
50 x.owner2.strong = ^{ (void) x; };
51 x.owner3.strong = ^{ (void) x; };
54 @implementation Test1 {
55 BlockOwner * __unsafe_unretained owner3ivar;
56 __weak BlockOwner *weakowner;
60 @synthesize owner3 = owner3ivar;
63 self.owner.strong = ^{ (void) owner; }; // expected-warning {{retain cycle}} expected-note {{block will be retained by an object strongly retained by the captured object}}
64 self.owner2.strong = ^{ (void) owner; };
66 // TODO: should we warn here? What's the story with this kind of mismatch?
67 self.owner3.strong = ^{ (void) owner; };
69 owner.strong = ^{ (void) owner; }; // expected-warning {{retain cycle}} expected-note {{block will be retained by an object strongly retained by the captured object}}
71 owner.strong = ^{ ^{ (void) owner; }(); }; // expected-warning {{retain cycle}} expected-note {{block will be retained by an object strongly retained by the captured object}}
73 owner.strong = ^{ (void) sizeof(self); // expected-note {{block will be retained by an object strongly retained by the captured object}}
74 (void) owner; }; // expected-warning {{capturing 'self' strongly in this block is likely to lead to a retain cycle}}
76 weakowner.strong = ^{ (void) owner; };
81 owner.strong = ^{ (void) owner; }; // expected-warning {{retain cycle}} expected-note {{block will be retained by an object strongly retained by the captured object}}
85 void test2_helper(id);
93 block = ^{ // expected-note {{block will be retained by an object strongly retained by the captured object}}
94 test2_helper(x); // expected-warning {{capturing 'self' strongly in this block is likely to lead to a retain cycle}}
100 @interface NSOperationQueue {}
101 - (void)addOperationWithBlock:(void (^)(void))block;
102 - (void)addSomethingElse:(void (^)(void))block;
107 NSOperationQueue *myOperationQueue;
111 void doSomething(unsigned v);
112 @implementation Test3
114 // 'addOperationWithBlock:' is specifically allowlisted.
115 [myOperationQueue addOperationWithBlock:^() { // no-warning
121 - (void) test_positive {
122 // Check that we are really allowlisting 'addOperationWithBlock:' and not doing
124 [myOperationQueue addSomethingElse:^() { // expected-note {{block will be retained by an object strongly retained by the captured object}}
126 doSomething(count); // expected-warning {{capturing 'self' strongly in this block is likely to lead to a retain cycle}}
133 void testBlockVariable(void) {
134 typedef void (^block_t)(void);
136 // This case will be caught by -Wuninitialized, and does not create a
142 // This case will also be caught by -Wuninitialized.
148 __block block_t b1 = ^{ // expected-note{{block will be retained by the captured object}}
149 b1(); // expected-warning{{capturing 'b1' strongly in this block is likely to lead to a retain cycle}}
153 b2 = ^{ // expected-note{{block will be retained by the captured object}}
154 b2(); // expected-warning{{capturing 'b2' strongly in this block is likely to lead to a retain cycle}}
162 - (void (^)(void))someRandomMethodReturningABlock;
166 void testCopying(Test0 *obj) {
167 typedef void (^block_t)(void);
169 [obj setBlock:[^{ // expected-note{{block will be retained by the captured object}}
170 [obj actNow]; // expected-warning{{capturing 'obj' strongly in this block is likely to lead to a retain cycle}}
173 [obj addBlock:(__bridge_transfer block_t)_Block_copy((__bridge void *)^{ // expected-note{{block will be retained by the captured object}}
174 [obj actNow]; // expected-warning{{capturing 'obj' strongly in this block is likely to lead to a retain cycle}}
178 [obj actNow]; // no-warning
179 } someRandomMethodReturningABlock]];
181 extern block_t someRandomFunctionReturningABlock(block_t);
182 [obj setBlock:someRandomFunctionReturningABlock(^{
183 [obj actNow]; // no-warning
188 void func(int someCondition) {
190 __block void(^myBlock)(void) = ^{
196 myBlock = ((void*)0);
202 typedef void (^a_block_t)(void);
204 @interface HonorNoEscape
205 - (void)addStuffUsingBlock:(__attribute__((noescape)) a_block_t)block;
208 void testNoEscape(HonorNoEscape *obj) {
209 [obj addStuffUsingBlock:^{