blob: 60bd6735dbf5b237619a897e9858d60fd1187771 [file] [log] [blame]
Patrick Beardb2f68202012-04-06 18:12:22 +00001// RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fobjc-runtime-has-weak -fsyntax-only -fobjc-arc -fblocks -verify -Wno-objc-root-class %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);
Ted Kremenek9d084012012-12-21 08:04:28 +00007@protocol NSCopying @end
8@interface NSDictionary
9+ (id)dictionaryWithObjects:(const id [])objects forKeys:(const id <NSCopying> [])keys count:(NSUInteger)cnt;
10- (void)setObject:(id)object forKeyedSubscript:(id)key;
11@end
12@class NSFastEnumerationState;
13@protocol NSFastEnumeration
Douglas Gregorf373c5d2013-01-18 01:41:40 +000014- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len;
Ted Kremenek9d084012012-12-21 08:04:28 +000015@end
16@interface NSNumber
17+ (NSNumber *)numberWithInt:(int)value;
18@end
19@interface NSArray <NSFastEnumeration>
20+ (id)arrayWithObjects:(const id [])objects count:(NSUInteger)cnt;
21@end
John McCallf85e1932011-06-15 23:02:42 +000022
23void test0(void (*fn)(int), int val) {
24 fn(val);
25}
26
27@interface A
28- (id)retain;
29- (id)autorelease;
30- (oneway void)release;
31- (void)dealloc;
32- (NSUInteger)retainCount;
33@end
34
35void test1(A *a) {
36 SEL s = @selector(retain); // expected-error {{ARC forbids use of 'retain' in a @selector}}
37 s = @selector(release); // expected-error {{ARC forbids use of 'release' in a @selector}}
38 s = @selector(autorelease); // expected-error {{ARC forbids use of 'autorelease' in a @selector}}
39 s = @selector(dealloc); // expected-error {{ARC forbids use of 'dealloc' in a @selector}}
40 [a dealloc]; // expected-error {{ARC forbids explicit message send of 'dealloc'}}
41 [a retain]; // expected-error {{ARC forbids explicit message send of 'retain'}}
42 [a retainCount]; // expected-error {{ARC forbids explicit message send of 'retainCount'}}
43 [a release]; // expected-error {{ARC forbids explicit message send of 'release'}}
44 [a autorelease]; // expected-error {{ARC forbids explicit message send of 'autorelease'}}
45}
46
47@interface Test2 : A
48- (void) dealloc;
49@end
50@implementation Test2
51- (void) dealloc {
52 // This should maybe just be ignored. We're just going to warn about it for now.
53 [super dealloc]; // expected-error {{ARC forbids explicit message send of 'dealloc'}}
54}
55@end
56
John McCallf85e1932011-06-15 23:02:42 +000057// rdar://8843638
58
59@interface I
Ted Kremenek3306ec12012-02-27 22:55:11 +000060- (id)retain; // expected-note {{method 'retain' declared here}}
61- (id)autorelease; // expected-note {{method 'autorelease' declared here}}
62- (oneway void)release; // expected-note {{method 'release' declared here}}
63- (NSUInteger)retainCount; // expected-note {{method 'retainCount' declared here}}
John McCallf85e1932011-06-15 23:02:42 +000064@end
65
66@implementation I
67- (id)retain{return 0;} // expected-error {{ARC forbids implementation of 'retain'}}
68- (id)autorelease{return 0;} // expected-error {{ARC forbids implementation of 'autorelease'}}
69- (oneway void)release{} // expected-error {{ARC forbids implementation of 'release'}}
70- (NSUInteger)retainCount{ return 0; } // expected-error {{ARC forbids implementation of 'retainCount'}}
71@end
72
73@implementation I(CAT)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +000074- (id)retain{return 0;} // expected-error {{ARC forbids implementation of 'retain'}} \
75 // expected-warning {{category is implementing a method which will also be implemented by its primary class}}
76- (id)autorelease{return 0;} // expected-error {{ARC forbids implementation of 'autorelease'}} \
77 // expected-warning {{category is implementing a method which will also be implemented by its primary class}}
78- (oneway void)release{} // expected-error {{ARC forbids implementation of 'release'}} \
79 // expected-warning {{category is implementing a method which will also be implemented by its primary class}}
80- (NSUInteger)retainCount{ return 0; } // expected-error {{ARC forbids implementation of 'retainCount'}} \
81 // expected-warning {{category is implementing a method which will also be implemented by its primary class}}
John McCallf85e1932011-06-15 23:02:42 +000082@end
83
84// rdar://8861761
85
86@interface B
Jean-Daniel Dupas4bdb6022013-07-19 20:25:56 +000087+ (id)alloc;
John McCallf85e1932011-06-15 23:02:42 +000088- (id)initWithInt: (int) i;
Jean-Daniel Dupas4bdb6022013-07-19 20:25:56 +000089- (id)myInit __attribute__((objc_method_family(init)));
Aaron Ballman624421f2013-08-31 01:11:41 +000090- (id)myBadInit __attribute__((objc_method_family(12))); // expected-error {{'objc_method_family' attribute requires parameter 1 to be an identifier}}
91
John McCallf85e1932011-06-15 23:02:42 +000092@end
93
94void rdar8861761() {
95 B *o1 = [[B alloc] initWithInt:0];
96 B *o2 = [B alloc];
97 [o2 initWithInt:0]; // expected-warning {{expression result unused}}
Jean-Daniel Dupas4bdb6022013-07-19 20:25:56 +000098 B *o3 = [[B alloc] myInit];
99 [[B alloc] myInit]; // expected-warning {{expression result unused}}
John McCallf85e1932011-06-15 23:02:42 +0000100}
101
102// rdar://8925835
103@interface rdar8925835
104- (void)foo:(void (^)(unsigned captureCount, I * const capturedStrings[captureCount]))block;
105@end
106
107void test5() {
108 extern void test5_helper(__autoreleasing id *);
109 id x;
110
111 // Okay because of magic temporaries.
112 test5_helper(&x);
113
114 __autoreleasing id *a = &x; // expected-error {{initializing '__autoreleasing id *' with an expression of type '__strong id *' changes retain/release properties of pointer}}
115
116 a = &x; // expected-error {{assigning '__strong id *' to '__autoreleasing id *' changes retain/release properties of pointer}}
117
118 extern void test5_helper2(id const *);
119 test5_helper2(&x);
120
121 extern void test5_helper3(__weak id *); // expected-note {{passing argument to parameter here}}
122 test5_helper3(&x); // expected-error {{passing '__strong id *' to parameter of type '__weak id *' changes retain/release properties of pointer}}
123}
124
125// rdar://problem/8937869
126void test6(unsigned cond) {
127 switch (cond) {
128 case 0:
129 ;
130 id x; // expected-note {{jump bypasses initialization of retaining variable}}
131
Stephen Hines176edba2014-12-01 14:53:08 -0800132 case 1: // expected-error {{cannot jump}}
John McCallf85e1932011-06-15 23:02:42 +0000133 break;
134 }
135}
136
137@class NSError;
138void test7(void) {
139 extern void test7_helper(NSError **);
140 NSError *err;
141 test7_helper(&err);
142}
143void test7_weak(void) {
144 extern void test7_helper(NSError **);
145 __weak NSError *err;
146 test7_helper(&err);
147}
148void test7_unsafe(void) {
149 extern void test7_helper(NSError **); // expected-note {{passing argument to parameter here}}
150 __unsafe_unretained NSError *err;
151 test7_helper(&err); // expected-error {{passing 'NSError *__unsafe_unretained *' to parameter of type 'NSError *__autoreleasing *' changes retain/release properties of pointer}}
152}
153
154@class Test8_incomplete;
155@interface Test8_complete @end;
156@interface Test8_super @end;
157@interface Test8 : Test8_super
158- (id) init00;
159- (id) init01; // expected-note {{declaration in interface}} \
160 // expected-note{{overridden method}}
161- (id) init02; // expected-note{{overridden method}}
162- (id) init03; // covariance
163- (id) init04; // covariance
164- (id) init05; // expected-note{{overridden method}}
165
166- (void) init10; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
167- (void) init11;
168- (void) init12;
169- (void) init13; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
170- (void) init14; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
171- (void) init15;
172
173// These should be invalid to actually call.
174- (Test8_incomplete*) init20;
175- (Test8_incomplete*) init21; // expected-note {{declaration in interface}}
176- (Test8_incomplete*) init22;
177- (Test8_incomplete*) init23;
178- (Test8_incomplete*) init24;
179- (Test8_incomplete*) init25;
180
181- (Test8_super*) init30; // id exception to covariance
182- (Test8_super*) init31; // expected-note {{declaration in interface}} \
183 // expected-note{{overridden method}}
184- (Test8_super*) init32; // expected-note{{overridden method}}
185- (Test8_super*) init33;
186- (Test8_super*) init34; // covariance
187- (Test8_super*) init35; // expected-note{{overridden method}}
188
189- (Test8*) init40; // id exception to covariance
190- (Test8*) init41; // expected-note {{declaration in interface}} \
191 // expected-note{{overridden method}}
192- (Test8*) init42; // expected-note{{overridden method}}
193- (Test8*) init43; // this should be a warning, but that's a general language thing, not an ARC thing
194- (Test8*) init44;
195- (Test8*) init45; // expected-note{{overridden method}}
196
197- (Test8_complete*) init50; // expected-error {{init methods must return a type related to the receiver type}}
198- (Test8_complete*) init51; // expected-error {{init methods must return a type related to the receiver type}}
199- (Test8_complete*) init52; // expected-error {{init methods must return a type related to the receiver type}}
200- (Test8_complete*) init53; // expected-error {{init methods must return a type related to the receiver type}}
201- (Test8_complete*) init54; // expected-error {{init methods must return a type related to the receiver type}}
202- (Test8_complete*) init55; // expected-error {{init methods must return a type related to the receiver type}}
203@end
204@implementation Test8
205- (id) init00 { return 0; }
206- (id) init10 { return 0; } // expected-error {{method implementation does not match its declaration}}
207- (id) init20 { return 0; }
208- (id) init30 { return 0; }
209- (id) init40 { return 0; }
210- (id) init50 { return 0; }
211
212- (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 +0000213 // 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 +0000214- (void) init11 {}
215- (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}}
216- (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 +0000217 // 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 +0000218- (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 +0000219 // 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 +0000220- (void) init51 {}
221
222- (Test8_incomplete*) init02 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
223 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_incomplete *'}}
224- (Test8_incomplete*) init12 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
225- (Test8_incomplete*) init22 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
226- (Test8_incomplete*) init32 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
227 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_incomplete *'}}
228- (Test8_incomplete*) init42 { 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_incomplete *'}}
230- (Test8_incomplete*) init52 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
231
232- (Test8_super*) init03 { return 0; }
233- (Test8_super*) init13 { return 0; } // expected-error {{method implementation does not match its declaration}}
234- (Test8_super*) init23 { return 0; }
235- (Test8_super*) init33 { return 0; }
236- (Test8_super*) init43 { return 0; }
237- (Test8_super*) init53 { return 0; }
238
239- (Test8*) init04 { return 0; }
240- (Test8*) init14 { return 0; } // expected-error {{method implementation does not match its declaration}}
241- (Test8*) init24 { return 0; }
242- (Test8*) init34 { return 0; }
243- (Test8*) init44 { return 0; }
244- (Test8*) init54 { return 0; }
245
246- (Test8_complete*) init05 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
247 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_complete *'}}
248- (Test8_complete*) init15 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
249- (Test8_complete*) init25 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
250- (Test8_complete*) init35 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
251 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_complete *'}}
252- (Test8_complete*) init45 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
253 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_complete *'}}
254- (Test8_complete*) init55 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
255@end
256
257@class Test9_incomplete;
258@interface Test9
259- (Test9_incomplete*) init1; // expected-error {{init methods must return a type related to the receiver type}}
260- (Test9_incomplete*) init2;
261@end
262id test9(Test9 *v) {
263 return [v init1];
264}
265
266// Test that the inference rules are different for fast enumeration variables.
267void test10(id collection) {
268 for (id x in collection) {
John McCall7acddac2011-06-17 06:42:21 +0000269 __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 +0000270 }
271
272 for (__strong id x in collection) {
273 __weak id *ptr = &x; // expected-error {{initializing '__weak id *' with an expression of type '__strong id *' changes retain/release properties of pointer}}
274 }
275}
276
277// rdar://problem/9078626
278#define nil ((void*) 0)
279void test11(id op, void *vp) {
280 _Bool b;
281 b = (op == nil);
282 b = (nil == op);
283
284 b = (vp == nil);
285 b = (nil == vp);
286
Fariborz Jahanian607f5872012-07-27 21:34:23 +0000287 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}}
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700288 b = (op == vp);
John McCallf85e1932011-06-15 23:02:42 +0000289}
290
291void test12(id collection) {
292 for (id x in collection) {
293 x = 0; // expected-error {{fast enumeration variables can't be modified in ARC by default; declare the variable __strong to allow this}}
294 }
295
Pirama Arumuga Nainar33337ca2015-05-06 11:48:57 -0700296 for (const id x in collection) { // expected-note {{variable 'x' declared const here}}
297 x = 0; // expected-error {{cannot assign to variable 'x' with const-qualified type 'const __strong id'}}
John McCallf85e1932011-06-15 23:02:42 +0000298 }
299
300 for (__strong id x in collection) {
301 x = 0;
302 }
303}
304
305@interface Test13
306- (id) init0;
307- (void) noninit;
308@end
309@implementation Test13
310- (id) init0 {
311 self = 0;
312}
313- (void) noninit {
314 self = 0; // expected-error {{cannot assign to 'self' outside of a method in the init family}}
315}
316@end
317
Ted Kremenek3045ce72011-10-25 04:52:20 +0000318// <rdar://problem/10274056>
319@interface Test13_B
320- (id) consumesSelf __attribute__((ns_consumes_self));
321@end
322@implementation Test13_B
323- (id) consumesSelf {
324 self = 0; // no-warning
325}
326@end
327
John McCallf85e1932011-06-15 23:02:42 +0000328// rdar://problem/9172151
329@class Test14A, Test14B;
330void test14() {
331 extern void test14_consume(id *);
332 extern int test14_cond(void);
333 extern float test14_nowriteback(id __autoreleasing const *); // expected-note{{passing argument to parameter here}}
334
335 Test14A *a;
336 Test14B *b;
337 id i;
338 id cla[10];
339 id vla[test14_cond() + 10];
340
341 test14_consume((__strong id*) &a);
342 test14_consume((test14_cond() ? (__strong id*) &b : &i));
343 test14_consume(test14_cond() ? 0 : &a);
344 test14_consume(test14_cond() ? (void*) 0 : (&a));
345 test14_consume(cla); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}}
346 test14_consume(vla); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}}
347 test14_consume(&cla[5]); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}}
348
349 __strong id *test14_indirect(void);
350 test14_consume(test14_indirect()); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
351
352 extern id test14_global;
353 test14_consume(&test14_global); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
354
355 extern __strong id *test14_global_ptr;
356 test14_consume(test14_global_ptr); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
357
358 static id static_local;
359 test14_consume(&static_local); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
360
361 __weak id* wip;
362 test14_nowriteback(&static_local); // okay, not a write-back.
363 test14_nowriteback(wip); // expected-error{{passing '__weak id *' to parameter of type '__autoreleasing id const *' changes retain/release properties of pointer}}
364}
365
366void test15() {
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000367 __block __autoreleasing id x; // expected-error {{__block variables cannot have __autoreleasing ownership}}
John McCallf85e1932011-06-15 23:02:42 +0000368}
369
370struct Test16;
371@interface Test16a
372- (void) test16_0: (int) x;
373- (int) test16_1: (int) x; // expected-note {{one possibility}}
374- (int) test16_2: (int) x; // expected-note {{one possibility}}
375- (id) test16_3: (int) x __attribute__((ns_returns_retained)); // expected-note {{one possibility}}
376- (void) test16_4: (int) x __attribute__((ns_consumes_self)); // expected-note {{one possibility}}
377- (void) test16_5: (id) __attribute__((ns_consumed)) x; // expected-note {{one possibility}}
378- (void) test16_6: (id) x;
379@end
380
381@interface Test16b
382- (void) test16_0: (int) x;
383- (int) test16_1: (char*) x; // expected-note {{also found}}
384- (char*) test16_2: (int) x; // expected-note {{also found}}
385- (id) test16_3: (int) x; // expected-note {{also found}}
386- (void) test16_4: (int) x; // expected-note {{also found}}
387- (void) test16_5: (id) x; // expected-note {{also found}}
388- (void) test16_6: (struct Test16 *) x;
389@end
390
391void test16(void) {
392 id v;
393 [v test16_0: 0];
394 [v test16_1: 0]; // expected-error {{multiple methods named 'test16_1:' found with mismatched result, parameter type or attributes}}
395 [v test16_2: 0]; // expected-error {{multiple methods named}}
396 [v test16_3: 0]; // expected-error {{multiple methods named}}
397 [v test16_4: 0]; // expected-error {{multiple methods named}}
398 [v test16_5: 0]; // expected-error {{multiple methods named}}
399 [v test16_6: 0];
400}
401
Douglas Gregorb3029962011-11-14 22:10:01 +0000402@class Test17; // expected-note 2{{forward declaration of class here}}
John McCallf85e1932011-06-15 23:02:42 +0000403@protocol Test17p
404- (void) test17;
405+ (void) test17;
406@end
407void test17(void) {
408 Test17 *v0;
409 [v0 test17]; // expected-error {{receiver type 'Test17' for instance message is a forward declaration}}
410
411 Test17<Test17p> *v1;
412 [v1 test17]; // expected-error {{receiver type 'Test17<Test17p>' for instance message is a forward declaration}}
413
414 [Test17 test17]; // expected-error {{receiver 'Test17' for class message is a forward declaration}}
415}
416
417void test18(void) {
418 id x;
Fariborz Jahanian419245e2013-06-18 15:31:36 +0000419 [x test18]; // expected-error {{instance method 'test18' not found ; did you mean 'test17'?}}
John McCallf85e1932011-06-15 23:02:42 +0000420}
421
422extern struct Test19 *test19a;
423struct Test19 *const test19b = 0;
424void test19(void) {
425 id x;
426 x = (id) test19a; // expected-error {{bridged cast}} \
Fariborz Jahanian607f5872012-07-27 21:34:23 +0000427 // expected-note{{use __bridge to convert directly (no change in ownership)}} \
Fariborz Jahanian52b62362012-02-01 22:56:20 +0000428 // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'struct Test19 *' into ARC}}
John McCallf85e1932011-06-15 23:02:42 +0000429 x = (id) test19b; // expected-error {{bridged cast}} \
Fariborz Jahanian607f5872012-07-27 21:34:23 +0000430 // expected-note{{use __bridge to convert directly (no change in ownership)}} \
Fariborz Jahanian52b62362012-02-01 22:56:20 +0000431 // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'struct Test19 *' into ARC}}
John McCallf85e1932011-06-15 23:02:42 +0000432}
433
434// rdar://problem/8951453
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000435static __thread id test20_implicit; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
436static __thread __strong id test20_strong; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
437static __thread __weak id test20_weak; // expected-error {{thread-local variable has non-trivial ownership: type is '__weak id'}}
438static __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 +0000439static __thread __unsafe_unretained id test20_unsafe;
440void test20(void) {
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000441 static __thread id test20_implicit; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
442 static __thread __strong id test20_strong; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
443 static __thread __weak id test20_weak; // expected-error {{thread-local variable has non-trivial ownership: type is '__weak id'}}
444 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 +0000445 static __thread __unsafe_unretained id test20_unsafe;
446}
447
448// rdar://9310049
449_Bool fn(id obj) {
450 return (_Bool)obj;
451}
452
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000453// Check casting w/ ownership qualifiers.
John McCallf85e1932011-06-15 23:02:42 +0000454void test21() {
455 __strong id *sip;
456 (void)(__weak id *)sip; // expected-error{{casting '__strong id *' to type '__weak id *' changes retain/release properties of pointer}}
457 (void)(__weak const id *)sip; // expected-error{{casting '__strong id *' to type '__weak id const *' changes retain/release properties of pointer}}
458 (void)(__autoreleasing id *)sip; // expected-error{{casting '__strong id *' to type '__autoreleasing id *' changes retain/release properties of pointer}}
459 (void)(__autoreleasing const id *)sip; // okay
460}
461
462// rdar://problem/9340462
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000463void test22(id x[]) { // expected-error {{must explicitly describe intended ownership of an object array parameter}}
John McCallf85e1932011-06-15 23:02:42 +0000464}
465
466// rdar://problem/9400219
467void test23(void) {
468 void *ptr;
469 ptr = @"foo";
470 ptr = (ptr ? @"foo" : 0);
471 ptr = (ptr ? @"foo" : @"bar");
472}
473
474id test24(void) {
475 extern void test24_helper(void);
476 return test24_helper(), (void*) 0;
477}
478
479// rdar://9400841
480@interface Base
481@property (assign) id content;
482@end
483
484@interface Foo : Base
485-(void)test;
486@end
487
488@implementation Foo
489-(void)test {
490 super.content = 0;
491}
492@end
493
494// <rdar://problem/9398437>
495void test25(Class *classes) {
496 Class *other_classes;
497 test25(other_classes);
498}
499
500void test26(id y) {
501 extern id test26_var1;
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000502 __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 +0000503
504 extern __unsafe_unretained id test26_var2;
505 __sync_swap(&test26_var2, 0, y);
506}
507
508@interface Test26
509- (id) init;
510- (id) initWithInt: (int) x;
511@end
512@implementation Test26
513- (id) init { return self; }
514- (id) initWithInt: (int) x {
515 [self init]; // expected-error {{the result of a delegate init call must be immediately returned or assigned to 'self'}}
516 return self;
517}
518@end
519
520// rdar://9525555
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000521@interface Test27 {
522 __weak id _myProp1;
523 id myProp2;
524}
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +0000525@property id x;
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +0000526@property (readonly) id ro;
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000527@property (readonly) id custom_ro;
John McCallf85e1932011-06-15 23:02:42 +0000528@property int y;
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000529
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +0000530@property (readonly) __weak id myProp1;
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000531@property (readonly) id myProp2;
532@property (readonly) __strong id myProp3;
John McCallf85e1932011-06-15 23:02:42 +0000533@end
534
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000535@implementation Test27
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +0000536@synthesize x;
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +0000537@synthesize ro;
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000538@synthesize y;
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000539
540@synthesize myProp1 = _myProp1;
541@synthesize myProp2;
542@synthesize myProp3;
543
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000544-(id)custom_ro { return 0; }
545@end
546
John McCallf85e1932011-06-15 23:02:42 +0000547// rdar://9569264
548@interface Test28
549@property (nonatomic, assign) __strong id a; // expected-error {{unsafe_unretained property 'a' may not also be declared __strong}}
550@end
551
552@interface Test28 ()
553@property (nonatomic, assign) __strong id b; // expected-error {{unsafe_unretained property 'b' may not also be declared __strong}}
554@end
555
556@implementation Test28
557@synthesize a;
558@synthesize b;
559@end
560
561// rdar://9573962
562typedef struct Bark Bark;
563@interface Test29
564@property Bark* P;
565@end
566
567@implementation Test29
568@synthesize P;
569- (id)Meth {
570 Bark** f = &P;
571 return 0;
572}
573@end
574
575// rdar://9495837
576@interface Test30
Fariborz Jahanian921c1432011-06-24 18:25:34 +0000577+ (id) new;
John McCallf85e1932011-06-15 23:02:42 +0000578- (void)Meth;
579@end
580
581@implementation Test30
Fariborz Jahanian921c1432011-06-24 18:25:34 +0000582+ (id) new { return 0; }
John McCallf85e1932011-06-15 23:02:42 +0000583- (void) Meth {
Argyrios Kyrtzidisd26fef02011-06-22 18:03:53 +0000584 __weak id x = [Test30 new]; // expected-warning {{assigning retained object to weak variable}}
585 id __unsafe_unretained u = [Test30 new]; // expected-warning {{assigning retained object to unsafe_unretained variable}}
John McCallf85e1932011-06-15 23:02:42 +0000586 id y = [Test30 new];
Argyrios Kyrtzidisd26fef02011-06-22 18:03:53 +0000587 x = [Test30 new]; // expected-warning {{assigning retained object to weak variable}}
588 u = [Test30 new]; // expected-warning {{assigning retained object to unsafe_unretained variable}}
John McCallf85e1932011-06-15 23:02:42 +0000589 y = [Test30 new];
590}
591@end
592
593// rdar://9411838
594@protocol PTest31 @end
595
596int Test31() {
597 Class cls;
598 id ids;
599 id<PTest31> pids;
600 Class<PTest31> pcls;
601
602 int i = (ids->isa ? 1 : 0); // expected-error {{member reference base type 'id' is not a structure or union}}
603 int j = (pids->isa ? 1 : 0); // expected-error {{member reference base type 'id<PTest31>' is not a structure or union}}
604 int k = (pcls->isa ? i : j); // expected-error {{member reference base type 'Class<PTest31>' is not a structure or union}}
605 return cls->isa ? i : j; // expected-error {{member reference base type 'Class' is not a structure or union}}
606}
Fariborz Jahanianb1f7d242011-06-16 17:29:56 +0000607
608// rdar://9612030
609@interface ITest32 {
610@public
611 id ivar;
612}
613@end
614
615id Test32(__weak ITest32 *x) {
616 __weak ITest32 *y;
617 x->ivar = 0; // expected-error {{dereferencing a __weak pointer is not allowed}}
618 return y ? y->ivar // expected-error {{dereferencing a __weak pointer is not allowed}}
619 : (*x).ivar; // expected-error {{dereferencing a __weak pointer is not allowed}}
620}
621
Fariborz Jahanian8295b7b2011-06-22 16:36:45 +0000622// rdar://9619861
623extern int printf(const char*, ...);
624typedef long intptr_t;
625
626int Test33(id someid) {
627 printf( "Hello%ld", (intptr_t)someid);
628 return (int)someid;
629}
630
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000631// rdar://9636091
632@interface I34
633@property (nonatomic, retain) id newName __attribute__((ns_returns_not_retained)) ;
634
635@property (nonatomic, retain) id newName1 __attribute__((ns_returns_not_retained)) ;
636- (id) newName1 __attribute__((ns_returns_not_retained));
637
638@property (nonatomic, retain) id newName2 __attribute__((ns_returns_not_retained)); // expected-note {{roperty declared here}}
639- (id) newName2; // expected-warning {{property declared as returning non-retained objects; getter returning retained objects}}
640@end
641
642@implementation I34
643@synthesize newName;
644
645@synthesize newName1;
Fariborz Jahanian78980052011-08-01 22:39:49 +0000646- (id) newName1 { return 0; }
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000647
Fariborz Jahanian78980052011-08-01 22:39:49 +0000648@synthesize newName2;
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000649@end
650
John McCallc03fa492011-06-27 23:59:58 +0000651void test35(void) {
652 extern void test36_helper(id*);
653 id x;
654 __strong id *xp = 0;
655
656 test36_helper(&x);
657 test36_helper(xp); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
658
659 // rdar://problem/9665710
660 __block id y;
661 test36_helper(&y);
662 ^{ test36_helper(&y); }();
Argyrios Kyrtzidis05d48762011-07-01 22:23:09 +0000663
Jordan Roseb13291a2012-07-19 18:10:18 +0000664 __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 +0000665}
Douglas Gregorf96e9042011-07-12 17:28:52 +0000666
667void test36(int first, ...) {
668 // <rdar://problem/9758798>
669 __builtin_va_list arglist;
670 __builtin_va_start(arglist, first);
671 id obj = __builtin_va_arg(arglist, id);
672 __builtin_va_end(arglist);
673}
John McCall990567c2011-07-27 01:07:15 +0000674
Douglas Gregorb3029962011-11-14 22:10:01 +0000675@class Test37; // expected-note{{forward declaration of class here}}
John McCall990567c2011-07-27 01:07:15 +0000676void test37(Test37 *c) {
677 for (id y in c) { // expected-error {{collection expression type 'Test37' is a forward declaration}}
678 (void) y;
679 }
Argyrios Kyrtzidis5b76f372011-09-20 23:49:22 +0000680
681 (void)sizeof(id*); // no error.
John McCall990567c2011-07-27 01:07:15 +0000682}
John McCalla8e0cd82011-08-06 07:30:58 +0000683
684// rdar://problem/9887979
685@interface Test38
686@property int value;
687@end
688void test38() {
689 extern Test38 *test38_helper(void);
690 switch (test38_helper().value) {
691 case 0:
692 case 1:
693 ;
694 }
695}
Fariborz Jahanian175fb102011-10-03 22:11:57 +0000696
697// rdar://10186536
698@class NSColor;
699void _NSCalc(NSColor* color, NSColor* bezelColors[]) __attribute__((unavailable("not available in automatic reference counting mode")));
700
701void _NSCalcBeze(NSColor* color, NSColor* bezelColors[]); // expected-error {{must explicitly describe intended ownership of an object array parameter}}
Fariborz Jahaniana1de8cb2011-10-14 17:34:08 +0000702
703// rdar://9970739
704@interface RestaurantTableViewCell
705- (void) restaurantLocation;
706@end
707
708@interface Radar9970739
709- (void) Meth;
710@end
711
712@implementation Radar9970739
713- (void) Meth {
714 RestaurantTableViewCell *cell;
715 [cell restaurantLocatoin]; // expected-error {{no visible @interface for 'RestaurantTableViewCell' declares the selector 'restaurantLocatoin'}}
716}
717@end
718
Fariborz Jahanianbd2e27e2012-07-06 21:09:27 +0000719// rdar://11814185
720@interface Radar11814185
721@property (nonatomic, weak) Radar11814185* picker1;
722+ alloc;
723- init;
724@end
725
726@implementation Radar11814185
727
728@synthesize picker1;
729
730- (void)viewDidLoad
731{
732 picker1 = [[Radar11814185 alloc] init]; // expected-warning {{assigning retained object to weak variable; object will be released after assignment}}
733 self.picker1 = [[Radar11814185 alloc] init]; // expected-warning {{assigning retained object to weak property; object will be released after assignment}}
734}
735
736+ alloc { return 0; }
737- init { return 0; }
738@end
739
Ted Kremenek9d084012012-12-21 08:04:28 +0000740// <rdar://problem/12569201>. Warn on cases of initializing a weak variable
741// with an Objective-C object literal.
742void rdar12569201(id key, id value) {
743 // Declarations.
744 __weak id x = @"foo"; // no-warning
745 __weak id y = @{ key : value }; // expected-warning {{assigning dictionary literal to a weak variable; object will be released after assignment}}
746 __weak id z = @[ value ]; // expected-warning {{assigning array literal to a weak variable; object will be released after assignment}}
747 __weak id b = ^() {}; // expected-warning {{assigning block literal to a weak variable; object will be released after assignment}}
Ted Kremenekf530ff72012-12-21 21:59:39 +0000748 __weak id n = @42; // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
749 __weak id e = @(42); // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
750 __weak id m = @(41 + 1); // expected-warning {{assigning boxed expression to a weak variable; object will be released after assignment}}
Ted Kremenek9d084012012-12-21 08:04:28 +0000751
752 // Assignments.
753 y = @{ key : value }; // expected-warning {{assigning dictionary literal to a weak variable; object will be released after assignment}}
754 z = @[ value ]; // expected-warning {{assigning array literal to a weak variable; object will be released after assignment}}
755 b = ^() {}; // expected-warning {{assigning block literal to a weak variable; object will be released after assignment}}
Ted Kremenekf530ff72012-12-21 21:59:39 +0000756 n = @42; // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
757 e = @(42); // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
758 m = @(41 + 1); // expected-warning {{assigning boxed expression to a weak variable; object will be released after assignment}}
Ted Kremenek9d084012012-12-21 08:04:28 +0000759}
Douglas Gregorf373c5d2013-01-18 01:41:40 +0000760
761@interface C
762- (void)method:(id[])objects; // expected-error{{must explicitly describe intended ownership of an object array parameter}}
763@end
Fariborz Jahanian443adec2013-04-30 00:30:48 +0000764
765// rdar://13752880
766@interface NSMutableArray : NSArray @end
767
768typedef __strong NSMutableArray * PSNS;
769
770void test(NSArray *x) {
771 NSMutableArray *y = x; // expected-warning {{incompatible pointer types initializing 'NSMutableArray *' with an expression of type 'NSArray *'}}
772 __strong NSMutableArray *y1 = x; // expected-warning {{incompatible pointer types initializing 'NSMutableArray *' with an expression of type 'NSArray *'}}
773 PSNS y2 = x; // expected-warning {{incompatible pointer types initializing 'NSMutableArray *' with an expression of type 'NSArray *'}}
774}
Fariborz Jahanian84e41cd2013-10-10 21:58:04 +0000775
776// rdar://15123684
777@class NSString;
778
779void foo(NSArray *array) {
780 for (NSString *string in array) {
781 for (string in @[@"blah", @"more blah", string]) { // expected-error {{selector element of type 'NSString *const __strong' cannot be a constant l-value}}
782 }
783 }
784}
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700785
786// rdar://16627903
787extern void abort();
788#define TKAssertEqual(a, b) do{\
789 __typeof(a) a_res = (a);\
790 __typeof(b) b_res = (b);\
791 if ((a_res) != (b_res)) {\
792 abort();\
793 }\
794}while(0)
795
796int garf() {
797 id object;
798 TKAssertEqual(object, nil);
799 TKAssertEqual(object, (id)nil);
800}