blob: 7a9655037ebffc370a05d3a956756ef23105cb9f [file] [log] [blame]
John McCalld1e40d52011-10-02 01:16:38 +00001// RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fobjc-runtime-has-weak -fsyntax-only -fobjc-arc -fblocks -verify %s
John McCallf85e1932011-06-15 23:02:42 +00002
3typedef unsigned long NSUInteger;
Fariborz Jahanian52b62362012-02-01 22:56:20 +00004typedef const void * CFTypeRef;
5CFTypeRef CFBridgingRetain(id X);
6id CFBridgingRelease(CFTypeRef);
John McCallf85e1932011-06-15 23:02:42 +00007
8void test0(void (*fn)(int), int val) {
9 fn(val);
10}
11
12@interface A
13- (id)retain;
14- (id)autorelease;
15- (oneway void)release;
16- (void)dealloc;
17- (NSUInteger)retainCount;
18@end
19
20void test1(A *a) {
21 SEL s = @selector(retain); // expected-error {{ARC forbids use of 'retain' in a @selector}}
22 s = @selector(release); // expected-error {{ARC forbids use of 'release' in a @selector}}
23 s = @selector(autorelease); // expected-error {{ARC forbids use of 'autorelease' in a @selector}}
24 s = @selector(dealloc); // expected-error {{ARC forbids use of 'dealloc' in a @selector}}
25 [a dealloc]; // expected-error {{ARC forbids explicit message send of 'dealloc'}}
26 [a retain]; // expected-error {{ARC forbids explicit message send of 'retain'}}
27 [a retainCount]; // expected-error {{ARC forbids explicit message send of 'retainCount'}}
28 [a release]; // expected-error {{ARC forbids explicit message send of 'release'}}
29 [a autorelease]; // expected-error {{ARC forbids explicit message send of 'autorelease'}}
30}
31
32@interface Test2 : A
33- (void) dealloc;
34@end
35@implementation Test2
36- (void) dealloc {
37 // This should maybe just be ignored. We're just going to warn about it for now.
38 [super dealloc]; // expected-error {{ARC forbids explicit message send of 'dealloc'}}
39}
40@end
41
42__weak __strong id x; // expected-error {{the type '__strong id' already has retainment attributes}}
43
44// rdar://8843638
45
46@interface I
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +000047- (id)retain; // expected-note {{method declared here}}
48- (id)autorelease; // expected-note {{method declared here}}
49- (oneway void)release; // expected-note {{method declared here}}
50- (NSUInteger)retainCount; // expected-note {{method declared here}}
John McCallf85e1932011-06-15 23:02:42 +000051@end
52
53@implementation I
54- (id)retain{return 0;} // expected-error {{ARC forbids implementation of 'retain'}}
55- (id)autorelease{return 0;} // expected-error {{ARC forbids implementation of 'autorelease'}}
56- (oneway void)release{} // expected-error {{ARC forbids implementation of 'release'}}
57- (NSUInteger)retainCount{ return 0; } // expected-error {{ARC forbids implementation of 'retainCount'}}
58@end
59
60@implementation I(CAT)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +000061- (id)retain{return 0;} // expected-error {{ARC forbids implementation of 'retain'}} \
62 // expected-warning {{category is implementing a method which will also be implemented by its primary class}}
63- (id)autorelease{return 0;} // expected-error {{ARC forbids implementation of 'autorelease'}} \
64 // expected-warning {{category is implementing a method which will also be implemented by its primary class}}
65- (oneway void)release{} // expected-error {{ARC forbids implementation of 'release'}} \
66 // expected-warning {{category is implementing a method which will also be implemented by its primary class}}
67- (NSUInteger)retainCount{ return 0; } // expected-error {{ARC forbids implementation of 'retainCount'}} \
68 // expected-warning {{category is implementing a method which will also be implemented by its primary class}}
John McCallf85e1932011-06-15 23:02:42 +000069@end
70
71// rdar://8861761
72
73@interface B
74-(id)alloc;
75- (id)initWithInt: (int) i;
76@end
77
78void rdar8861761() {
79 B *o1 = [[B alloc] initWithInt:0];
80 B *o2 = [B alloc];
81 [o2 initWithInt:0]; // expected-warning {{expression result unused}}
82}
83
84// rdar://8925835
85@interface rdar8925835
86- (void)foo:(void (^)(unsigned captureCount, I * const capturedStrings[captureCount]))block;
87@end
88
89void test5() {
90 extern void test5_helper(__autoreleasing id *);
91 id x;
92
93 // Okay because of magic temporaries.
94 test5_helper(&x);
95
96 __autoreleasing id *a = &x; // expected-error {{initializing '__autoreleasing id *' with an expression of type '__strong id *' changes retain/release properties of pointer}}
97
98 a = &x; // expected-error {{assigning '__strong id *' to '__autoreleasing id *' changes retain/release properties of pointer}}
99
100 extern void test5_helper2(id const *);
101 test5_helper2(&x);
102
103 extern void test5_helper3(__weak id *); // expected-note {{passing argument to parameter here}}
104 test5_helper3(&x); // expected-error {{passing '__strong id *' to parameter of type '__weak id *' changes retain/release properties of pointer}}
105}
106
107// rdar://problem/8937869
108void test6(unsigned cond) {
109 switch (cond) {
110 case 0:
111 ;
112 id x; // expected-note {{jump bypasses initialization of retaining variable}}
113
114 case 1: // expected-error {{switch case is in protected scope}}
115 break;
116 }
117}
118
119@class NSError;
120void test7(void) {
121 extern void test7_helper(NSError **);
122 NSError *err;
123 test7_helper(&err);
124}
125void test7_weak(void) {
126 extern void test7_helper(NSError **);
127 __weak NSError *err;
128 test7_helper(&err);
129}
130void test7_unsafe(void) {
131 extern void test7_helper(NSError **); // expected-note {{passing argument to parameter here}}
132 __unsafe_unretained NSError *err;
133 test7_helper(&err); // expected-error {{passing 'NSError *__unsafe_unretained *' to parameter of type 'NSError *__autoreleasing *' changes retain/release properties of pointer}}
134}
135
136@class Test8_incomplete;
137@interface Test8_complete @end;
138@interface Test8_super @end;
139@interface Test8 : Test8_super
140- (id) init00;
141- (id) init01; // expected-note {{declaration in interface}} \
142 // expected-note{{overridden method}}
143- (id) init02; // expected-note{{overridden method}}
144- (id) init03; // covariance
145- (id) init04; // covariance
146- (id) init05; // expected-note{{overridden method}}
147
148- (void) init10; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
149- (void) init11;
150- (void) init12;
151- (void) init13; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
152- (void) init14; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
153- (void) init15;
154
155// These should be invalid to actually call.
156- (Test8_incomplete*) init20;
157- (Test8_incomplete*) init21; // expected-note {{declaration in interface}}
158- (Test8_incomplete*) init22;
159- (Test8_incomplete*) init23;
160- (Test8_incomplete*) init24;
161- (Test8_incomplete*) init25;
162
163- (Test8_super*) init30; // id exception to covariance
164- (Test8_super*) init31; // expected-note {{declaration in interface}} \
165 // expected-note{{overridden method}}
166- (Test8_super*) init32; // expected-note{{overridden method}}
167- (Test8_super*) init33;
168- (Test8_super*) init34; // covariance
169- (Test8_super*) init35; // expected-note{{overridden method}}
170
171- (Test8*) init40; // id exception to covariance
172- (Test8*) init41; // expected-note {{declaration in interface}} \
173 // expected-note{{overridden method}}
174- (Test8*) init42; // expected-note{{overridden method}}
175- (Test8*) init43; // this should be a warning, but that's a general language thing, not an ARC thing
176- (Test8*) init44;
177- (Test8*) init45; // expected-note{{overridden method}}
178
179- (Test8_complete*) init50; // expected-error {{init methods must return a type related to the receiver type}}
180- (Test8_complete*) init51; // expected-error {{init methods must return a type related to the receiver type}}
181- (Test8_complete*) init52; // expected-error {{init methods must return a type related to the receiver type}}
182- (Test8_complete*) init53; // expected-error {{init methods must return a type related to the receiver type}}
183- (Test8_complete*) init54; // expected-error {{init methods must return a type related to the receiver type}}
184- (Test8_complete*) init55; // expected-error {{init methods must return a type related to the receiver type}}
185@end
186@implementation Test8
187- (id) init00 { return 0; }
188- (id) init10 { return 0; } // expected-error {{method implementation does not match its declaration}}
189- (id) init20 { return 0; }
190- (id) init30 { return 0; }
191- (id) init40 { return 0; }
192- (id) init50 { return 0; }
193
194- (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}} \
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000195 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'void'}}
John McCallf85e1932011-06-15 23:02:42 +0000196- (void) init11 {}
197- (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}}
198- (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}} \
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000199 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'void'}}
John McCallf85e1932011-06-15 23:02:42 +0000200- (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}} \
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000201 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'void'}}
John McCallf85e1932011-06-15 23:02:42 +0000202- (void) init51 {}
203
204- (Test8_incomplete*) init02 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
205 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_incomplete *'}}
206- (Test8_incomplete*) init12 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
207- (Test8_incomplete*) init22 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
208- (Test8_incomplete*) init32 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
209 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_incomplete *'}}
210- (Test8_incomplete*) init42 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
211 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_incomplete *'}}
212- (Test8_incomplete*) init52 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
213
214- (Test8_super*) init03 { return 0; }
215- (Test8_super*) init13 { return 0; } // expected-error {{method implementation does not match its declaration}}
216- (Test8_super*) init23 { return 0; }
217- (Test8_super*) init33 { return 0; }
218- (Test8_super*) init43 { return 0; }
219- (Test8_super*) init53 { return 0; }
220
221- (Test8*) init04 { return 0; }
222- (Test8*) init14 { return 0; } // expected-error {{method implementation does not match its declaration}}
223- (Test8*) init24 { return 0; }
224- (Test8*) init34 { return 0; }
225- (Test8*) init44 { return 0; }
226- (Test8*) init54 { return 0; }
227
228- (Test8_complete*) init05 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
229 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_complete *'}}
230- (Test8_complete*) init15 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
231- (Test8_complete*) init25 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
232- (Test8_complete*) init35 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
233 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_complete *'}}
234- (Test8_complete*) init45 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
235 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_complete *'}}
236- (Test8_complete*) init55 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
237@end
238
239@class Test9_incomplete;
240@interface Test9
241- (Test9_incomplete*) init1; // expected-error {{init methods must return a type related to the receiver type}}
242- (Test9_incomplete*) init2;
243@end
244id test9(Test9 *v) {
245 return [v init1];
246}
247
248// Test that the inference rules are different for fast enumeration variables.
249void test10(id collection) {
250 for (id x in collection) {
John McCall7acddac2011-06-17 06:42:21 +0000251 __strong id *ptr = &x; // expected-warning {{initializing '__strong id *' with an expression of type 'const __strong id *' discards qualifiers}}
John McCallf85e1932011-06-15 23:02:42 +0000252 }
253
254 for (__strong id x in collection) {
255 __weak id *ptr = &x; // expected-error {{initializing '__weak id *' with an expression of type '__strong id *' changes retain/release properties of pointer}}
256 }
257}
258
259// rdar://problem/9078626
260#define nil ((void*) 0)
261void test11(id op, void *vp) {
262 _Bool b;
263 b = (op == nil);
264 b = (nil == op);
265
266 b = (vp == nil);
267 b = (nil == vp);
268
Fariborz Jahanian52b62362012-02-01 22:56:20 +0000269 b = (vp == op); // expected-error {{implicit conversion of Objective-C pointer type 'id' to C pointer type 'void *' requires a bridged cast}} expected-note {{use __bridge}} expected-note {{use CFBridgingRetain call}}
270 b = (op == vp); // expected-error {{implicit conversion of C pointer type 'void *' to Objective-C pointer type 'id' requires a bridged cast}} expected-note {{use __bridge}} expected-note {{use CFBridgingRelease call}}
John McCallf85e1932011-06-15 23:02:42 +0000271}
272
273void test12(id collection) {
274 for (id x in collection) {
275 x = 0; // expected-error {{fast enumeration variables can't be modified in ARC by default; declare the variable __strong to allow this}}
276 }
277
278 for (const id x in collection) {
279 x = 0; // expected-error {{read-only variable is not assignable}}
280 }
281
282 for (__strong id x in collection) {
283 x = 0;
284 }
285}
286
287@interface Test13
288- (id) init0;
289- (void) noninit;
290@end
291@implementation Test13
292- (id) init0 {
293 self = 0;
294}
295- (void) noninit {
296 self = 0; // expected-error {{cannot assign to 'self' outside of a method in the init family}}
297}
298@end
299
Ted Kremenek3045ce72011-10-25 04:52:20 +0000300// <rdar://problem/10274056>
301@interface Test13_B
302- (id) consumesSelf __attribute__((ns_consumes_self));
303@end
304@implementation Test13_B
305- (id) consumesSelf {
306 self = 0; // no-warning
307}
308@end
309
John McCallf85e1932011-06-15 23:02:42 +0000310// rdar://problem/9172151
311@class Test14A, Test14B;
312void test14() {
313 extern void test14_consume(id *);
314 extern int test14_cond(void);
315 extern float test14_nowriteback(id __autoreleasing const *); // expected-note{{passing argument to parameter here}}
316
317 Test14A *a;
318 Test14B *b;
319 id i;
320 id cla[10];
321 id vla[test14_cond() + 10];
322
323 test14_consume((__strong id*) &a);
324 test14_consume((test14_cond() ? (__strong id*) &b : &i));
325 test14_consume(test14_cond() ? 0 : &a);
326 test14_consume(test14_cond() ? (void*) 0 : (&a));
327 test14_consume(cla); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}}
328 test14_consume(vla); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}}
329 test14_consume(&cla[5]); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}}
330
331 __strong id *test14_indirect(void);
332 test14_consume(test14_indirect()); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
333
334 extern id test14_global;
335 test14_consume(&test14_global); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
336
337 extern __strong id *test14_global_ptr;
338 test14_consume(test14_global_ptr); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
339
340 static id static_local;
341 test14_consume(&static_local); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
342
343 __weak id* wip;
344 test14_nowriteback(&static_local); // okay, not a write-back.
345 test14_nowriteback(wip); // expected-error{{passing '__weak id *' to parameter of type '__autoreleasing id const *' changes retain/release properties of pointer}}
346}
347
348void test15() {
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000349 __block __autoreleasing id x; // expected-error {{__block variables cannot have __autoreleasing ownership}}
John McCallf85e1932011-06-15 23:02:42 +0000350}
351
352struct Test16;
353@interface Test16a
354- (void) test16_0: (int) x;
355- (int) test16_1: (int) x; // expected-note {{one possibility}}
356- (int) test16_2: (int) x; // expected-note {{one possibility}}
357- (id) test16_3: (int) x __attribute__((ns_returns_retained)); // expected-note {{one possibility}}
358- (void) test16_4: (int) x __attribute__((ns_consumes_self)); // expected-note {{one possibility}}
359- (void) test16_5: (id) __attribute__((ns_consumed)) x; // expected-note {{one possibility}}
360- (void) test16_6: (id) x;
361@end
362
363@interface Test16b
364- (void) test16_0: (int) x;
365- (int) test16_1: (char*) x; // expected-note {{also found}}
366- (char*) test16_2: (int) x; // expected-note {{also found}}
367- (id) test16_3: (int) x; // expected-note {{also found}}
368- (void) test16_4: (int) x; // expected-note {{also found}}
369- (void) test16_5: (id) x; // expected-note {{also found}}
370- (void) test16_6: (struct Test16 *) x;
371@end
372
373void test16(void) {
374 id v;
375 [v test16_0: 0];
376 [v test16_1: 0]; // expected-error {{multiple methods named 'test16_1:' found with mismatched result, parameter type or attributes}}
377 [v test16_2: 0]; // expected-error {{multiple methods named}}
378 [v test16_3: 0]; // expected-error {{multiple methods named}}
379 [v test16_4: 0]; // expected-error {{multiple methods named}}
380 [v test16_5: 0]; // expected-error {{multiple methods named}}
381 [v test16_6: 0];
382}
383
Douglas Gregorb3029962011-11-14 22:10:01 +0000384@class Test17; // expected-note 2{{forward declaration of class here}}
John McCallf85e1932011-06-15 23:02:42 +0000385@protocol Test17p
386- (void) test17;
387+ (void) test17;
388@end
389void test17(void) {
390 Test17 *v0;
391 [v0 test17]; // expected-error {{receiver type 'Test17' for instance message is a forward declaration}}
392
393 Test17<Test17p> *v1;
394 [v1 test17]; // expected-error {{receiver type 'Test17<Test17p>' for instance message is a forward declaration}}
395
396 [Test17 test17]; // expected-error {{receiver 'Test17' for class message is a forward declaration}}
397}
398
399void test18(void) {
400 id x;
401 [x test18]; // expected-error {{no known instance method for selector 'test18'}}
402}
403
404extern struct Test19 *test19a;
405struct Test19 *const test19b = 0;
406void test19(void) {
407 id x;
408 x = (id) test19a; // expected-error {{bridged cast}} \
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000409 // expected-note{{use __bridge to convert directly (no change in ownership)}} \
Fariborz Jahanian52b62362012-02-01 22:56:20 +0000410 // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'struct Test19 *' into ARC}}
John McCallf85e1932011-06-15 23:02:42 +0000411 x = (id) test19b; // expected-error {{bridged cast}} \
412 // expected-note{{use __bridge to convert directly (no change in ownership)}} \
Fariborz Jahanian52b62362012-02-01 22:56:20 +0000413 // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'struct Test19 *' into ARC}}
John McCallf85e1932011-06-15 23:02:42 +0000414}
415
416// rdar://problem/8951453
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000417static __thread id test20_implicit; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
418static __thread __strong id test20_strong; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
419static __thread __weak id test20_weak; // expected-error {{thread-local variable has non-trivial ownership: type is '__weak id'}}
420static __thread __autoreleasing id test20_autoreleasing; // expected-error {{thread-local variable has non-trivial ownership: type is '__autoreleasing id'}} expected-error {{global variables cannot have __autoreleasing ownership}}
John McCallf85e1932011-06-15 23:02:42 +0000421static __thread __unsafe_unretained id test20_unsafe;
422void test20(void) {
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000423 static __thread id test20_implicit; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
424 static __thread __strong id test20_strong; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
425 static __thread __weak id test20_weak; // expected-error {{thread-local variable has non-trivial ownership: type is '__weak id'}}
426 static __thread __autoreleasing id test20_autoreleasing; // expected-error {{thread-local variable has non-trivial ownership: type is '__autoreleasing id'}} expected-error {{global variables cannot have __autoreleasing ownership}}
John McCallf85e1932011-06-15 23:02:42 +0000427 static __thread __unsafe_unretained id test20_unsafe;
428}
429
430// rdar://9310049
431_Bool fn(id obj) {
432 return (_Bool)obj;
433}
434
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000435// Check casting w/ ownership qualifiers.
John McCallf85e1932011-06-15 23:02:42 +0000436void test21() {
437 __strong id *sip;
438 (void)(__weak id *)sip; // expected-error{{casting '__strong id *' to type '__weak id *' changes retain/release properties of pointer}}
439 (void)(__weak const id *)sip; // expected-error{{casting '__strong id *' to type '__weak id const *' changes retain/release properties of pointer}}
440 (void)(__autoreleasing id *)sip; // expected-error{{casting '__strong id *' to type '__autoreleasing id *' changes retain/release properties of pointer}}
441 (void)(__autoreleasing const id *)sip; // okay
442}
443
444// rdar://problem/9340462
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000445void test22(id x[]) { // expected-error {{must explicitly describe intended ownership of an object array parameter}}
John McCallf85e1932011-06-15 23:02:42 +0000446}
447
448// rdar://problem/9400219
449void test23(void) {
450 void *ptr;
451 ptr = @"foo";
452 ptr = (ptr ? @"foo" : 0);
453 ptr = (ptr ? @"foo" : @"bar");
454}
455
456id test24(void) {
457 extern void test24_helper(void);
458 return test24_helper(), (void*) 0;
459}
460
461// rdar://9400841
462@interface Base
463@property (assign) id content;
464@end
465
466@interface Foo : Base
467-(void)test;
468@end
469
470@implementation Foo
471-(void)test {
472 super.content = 0;
473}
474@end
475
476// <rdar://problem/9398437>
477void test25(Class *classes) {
478 Class *other_classes;
479 test25(other_classes);
480}
481
482void test26(id y) {
483 extern id test26_var1;
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000484 __sync_swap(&test26_var1, 0, y); // expected-error {{cannot perform atomic operation on a pointer to type '__strong id': type has non-trivial ownership}}
John McCallf85e1932011-06-15 23:02:42 +0000485
486 extern __unsafe_unretained id test26_var2;
487 __sync_swap(&test26_var2, 0, y);
488}
489
490@interface Test26
491- (id) init;
492- (id) initWithInt: (int) x;
493@end
494@implementation Test26
495- (id) init { return self; }
496- (id) initWithInt: (int) x {
497 [self init]; // expected-error {{the result of a delegate init call must be immediately returned or assigned to 'self'}}
498 return self;
499}
500@end
501
502// rdar://9525555
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000503@interface Test27 {
504 __weak id _myProp1;
505 id myProp2;
506}
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +0000507@property id x;
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +0000508@property (readonly) id ro;
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000509@property (readonly) id custom_ro;
John McCallf85e1932011-06-15 23:02:42 +0000510@property int y;
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000511
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +0000512@property (readonly) __weak id myProp1;
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000513@property (readonly) id myProp2;
514@property (readonly) __strong id myProp3;
John McCallf85e1932011-06-15 23:02:42 +0000515@end
516
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000517@implementation Test27
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +0000518@synthesize x;
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +0000519@synthesize ro;
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000520@synthesize y;
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000521
522@synthesize myProp1 = _myProp1;
523@synthesize myProp2;
524@synthesize myProp3;
525
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000526-(id)custom_ro { return 0; }
527@end
528
John McCallf85e1932011-06-15 23:02:42 +0000529// rdar://9569264
530@interface Test28
531@property (nonatomic, assign) __strong id a; // expected-error {{unsafe_unretained property 'a' may not also be declared __strong}}
532@end
533
534@interface Test28 ()
535@property (nonatomic, assign) __strong id b; // expected-error {{unsafe_unretained property 'b' may not also be declared __strong}}
536@end
537
538@implementation Test28
539@synthesize a;
540@synthesize b;
541@end
542
543// rdar://9573962
544typedef struct Bark Bark;
545@interface Test29
546@property Bark* P;
547@end
548
549@implementation Test29
550@synthesize P;
551- (id)Meth {
552 Bark** f = &P;
553 return 0;
554}
555@end
556
557// rdar://9495837
558@interface Test30
Fariborz Jahanian921c1432011-06-24 18:25:34 +0000559+ (id) new;
John McCallf85e1932011-06-15 23:02:42 +0000560- (void)Meth;
561@end
562
563@implementation Test30
Fariborz Jahanian921c1432011-06-24 18:25:34 +0000564+ (id) new { return 0; }
John McCallf85e1932011-06-15 23:02:42 +0000565- (void) Meth {
Argyrios Kyrtzidisd26fef02011-06-22 18:03:53 +0000566 __weak id x = [Test30 new]; // expected-warning {{assigning retained object to weak variable}}
567 id __unsafe_unretained u = [Test30 new]; // expected-warning {{assigning retained object to unsafe_unretained variable}}
John McCallf85e1932011-06-15 23:02:42 +0000568 id y = [Test30 new];
Argyrios Kyrtzidisd26fef02011-06-22 18:03:53 +0000569 x = [Test30 new]; // expected-warning {{assigning retained object to weak variable}}
570 u = [Test30 new]; // expected-warning {{assigning retained object to unsafe_unretained variable}}
John McCallf85e1932011-06-15 23:02:42 +0000571 y = [Test30 new];
572}
573@end
574
575// rdar://9411838
576@protocol PTest31 @end
577
578int Test31() {
579 Class cls;
580 id ids;
581 id<PTest31> pids;
582 Class<PTest31> pcls;
583
584 int i = (ids->isa ? 1 : 0); // expected-error {{member reference base type 'id' is not a structure or union}}
585 int j = (pids->isa ? 1 : 0); // expected-error {{member reference base type 'id<PTest31>' is not a structure or union}}
586 int k = (pcls->isa ? i : j); // expected-error {{member reference base type 'Class<PTest31>' is not a structure or union}}
587 return cls->isa ? i : j; // expected-error {{member reference base type 'Class' is not a structure or union}}
588}
Fariborz Jahanianb1f7d242011-06-16 17:29:56 +0000589
590// rdar://9612030
591@interface ITest32 {
592@public
593 id ivar;
594}
595@end
596
597id Test32(__weak ITest32 *x) {
598 __weak ITest32 *y;
599 x->ivar = 0; // expected-error {{dereferencing a __weak pointer is not allowed}}
600 return y ? y->ivar // expected-error {{dereferencing a __weak pointer is not allowed}}
601 : (*x).ivar; // expected-error {{dereferencing a __weak pointer is not allowed}}
602}
603
Fariborz Jahanian8295b7b2011-06-22 16:36:45 +0000604// rdar://9619861
605extern int printf(const char*, ...);
606typedef long intptr_t;
607
608int Test33(id someid) {
609 printf( "Hello%ld", (intptr_t)someid);
610 return (int)someid;
611}
612
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000613// rdar://9636091
614@interface I34
615@property (nonatomic, retain) id newName __attribute__((ns_returns_not_retained)) ;
616
617@property (nonatomic, retain) id newName1 __attribute__((ns_returns_not_retained)) ;
618- (id) newName1 __attribute__((ns_returns_not_retained));
619
620@property (nonatomic, retain) id newName2 __attribute__((ns_returns_not_retained)); // expected-note {{roperty declared here}}
621- (id) newName2; // expected-warning {{property declared as returning non-retained objects; getter returning retained objects}}
622@end
623
624@implementation I34
625@synthesize newName;
626
627@synthesize newName1;
Fariborz Jahanian78980052011-08-01 22:39:49 +0000628- (id) newName1 { return 0; }
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000629
Fariborz Jahanian78980052011-08-01 22:39:49 +0000630@synthesize newName2;
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000631@end
632
John McCallc03fa492011-06-27 23:59:58 +0000633void test35(void) {
634 extern void test36_helper(id*);
635 id x;
636 __strong id *xp = 0;
637
638 test36_helper(&x);
639 test36_helper(xp); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
640
641 // rdar://problem/9665710
642 __block id y;
643 test36_helper(&y);
644 ^{ test36_helper(&y); }();
Argyrios Kyrtzidis05d48762011-07-01 22:23:09 +0000645
646 __strong int non_objc_type; // expected-warning {{'__strong' only applies to objective-c object or block pointer types}}
John McCallc03fa492011-06-27 23:59:58 +0000647}
Douglas Gregorf96e9042011-07-12 17:28:52 +0000648
649void test36(int first, ...) {
650 // <rdar://problem/9758798>
651 __builtin_va_list arglist;
652 __builtin_va_start(arglist, first);
653 id obj = __builtin_va_arg(arglist, id);
654 __builtin_va_end(arglist);
655}
John McCall990567c2011-07-27 01:07:15 +0000656
Douglas Gregorb3029962011-11-14 22:10:01 +0000657@class Test37; // expected-note{{forward declaration of class here}}
John McCall990567c2011-07-27 01:07:15 +0000658void test37(Test37 *c) {
659 for (id y in c) { // expected-error {{collection expression type 'Test37' is a forward declaration}}
660 (void) y;
661 }
Argyrios Kyrtzidis5b76f372011-09-20 23:49:22 +0000662
663 (void)sizeof(id*); // no error.
John McCall990567c2011-07-27 01:07:15 +0000664}
John McCalla8e0cd82011-08-06 07:30:58 +0000665
666// rdar://problem/9887979
667@interface Test38
668@property int value;
669@end
670void test38() {
671 extern Test38 *test38_helper(void);
672 switch (test38_helper().value) {
673 case 0:
674 case 1:
675 ;
676 }
677}
Fariborz Jahanian175fb102011-10-03 22:11:57 +0000678
679// rdar://10186536
680@class NSColor;
681void _NSCalc(NSColor* color, NSColor* bezelColors[]) __attribute__((unavailable("not available in automatic reference counting mode")));
682
683void _NSCalcBeze(NSColor* color, NSColor* bezelColors[]); // expected-error {{must explicitly describe intended ownership of an object array parameter}}
Fariborz Jahaniana1de8cb2011-10-14 17:34:08 +0000684
685// rdar://9970739
686@interface RestaurantTableViewCell
687- (void) restaurantLocation;
688@end
689
690@interface Radar9970739
691- (void) Meth;
692@end
693
694@implementation Radar9970739
695- (void) Meth {
696 RestaurantTableViewCell *cell;
697 [cell restaurantLocatoin]; // expected-error {{no visible @interface for 'RestaurantTableViewCell' declares the selector 'restaurantLocatoin'}}
698}
699@end
700