blob: d89d035fca21c54c62cd5ead43951f170e79cbb5 [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
87-(id)alloc;
88- (id)initWithInt: (int) i;
89@end
90
91void rdar8861761() {
92 B *o1 = [[B alloc] initWithInt:0];
93 B *o2 = [B alloc];
94 [o2 initWithInt:0]; // expected-warning {{expression result unused}}
95}
96
97// rdar://8925835
98@interface rdar8925835
99- (void)foo:(void (^)(unsigned captureCount, I * const capturedStrings[captureCount]))block;
100@end
101
102void test5() {
103 extern void test5_helper(__autoreleasing id *);
104 id x;
105
106 // Okay because of magic temporaries.
107 test5_helper(&x);
108
109 __autoreleasing id *a = &x; // expected-error {{initializing '__autoreleasing id *' with an expression of type '__strong id *' changes retain/release properties of pointer}}
110
111 a = &x; // expected-error {{assigning '__strong id *' to '__autoreleasing id *' changes retain/release properties of pointer}}
112
113 extern void test5_helper2(id const *);
114 test5_helper2(&x);
115
116 extern void test5_helper3(__weak id *); // expected-note {{passing argument to parameter here}}
117 test5_helper3(&x); // expected-error {{passing '__strong id *' to parameter of type '__weak id *' changes retain/release properties of pointer}}
118}
119
120// rdar://problem/8937869
121void test6(unsigned cond) {
122 switch (cond) {
123 case 0:
124 ;
125 id x; // expected-note {{jump bypasses initialization of retaining variable}}
126
127 case 1: // expected-error {{switch case is in protected scope}}
128 break;
129 }
130}
131
132@class NSError;
133void test7(void) {
134 extern void test7_helper(NSError **);
135 NSError *err;
136 test7_helper(&err);
137}
138void test7_weak(void) {
139 extern void test7_helper(NSError **);
140 __weak NSError *err;
141 test7_helper(&err);
142}
143void test7_unsafe(void) {
144 extern void test7_helper(NSError **); // expected-note {{passing argument to parameter here}}
145 __unsafe_unretained NSError *err;
146 test7_helper(&err); // expected-error {{passing 'NSError *__unsafe_unretained *' to parameter of type 'NSError *__autoreleasing *' changes retain/release properties of pointer}}
147}
148
149@class Test8_incomplete;
150@interface Test8_complete @end;
151@interface Test8_super @end;
152@interface Test8 : Test8_super
153- (id) init00;
154- (id) init01; // expected-note {{declaration in interface}} \
155 // expected-note{{overridden method}}
156- (id) init02; // expected-note{{overridden method}}
157- (id) init03; // covariance
158- (id) init04; // covariance
159- (id) init05; // expected-note{{overridden method}}
160
161- (void) init10; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
162- (void) init11;
163- (void) init12;
164- (void) init13; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
165- (void) init14; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
166- (void) init15;
167
168// These should be invalid to actually call.
169- (Test8_incomplete*) init20;
170- (Test8_incomplete*) init21; // expected-note {{declaration in interface}}
171- (Test8_incomplete*) init22;
172- (Test8_incomplete*) init23;
173- (Test8_incomplete*) init24;
174- (Test8_incomplete*) init25;
175
176- (Test8_super*) init30; // id exception to covariance
177- (Test8_super*) init31; // expected-note {{declaration in interface}} \
178 // expected-note{{overridden method}}
179- (Test8_super*) init32; // expected-note{{overridden method}}
180- (Test8_super*) init33;
181- (Test8_super*) init34; // covariance
182- (Test8_super*) init35; // expected-note{{overridden method}}
183
184- (Test8*) init40; // id exception to covariance
185- (Test8*) init41; // expected-note {{declaration in interface}} \
186 // expected-note{{overridden method}}
187- (Test8*) init42; // expected-note{{overridden method}}
188- (Test8*) init43; // this should be a warning, but that's a general language thing, not an ARC thing
189- (Test8*) init44;
190- (Test8*) init45; // expected-note{{overridden method}}
191
192- (Test8_complete*) init50; // expected-error {{init methods must return a type related to the receiver type}}
193- (Test8_complete*) init51; // expected-error {{init methods must return a type related to the receiver type}}
194- (Test8_complete*) init52; // expected-error {{init methods must return a type related to the receiver type}}
195- (Test8_complete*) init53; // expected-error {{init methods must return a type related to the receiver type}}
196- (Test8_complete*) init54; // expected-error {{init methods must return a type related to the receiver type}}
197- (Test8_complete*) init55; // expected-error {{init methods must return a type related to the receiver type}}
198@end
199@implementation Test8
200- (id) init00 { return 0; }
201- (id) init10 { return 0; } // expected-error {{method implementation does not match its declaration}}
202- (id) init20 { return 0; }
203- (id) init30 { return 0; }
204- (id) init40 { return 0; }
205- (id) init50 { return 0; }
206
207- (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 +0000208 // 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 +0000209- (void) init11 {}
210- (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}}
211- (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 +0000212 // 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 +0000213- (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 +0000214 // 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 +0000215- (void) init51 {}
216
217- (Test8_incomplete*) init02 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
218 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_incomplete *'}}
219- (Test8_incomplete*) init12 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
220- (Test8_incomplete*) init22 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
221- (Test8_incomplete*) init32 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
222 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_incomplete *'}}
223- (Test8_incomplete*) init42 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
224 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_incomplete *'}}
225- (Test8_incomplete*) init52 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
226
227- (Test8_super*) init03 { return 0; }
228- (Test8_super*) init13 { return 0; } // expected-error {{method implementation does not match its declaration}}
229- (Test8_super*) init23 { return 0; }
230- (Test8_super*) init33 { return 0; }
231- (Test8_super*) init43 { return 0; }
232- (Test8_super*) init53 { return 0; }
233
234- (Test8*) init04 { return 0; }
235- (Test8*) init14 { return 0; } // expected-error {{method implementation does not match its declaration}}
236- (Test8*) init24 { return 0; }
237- (Test8*) init34 { return 0; }
238- (Test8*) init44 { return 0; }
239- (Test8*) init54 { return 0; }
240
241- (Test8_complete*) init05 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
242 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_complete *'}}
243- (Test8_complete*) init15 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
244- (Test8_complete*) init25 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
245- (Test8_complete*) init35 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
246 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_complete *'}}
247- (Test8_complete*) init45 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
248 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_complete *'}}
249- (Test8_complete*) init55 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
250@end
251
252@class Test9_incomplete;
253@interface Test9
254- (Test9_incomplete*) init1; // expected-error {{init methods must return a type related to the receiver type}}
255- (Test9_incomplete*) init2;
256@end
257id test9(Test9 *v) {
258 return [v init1];
259}
260
261// Test that the inference rules are different for fast enumeration variables.
262void test10(id collection) {
263 for (id x in collection) {
John McCall7acddac2011-06-17 06:42:21 +0000264 __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 +0000265 }
266
267 for (__strong id x in collection) {
268 __weak id *ptr = &x; // expected-error {{initializing '__weak id *' with an expression of type '__strong id *' changes retain/release properties of pointer}}
269 }
270}
271
272// rdar://problem/9078626
273#define nil ((void*) 0)
274void test11(id op, void *vp) {
275 _Bool b;
276 b = (op == nil);
277 b = (nil == op);
278
279 b = (vp == nil);
280 b = (nil == vp);
281
Fariborz Jahanian607f5872012-07-27 21:34:23 +0000282 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}}
283 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 +0000284}
285
286void test12(id collection) {
287 for (id x in collection) {
288 x = 0; // expected-error {{fast enumeration variables can't be modified in ARC by default; declare the variable __strong to allow this}}
289 }
290
291 for (const id x in collection) {
292 x = 0; // expected-error {{read-only variable is not assignable}}
293 }
294
295 for (__strong id x in collection) {
296 x = 0;
297 }
298}
299
300@interface Test13
301- (id) init0;
302- (void) noninit;
303@end
304@implementation Test13
305- (id) init0 {
306 self = 0;
307}
308- (void) noninit {
309 self = 0; // expected-error {{cannot assign to 'self' outside of a method in the init family}}
310}
311@end
312
Ted Kremenek3045ce72011-10-25 04:52:20 +0000313// <rdar://problem/10274056>
314@interface Test13_B
315- (id) consumesSelf __attribute__((ns_consumes_self));
316@end
317@implementation Test13_B
318- (id) consumesSelf {
319 self = 0; // no-warning
320}
321@end
322
John McCallf85e1932011-06-15 23:02:42 +0000323// rdar://problem/9172151
324@class Test14A, Test14B;
325void test14() {
326 extern void test14_consume(id *);
327 extern int test14_cond(void);
328 extern float test14_nowriteback(id __autoreleasing const *); // expected-note{{passing argument to parameter here}}
329
330 Test14A *a;
331 Test14B *b;
332 id i;
333 id cla[10];
334 id vla[test14_cond() + 10];
335
336 test14_consume((__strong id*) &a);
337 test14_consume((test14_cond() ? (__strong id*) &b : &i));
338 test14_consume(test14_cond() ? 0 : &a);
339 test14_consume(test14_cond() ? (void*) 0 : (&a));
340 test14_consume(cla); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}}
341 test14_consume(vla); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}}
342 test14_consume(&cla[5]); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}}
343
344 __strong id *test14_indirect(void);
345 test14_consume(test14_indirect()); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
346
347 extern id test14_global;
348 test14_consume(&test14_global); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
349
350 extern __strong id *test14_global_ptr;
351 test14_consume(test14_global_ptr); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
352
353 static id static_local;
354 test14_consume(&static_local); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
355
356 __weak id* wip;
357 test14_nowriteback(&static_local); // okay, not a write-back.
358 test14_nowriteback(wip); // expected-error{{passing '__weak id *' to parameter of type '__autoreleasing id const *' changes retain/release properties of pointer}}
359}
360
361void test15() {
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000362 __block __autoreleasing id x; // expected-error {{__block variables cannot have __autoreleasing ownership}}
John McCallf85e1932011-06-15 23:02:42 +0000363}
364
365struct Test16;
366@interface Test16a
367- (void) test16_0: (int) x;
368- (int) test16_1: (int) x; // expected-note {{one possibility}}
369- (int) test16_2: (int) x; // expected-note {{one possibility}}
370- (id) test16_3: (int) x __attribute__((ns_returns_retained)); // expected-note {{one possibility}}
371- (void) test16_4: (int) x __attribute__((ns_consumes_self)); // expected-note {{one possibility}}
372- (void) test16_5: (id) __attribute__((ns_consumed)) x; // expected-note {{one possibility}}
373- (void) test16_6: (id) x;
374@end
375
376@interface Test16b
377- (void) test16_0: (int) x;
378- (int) test16_1: (char*) x; // expected-note {{also found}}
379- (char*) test16_2: (int) x; // expected-note {{also found}}
380- (id) test16_3: (int) x; // expected-note {{also found}}
381- (void) test16_4: (int) x; // expected-note {{also found}}
382- (void) test16_5: (id) x; // expected-note {{also found}}
383- (void) test16_6: (struct Test16 *) x;
384@end
385
386void test16(void) {
387 id v;
388 [v test16_0: 0];
389 [v test16_1: 0]; // expected-error {{multiple methods named 'test16_1:' found with mismatched result, parameter type or attributes}}
390 [v test16_2: 0]; // expected-error {{multiple methods named}}
391 [v test16_3: 0]; // expected-error {{multiple methods named}}
392 [v test16_4: 0]; // expected-error {{multiple methods named}}
393 [v test16_5: 0]; // expected-error {{multiple methods named}}
394 [v test16_6: 0];
395}
396
Douglas Gregorb3029962011-11-14 22:10:01 +0000397@class Test17; // expected-note 2{{forward declaration of class here}}
John McCallf85e1932011-06-15 23:02:42 +0000398@protocol Test17p
399- (void) test17;
400+ (void) test17;
401@end
402void test17(void) {
403 Test17 *v0;
404 [v0 test17]; // expected-error {{receiver type 'Test17' for instance message is a forward declaration}}
405
406 Test17<Test17p> *v1;
407 [v1 test17]; // expected-error {{receiver type 'Test17<Test17p>' for instance message is a forward declaration}}
408
409 [Test17 test17]; // expected-error {{receiver 'Test17' for class message is a forward declaration}}
410}
411
412void test18(void) {
413 id x;
414 [x test18]; // expected-error {{no known instance method for selector 'test18'}}
415}
416
417extern struct Test19 *test19a;
418struct Test19 *const test19b = 0;
419void test19(void) {
420 id x;
421 x = (id) test19a; // expected-error {{bridged cast}} \
Fariborz Jahanian607f5872012-07-27 21:34:23 +0000422 // expected-note{{use __bridge to convert directly (no change in ownership)}} \
Fariborz Jahanian52b62362012-02-01 22:56:20 +0000423 // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'struct Test19 *' into ARC}}
John McCallf85e1932011-06-15 23:02:42 +0000424 x = (id) test19b; // expected-error {{bridged cast}} \
Fariborz Jahanian607f5872012-07-27 21:34:23 +0000425 // expected-note{{use __bridge to convert directly (no change in ownership)}} \
Fariborz Jahanian52b62362012-02-01 22:56:20 +0000426 // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'struct Test19 *' into ARC}}
John McCallf85e1932011-06-15 23:02:42 +0000427}
428
429// rdar://problem/8951453
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000430static __thread id test20_implicit; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
431static __thread __strong id test20_strong; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
432static __thread __weak id test20_weak; // expected-error {{thread-local variable has non-trivial ownership: type is '__weak id'}}
433static __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 +0000434static __thread __unsafe_unretained id test20_unsafe;
435void test20(void) {
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000436 static __thread id test20_implicit; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
437 static __thread __strong id test20_strong; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
438 static __thread __weak id test20_weak; // expected-error {{thread-local variable has non-trivial ownership: type is '__weak id'}}
439 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 +0000440 static __thread __unsafe_unretained id test20_unsafe;
441}
442
443// rdar://9310049
444_Bool fn(id obj) {
445 return (_Bool)obj;
446}
447
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000448// Check casting w/ ownership qualifiers.
John McCallf85e1932011-06-15 23:02:42 +0000449void test21() {
450 __strong id *sip;
451 (void)(__weak id *)sip; // expected-error{{casting '__strong id *' to type '__weak id *' changes retain/release properties of pointer}}
452 (void)(__weak const id *)sip; // expected-error{{casting '__strong id *' to type '__weak id const *' changes retain/release properties of pointer}}
453 (void)(__autoreleasing id *)sip; // expected-error{{casting '__strong id *' to type '__autoreleasing id *' changes retain/release properties of pointer}}
454 (void)(__autoreleasing const id *)sip; // okay
455}
456
457// rdar://problem/9340462
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000458void test22(id x[]) { // expected-error {{must explicitly describe intended ownership of an object array parameter}}
John McCallf85e1932011-06-15 23:02:42 +0000459}
460
461// rdar://problem/9400219
462void test23(void) {
463 void *ptr;
464 ptr = @"foo";
465 ptr = (ptr ? @"foo" : 0);
466 ptr = (ptr ? @"foo" : @"bar");
467}
468
469id test24(void) {
470 extern void test24_helper(void);
471 return test24_helper(), (void*) 0;
472}
473
474// rdar://9400841
475@interface Base
476@property (assign) id content;
477@end
478
479@interface Foo : Base
480-(void)test;
481@end
482
483@implementation Foo
484-(void)test {
485 super.content = 0;
486}
487@end
488
489// <rdar://problem/9398437>
490void test25(Class *classes) {
491 Class *other_classes;
492 test25(other_classes);
493}
494
495void test26(id y) {
496 extern id test26_var1;
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000497 __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 +0000498
499 extern __unsafe_unretained id test26_var2;
500 __sync_swap(&test26_var2, 0, y);
501}
502
503@interface Test26
504- (id) init;
505- (id) initWithInt: (int) x;
506@end
507@implementation Test26
508- (id) init { return self; }
509- (id) initWithInt: (int) x {
510 [self init]; // expected-error {{the result of a delegate init call must be immediately returned or assigned to 'self'}}
511 return self;
512}
513@end
514
515// rdar://9525555
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000516@interface Test27 {
517 __weak id _myProp1;
518 id myProp2;
519}
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +0000520@property id x;
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +0000521@property (readonly) id ro;
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000522@property (readonly) id custom_ro;
John McCallf85e1932011-06-15 23:02:42 +0000523@property int y;
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000524
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +0000525@property (readonly) __weak id myProp1;
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000526@property (readonly) id myProp2;
527@property (readonly) __strong id myProp3;
John McCallf85e1932011-06-15 23:02:42 +0000528@end
529
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000530@implementation Test27
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +0000531@synthesize x;
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +0000532@synthesize ro;
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000533@synthesize y;
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000534
535@synthesize myProp1 = _myProp1;
536@synthesize myProp2;
537@synthesize myProp3;
538
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000539-(id)custom_ro { return 0; }
540@end
541
John McCallf85e1932011-06-15 23:02:42 +0000542// rdar://9569264
543@interface Test28
544@property (nonatomic, assign) __strong id a; // expected-error {{unsafe_unretained property 'a' may not also be declared __strong}}
545@end
546
547@interface Test28 ()
548@property (nonatomic, assign) __strong id b; // expected-error {{unsafe_unretained property 'b' may not also be declared __strong}}
549@end
550
551@implementation Test28
552@synthesize a;
553@synthesize b;
554@end
555
556// rdar://9573962
557typedef struct Bark Bark;
558@interface Test29
559@property Bark* P;
560@end
561
562@implementation Test29
563@synthesize P;
564- (id)Meth {
565 Bark** f = &P;
566 return 0;
567}
568@end
569
570// rdar://9495837
571@interface Test30
Fariborz Jahanian921c1432011-06-24 18:25:34 +0000572+ (id) new;
John McCallf85e1932011-06-15 23:02:42 +0000573- (void)Meth;
574@end
575
576@implementation Test30
Fariborz Jahanian921c1432011-06-24 18:25:34 +0000577+ (id) new { return 0; }
John McCallf85e1932011-06-15 23:02:42 +0000578- (void) Meth {
Argyrios Kyrtzidisd26fef02011-06-22 18:03:53 +0000579 __weak id x = [Test30 new]; // expected-warning {{assigning retained object to weak variable}}
580 id __unsafe_unretained u = [Test30 new]; // expected-warning {{assigning retained object to unsafe_unretained variable}}
John McCallf85e1932011-06-15 23:02:42 +0000581 id y = [Test30 new];
Argyrios Kyrtzidisd26fef02011-06-22 18:03:53 +0000582 x = [Test30 new]; // expected-warning {{assigning retained object to weak variable}}
583 u = [Test30 new]; // expected-warning {{assigning retained object to unsafe_unretained variable}}
John McCallf85e1932011-06-15 23:02:42 +0000584 y = [Test30 new];
585}
586@end
587
588// rdar://9411838
589@protocol PTest31 @end
590
591int Test31() {
592 Class cls;
593 id ids;
594 id<PTest31> pids;
595 Class<PTest31> pcls;
596
597 int i = (ids->isa ? 1 : 0); // expected-error {{member reference base type 'id' is not a structure or union}}
598 int j = (pids->isa ? 1 : 0); // expected-error {{member reference base type 'id<PTest31>' is not a structure or union}}
599 int k = (pcls->isa ? i : j); // expected-error {{member reference base type 'Class<PTest31>' is not a structure or union}}
600 return cls->isa ? i : j; // expected-error {{member reference base type 'Class' is not a structure or union}}
601}
Fariborz Jahanianb1f7d242011-06-16 17:29:56 +0000602
603// rdar://9612030
604@interface ITest32 {
605@public
606 id ivar;
607}
608@end
609
610id Test32(__weak ITest32 *x) {
611 __weak ITest32 *y;
612 x->ivar = 0; // expected-error {{dereferencing a __weak pointer is not allowed}}
613 return y ? y->ivar // expected-error {{dereferencing a __weak pointer is not allowed}}
614 : (*x).ivar; // expected-error {{dereferencing a __weak pointer is not allowed}}
615}
616
Fariborz Jahanian8295b7b2011-06-22 16:36:45 +0000617// rdar://9619861
618extern int printf(const char*, ...);
619typedef long intptr_t;
620
621int Test33(id someid) {
622 printf( "Hello%ld", (intptr_t)someid);
623 return (int)someid;
624}
625
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000626// rdar://9636091
627@interface I34
628@property (nonatomic, retain) id newName __attribute__((ns_returns_not_retained)) ;
629
630@property (nonatomic, retain) id newName1 __attribute__((ns_returns_not_retained)) ;
631- (id) newName1 __attribute__((ns_returns_not_retained));
632
633@property (nonatomic, retain) id newName2 __attribute__((ns_returns_not_retained)); // expected-note {{roperty declared here}}
634- (id) newName2; // expected-warning {{property declared as returning non-retained objects; getter returning retained objects}}
635@end
636
637@implementation I34
638@synthesize newName;
639
640@synthesize newName1;
Fariborz Jahanian78980052011-08-01 22:39:49 +0000641- (id) newName1 { return 0; }
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000642
Fariborz Jahanian78980052011-08-01 22:39:49 +0000643@synthesize newName2;
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000644@end
645
John McCallc03fa492011-06-27 23:59:58 +0000646void test35(void) {
647 extern void test36_helper(id*);
648 id x;
649 __strong id *xp = 0;
650
651 test36_helper(&x);
652 test36_helper(xp); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
653
654 // rdar://problem/9665710
655 __block id y;
656 test36_helper(&y);
657 ^{ test36_helper(&y); }();
Argyrios Kyrtzidis05d48762011-07-01 22:23:09 +0000658
Jordan Roseb13291a2012-07-19 18:10:18 +0000659 __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 +0000660}
Douglas Gregorf96e9042011-07-12 17:28:52 +0000661
662void test36(int first, ...) {
663 // <rdar://problem/9758798>
664 __builtin_va_list arglist;
665 __builtin_va_start(arglist, first);
666 id obj = __builtin_va_arg(arglist, id);
667 __builtin_va_end(arglist);
668}
John McCall990567c2011-07-27 01:07:15 +0000669
Douglas Gregorb3029962011-11-14 22:10:01 +0000670@class Test37; // expected-note{{forward declaration of class here}}
John McCall990567c2011-07-27 01:07:15 +0000671void test37(Test37 *c) {
672 for (id y in c) { // expected-error {{collection expression type 'Test37' is a forward declaration}}
673 (void) y;
674 }
Argyrios Kyrtzidis5b76f372011-09-20 23:49:22 +0000675
676 (void)sizeof(id*); // no error.
John McCall990567c2011-07-27 01:07:15 +0000677}
John McCalla8e0cd82011-08-06 07:30:58 +0000678
679// rdar://problem/9887979
680@interface Test38
681@property int value;
682@end
683void test38() {
684 extern Test38 *test38_helper(void);
685 switch (test38_helper().value) {
686 case 0:
687 case 1:
688 ;
689 }
690}
Fariborz Jahanian175fb102011-10-03 22:11:57 +0000691
692// rdar://10186536
693@class NSColor;
694void _NSCalc(NSColor* color, NSColor* bezelColors[]) __attribute__((unavailable("not available in automatic reference counting mode")));
695
696void _NSCalcBeze(NSColor* color, NSColor* bezelColors[]); // expected-error {{must explicitly describe intended ownership of an object array parameter}}
Fariborz Jahaniana1de8cb2011-10-14 17:34:08 +0000697
698// rdar://9970739
699@interface RestaurantTableViewCell
700- (void) restaurantLocation;
701@end
702
703@interface Radar9970739
704- (void) Meth;
705@end
706
707@implementation Radar9970739
708- (void) Meth {
709 RestaurantTableViewCell *cell;
710 [cell restaurantLocatoin]; // expected-error {{no visible @interface for 'RestaurantTableViewCell' declares the selector 'restaurantLocatoin'}}
711}
712@end
713
Fariborz Jahanianbd2e27e2012-07-06 21:09:27 +0000714// rdar://11814185
715@interface Radar11814185
716@property (nonatomic, weak) Radar11814185* picker1;
717+ alloc;
718- init;
719@end
720
721@implementation Radar11814185
722
723@synthesize picker1;
724
725- (void)viewDidLoad
726{
727 picker1 = [[Radar11814185 alloc] init]; // expected-warning {{assigning retained object to weak variable; object will be released after assignment}}
728 self.picker1 = [[Radar11814185 alloc] init]; // expected-warning {{assigning retained object to weak property; object will be released after assignment}}
729}
730
731+ alloc { return 0; }
732- init { return 0; }
733@end
734
Ted Kremenek9d084012012-12-21 08:04:28 +0000735// <rdar://problem/12569201>. Warn on cases of initializing a weak variable
736// with an Objective-C object literal.
737void rdar12569201(id key, id value) {
738 // Declarations.
739 __weak id x = @"foo"; // no-warning
740 __weak id y = @{ key : value }; // expected-warning {{assigning dictionary literal to a weak variable; object will be released after assignment}}
741 __weak id z = @[ value ]; // expected-warning {{assigning array literal to a weak variable; object will be released after assignment}}
742 __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 +0000743 __weak id n = @42; // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
744 __weak id e = @(42); // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
745 __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 +0000746
747 // Assignments.
748 y = @{ key : value }; // expected-warning {{assigning dictionary literal to a weak variable; object will be released after assignment}}
749 z = @[ value ]; // expected-warning {{assigning array literal to a weak variable; object will be released after assignment}}
750 b = ^() {}; // expected-warning {{assigning block literal to a weak variable; object will be released after assignment}}
Ted Kremenekf530ff72012-12-21 21:59:39 +0000751 n = @42; // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
752 e = @(42); // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
753 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 +0000754}
Douglas Gregorf373c5d2013-01-18 01:41:40 +0000755
756@interface C
757- (void)method:(id[])objects; // expected-error{{must explicitly describe intended ownership of an object array parameter}}
758@end