2012-07-25 15:26:32 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -fobjc-runtime-has-weak -fobjc-arc -fblocks -Wno-objc-root-class -Wreceiver-is-weak -verify %s
|
2012-04-05 04:05:25 +08:00
|
|
|
// rdar://10225276
|
|
|
|
|
|
|
|
@interface Test0
|
|
|
|
- (void) setBlock: (void(^)(void)) block;
|
|
|
|
- (void) addBlock: (void(^)(void)) block;
|
|
|
|
- (void) actNow;
|
|
|
|
@end
|
|
|
|
|
|
|
|
void test0(Test0 *x) {
|
|
|
|
__weak Test0 *weakx = x;
|
2012-09-29 06:21:42 +08:00
|
|
|
[x addBlock: ^{ [weakx actNow]; }]; // expected-warning {{weak receiver may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
|
|
|
|
[x setBlock: ^{ [weakx actNow]; }]; // expected-warning {{weak receiver may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
|
|
|
|
x.block = ^{ [weakx actNow]; }; // expected-warning {{weak receiver may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
|
2012-04-05 04:05:25 +08:00
|
|
|
|
2012-09-29 06:21:42 +08:00
|
|
|
[weakx addBlock: ^{ [x actNow]; }]; // expected-warning {{weak receiver may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
|
|
|
|
[weakx setBlock: ^{ [x actNow]; }]; // expected-warning {{weak receiver may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
|
|
|
|
weakx.block = ^{ [x actNow]; }; // expected-warning {{weak receiver may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
|
2012-04-05 04:05:25 +08:00
|
|
|
}
|
2012-04-20 05:44:57 +08:00
|
|
|
|
|
|
|
@interface Test
|
|
|
|
{
|
|
|
|
__weak Test* weak_prop;
|
|
|
|
}
|
|
|
|
- (void) Meth;
|
2012-04-20 07:49:39 +08:00
|
|
|
@property __weak Test* weak_prop; // expected-note {{property declared here}}
|
|
|
|
@property (weak, atomic) id weak_atomic_prop; // expected-note {{property declared here}}
|
|
|
|
- (__weak id) P; // expected-note {{method 'P' declared here}}
|
2012-04-20 05:44:57 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation Test
|
|
|
|
- (void) Meth {
|
2012-04-20 07:49:39 +08:00
|
|
|
if (self.weak_prop) {
|
2012-04-20 05:44:57 +08:00
|
|
|
self.weak_prop = 0;
|
|
|
|
}
|
2012-04-20 07:49:39 +08:00
|
|
|
if (self.weak_atomic_prop) {
|
2012-04-20 05:44:57 +08:00
|
|
|
self.weak_atomic_prop = 0;
|
|
|
|
}
|
2012-09-29 06:21:42 +08:00
|
|
|
[self.weak_prop Meth]; // expected-warning {{weak property may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
|
2012-04-20 07:49:39 +08:00
|
|
|
id pi = self.P;
|
2012-04-20 05:44:57 +08:00
|
|
|
|
2012-09-29 06:21:42 +08:00
|
|
|
[self.weak_atomic_prop Meth]; // expected-warning {{weak property may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
|
2012-04-20 05:44:57 +08:00
|
|
|
|
2012-09-29 06:21:42 +08:00
|
|
|
[self.P Meth]; // expected-warning {{weak implicit property may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
|
2012-04-20 05:44:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (__weak id) P { return 0; }
|
|
|
|
@dynamic weak_prop, weak_atomic_prop;
|
|
|
|
@end
|
|
|
|
|
2012-06-05 03:16:34 +08:00
|
|
|
|
|
|
|
@interface MyClass {
|
|
|
|
__weak MyClass *_parent;
|
|
|
|
}
|
2012-10-11 00:42:54 +08:00
|
|
|
@property (weak) MyClass *parent; // expected-note 4 {{property declared here}}
|
2012-06-05 03:16:34 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation MyClass
|
|
|
|
@synthesize parent = _parent;
|
|
|
|
|
|
|
|
- (void)doSomething
|
|
|
|
{
|
2012-09-29 06:21:42 +08:00
|
|
|
[[self parent] doSomething]; // expected-warning {{weak property may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
|
2012-06-05 03:16:34 +08:00
|
|
|
|
2012-09-29 06:21:42 +08:00
|
|
|
(void)self.parent.doSomething; // expected-warning {{weak property may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
|
2012-06-05 03:16:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2012-06-29 00:39:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
// Weak properties on protocols can be synthesized by an adopting class.
|
|
|
|
@protocol MyProtocol
|
|
|
|
@property (weak) id object; // expected-note 2 {{property declared here}}
|
|
|
|
@end
|
|
|
|
|
|
|
|
void testProtocol(id <MyProtocol> input) {
|
2012-09-29 06:21:42 +08:00
|
|
|
[[input object] Meth]; // expected-warning {{weak property may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
|
|
|
|
[input.object Meth]; // expected-warning {{weak property may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
|
2012-06-29 00:39:28 +08:00
|
|
|
}
|
|
|
|
|
2012-10-11 00:42:54 +08:00
|
|
|
|
|
|
|
@interface Subclass : MyClass
|
|
|
|
// Unnecessarily redeclare -parent.
|
|
|
|
- (id)parent;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation Subclass
|
|
|
|
|
|
|
|
- (id)parent {
|
|
|
|
return [super parent];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)doSomethingElse {
|
|
|
|
[[self parent] doSomething]; // expected-warning {{weak property may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
|
|
|
|
|
|
|
|
(void)self.parent.doSomething; // expected-warning {{weak property may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|