[PowerPC] Collect some CallLowering arguments into a struct. [NFC]
[llvm-project.git] / clang / test / SemaObjC / arc.m
blobdea3ba5e9b7999287d44539ff376d179b13b6792
1 // RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fobjc-runtime-has-weak -fsyntax-only -fobjc-arc -fblocks -verify -Wno-objc-root-class %s
2 // RUN: not %clang_cc1 -triple x86_64-apple-darwin11 -fobjc-runtime-has-weak -fsyntax-only -fobjc-arc -fblocks -Wno-objc-root-class -fdiagnostics-parseable-fixits %s 2>&1
4 typedef unsigned long NSUInteger;
5 typedef const void * CFTypeRef;
6 CFTypeRef CFBridgingRetain(id X);
7 id CFBridgingRelease(CFTypeRef);
8 @protocol NSCopying @end
9 @interface NSDictionary
10 + (id)dictionaryWithObjects:(const id [])objects forKeys:(const id <NSCopying> [])keys count:(NSUInteger)cnt;
11 - (void)setObject:(id)object forKeyedSubscript:(id)key;
12 @end
13 @class NSFastEnumerationState;
14 @protocol NSFastEnumeration
15 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len;
16 @end
17 @interface NSNumber 
18 + (NSNumber *)numberWithInt:(int)value;
19 @end
20 @interface NSArray <NSFastEnumeration>
21 + (id)arrayWithObjects:(const id [])objects count:(NSUInteger)cnt;
22 @end
24 void test0(void (*fn)(int), int val) {
25   fn(val);
28 @interface A
29 - (id)retain;
30 - (id)autorelease;
31 - (oneway void)release;
32 - (void)dealloc;
33 - (NSUInteger)retainCount;
34 @end
36 void test1(A *a) {
37   SEL s = @selector(retain);    // expected-error {{ARC forbids use of 'retain' in a @selector}}
38   s = @selector(release);       // expected-error {{ARC forbids use of 'release' in a @selector}}
39   s = @selector(autorelease);   // expected-error {{ARC forbids use of 'autorelease' in a @selector}}
40   s = @selector(dealloc);       // expected-error {{ARC forbids use of 'dealloc' in a @selector}}
41   [a dealloc]; // expected-error {{ARC forbids explicit message send of 'dealloc'}}
42   [a retain]; // expected-error {{ARC forbids explicit message send of 'retain'}}
43   [a retainCount]; // expected-error {{ARC forbids explicit message send of 'retainCount'}}
44   [a release]; // expected-error {{ARC forbids explicit message send of 'release'}}
45   [a autorelease]; // expected-error {{ARC forbids explicit message send of 'autorelease'}}
48 @interface Test2 : A
49 - (void) dealloc;
50 @end
51 @implementation Test2
52 - (void) dealloc {
53   // This should maybe just be ignored.  We're just going to warn about it for now.
54   [super dealloc]; // expected-error {{ARC forbids explicit message send of 'dealloc'}}
56 @end
58 // rdar://8843638
60 @interface I
61 - (id)retain; // expected-note {{method 'retain' declared here}}
62 - (id)autorelease; // expected-note {{method 'autorelease' declared here}}
63 - (oneway void)release; // expected-note {{method 'release' declared here}}
64 - (NSUInteger)retainCount; // expected-note {{method 'retainCount' declared here}}
65 @end
67 @implementation I
68 - (id)retain{return 0;} // expected-error {{ARC forbids implementation of 'retain'}}
69 - (id)autorelease{return 0;} // expected-error {{ARC forbids implementation of 'autorelease'}}
70 - (oneway void)release{} // expected-error {{ARC forbids implementation of 'release'}}
71 - (NSUInteger)retainCount{ return 0; } // expected-error {{ARC forbids implementation of 'retainCount'}}
72 @end
74 @implementation I(CAT)
75 - (id)retain{return 0;} // expected-error {{ARC forbids implementation of 'retain'}} \
76                         // expected-warning {{category is implementing a method which will also be implemented by its primary class}}
77 - (id)autorelease{return 0;} // expected-error {{ARC forbids implementation of 'autorelease'}} \
78                          // expected-warning {{category is implementing a method which will also be implemented by its primary class}}
79 - (oneway void)release{} // expected-error {{ARC forbids implementation of 'release'}} \
80                           // expected-warning {{category is implementing a method which will also be implemented by its primary class}}
81 - (NSUInteger)retainCount{ return 0; } // expected-error {{ARC forbids implementation of 'retainCount'}} \
82                           // expected-warning {{category is implementing a method which will also be implemented by its primary class}}
83 @end
85 // rdar://8861761
87 @interface B
88 + (id)alloc;
89 - (id)initWithInt: (int) i;
90 - (id)myInit __attribute__((objc_method_family(init)));
91 - (id)myBadInit __attribute__((objc_method_family(12)));  // expected-error {{'objc_method_family' attribute requires parameter 1 to be an identifier}}
93 @end
95 void rdar8861761() {
96   B *o1 = [[B alloc] initWithInt:0];
97   B *o2 = [B alloc];
98   [o2 initWithInt:0]; // expected-warning {{expression result unused}}
99   B *o3 = [[B alloc] myInit];
100   [[B alloc] myInit]; // expected-warning {{expression result unused}}
103 // rdar://8925835
104 @interface rdar8925835
105 - (void)foo:(void (^)(unsigned captureCount, I * const capturedStrings[captureCount]))block;
106 @end
108 void test5() {
109   extern void test5_helper(__autoreleasing id *);
110   id x;
112   // Okay because of magic temporaries.
113   test5_helper(&x);
115   __autoreleasing id *a = &x; // expected-error {{initializing '__autoreleasing id *' with an expression of type '__strong id *' changes retain/release properties of pointer}}
117   a = &x; // expected-error {{assigning '__strong id *' to '__autoreleasing id *' changes retain/release properties of pointer}}
119   extern void test5_helper2(id const *);
120   test5_helper2(&x);
122   extern void test5_helper3(__weak id *); // expected-note {{passing argument to parameter here}}
123   test5_helper3(&x); // expected-error {{passing '__strong id *' to parameter of type '__weak id *' changes retain/release properties of pointer}}
126 // rdar://problem/8937869
127 void test6(unsigned cond) {
128   switch (cond) {
129   case 0:
130     ;
131     id x; // expected-note {{jump bypasses initialization of __strong variable}}
133   case 1: // expected-error {{cannot jump}}
134     break;
135   }
137 void test6a(unsigned cond) {
138   switch (cond) {
139   case 0:
140     ;
141     __weak id x; // expected-note {{jump bypasses initialization of __weak variable}}
143   case 1: // expected-error {{cannot jump}}
144     break;
145   }
148 @class NSError;
149 void test7(void) {
150   extern void test7_helper(NSError **);
151   NSError *err;
152   test7_helper(&err);
154 void test7_weak(void) {
155   extern void test7_helper(NSError **);
156   __weak NSError *err;
157   test7_helper(&err);
159 void test7_unsafe(void) {
160   extern void test7_helper(NSError **); // expected-note {{passing argument to parameter here}}
161   __unsafe_unretained NSError *err;
162   test7_helper(&err); // expected-error {{passing 'NSError *__unsafe_unretained *' to parameter of type 'NSError *__autoreleasing *' changes retain/release properties of pointer}}
165 @class Test8_incomplete;
166 @interface Test8_complete @end;
167 @interface Test8_super @end;
168 @interface Test8 : Test8_super
169 - (id) init00;
170 - (id) init01; // expected-note {{declaration in interface}} \
171                // expected-note{{overridden method}}
172 - (id) init02; // expected-note{{overridden method}}
173 - (id) init03; // covariance
174 - (id) init04; // covariance
175 - (id) init05; // expected-note{{overridden method}}
177 - (void) init10; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
178 - (void) init11;
179 - (void) init12;
180 - (void) init13; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
181 - (void) init14; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
182 - (void) init15;
184 // These should be invalid to actually call.
185 - (Test8_incomplete*) init20;
186 - (Test8_incomplete*) init21; // expected-note {{declaration in interface}}
187 - (Test8_incomplete*) init22;
188 - (Test8_incomplete*) init23;
189 - (Test8_incomplete*) init24;
190 - (Test8_incomplete*) init25;
192 - (Test8_super*) init30; // id exception to covariance
193 - (Test8_super*) init31; // expected-note {{declaration in interface}} \
194                          // expected-note{{overridden method}}
195 - (Test8_super*) init32; // expected-note{{overridden method}}
196 - (Test8_super*) init33;
197 - (Test8_super*) init34; // covariance
198 - (Test8_super*) init35; // expected-note{{overridden method}}
200 - (Test8*) init40; // id exception to covariance
201 - (Test8*) init41; // expected-note {{declaration in interface}} \
202                    // expected-note{{overridden method}}
203 - (Test8*) init42; // expected-note{{overridden method}}
204 - (Test8*) init43; // this should be a warning, but that's a general language thing, not an ARC thing
205 - (Test8*) init44;
206 - (Test8*) init45; // expected-note{{overridden method}}
208 - (Test8_complete*) init50; // expected-error {{init methods must return a type related to the receiver type}}
209 - (Test8_complete*) init51; // expected-error {{init methods must return a type related to the receiver type}}
210 - (Test8_complete*) init52; // expected-error {{init methods must return a type related to the receiver type}}
211 - (Test8_complete*) init53; // expected-error {{init methods must return a type related to the receiver type}}
212 - (Test8_complete*) init54; // expected-error {{init methods must return a type related to the receiver type}}
213 - (Test8_complete*) init55; // expected-error {{init methods must return a type related to the receiver type}}
214 @end
215 @implementation Test8
216 - (id) init00 { return 0; }
217 - (id) init10 { return 0; } // expected-error {{method implementation does not match its declaration}}
218 - (id) init20 { return 0; }
219 - (id) init30 { return 0; }
220 - (id) init40 { return 0; }
221 - (id) init50 { return 0; }
223 - (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}} \
224                    // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'void'}}
225 - (void) init11 {}
226 - (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}}
227 - (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}} \
228                    // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'void'}}
229 - (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}} \
230                    // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'void'}}
231 - (void) init51 {}
233 - (Test8_incomplete*) init02 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
234                                            // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_incomplete *'}}
235 - (Test8_incomplete*) init12 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
236 - (Test8_incomplete*) init22 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
237 - (Test8_incomplete*) init32 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
238                                            // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_incomplete *'}}
239 - (Test8_incomplete*) init42 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
240                                            // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_incomplete *'}}
241 - (Test8_incomplete*) init52 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
243 - (Test8_super*) init03 { return 0; }
244 - (Test8_super*) init13 { return 0; } // expected-error {{method implementation does not match its declaration}}
245 - (Test8_super*) init23 { return 0; }
246 - (Test8_super*) init33 { return 0; }
247 - (Test8_super*) init43 { return 0; }
248 - (Test8_super*) init53 { return 0; }
250 - (Test8*) init04 { return 0; }
251 - (Test8*) init14 { return 0; } // expected-error {{method implementation does not match its declaration}}
252 - (Test8*) init24 { return 0; }
253 - (Test8*) init34 { return 0; }
254 - (Test8*) init44 { return 0; }
255 - (Test8*) init54 { return 0; }
257 - (Test8_complete*) init05 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
258                                          // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_complete *'}}
259 - (Test8_complete*) init15 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
260 - (Test8_complete*) init25 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
261 - (Test8_complete*) init35 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
262                                          // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_complete *'}}
263 - (Test8_complete*) init45 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
264                                          // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_complete *'}}
265 - (Test8_complete*) init55 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
266 @end
268 @class Test9_incomplete;
269 @interface Test9
270 - (Test9_incomplete*) init1; // expected-error {{init methods must return a type related to the receiver type}}
271 - (Test9_incomplete*) init2;
272 @end
273 id test9(Test9 *v) {
274   return [v init1];
277 // Test that the inference rules are different for fast enumeration variables.
278 void test10(id collection) {
279   for (id x in collection) {
280     __strong id *ptr = &x; // expected-warning {{initializing '__strong id *' with an expression of type 'const __strong id *' discards qualifiers}}
281   }
283   for (__strong id x in collection) {
284     __weak id *ptr = &x; // expected-error {{initializing '__weak id *' with an expression of type '__strong id *' changes retain/release properties of pointer}}
285   }
288 // rdar://problem/9078626
289 #define nil ((void*) 0)
290 void test11(id op, void *vp) {
291   _Bool b;
292   b = (op == nil);
293   b = (nil == op);
295   b = (vp == nil);
296   b = (nil == vp);
298   // FIXME: Shouldn't these be consistent?
299   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}}
300   b = (op == vp);
303 void test12(id collection) {
304   for (id x in collection) {
305     x = 0; // expected-error {{fast enumeration variables cannot be modified in ARC by default; declare the variable __strong to allow this}}
306   }
308   for (const id x in collection) { // expected-note {{variable 'x' declared const here}}
309     x = 0; // expected-error {{cannot assign to variable 'x' with const-qualified type 'const __strong id'}}
310   }
312   for (__strong id x in collection) {
313     x = 0;
314   }
317 @interface Test13
318 - (id) init0;
319 - (void) noninit;
320 @end
321 @implementation Test13
322 - (id) init0 {
323   self = 0;
325 - (void) noninit {
326   self = 0; // expected-error {{cannot assign to 'self' outside of a method in the init family}}
328 @end
330 // <rdar://problem/10274056>
331 @interface Test13_B
332 - (id) consumesSelf __attribute__((ns_consumes_self));
333 @end
334 @implementation Test13_B
335 - (id) consumesSelf {
336   self = 0; // no-warning
338 @end
340 // rdar://problem/9172151
341 @class Test14A, Test14B;
342 void test14() {
343   extern void test14_consume(id *);
344   extern int test14_cond(void);
345   extern float test14_nowriteback(id __autoreleasing const *); // expected-note{{passing argument to parameter here}}
347   Test14A *a;
348   Test14B *b;
349   id i;
350   id cla[10];
351   id vla[test14_cond() + 10];
353   test14_consume((__strong id*) &a);
354   test14_consume((test14_cond() ? (__strong id*) &b : &i));
355   test14_consume(test14_cond() ? 0 : &a);
356   test14_consume(test14_cond() ? (void*) 0 : (&a));
357   test14_consume(cla); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}}
358   test14_consume(vla); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}}
359   test14_consume(&cla[5]); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}}
361   __strong id *test14_indirect(void);
362   test14_consume(test14_indirect()); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
364   extern id test14_global;
365   test14_consume(&test14_global); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
367   extern __strong id *test14_global_ptr;
368   test14_consume(test14_global_ptr); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
370   static id static_local;
371   test14_consume(&static_local); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
373   __weak id* wip;
374   test14_nowriteback(&static_local); // okay, not a write-back.
375   test14_nowriteback(wip); // expected-error{{passing '__weak id *' to parameter of type '__autoreleasing id const *' changes retain/release properties of pointer}}
378 void test15() {
379   __block __autoreleasing id x; // expected-error {{__block variables cannot have __autoreleasing ownership}}
382 struct Test16;
383 @interface Test16a
384 - (void) test16_0: (int) x;
385 - (int) test16_1: (int) x; // expected-note {{one possibility}}
386 - (int) test16_2: (int) x; // expected-note {{one possibility}}
387 - (id) test16_3: (int) x __attribute__((ns_returns_retained)); // expected-note {{one possibility}}
388 - (void) test16_4: (int) x __attribute__((ns_consumes_self)); // expected-note {{one possibility}}
389 - (void) test16_5: (id) __attribute__((ns_consumed)) x; // expected-note {{one possibility}}
390 - (void) test16_6: (id) x;
391 @end
393 @interface Test16b 
394 - (void) test16_0: (int) x;
395 - (int) test16_1: (char*) x; // expected-note {{also found}}
396 - (char*) test16_2: (int) x; // expected-note {{also found}}
397 - (id) test16_3: (int) x; // expected-note {{also found}}
398 - (void) test16_4: (int) x; // expected-note {{also found}}
399 - (void) test16_5: (id) x; // expected-note {{also found}}
400 - (void) test16_6: (struct Test16 *) x;
401 @end
403 void test16(void) {
404   id v;
405   [v test16_0: 0];
406   [v test16_1: 0]; // expected-error {{multiple methods named 'test16_1:' found with mismatched result, parameter type or attributes}}
407   [v test16_2: 0]; // expected-error {{multiple methods named}}
408   [v test16_3: 0]; // expected-error {{multiple methods named}}
409   [v test16_4: 0]; // expected-error {{multiple methods named}}
410   [v test16_5: 0]; // expected-error {{multiple methods named}}
411   [v test16_6: 0];
414 @class Test17; // expected-note 3{{forward declaration of class here}}
415 @protocol Test17p
416 - (void) test17;
417 + (void) test17;
418 @end
419 void test17(void) {
420   Test17 *v0;
421   [v0 test17]; // expected-error {{receiver type 'Test17' for instance message is a forward declaration}}
423   Test17<Test17p> *v1;
424   [v1 test17]; // expected-error {{receiver type 'Test17<Test17p>' for instance message is a forward declaration}}
426   [Test17 test17]; // expected-error {{receiver 'Test17' for class message is a forward declaration}}
429 void test18(void) {
430   id x;
431   [x test18]; // expected-error {{instance method 'test18' not found ; did you mean 'test17'?}}
434 extern struct Test19 *test19a;
435 struct Test19 *const test19b = 0;
436 void test19(void) {
437   id x;
438   x = (id) test19a; // expected-error {{bridged cast}} \
439   // expected-note{{use __bridge to convert directly (no change in ownership)}} \
440   // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'struct Test19 *' into ARC}}
441   x = (id) test19b; // expected-error {{bridged cast}} \
442   // expected-note{{use __bridge to convert directly (no change in ownership)}} \
443   // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'struct Test19 *' into ARC}}
446 // rdar://problem/8951453
447 static __thread id test20_implicit; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
448 static __thread __strong id test20_strong; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
449 static __thread __weak id test20_weak; // expected-error {{thread-local variable has non-trivial ownership: type is '__weak id'}}
450 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}}
451 static __thread __unsafe_unretained id test20_unsafe;
452 void test20(void) {
453   static __thread id test20_implicit; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
454   static __thread __strong id test20_strong; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
455   static __thread __weak id test20_weak; // expected-error {{thread-local variable has non-trivial ownership: type is '__weak id'}}
456   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}}
457   static __thread __unsafe_unretained id test20_unsafe;
460 // rdar://9310049
461 _Bool fn(id obj) {
462     return (_Bool)obj;
465 // Check casting w/ ownership qualifiers.
466 void test21() {
467   __strong id *sip;
468   (void)(__weak id *)sip; // expected-error{{casting '__strong id *' to type '__weak id *' changes retain/release properties of pointer}}
469   (void)(__weak const id *)sip; // expected-error{{casting '__strong id *' to type '__weak id const *' changes retain/release properties of pointer}}
470   (void)(__autoreleasing id *)sip; // expected-error{{casting '__strong id *' to type '__autoreleasing id *' changes retain/release properties of pointer}}
471   (void)(__autoreleasing const id *)sip; // okay
474 // rdar://problem/9340462
475 void test22(id x[]) { // expected-error {{must explicitly describe intended ownership of an object array parameter}}
478 // rdar://problem/9400219
479 void test23(void) {
480   void *ptr;
481   ptr = @"foo";
482   ptr = (ptr ? @"foo" : 0);
483   ptr = (ptr ? @"foo" : @"bar");
486 id test24(void) {
487   extern void test24_helper(void);
488   return test24_helper(), (void*) 0;
491 // rdar://9400841
492 @interface Base
493 @property (assign) id content;
494 @end
496 @interface Foo : Base
497 -(void)test;
498 @end
500 @implementation Foo
501 -(void)test {
502         super.content = 0;
504 @end
506 // <rdar://problem/9398437>
507 void test25(Class *classes) {
508   Class *other_classes;
509   test25(other_classes);
512 void test26(id y) {
513   extern id test26_var1;
514   __sync_swap(&test26_var1, 0, y); // expected-error {{cannot perform atomic operation on a pointer to type '__strong id': type has non-trivial ownership}}
516   extern __unsafe_unretained id test26_var2;
517   __sync_swap(&test26_var2, 0, y);
520 @interface Test26
521 - (id) init;
522 - (id) initWithInt: (int) x;
523 @end
524 @implementation Test26
525 - (id) init { return self; }
526 - (id) initWithInt: (int) x {
527   [self init]; // expected-error {{the result of a delegate init call must be immediately returned or assigned to 'self'}}
528   return self;
530 @end
532 // rdar://9525555
533 @interface  Test27 {
534   __weak id _myProp1;
535   id myProp2;
537 @property id x;
538 @property (readonly) id ro;
539 @property (readonly) id custom_ro;
540 @property int y;
542 @property (readonly) __weak id myProp1;
543 @property (readonly) id myProp2;
544 @property (readonly) __strong id myProp3;
545 @end
547 @implementation Test27
548 @synthesize x;
549 @synthesize ro;
550 @synthesize y;
552 @synthesize myProp1 = _myProp1;
553 @synthesize myProp2;
554 @synthesize myProp3;
556 -(id)custom_ro { return 0; }
557 @end
559 // rdar://9569264
560 @interface Test28
561 @property (nonatomic, assign) __strong id a; // expected-error {{unsafe_unretained property 'a' may not also be declared __strong}}
562 @end
564 @interface Test28 ()
565 @property (nonatomic, assign) __strong id b; // expected-error {{unsafe_unretained property 'b' may not also be declared __strong}}
566 @end
568 @implementation Test28
569 @synthesize a;
570 @synthesize b;
571 @end
573 // rdar://9573962
574 typedef struct Bark Bark;
575 @interface Test29
576 @property Bark* P;
577 @end
579 @implementation Test29
580 @synthesize P;
581 - (id)Meth { 
582   Bark** f = &P; 
583   return 0; 
585 @end
587 // rdar://9495837
588 @interface Test30
589 + (id) new;
590 - (void)Meth;
591 @end
593 @implementation Test30
594 + (id) new { return 0; }
595 - (void) Meth {
596   __weak id x = [Test30 new]; // expected-warning {{assigning retained object to weak variable}}
597   id __unsafe_unretained u = [Test30 new]; // expected-warning {{assigning retained object to unsafe_unretained variable}}
598   id y = [Test30 new];
599   x = [Test30 new]; // expected-warning {{assigning retained object to weak variable}}
600   u = [Test30 new]; // expected-warning {{assigning retained object to unsafe_unretained variable}}
601   y = [Test30 new];
603 @end
605 // rdar://9411838
606 @protocol PTest31 @end
608 int Test31() {
609     Class cls;
610     id ids;
611     id<PTest31> pids;
612     Class<PTest31> pcls;
614     int i =  (ids->isa ? 1 : 0); // expected-error {{member reference base type 'id' is not a structure or union}}
615     int j = (pids->isa ? 1 : 0); // expected-error {{member reference base type 'id<PTest31>' is not a structure or union}}
616     int k = (pcls->isa ? i : j); // expected-error {{member reference base type 'Class<PTest31>' is not a structure or union}}
617     return cls->isa ? i : j; // expected-error {{member reference base type 'Class' is not a structure or union}}
620 // rdar://9612030
621 @interface ITest32 {
622 @public
623  id ivar;
625 @end
627 id Test32(__weak ITest32 *x) {
628   __weak ITest32 *y;
629   x->ivar = 0; // expected-error {{dereferencing a __weak pointer is not allowed}}
630   return y ? y->ivar     // expected-error {{dereferencing a __weak pointer is not allowed}}
631            : (*x).ivar;  // expected-error {{dereferencing a __weak pointer is not allowed}}
634 // rdar://9619861
635 extern int printf(const char*, ...);
636 typedef long intptr_t;
638 int Test33(id someid) {
639   printf( "Hello%ld", (intptr_t)someid);
640   return (int)someid;
643 // rdar://9636091
644 @interface I34
645 @property (nonatomic, retain) id newName __attribute__((ns_returns_not_retained)) ;
647 @property (nonatomic, retain) id newName1 __attribute__((ns_returns_not_retained)) ;
648 - (id) newName1 __attribute__((ns_returns_not_retained));
650 @property (nonatomic, retain) id newName2 __attribute__((ns_returns_not_retained)); // expected-note {{roperty declared here}}
651 - (id) newName2;   // expected-warning {{property declared as returning non-retained objects; getter returning retained objects}}
652 @end
654 @implementation I34
655 @synthesize newName;
657 @synthesize newName1;
658 - (id) newName1 { return 0; }
660 @synthesize newName2;
661 @end
663 void test35(void) {
664   extern void test36_helper(id*);
665   id x;
666   __strong id *xp = 0;
668   test36_helper(&x);
669   test36_helper(xp); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
671   // rdar://problem/9665710
672   __block id y;
673   test36_helper(&y);
674   ^{ test36_helper(&y); }();
676   __strong int non_objc_type; // expected-warning {{'__strong' only applies to Objective-C object or block pointer types}} 
679 void test36(int first, ...) {
680   // <rdar://problem/9758798>
681   __builtin_va_list arglist;
682   __builtin_va_start(arglist, first);
683   id obj = __builtin_va_arg(arglist, id);
684   __builtin_va_end(arglist);
687 @class Test37; // expected-note{{forward declaration of class here}}
688 void test37(Test37 *c) {
689   for (id y in c) { // expected-error {{collection expression type 'Test37' is a forward declaration}}
690     (void) y;
691   }
693   (void)sizeof(id*); // no error.
696 // rdar://problem/9887979
697 @interface Test38
698 @property int value;
699 @end
700 void test38() {
701   extern Test38 *test38_helper(void);
702   switch (test38_helper().value) {
703   case 0:
704   case 1:
705     ;
706   }
709 // rdar://10186536
710 @class NSColor;
711 void _NSCalc(NSColor* color, NSColor* bezelColors[]) __attribute__((unavailable("not available in automatic reference counting mode")));
713 void _NSCalcBeze(NSColor* color, NSColor* bezelColors[]); // expected-error {{must explicitly describe intended ownership of an object array parameter}}
715 // rdar://9970739
716 @interface RestaurantTableViewCell
717 - (void) restaurantLocation;
718 @end
720 @interface Radar9970739
721 - (void) Meth;
722 @end
724 @implementation Radar9970739
725 - (void) Meth { 
726   RestaurantTableViewCell *cell;
727   [cell restaurantLocatoin]; // expected-error {{no visible @interface for 'RestaurantTableViewCell' declares the selector 'restaurantLocatoin'}}
729 @end
731 // rdar://11814185
732 @interface Radar11814185
733 @property (nonatomic, weak)  Radar11814185* picker1;
734 + alloc;
735 - init;
736 @end
738 @implementation Radar11814185
740 @synthesize picker1;
742 - (void)viewDidLoad
744     picker1 = [[Radar11814185 alloc] init]; // expected-warning {{assigning retained object to weak variable; object will be released after assignment}}
745     self.picker1 = [[Radar11814185 alloc] init]; // expected-warning {{assigning retained object to weak property; object will be released after assignment}}
748 + alloc { return 0; }
749 - init { return 0; }
750 @end
752 // <rdar://problem/12569201>.  Warn on cases of initializing a weak variable
753 // with an Objective-C object literal.
754 void rdar12569201(id key, id value) {
755     // Declarations.
756     __weak id x = @"foo"; // no-warning
757     __weak id y = @{ key : value }; // expected-warning {{assigning dictionary literal to a weak variable; object will be released after assignment}}
758     __weak id z = @[ value ]; // expected-warning {{assigning array literal to a weak variable; object will be released after assignment}}
759     __weak id b = ^() {}; // expected-warning {{assigning block literal to a weak variable; object will be released after assignment}}
760     __weak id n = @42; // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
761     __weak id e = @(42); // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
762     __weak id m = @(41 + 1); // expected-warning {{assigning boxed expression to a weak variable; object will be released after assignment}}
763     
764     // Assignments.
765     y = @{ key : value }; // expected-warning {{assigning dictionary literal to a weak variable; object will be released after assignment}}
766     z = @[ value ]; // expected-warning {{assigning array literal to a weak variable; object will be released after assignment}}
767     b = ^() {}; // expected-warning {{assigning block literal to a weak variable; object will be released after assignment}}
768     n = @42; // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
769     e = @(42); // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
770     m = @(41 + 1); // expected-warning {{assigning boxed expression to a weak variable; object will be released after assignment}}
773 @interface C
774 - (void)method:(id[])objects; // expected-error{{must explicitly describe intended ownership of an object array parameter}}
775 @end
777 // rdar://13752880
778 @interface NSMutableArray : NSArray @end
780 typedef __strong NSMutableArray * PSNS;
782 void test(NSArray *x) {
783   NSMutableArray *y = x; // expected-warning {{incompatible pointer types initializing 'NSMutableArray *' with an expression of type 'NSArray *'}}
784   __strong NSMutableArray *y1 = x; // expected-warning {{incompatible pointer types initializing 'NSMutableArray *' with an expression of type 'NSArray *'}}
785   PSNS y2 = x; // expected-warning {{incompatible pointer types initializing 'NSMutableArray *' with an expression of type 'NSArray *'}}
788 // rdar://15123684
789 @class NSString;
791 void foo(NSArray *array) {
792   for (NSString *string in array) {
793     for (string in @[@"blah", @"more blah", string]) { // expected-error {{selector element of type 'NSString *const __strong' cannot be a constant l-value}}
794     }
795   }
798 // rdar://16627903
799 extern void abort();
800 #define TKAssertEqual(a, b) do{\
801     __typeof(a) a_res = (a);\
802     __typeof(b) b_res = (b);\
803     if ((a_res) != (b_res)) {\
804         abort();\
805     }\
806 }while(0)
808 int garf() {
809   id object;
810   TKAssertEqual(object, nil);
811   TKAssertEqual(object, (id)nil);
814 void block_capture_autoreleasing(A * __autoreleasing *a,
815                                  A **b, //  expected-note {{declare the parameter __strong or capture a __block __strong variable to keep values alive across autorelease pools}}
816                                  A * _Nullable *c, // expected-note {{declare the parameter __strong or capture a __block __strong variable to keep values alive across autorelease pools}}
817                                  A * _Nullable __autoreleasing *d,
818                                  A ** _Nullable e, // expected-note {{declare the parameter __strong or capture a __block __strong variable to keep values alive across autorelease pools}}
819                                  A * __autoreleasing * _Nullable f,
820                                  id __autoreleasing *g,
821                                  id *h, // expected-note {{declare the parameter __strong or capture a __block __strong variable to keep values alive across autorelease pools}}
822                                  id _Nullable *i, // expected-note {{declare the parameter __strong or capture a __block __strong variable to keep values alive across autorelease pools}}
823                                  id _Nullable __autoreleasing *j,
824                                  id * _Nullable k, // expected-note {{declare the parameter __strong or capture a __block __strong variable to keep values alive across autorelease pools}}
825                                  id __autoreleasing * _Nullable l) {
826   ^{
827     (void)*a;
828     (void)*b; // expected-warning {{block captures an autoreleasing out-parameter, which may result in use-after-free bugs}}
829     (void)*c; // expected-warning {{block captures an autoreleasing out-parameter, which may result in use-after-free bugs}}
830     (void)*d;
831     (void)*e; // expected-warning {{block captures an autoreleasing out-parameter, which may result in use-after-free bugs}}
832     (void)*f;
833     (void)*g;
834     (void)*h; // expected-warning {{block captures an autoreleasing out-parameter, which may result in use-after-free bugs}}
835     (void)*i; // expected-warning {{block captures an autoreleasing out-parameter, which may result in use-after-free bugs}}
836     (void)*j;
837     (void)*k; // expected-warning {{block captures an autoreleasing out-parameter, which may result in use-after-free bugs}}
838     (void)*l;
839   }();