xref: /llvm-project/clang/test/SemaObjC/arc.m (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1// RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fobjc-runtime-has-weak -fsyntax-only -fobjc-arc -fblocks -verify -Wno-pointer-to-int-cast -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-pointer-to-int-cast -Wno-objc-root-class -fdiagnostics-parseable-fixits %s 2>&1
3
4typedef unsigned long NSUInteger;
5typedef const void * CFTypeRef;
6CFTypeRef CFBridgingRetain(id X);
7id 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
23
24void test0(void (*fn)(int), int val) {
25  fn(val);
26}
27
28@interface A
29- (id)retain;
30- (id)autorelease;
31- (oneway void)release;
32- (void)dealloc;
33- (NSUInteger)retainCount;
34@end
35
36void 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'}}
46}
47
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'}}
55}
56@end
57
58@interface I
59- (id)retain; // expected-note {{method 'retain' declared here}}
60- (id)autorelease; // expected-note {{method 'autorelease' declared here}}
61- (oneway void)release; // expected-note {{method 'release' declared here}}
62- (NSUInteger)retainCount; // expected-note {{method 'retainCount' declared here}}
63@end
64
65@implementation I
66- (id)retain{return 0;} // expected-error {{ARC forbids implementation of 'retain'}}
67- (id)autorelease{return 0;} // expected-error {{ARC forbids implementation of 'autorelease'}}
68- (oneway void)release{} // expected-error {{ARC forbids implementation of 'release'}}
69- (NSUInteger)retainCount{ return 0; } // expected-error {{ARC forbids implementation of 'retainCount'}}
70@end
71
72@implementation I(CAT)
73- (id)retain{return 0;} // expected-error {{ARC forbids implementation of 'retain'}} \
74                        // expected-warning {{category is implementing a method which will also be implemented by its primary class}}
75- (id)autorelease{return 0;} // expected-error {{ARC forbids implementation of 'autorelease'}} \
76                         // expected-warning {{category is implementing a method which will also be implemented by its primary class}}
77- (oneway void)release{} // expected-error {{ARC forbids implementation of 'release'}} \
78                          // expected-warning {{category is implementing a method which will also be implemented by its primary class}}
79- (NSUInteger)retainCount{ return 0; } // expected-error {{ARC forbids implementation of 'retainCount'}} \
80                          // expected-warning {{category is implementing a method which will also be implemented by its primary class}}
81@end
82
83@interface B
84+ (id)alloc;
85- (id)initWithInt: (int) i;
86- (id)myInit __attribute__((objc_method_family(init)));
87- (id)myBadInit __attribute__((objc_method_family(12)));  // expected-error {{'objc_method_family' attribute requires parameter 1 to be an identifier}}
88
89@end
90
91void rdar8861761(void) {
92  B *o1 = [[B alloc] initWithInt:0];
93  B *o2 = [B alloc];
94  [o2 initWithInt:0]; // expected-warning {{expression result unused}}
95  B *o3 = [[B alloc] myInit];
96  [[B alloc] myInit]; // expected-warning {{expression result unused}}
97}
98
99@interface rdar8925835
100- (void)foo:(void (^)(unsigned captureCount, I * const capturedStrings[captureCount]))block;
101@end
102
103void test5(void) {
104  extern void test5_helper(__autoreleasing id *);
105  id x;
106
107  // Okay because of magic temporaries.
108  test5_helper(&x);
109
110  __autoreleasing id *a = &x; // expected-error {{initializing '__autoreleasing id *' with an expression of type '__strong id *' changes retain/release properties of pointer}}
111
112  __autoreleasing id *aa;
113  aa = &x; // expected-error {{assigning '__strong id *' to '__autoreleasing id *' changes retain/release properties of pointer}}
114
115  extern void test5_helper2(id const *);
116  test5_helper2(&x);
117
118  extern void test5_helper3(__weak id *); // expected-note {{passing argument to parameter here}}
119  test5_helper3(&x); // expected-error {{passing '__strong id *' to parameter of type '__weak id *' changes retain/release properties of pointer}}
120}
121
122void test6(unsigned cond) {
123  switch (cond) {
124  case 0:
125    ;
126    id x; // expected-note {{jump bypasses initialization of __strong variable}}
127
128  case 1: // expected-error {{cannot jump}}
129    break;
130  }
131}
132void test6a(unsigned cond) {
133  switch (cond) {
134  case 0:
135    ;
136    __weak id x; // expected-note {{jump bypasses initialization of __weak variable}}
137
138  case 1: // expected-error {{cannot jump}}
139    break;
140  }
141}
142
143@class NSError;
144void test7(void) {
145  extern void test7_helper(NSError **);
146  NSError *err;
147  test7_helper(&err);
148}
149void test7_weak(void) {
150  extern void test7_helper(NSError **);
151  __weak NSError *err;
152  test7_helper(&err);
153}
154void test7_unsafe(void) {
155  extern void test7_helper(NSError **); // expected-note {{passing argument to parameter here}}
156  __unsafe_unretained NSError *err;
157  test7_helper(&err); // expected-error {{passing 'NSError *__unsafe_unretained *' to parameter of type 'NSError *__autoreleasing *' changes retain/release properties of pointer}}
158}
159
160@class Test8_incomplete;
161@interface Test8_complete @end;
162@interface Test8_super @end;
163@interface Test8 : Test8_super
164- (id) init00;
165- (id) init01; // expected-note {{declaration in interface}} \
166               // expected-note{{overridden method}}
167- (id) init02; // expected-note{{overridden method}}
168- (id) init03; // covariance
169- (id) init04; // covariance
170- (id) init05; // expected-note{{overridden method}}
171
172- (void) init10; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
173- (void) init11;
174- (void) init12;
175- (void) init13; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
176- (void) init14; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
177- (void) init15;
178
179// These should be invalid to actually call.
180- (Test8_incomplete*) init20;
181- (Test8_incomplete*) init21; // expected-note {{declaration in interface}}
182- (Test8_incomplete*) init22;
183- (Test8_incomplete*) init23;
184- (Test8_incomplete*) init24;
185- (Test8_incomplete*) init25;
186
187- (Test8_super*) init30; // id exception to covariance
188- (Test8_super*) init31; // expected-note {{declaration in interface}} \
189                         // expected-note{{overridden method}}
190- (Test8_super*) init32; // expected-note{{overridden method}}
191- (Test8_super*) init33;
192- (Test8_super*) init34; // covariance
193- (Test8_super*) init35; // expected-note{{overridden method}}
194
195- (Test8*) init40; // id exception to covariance
196- (Test8*) init41; // expected-note {{declaration in interface}} \
197                   // expected-note{{overridden method}}
198- (Test8*) init42; // expected-note{{overridden method}}
199- (Test8*) init43; // this should be a warning, but that's a general language thing, not an ARC thing
200- (Test8*) init44;
201- (Test8*) init45; // expected-note{{overridden method}}
202
203- (Test8_complete*) init50; // expected-error {{init methods must return a type related to the receiver type}}
204- (Test8_complete*) init51; // expected-error {{init methods must return a type related to the receiver type}}
205- (Test8_complete*) init52; // expected-error {{init methods must return a type related to the receiver type}}
206- (Test8_complete*) init53; // expected-error {{init methods must return a type related to the receiver type}}
207- (Test8_complete*) init54; // expected-error {{init methods must return a type related to the receiver type}}
208- (Test8_complete*) init55; // expected-error {{init methods must return a type related to the receiver type}}
209@end
210@implementation Test8
211- (id) init00 { return 0; }
212- (id) init10 { return 0; } // expected-error {{method implementation does not match its declaration}}
213- (id) init20 { return 0; }
214- (id) init30 { return 0; }
215- (id) init40 { return 0; }
216- (id) init50 { return 0; }
217
218- (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}} \
219                   // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'void'}}
220- (void) init11 {}
221- (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}}
222- (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}} \
223                   // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'void'}}
224- (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}} \
225                   // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'void'}}
226- (void) init51 {}
227
228- (Test8_incomplete*) init02 { 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*) init12 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
231- (Test8_incomplete*) init22 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
232- (Test8_incomplete*) init32 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
233                                           // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_incomplete *'}}
234- (Test8_incomplete*) init42 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
235                                           // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_incomplete *'}}
236- (Test8_incomplete*) init52 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
237
238- (Test8_super*) init03 { return 0; }
239- (Test8_super*) init13 { return 0; } // expected-error {{method implementation does not match its declaration}}
240- (Test8_super*) init23 { return 0; }
241- (Test8_super*) init33 { return 0; }
242- (Test8_super*) init43 { return 0; }
243- (Test8_super*) init53 { return 0; }
244
245- (Test8*) init04 { return 0; }
246- (Test8*) init14 { return 0; } // expected-error {{method implementation does not match its declaration}}
247- (Test8*) init24 { return 0; }
248- (Test8*) init34 { return 0; }
249- (Test8*) init44 { return 0; }
250- (Test8*) init54 { return 0; }
251
252- (Test8_complete*) init05 { 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*) init15 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
255- (Test8_complete*) init25 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
256- (Test8_complete*) init35 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
257                                         // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_complete *'}}
258- (Test8_complete*) init45 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
259                                         // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_complete *'}}
260- (Test8_complete*) init55 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
261@end
262
263@class Test9_incomplete;
264@interface Test9
265- (Test9_incomplete*) init1; // expected-error {{init methods must return a type related to the receiver type}}
266- (Test9_incomplete*) init2;
267@end
268id test9(Test9 *v) {
269  return [v init1];
270}
271
272// Test that the inference rules are different for fast enumeration variables.
273void test10(id collection) {
274  for (id x in collection) {
275    __strong id *ptr = &x; // expected-warning {{initializing '__strong id *' with an expression of type 'const __strong id *' discards qualifiers}}
276  }
277
278  for (__strong id x in collection) {
279    __weak id *ptr = &x; // expected-error {{initializing '__weak id *' with an expression of type '__strong id *' changes retain/release properties of pointer}}
280  }
281}
282
283#define nil ((void*) 0)
284void test11(id op, void *vp) {
285  _Bool b;
286  b = (op == nil);
287  b = (nil == op);
288
289  b = (vp == nil);
290  b = (nil == vp);
291
292  // FIXME: Shouldn't these be consistent?
293  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}}
294  b = (op == vp);
295}
296
297void test12(id collection) {
298  for (id x in collection) {
299    x = 0; // expected-error {{fast enumeration variables cannot be modified in ARC by default; declare the variable __strong to allow this}}
300  }
301
302  for (const id x in collection) { // expected-note {{variable 'x' declared const here}}
303    x = 0; // expected-error {{cannot assign to variable 'x' with const-qualified type 'const __strong id'}}
304  }
305
306  for (__strong id x in collection) {
307    x = 0;
308  }
309}
310
311@interface Test13
312- (id) init0;
313- (void) noninit;
314@end
315@implementation Test13
316- (id) init0 {
317  self = 0;
318}
319- (void) noninit {
320  self = 0; // expected-error {{cannot assign to 'self' outside of a method in the init family}}
321}
322@end
323
324@interface Test13_B
325- (id) consumesSelf __attribute__((ns_consumes_self));
326@end
327@implementation Test13_B
328- (id) consumesSelf {
329  self = 0; // no-warning
330}
331@end
332
333@class Test14A, Test14B;
334void test14(void) {
335  extern void test14_consume(id *);
336  extern int test14_cond(void);
337  extern float test14_nowriteback(id __autoreleasing const *); // expected-note{{passing argument to parameter here}}
338
339  Test14A *a;
340  Test14B *b;
341  id i;
342  id cla[10];
343  id vla[test14_cond() + 10];
344
345  test14_consume((__strong id*) &a);
346  test14_consume((test14_cond() ? (__strong id*) &b : &i));
347  test14_consume(test14_cond() ? 0 : &a);
348  test14_consume(test14_cond() ? (void*) 0 : (&a));
349  test14_consume(cla); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}}
350  test14_consume(vla); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}}
351  test14_consume(&cla[5]); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}}
352
353  __strong id *test14_indirect(void);
354  test14_consume(test14_indirect()); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
355
356  extern id test14_global;
357  test14_consume(&test14_global); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
358
359  extern __strong id *test14_global_ptr;
360  test14_consume(test14_global_ptr); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
361
362  static id static_local;
363  test14_consume(&static_local); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
364
365  __weak id* wip;
366  test14_nowriteback(&static_local); // okay, not a write-back.
367  test14_nowriteback(wip); // expected-error{{passing '__weak id *' to parameter of type '__autoreleasing id const *' changes retain/release properties of pointer}}
368}
369
370void test15(void) {
371  __block __autoreleasing id x; // expected-error {{__block variables cannot have __autoreleasing ownership}}
372}
373
374struct Test16;
375@interface Test16a
376- (void) test16_0: (int) x;
377- (int) test16_1: (int) x; // expected-note {{one possibility}}
378- (int) test16_2: (int) x; // expected-note {{one possibility}}
379- (id) test16_3: (int) x __attribute__((ns_returns_retained)); // expected-note {{one possibility}}
380- (void) test16_4: (int) x __attribute__((ns_consumes_self)); // expected-note {{one possibility}}
381- (void) test16_5: (id) __attribute__((ns_consumed)) x; // expected-note {{one possibility}}
382- (void) test16_6: (id) x;
383@end
384
385@interface Test16b
386- (void) test16_0: (int) x;
387- (int) test16_1: (char*) x; // expected-note {{also found}}
388- (char*) test16_2: (int) x; // expected-note {{also found}}
389- (id) test16_3: (int) x; // expected-note {{also found}}
390- (void) test16_4: (int) x; // expected-note {{also found}}
391- (void) test16_5: (id) x; // expected-note {{also found}}
392- (void) test16_6: (struct Test16 *) x;
393@end
394
395void test16(void) {
396  id v;
397  [v test16_0: 0];
398  [v test16_1: 0]; // expected-error {{multiple methods named 'test16_1:' found with mismatched result, parameter type or attributes}}
399  [v test16_2: 0]; // expected-error {{multiple methods named}}
400  [v test16_3: 0]; // expected-error {{multiple methods named}}
401  [v test16_4: 0]; // expected-error {{multiple methods named}}
402  [v test16_5: 0]; // expected-error {{multiple methods named}}
403  [v test16_6: 0];
404}
405
406@class Test17; // expected-note 3{{forward declaration of class here}}
407@protocol Test17p
408- (void) test17;
409+ (void) test17;
410@end
411void test17(void) {
412  Test17 *v0;
413  [v0 test17]; // expected-error {{receiver type 'Test17' for instance message is a forward declaration}}
414
415  Test17<Test17p> *v1;
416  [v1 test17]; // expected-error {{receiver type 'Test17<Test17p>' for instance message is a forward declaration}}
417
418  [Test17 test17]; // expected-error {{receiver 'Test17' for class message is a forward declaration}}
419}
420
421void test18(void) {
422  id x;
423  [x test18]; // expected-error {{instance method 'test18' not found ; did you mean 'test17'?}}
424}
425
426extern struct Test19 *test19a;
427struct Test19 *const test19b = 0;
428void test19(void) {
429  id x;
430  x = (id) test19a; // expected-error {{bridged cast}} \
431  // expected-note{{use __bridge to convert directly (no change in ownership)}} \
432  // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'struct Test19 *' into ARC}}
433  x = (id) test19b; // expected-error {{bridged cast}} \
434  // expected-note{{use __bridge to convert directly (no change in ownership)}} \
435  // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'struct Test19 *' into ARC}}
436}
437
438static __thread id test20_implicit; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
439static __thread __strong id test20_strong; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
440static __thread __weak id test20_weak; // expected-error {{thread-local variable has non-trivial ownership: type is '__weak id'}}
441static __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}}
442static __thread __unsafe_unretained id test20_unsafe;
443void test20(void) {
444  static __thread id test20_implicit; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
445  static __thread __strong id test20_strong; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
446  static __thread __weak id test20_weak; // expected-error {{thread-local variable has non-trivial ownership: type is '__weak id'}}
447  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}}
448  static __thread __unsafe_unretained id test20_unsafe;
449}
450
451_Bool fn(id obj) {
452    return (_Bool)obj;
453}
454
455// Check casting w/ ownership qualifiers.
456void test21(void) {
457  __strong id *sip;
458  (void)(__weak id *)sip; // expected-error{{casting '__strong id *' to type '__weak id *' changes retain/release properties of pointer}}
459  (void)(__weak const id *)sip; // expected-error{{casting '__strong id *' to type '__weak id const *' changes retain/release properties of pointer}}
460  (void)(__autoreleasing id *)sip; // expected-error{{casting '__strong id *' to type '__autoreleasing id *' changes retain/release properties of pointer}}
461  (void)(__autoreleasing const id *)sip; // okay
462}
463
464void test22(id x[]) { // expected-error {{must explicitly describe intended ownership of an object array parameter}}
465}
466
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@interface Base
480@property (assign) id content;
481@end
482
483@interface Foo : Base
484-(void)test;
485@end
486
487@implementation Foo
488-(void)test {
489	super.content = 0;
490}
491@end
492
493void test25(Class *classes) {
494  Class *other_classes;
495  test25(other_classes);
496}
497
498void test26(id y) {
499  extern id test26_var1;
500  __sync_swap(&test26_var1, 0, y); // expected-error {{cannot perform atomic operation on a pointer to type '__strong id': type has non-trivial ownership}}
501
502  extern __unsafe_unretained id test26_var2;
503  __sync_swap(&test26_var2, 0, y);
504}
505
506@interface Test26
507- (id) init;
508- (id) initWithInt: (int) x;
509@end
510@implementation Test26
511- (id) init { return self; }
512- (id) initWithInt: (int) x {
513  [self init]; // expected-error {{the result of a delegate init call must be immediately returned or assigned to 'self'}}
514  return self;
515}
516@end
517
518@interface  Test27 {
519  __weak id _myProp1;
520  id myProp2;
521}
522@property id x;
523@property (readonly) id ro;
524@property (readonly) id custom_ro;
525@property int y;
526
527@property (readonly) __weak id myProp1;
528@property (readonly) id myProp2;
529@property (readonly) __strong id myProp3;
530@end
531
532@implementation Test27
533@synthesize x;
534@synthesize ro;
535@synthesize y;
536
537@synthesize myProp1 = _myProp1;
538@synthesize myProp2;
539@synthesize myProp3;
540
541-(id)custom_ro { return 0; }
542@end
543
544@interface Test28
545@property (nonatomic, assign) __strong id a; // expected-error {{unsafe_unretained property 'a' may not also be declared __strong}}
546@end
547
548@interface Test28 ()
549@property (nonatomic, assign) __strong id b; // expected-error {{unsafe_unretained property 'b' may not also be declared __strong}}
550@end
551
552@implementation Test28
553@synthesize a;
554@synthesize b;
555@end
556
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@interface Test30
571+ (id) new;
572- (void)Meth;
573@end
574
575@implementation Test30
576+ (id) new { return 0; }
577- (void) Meth {
578  __weak id x = [Test30 new]; // expected-warning {{assigning retained object to weak variable}}
579  id __unsafe_unretained u = [Test30 new]; // expected-warning {{assigning retained object to unsafe_unretained variable}}
580  id y = [Test30 new];
581  x = [Test30 new]; // expected-warning {{assigning retained object to weak variable}}
582  u = [Test30 new]; // expected-warning {{assigning retained object to unsafe_unretained variable}}
583  y = [Test30 new];
584}
585@end
586
587@protocol PTest31 @end
588
589int Test31(void) {
590    Class cls;
591    id ids;
592    id<PTest31> pids;
593    Class<PTest31> pcls;
594
595    int i =  (ids->isa ? 1 : 0); // expected-error {{member reference base type 'id' is not a structure or union}}
596    int j = (pids->isa ? 1 : 0); // expected-error {{member reference base type 'id<PTest31>' is not a structure or union}}
597    int k = (pcls->isa ? i : j); // expected-error {{member reference base type 'Class<PTest31>' is not a structure or union}}
598    return cls->isa ? i : j; // expected-error {{member reference base type 'Class' is not a structure or union}}
599}
600
601@interface ITest32 {
602@public
603 id ivar;
604}
605@end
606
607id Test32(__weak ITest32 *x) {
608  __weak ITest32 *y;
609  x->ivar = 0; // expected-error {{dereferencing a __weak pointer is not allowed}}
610  return y ? y->ivar     // expected-error {{dereferencing a __weak pointer is not allowed}}
611           : (*x).ivar;  // expected-error {{dereferencing a __weak pointer is not allowed}}
612}
613
614extern int printf(const char*, ...);
615typedef long intptr_t;
616
617int Test33(id someid) {
618  printf( "Hello%ld", (intptr_t)someid);
619  return (int)someid;
620}
621
622@interface I34
623@property (nonatomic, retain) id newName __attribute__((ns_returns_not_retained)) ;
624
625@property (nonatomic, retain) id newName1 __attribute__((ns_returns_not_retained)) ;
626- (id) newName1 __attribute__((ns_returns_not_retained));
627
628@property (nonatomic, retain) id newName2 __attribute__((ns_returns_not_retained)); // expected-note {{roperty declared here}}
629- (id) newName2;   // expected-warning {{property declared as returning non-retained objects; getter returning retained objects}}
630@end
631
632@implementation I34
633@synthesize newName;
634
635@synthesize newName1;
636- (id) newName1 { return 0; }
637
638@synthesize newName2;
639@end
640
641void test35(void) {
642  extern void test36_helper(id*);
643  id x;
644  __strong id *xp = 0;
645
646  test36_helper(&x);
647  test36_helper(xp); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
648
649  __block id y;
650  test36_helper(&y);
651  ^{ test36_helper(&y); }();
652
653  __strong int non_objc_type; // expected-warning {{'__strong' only applies to Objective-C object or block pointer types}}
654}
655
656void test36(int first, ...) {
657  __builtin_va_list arglist;
658  __builtin_va_start(arglist, first);
659  id obj = __builtin_va_arg(arglist, id);
660  __builtin_va_end(arglist);
661}
662
663@class Test37; // expected-note{{forward declaration of class here}}
664void test37(Test37 *c) {
665  for (id y in c) { // expected-error {{collection expression type 'Test37' is a forward declaration}}
666    (void) y;
667  }
668
669  (void)sizeof(id*); // no error.
670}
671
672@interface Test38
673@property int value;
674@end
675void test38(void) {
676  extern Test38 *test38_helper(void);
677  switch (test38_helper().value) {
678  case 0:
679  case 1:
680    ;
681  }
682}
683
684@class NSColor;
685void _NSCalc(NSColor* color, NSColor* bezelColors[]) __attribute__((unavailable("not available in automatic reference counting mode")));
686
687void _NSCalcBeze(NSColor* color, NSColor* bezelColors[]); // expected-error {{must explicitly describe intended ownership of an object array parameter}}
688
689@interface RestaurantTableViewCell
690- (void) restaurantLocation;
691@end
692
693@interface Radar9970739
694- (void) Meth;
695@end
696
697@implementation Radar9970739
698- (void) Meth {
699  RestaurantTableViewCell *cell;
700  [cell restaurantLocatoin]; // expected-error {{no visible @interface for 'RestaurantTableViewCell' declares the selector 'restaurantLocatoin'}}
701}
702@end
703
704@interface Radar11814185
705@property (nonatomic, weak)  Radar11814185* picker1;
706+ alloc;
707- init;
708@end
709
710@implementation Radar11814185
711
712@synthesize picker1;
713
714- (void)viewDidLoad
715{
716    picker1 = [[Radar11814185 alloc] init]; // expected-warning {{assigning retained object to weak variable; object will be released after assignment}}
717    self.picker1 = [[Radar11814185 alloc] init]; // expected-warning {{assigning retained object to weak property; object will be released after assignment}}
718}
719
720+ alloc { return 0; }
721- init { return 0; }
722@end
723
724// Warn on cases of initializing a weak variable with an Objective-C object
725// literal.
726void rdar12569201(id key, id value) {
727    // Declarations.
728    __weak id x = @"foo"; // no-warning
729    __weak id y = @{ key : value }; // expected-warning {{assigning dictionary literal to a weak variable; object will be released after assignment}}
730    __weak id z = @[ value ]; // expected-warning {{assigning array literal to a weak variable; object will be released after assignment}}
731    __weak id b = ^() {}; // expected-warning {{assigning block literal to a weak variable; object will be released after assignment}}
732    __weak id n = @42; // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
733    __weak id e = @(42); // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
734    __weak id m = @(41 + 1); // expected-warning {{assigning boxed expression to a weak variable; object will be released after assignment}}
735
736    // Assignments.
737    y = @{ key : value }; // expected-warning {{assigning dictionary literal to a weak variable; object will be released after assignment}}
738    z = @[ value ]; // expected-warning {{assigning array literal to a weak variable; object will be released after assignment}}
739    b = ^() {}; // expected-warning {{assigning block literal to a weak variable; object will be released after assignment}}
740    n = @42; // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
741    e = @(42); // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
742    m = @(41 + 1); // expected-warning {{assigning boxed expression to a weak variable; object will be released after assignment}}
743}
744
745@interface C
746- (void)method:(id[])objects; // expected-error{{must explicitly describe intended ownership of an object array parameter}}
747@end
748
749@interface NSMutableArray : NSArray @end
750
751typedef __strong NSMutableArray * PSNS;
752
753void test(NSArray *x) {
754  NSMutableArray *y = x; // expected-warning {{incompatible pointer types initializing 'NSMutableArray *' with an expression of type 'NSArray *'}}
755  __strong NSMutableArray *y1 = x; // expected-warning {{incompatible pointer types initializing 'NSMutableArray *' with an expression of type 'NSArray *'}}
756  PSNS y2 = x; // expected-warning {{incompatible pointer types initializing 'NSMutableArray *' with an expression of type 'NSArray *'}}
757}
758
759@class NSString;
760
761void foo(NSArray *array) {
762  for (NSString *string in array) {
763    for (string in @[@"blah", @"more blah", string]) { // expected-error {{selector element of type 'NSString *const __strong' cannot be a constant lvalue}}
764    }
765  }
766}
767
768extern void abort(void);
769#define TKAssertEqual(a, b) do{\
770    __typeof(a) a_res = (a);\
771    __typeof(b) b_res = (b);\
772    if ((a_res) != (b_res)) {\
773        abort();\
774    }\
775}while(0)
776
777int garf(void) {
778  id object;
779  TKAssertEqual(object, nil);
780  TKAssertEqual(object, (id)nil);
781}
782
783void block_capture_autoreleasing(A * __autoreleasing *a,
784                                 A **b, //  expected-note {{declare the parameter __strong or capture a __block __strong variable to keep values alive across autorelease pools}}
785                                 A * _Nullable *c, // expected-note {{declare the parameter __strong or capture a __block __strong variable to keep values alive across autorelease pools}}
786                                 A * _Nullable __autoreleasing *d,
787                                 A ** _Nullable e, // expected-note {{declare the parameter __strong or capture a __block __strong variable to keep values alive across autorelease pools}}
788                                 A * __autoreleasing * _Nullable f,
789                                 id __autoreleasing *g,
790                                 id *h, // expected-note {{declare the parameter __strong or capture a __block __strong variable to keep values alive across autorelease pools}}
791                                 id _Nullable *i, // expected-note {{declare the parameter __strong or capture a __block __strong variable to keep values alive across autorelease pools}}
792                                 id _Nullable __autoreleasing *j,
793                                 id * _Nullable k, // expected-note {{declare the parameter __strong or capture a __block __strong variable to keep values alive across autorelease pools}}
794                                 id __autoreleasing * _Nullable l) {
795  ^{
796    (void)*a;
797    (void)*b; // expected-warning {{block captures an autoreleasing out-parameter, which may result in use-after-free bugs}}
798    (void)*c; // expected-warning {{block captures an autoreleasing out-parameter, which may result in use-after-free bugs}}
799    (void)*d;
800    (void)*e; // expected-warning {{block captures an autoreleasing out-parameter, which may result in use-after-free bugs}}
801    (void)*f;
802    (void)*g;
803    (void)*h; // expected-warning {{block captures an autoreleasing out-parameter, which may result in use-after-free bugs}}
804    (void)*i; // expected-warning {{block captures an autoreleasing out-parameter, which may result in use-after-free bugs}}
805    (void)*j;
806    (void)*k; // expected-warning {{block captures an autoreleasing out-parameter, which may result in use-after-free bugs}}
807    (void)*l;
808  }();
809}
810
811void test_vla_fold_keeps_strong(void) {
812  const unsigned bounds = 1;
813
814  static id array[bounds]; // expected-warning {{variable length array folded to constant array as an extension}}
815  typedef __typeof__(array) array_type;
816  typedef id __strong array_type[1];
817
818  static id weak_array[bounds] __weak; // expected-warning {{variable length array folded to constant array as an extension}}
819  typedef __typeof__(weak_array) weak_array_type;
820  typedef id __weak weak_array_type[1];
821}
822