blob: a550633171d1e52fafa4ba6b929e94aae04209ce [file] [log] [blame]
Argyrios Kyrtzidisffe76dd2012-06-20 01:10:40 +00001// RUN: %clang_cc1 -arcmt-check -verify -triple x86_64-apple-darwin10 -fblocks -Werror %s
Argyrios Kyrtzidisd8b42162012-01-12 02:34:32 +00002// DISABLE: mingw32
John McCall8f0e8d22011-06-15 23:25:17 +00003
Argyrios Kyrtzidis48977872011-12-09 00:17:47 +00004#if __has_feature(objc_arc)
5#define NS_AUTOMATED_REFCOUNT_UNAVAILABLE __attribute__((unavailable("not available in automatic reference counting mode")))
6#else
7#define NS_AUTOMATED_REFCOUNT_UNAVAILABLE
8#endif
9
Fariborz Jahanian52b62362012-02-01 22:56:20 +000010typedef const void * CFTypeRef;
11CFTypeRef CFBridgingRetain(id X);
12id CFBridgingRelease(CFTypeRef);
13
Argyrios Kyrtzidis48977872011-12-09 00:17:47 +000014typedef int BOOL;
15typedef unsigned NSUInteger;
16
17@protocol NSObject
Ted Kremenekd6cf9122012-02-10 02:45:47 +000018- (id)retain NS_AUTOMATED_REFCOUNT_UNAVAILABLE;
Argyrios Kyrtzidis48977872011-12-09 00:17:47 +000019- (NSUInteger)retainCount NS_AUTOMATED_REFCOUNT_UNAVAILABLE;
Ted Kremenekd6cf9122012-02-10 02:45:47 +000020- (oneway void)release NS_AUTOMATED_REFCOUNT_UNAVAILABLE;
Argyrios Kyrtzidis48977872011-12-09 00:17:47 +000021- (id)autorelease NS_AUTOMATED_REFCOUNT_UNAVAILABLE;
22@end
23
24@interface NSObject <NSObject> {}
25- (id)init;
26
27+ (id)new;
28+ (id)alloc;
29- (void)dealloc;
30
31- (void)finalize;
32
33- (id)copy;
34- (id)mutableCopy;
35@end
John McCall8f0e8d22011-06-15 23:25:17 +000036
37typedef const struct __CFString * CFStringRef;
38extern const CFStringRef kUTTypePlainText;
39extern const CFStringRef kUTTypeRTF;
40@class NSString;
41@class A;
42
43struct UnsafeS {
44 A *__unsafe_unretained unsafeObj;
45};
46
47@interface A : NSObject
Fariborz Jahanian1ea67442012-06-05 21:14:46 +000048- (id)retain; // expected-note {{declaration has been explicitly marked unavailable here}}
49- (id)retainCount; // expected-note {{declaration has been explicitly marked unavailable here}}
50- (id)autorelease; // expected-note 2 {{declaration has been explicitly marked unavailable here}}
John McCall8f0e8d22011-06-15 23:25:17 +000051- (id)init;
52- (oneway void)release;
53- (void)dealloc;
54-(void)test;
Argyrios Kyrtzidis82a921a2011-07-15 23:48:56 +000055-(id)delegate;
John McCall8f0e8d22011-06-15 23:25:17 +000056@end
57
58@implementation A
59-(void)test {
60 [super dealloc];
61}
62-(void)dealloc {
63 [super dealloc];
64}
65
66- (id)retain { return self; } // expected-error {{ARC forbids implementation}}
67- (id)retainCount { return self; } // expected-error {{ARC forbids implementation}}
68- (id)autorelease { return self; } // expected-error {{ARC forbids implementation}}
69- (oneway void)release { } // expected-error {{ARC forbids implementation}}
Argyrios Kyrtzidis82a921a2011-07-15 23:48:56 +000070
71-(id)delegate { return self; }
John McCall8f0e8d22011-06-15 23:25:17 +000072@end
73
Argyrios Kyrtzidis2c18ca02011-07-14 23:32:04 +000074id global_foo;
75
John McCall8f0e8d22011-06-15 23:25:17 +000076void test1(A *a, BOOL b, struct UnsafeS *unsafeS) {
Argyrios Kyrtzidis82a921a2011-07-15 23:48:56 +000077 [[a delegate] release]; // expected-error {{it is not safe to remove 'retain' message on the result of a 'delegate' message; the object that was passed to 'setDelegate:' may not be properly retained}} \
Ted Kremenekd6cf9122012-02-10 02:45:47 +000078 // expected-error {{ARC forbids explicit message send}}
Argyrios Kyrtzidis82a921a2011-07-15 23:48:56 +000079 [a.delegate release]; // expected-error {{it is not safe to remove 'retain' message on the result of a 'delegate' message; the object that was passed to 'setDelegate:' may not be properly retained}} \
Ted Kremenekd6cf9122012-02-10 02:45:47 +000080 // expected-error {{ARC forbids explicit message send}}
Argyrios Kyrtzidise0e40762011-07-14 21:26:49 +000081 [unsafeS->unsafeObj retain]; // expected-error {{it is not safe to remove 'retain' message on an __unsafe_unretained type}} \
Fariborz Jahanian1ea67442012-06-05 21:14:46 +000082 // expected-error {{ARC forbids explicit message send}} \
83 // expected-error {{'retain' is unavailable}}
Argyrios Kyrtzidis2cd53662011-07-14 22:46:12 +000084 id foo = [unsafeS->unsafeObj retain]; // no warning.
Argyrios Kyrtzidis2c18ca02011-07-14 23:32:04 +000085 [global_foo retain]; // expected-error {{it is not safe to remove 'retain' message on a global variable}} \
Ted Kremenekd6cf9122012-02-10 02:45:47 +000086 // expected-error {{ARC forbids explicit message send}}
Argyrios Kyrtzidis2c18ca02011-07-14 23:32:04 +000087 [global_foo release]; // expected-error {{it is not safe to remove 'release' message on a global variable}} \
Ted Kremenekd6cf9122012-02-10 02:45:47 +000088 // expected-error {{ARC forbids explicit message send}}
John McCall8f0e8d22011-06-15 23:25:17 +000089 [a dealloc];
90 [a retain];
Fariborz Jahanian1ea67442012-06-05 21:14:46 +000091 [a retainCount]; // expected-error {{ARC forbids explicit message send of 'retainCount'}} \
92 // expected-error {{'retainCount' is unavailable}}
John McCall8f0e8d22011-06-15 23:25:17 +000093 [a release];
Argyrios Kyrtzidis5a42a022012-05-21 17:48:31 +000094 [a autorelease]; // expected-error {{it is not safe to remove an unused 'autorelease' message; its receiver may be destroyed immediately}} \
Fariborz Jahanian1ea67442012-06-05 21:14:46 +000095 // expected-error {{ARC forbids explicit message send}} \
96 // expected-error {{'autorelease' is unavailable}}
Argyrios Kyrtzidis1b8fbd32012-05-23 21:50:04 +000097 [a autorelease]; // expected-error {{it is not safe to remove an unused 'autorelease' message; its receiver may be destroyed immediately}} \
Fariborz Jahanian1ea67442012-06-05 21:14:46 +000098 // expected-error {{ARC forbids explicit message send}} \
99 // expected-error {{'autorelease' is unavailable}}
Argyrios Kyrtzidis1b8fbd32012-05-23 21:50:04 +0000100 a = 0;
John McCall8f0e8d22011-06-15 23:25:17 +0000101
102 CFStringRef cfstr;
103 NSString *str = (NSString *)cfstr; // expected-error {{cast of C pointer type 'CFStringRef' (aka 'const struct __CFString *') to Objective-C pointer type 'NSString *' requires a bridged cast}} \
Fariborz Jahanian607f5872012-07-27 21:34:23 +0000104 // expected-note {{use __bridge to convert directly (no change in ownership)}} \
Fariborz Jahanian52b62362012-02-01 22:56:20 +0000105 // expected-note {{use CFBridgingRelease call to transfer ownership of a +1 'CFStringRef' (aka 'const struct __CFString *') into ARC}} \
John McCall8f0e8d22011-06-15 23:25:17 +0000106 str = (NSString *)kUTTypePlainText;
107 str = b ? kUTTypeRTF : kUTTypePlainText;
108 str = (NSString *)(b ? kUTTypeRTF : kUTTypePlainText);
109 str = (NSString *)a; // no change.
110
111 SEL s = @selector(retain); // expected-error {{ARC forbids use of 'retain' in a @selector}}
112 s = @selector(release); // expected-error {{ARC forbids use of 'release' in a @selector}}
113 s = @selector(autorelease); // expected-error {{ARC forbids use of 'autorelease' in a @selector}}
114 s = @selector(dealloc); // expected-error {{ARC forbids use of 'dealloc' in a @selector}}
115
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000116 static id __autoreleasing X1; // expected-error {{global variables cannot have __autoreleasing ownership}}
John McCall8f0e8d22011-06-15 23:25:17 +0000117}
118
119struct S {
Douglas Gregorbde67cf2013-01-28 20:13:44 +0000120 A* a; // expected-error {{ARC forbids Objective-C objects in struct}}
John McCall8f0e8d22011-06-15 23:25:17 +0000121};
122
123@interface B
124-(id)alloc;
125- (id)initWithInt: (int) i;
126@end
127
128void rdar8861761() {
129 B *o1 = [[B alloc] initWithInt:0];
130 B *o2 = [B alloc];
131 [o2 initWithInt:0];
132}
133
134@interface Test13
135- (id) init0;
136- (void) noninit;
137@end
138@implementation Test13
139- (id) init0 {
140 self = 0;
141}
142- (void) noninit {
143 self = 0; // expected-error {{cannot assign to 'self' outside of a method in the init family}}
144
Fariborz Jahaniana1eec4b2012-07-03 22:00:52 +0000145 for (__strong id x in collection) { // expected-error {{use of undeclared identifier 'collection'}}
John McCall8f0e8d22011-06-15 23:25:17 +0000146 x = 0;
147 }
148}
149@end
150
151void * cvt(id arg)
152{
153 void* voidp_val;
154 (void)(int*)arg; // expected-error {{disallowed}}
155 (void)(id)arg;
156 (void)(__autoreleasing id*)arg; // expected-error {{disallowed}}
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +0000157 (void)(id*)arg; // expected-error {{disallowed}}
John McCall8f0e8d22011-06-15 23:25:17 +0000158
159 (void)(__autoreleasing id**)voidp_val;
160 (void)(void*)voidp_val;
161 (void)(void**)arg; // expected-error {{disallowed}}
John McCall5acb0c92011-10-17 18:40:02 +0000162 cvt((void*)arg); // expected-error 2 {{requires a bridged cast}} \
Fariborz Jahanian607f5872012-07-27 21:34:23 +0000163 // expected-note 2 {{use __bridge to}} expected-note {{use CFBridgingRelease call}} expected-note {{use CFBridgingRetain call}}
John McCall8f0e8d22011-06-15 23:25:17 +0000164 cvt(0);
165 (void)(__strong id**)(0);
Fariborz Jahanian607f5872012-07-27 21:34:23 +0000166 return arg; // expected-error {{requires a bridged cast}} expected-note {{use __bridge}} expected-note {{use CFBridgingRetain call}}
John McCall8f0e8d22011-06-15 23:25:17 +0000167}
168
169
170void test12(id collection) {
171 for (id x in collection) {
172 x = 0;
173 }
174
175 for (__strong id x in collection) {
176 x = 0;
177 }
178}
179
180void test6(unsigned cond) {
John McCall8f0e8d22011-06-15 23:25:17 +0000181 switch (cond) {
182 case 0:
183 ;
Argyrios Kyrtzidis4ce831c2013-01-08 00:58:25 +0000184 id x; // expected-note {{jump bypasses initialization of retaining variable}}
John McCall8f0e8d22011-06-15 23:25:17 +0000185
Argyrios Kyrtzidis4ce831c2013-01-08 00:58:25 +0000186 case 1: // expected-error {{switch case is in protected scope}}
187 x = 0;
John McCall8f0e8d22011-06-15 23:25:17 +0000188 break;
189 }
190}
191
192@class Test8_incomplete;
193@interface Test8_complete @end;
194@interface Test8_super @end;
195@interface Test8 : Test8_super
196- (id) init00;
197- (id) init01; // expected-note {{declaration in interface}}
198- (id) init02;
199- (id) init03; // covariance
200- (id) init04; // covariance
201- (id) init05;
202
203- (void) init10; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
204- (void) init11;
205- (void) init12;
206- (void) init13; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
207- (void) init14; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
208- (void) init15;
209
210// These should be invalid to actually call.
211- (Test8_incomplete*) init20;
212- (Test8_incomplete*) init21; // expected-note {{declaration in interface}}
213- (Test8_incomplete*) init22;
214- (Test8_incomplete*) init23;
215- (Test8_incomplete*) init24;
216- (Test8_incomplete*) init25;
217
218- (Test8_super*) init30; // id exception to covariance
219- (Test8_super*) init31; // expected-note {{declaration in interface}}
220- (Test8_super*) init32;
221- (Test8_super*) init33;
222- (Test8_super*) init34; // covariance
223- (Test8_super*) init35;
224
225- (Test8*) init40; // id exception to covariance
226- (Test8*) init41; // expected-note {{declaration in interface}}
227- (Test8*) init42;
228- (Test8*) init43; // this should be a warning, but that's a general language thing, not an ARC thing
229- (Test8*) init44;
230- (Test8*) init45;
231
232- (Test8_complete*) init50; // expected-error {{init methods must return a type related to the receiver type}}
233- (Test8_complete*) init51; // expected-error {{init methods must return a type related to the receiver type}}
234- (Test8_complete*) init52; // expected-error {{init methods must return a type related to the receiver type}}
235- (Test8_complete*) init53; // expected-error {{init methods must return a type related to the receiver type}}
236- (Test8_complete*) init54; // expected-error {{init methods must return a type related to the receiver type}}
237- (Test8_complete*) init55; // expected-error {{init methods must return a type related to the receiver type}}
238@end
239@implementation Test8
240- (id) init00 { return 0; }
241- (id) init10 { return 0; } // expected-error {{method implementation does not match its declaration}}
242- (id) init20 { return 0; }
243- (id) init30 { return 0; }
244- (id) init40 { return 0; }
245- (id) init50 { return 0; }
246
247- (void) init01 {} // expected-error {{method was declared as an 'init' method, but its implementation doesn't match because its result type is not an object pointer}}
248- (void) init11 {}
249- (void) init21 {} // expected-error {{method was declared as an 'init' method, but its implementation doesn't match because its result type is not an object pointer}}
250- (void) init31 {} // expected-error {{method was declared as an 'init' method, but its implementation doesn't match because its result type is not an object pointer}}
251- (void) init41 {} // expected-error {{method was declared as an 'init' method, but its implementation doesn't match because its result type is not an object pointer}}
252- (void) init51 {}
253
254- (Test8_incomplete*) init02 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
255- (Test8_incomplete*) init12 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
256- (Test8_incomplete*) init22 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
257- (Test8_incomplete*) init32 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
258- (Test8_incomplete*) init42 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
259- (Test8_incomplete*) init52 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
260
261- (Test8_super*) init03 { return 0; }
262- (Test8_super*) init13 { return 0; } // expected-error {{method implementation does not match its declaration}}
263- (Test8_super*) init23 { return 0; }
264- (Test8_super*) init33 { return 0; }
265- (Test8_super*) init43 { return 0; }
266- (Test8_super*) init53 { return 0; }
267
268- (Test8*) init04 { return 0; }
269- (Test8*) init14 { return 0; } // expected-error {{method implementation does not match its declaration}}
270- (Test8*) init24 { return 0; }
271- (Test8*) init34 { return 0; }
272- (Test8*) init44 { return 0; }
273- (Test8*) init54 { return 0; }
274
275- (Test8_complete*) init05 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
276- (Test8_complete*) init15 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
277- (Test8_complete*) init25 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
278- (Test8_complete*) init35 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
279- (Test8_complete*) init45 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
280- (Test8_complete*) init55 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
281@end
282
283@class Test9_incomplete;
284@interface Test9
285- (Test9_incomplete*) init1; // expected-error {{init methods must return a type related to the receiver type}}
286- (Test9_incomplete*) init2;
287@end
288id test9(Test9 *v) {
289 return [v init1];
290}
291
292// rdar://9491791
293void rdar9491791(int p) {
294 switch (p) {
295 case 3:;
Argyrios Kyrtzidisea2224d2013-01-04 18:30:08 +0000296 NSObject *o = [[NSObject alloc] init];
John McCall8f0e8d22011-06-15 23:25:17 +0000297 [o release];
298 break;
Argyrios Kyrtzidisea2224d2013-01-04 18:30:08 +0000299 default:
John McCall8f0e8d22011-06-15 23:25:17 +0000300 break;
301 }
302}
303
304#define RELEASE_MACRO(x) do { [x release]; } while(1)
305
306// rdar://9504750
307void rdar9504750(id p) {
Ted Kremenekd6cf9122012-02-10 02:45:47 +0000308 RELEASE_MACRO(p); // expected-error {{ARC forbids explicit message send of 'release'}}
John McCall8f0e8d22011-06-15 23:25:17 +0000309}
Argyrios Kyrtzidis7bf952e2011-06-22 18:03:59 +0000310
311// rdar://8939557
312@interface TestReadonlyProperty : NSObject
313@property(assign,readonly) NSObject *value;
314@end
315
316@implementation TestReadonlyProperty
317@synthesize value;
318- (void)viewDidLoad {
319 value = [NSObject new]; // expected-error {{assigning retained object}}
320}
321@end
Argyrios Kyrtzidiscf79eba2011-06-24 18:41:23 +0000322
323// rdar://9601437
324@interface I9601437 {
325 __unsafe_unretained id x;
326}
327-(void)Meth;
328@end
329
330@implementation I9601437
331-(void)Meth {
332 self->x = [NSObject new]; // expected-error {{assigning retained object}}
333}
334@end
Argyrios Kyrtzidisffe76dd2012-06-20 01:10:40 +0000335
Argyrios Kyrtzidis24016762013-08-09 20:20:25 +0000336@interface Test10 : NSObject {
337 CFStringRef cfstr;
338}
Argyrios Kyrtzidisffe76dd2012-06-20 01:10:40 +0000339@property (retain) id prop;
340-(void)foo;
341@end
342
343void test(Test10 *x) {
344 x.prop = ^{ [x foo]; }; // expected-warning {{likely to lead to a retain cycle}} \
345 // expected-note {{retained by the captured object}}
346}
Argyrios Kyrtzidis24016762013-08-09 20:20:25 +0000347
348@implementation Test10
349-(void)foo {
350 ^{
351 NSString *str = (NSString *)cfstr; // expected-error {{cast of C pointer type 'CFStringRef' (aka 'const struct __CFString *') to Objective-C pointer type 'NSString *' requires a bridged cast}} \
352 // expected-note {{use __bridge to convert directly (no change in ownership)}} \
353 // expected-note {{use CFBridgingRelease call to transfer ownership of a +1 'CFStringRef' (aka 'const struct __CFString *') into ARC}}
354 };
355}
356@end