xref: /aosp_15_r20/external/clang/test/SemaObjC/arc-property-lifetime.m (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li// RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fobjc-runtime-has-weak -fsyntax-only -fobjc-arc -verify -Wno-objc-root-class %s
2*67e74705SXin Li// rdar://9340606
3*67e74705SXin Li
4*67e74705SXin Li@interface Foo {
5*67e74705SXin Li@public
6*67e74705SXin Li    id __unsafe_unretained x; // expected-error {{existing instance variable 'x' for strong property 'x' may not be __unsafe_unretained}}
7*67e74705SXin Li    id __weak y; // expected-error {{existing instance variable 'y' for strong property 'y' may not be __weak}}
8*67e74705SXin Li    id __autoreleasing z; // expected-error {{instance variables cannot have __autoreleasing ownership}}
9*67e74705SXin Li}
10*67e74705SXin Li@property(strong) id x; // expected-note {{property declared here}}
11*67e74705SXin Li@property(strong) id y; // expected-note {{property declared here}}
12*67e74705SXin Li@property(strong) id z;
13*67e74705SXin Li@end
14*67e74705SXin Li
15*67e74705SXin Li@implementation Foo
16*67e74705SXin Li@synthesize x; // expected-note {{property synthesized here}}
17*67e74705SXin Li@synthesize y; // expected-note {{property synthesized here}}
18*67e74705SXin Li@synthesize z; // suppressed
19*67e74705SXin Li@end
20*67e74705SXin Li
21*67e74705SXin Li@interface Bar {
22*67e74705SXin Li@public
23*67e74705SXin Li    id __unsafe_unretained x; // expected-error {{existing instance variable 'x' for strong property 'x' may not be __unsafe_unretained}}
24*67e74705SXin Li    id __weak y; // expected-error {{existing instance variable 'y' for strong property 'y' may not be __weak}}
25*67e74705SXin Li    id __autoreleasing z; // expected-error {{instance variables cannot have __autoreleasing ownership}}
26*67e74705SXin Li}
27*67e74705SXin Li@property(retain) id x; // expected-note {{property declared here}}
28*67e74705SXin Li@property(retain) id y; // expected-note {{property declared here}}
29*67e74705SXin Li@property(retain) id z;
30*67e74705SXin Li@end
31*67e74705SXin Li
32*67e74705SXin Li@implementation Bar
33*67e74705SXin Li@synthesize x; // expected-note {{property synthesized here}}
34*67e74705SXin Li@synthesize y; // expected-note {{property synthesized here}}
35*67e74705SXin Li@synthesize z; // suppressed
36*67e74705SXin Li@end
37*67e74705SXin Li
38*67e74705SXin Li@interface Bas {
39*67e74705SXin Li@public
40*67e74705SXin Li    id __unsafe_unretained x; // expected-error {{existing instance variable 'x' for strong property 'x' may not be __unsafe_unretained}}
41*67e74705SXin Li    id __weak y; // expected-error {{existing instance variable 'y' for strong property 'y' may not be __weak}}
42*67e74705SXin Li    id __autoreleasing z; // expected-error {{instance variables cannot have __autoreleasing ownership}}
43*67e74705SXin Li}
44*67e74705SXin Li@property(copy) id x; // expected-note {{property declared here}}
45*67e74705SXin Li@property(copy) id y; // expected-note {{property declared here}}
46*67e74705SXin Li@property(copy) id z;
47*67e74705SXin Li@end
48*67e74705SXin Li
49*67e74705SXin Li@implementation Bas
50*67e74705SXin Li@synthesize x; // expected-note {{property synthesized here}}
51*67e74705SXin Li@synthesize y; // expected-note {{property synthesized here}}
52*67e74705SXin Li@synthesize z; // suppressed
53*67e74705SXin Li@end
54*67e74705SXin Li
55*67e74705SXin Li@interface Bat
56*67e74705SXin Li@property(strong) __unsafe_unretained id x; // expected-error {{strong property 'x' may not also be declared __unsafe_unretained}}
57*67e74705SXin Li@property(strong) __autoreleasing id z; // expected-error {{strong property 'z' may not also be declared __autoreleasing}}
58*67e74705SXin Li@end
59*67e74705SXin Li
60*67e74705SXin Li@interface Bau
61*67e74705SXin Li@property(retain) __unsafe_unretained id x; // expected-error {{strong property 'x' may not also be declared __unsafe_unretained}}
62*67e74705SXin Li@property(retain) __autoreleasing id z; // expected-error {{strong property 'z' may not also be declared __autoreleasing}}
63*67e74705SXin Li@end
64*67e74705SXin Li
65*67e74705SXin Li@interface Bav
66*67e74705SXin Li@property(copy) __unsafe_unretained id x; // expected-error {{strong property 'x' may not also be declared __unsafe_unretained}}
67*67e74705SXin Li@property(copy) __autoreleasing id z; // expected-error {{strong property 'z' may not also be declared __autoreleasing}}
68*67e74705SXin Li@end
69*67e74705SXin Li
70*67e74705SXin Li// rdar://9341593
71*67e74705SXin Li@interface Gorf  {
72*67e74705SXin Li   id __unsafe_unretained x;
73*67e74705SXin Li   id y; // expected-error {{existing instance variable 'y' for property 'y' with assign attribute must be __unsafe_unretained}}
74*67e74705SXin Li}
75*67e74705SXin Li@property(assign) id __unsafe_unretained x;
76*67e74705SXin Li@property(assign) id y; // expected-note {{property declared here}}
77*67e74705SXin Li@property(assign) id z;
78*67e74705SXin Li@end
79*67e74705SXin Li
80*67e74705SXin Li@implementation Gorf
81*67e74705SXin Li@synthesize x;
82*67e74705SXin Li@synthesize y; // expected-note {{property synthesized here}}
83*67e74705SXin Li@synthesize z;
84*67e74705SXin Li@end
85*67e74705SXin Li
86*67e74705SXin Li@interface Gorf2  {
87*67e74705SXin Li   id __unsafe_unretained x;
88*67e74705SXin Li   id y; // expected-error {{existing instance variable 'y' for property 'y' with unsafe_unretained attribute must be __unsafe_unretained}}
89*67e74705SXin Li}
90*67e74705SXin Li@property(unsafe_unretained) id __unsafe_unretained x;
91*67e74705SXin Li@property(unsafe_unretained) id y; // expected-note {{property declared here}}
92*67e74705SXin Li@property(unsafe_unretained) id z;
93*67e74705SXin Li@end
94*67e74705SXin Li
95*67e74705SXin Li@implementation Gorf2
96*67e74705SXin Li@synthesize x;
97*67e74705SXin Li@synthesize y; // expected-note {{property synthesized here}}
98*67e74705SXin Li@synthesize z;
99*67e74705SXin Li@end
100*67e74705SXin Li
101*67e74705SXin Li// rdar://9355230
102*67e74705SXin Li@interface I {
103*67e74705SXin Li  char _isAutosaving;
104*67e74705SXin Li}
105*67e74705SXin Li@property char isAutosaving;
106*67e74705SXin Li
107*67e74705SXin Li@end
108*67e74705SXin Li
109*67e74705SXin Li@implementation I
110*67e74705SXin Li@synthesize isAutosaving = _isAutosaving;
111*67e74705SXin Li@end
112*67e74705SXin Li
113*67e74705SXin Li// rdar://10239594
114*67e74705SXin Li// Test for 'Class' properties being unretained.
115*67e74705SXin Li@interface MyClass {
116*67e74705SXin Li@private
117*67e74705SXin Li    Class _controllerClass;
118*67e74705SXin Li    id _controllerId;
119*67e74705SXin Li}
120*67e74705SXin Li@property (copy) Class controllerClass;
121*67e74705SXin Li@property (copy) id controllerId;
122*67e74705SXin Li@end
123*67e74705SXin Li
124*67e74705SXin Li@implementation MyClass
125*67e74705SXin Li@synthesize controllerClass = _controllerClass;
126*67e74705SXin Li@synthesize controllerId = _controllerId;
127*67e74705SXin Li@end
128*67e74705SXin Li
129*67e74705SXin Li// rdar://10630891
130*67e74705SXin Li@interface UIView @end
131*67e74705SXin Li@class UIColor;
132*67e74705SXin Li
133*67e74705SXin Li@interface UIView(UIViewRendering)
134*67e74705SXin Li@property(nonatomic,copy) UIColor *backgroundColor;
135*67e74705SXin Li@end
136*67e74705SXin Li
137*67e74705SXin Li@interface UILabel : UIView
138*67e74705SXin Li@end
139*67e74705SXin Li
140*67e74705SXin Li@interface MyView
141*67e74705SXin Li@property (strong) UILabel *label;
142*67e74705SXin Li@end
143*67e74705SXin Li
144*67e74705SXin Li@interface MyView2 : MyView @end
145*67e74705SXin Li
146*67e74705SXin Li@implementation MyView2
147*67e74705SXin Li- (void)foo {
148*67e74705SXin Li  super.label.backgroundColor = 0;
149*67e74705SXin Li}
150*67e74705SXin Li@end
151*67e74705SXin Li
152*67e74705SXin Li// rdar://10694932
153*67e74705SXin Li@interface Baz
154*67e74705SXin Li@property  id prop;
155*67e74705SXin Li@property  __strong id strong_prop;
156*67e74705SXin Li@property  (strong) id strong_attr_prop;
157*67e74705SXin Li@property  (strong) __strong id really_strong_attr_prop;
158*67e74705SXin Li+ (id) alloc;
159*67e74705SXin Li- (id) init;
160*67e74705SXin Li- (id) implicit;
161*67e74705SXin Li- (void) setImplicit : (id) arg;
162*67e74705SXin Li@end
163*67e74705SXin Li
164*67e74705SXin Livoid foo(Baz *f) {
165*67e74705SXin Li        f.prop = [[Baz alloc] init];
166*67e74705SXin Li        f.strong_prop = [[Baz alloc] init];
167*67e74705SXin Li        f.strong_attr_prop = [[Baz alloc] init];
168*67e74705SXin Li        f.really_strong_attr_prop = [[Baz alloc] init];
169*67e74705SXin Li        f.implicit = [[Baz alloc] init];
170*67e74705SXin Li}
171*67e74705SXin Li
172*67e74705SXin Li// rdar://11253688
173*67e74705SXin Li@interface Boom
174*67e74705SXin Li{
175*67e74705SXin Li  const void * innerPointerIvar __attribute__((objc_returns_inner_pointer)); // expected-error {{'objc_returns_inner_pointer' attribute only applies to methods and properties}}
176*67e74705SXin Li}
177*67e74705SXin Li@property (readonly) Boom * NotInnerPointer __attribute__((objc_returns_inner_pointer)); // expected-warning {{'objc_returns_inner_pointer' attribute only applies to properties that return a non-retainable pointer}}
178*67e74705SXin Li- (Boom *) NotInnerPointerMethod __attribute__((objc_returns_inner_pointer)); // expected-warning {{'objc_returns_inner_pointer' attribute only applies to methods that return a non-retainable pointer}}
179*67e74705SXin Li@property (readonly) const void * innerPointer __attribute__((objc_returns_inner_pointer));
180*67e74705SXin Li@end
181*67e74705SXin Li
182*67e74705SXin Li@interface Foo2 {
183*67e74705SXin Li  id _prop; // expected-error {{existing instance variable '_prop' for property 'prop' with assign attribute must be __unsafe_unretained}}
184*67e74705SXin Li}
185*67e74705SXin Li@property (nonatomic, assign) id prop; // expected-note {{property declared here}}
186*67e74705SXin Li@end
187*67e74705SXin Li
188*67e74705SXin Li@implementation Foo2
189*67e74705SXin Li@end
190*67e74705SXin Li
191*67e74705SXin Li// rdar://13885083
192*67e74705SXin Li@interface NSObject
193*67e74705SXin Li-(id)init;
194*67e74705SXin Li@end
195*67e74705SXin Li
196*67e74705SXin Litypedef char BOOL;
197*67e74705SXin Li@interface Test13885083 : NSObject
198*67e74705SXin Li
199*67e74705SXin Li@property (nonatomic, assign) BOOL retain; // expected-error {{ARC forbids synthesis of 'retain'}}
200*67e74705SXin Li
201*67e74705SXin Li-(id)init;
202*67e74705SXin Li
203*67e74705SXin Li@end
204*67e74705SXin Li
205*67e74705SXin Li@implementation Test13885083
206*67e74705SXin Li-(id) init
207*67e74705SXin Li{
208*67e74705SXin Li  self = [super init];
209*67e74705SXin Li  return self;
210*67e74705SXin Li}
211*67e74705SXin Li@end
212*67e74705SXin Li
213