Home | History | Annotate | Download | only in SemaObjC
      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 
      3 typedef unsigned long NSUInteger;
      4 typedef const void * CFTypeRef;
      5 CFTypeRef CFBridgingRetain(id X);
      6 id CFBridgingRelease(CFTypeRef);
      7 @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
     14 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len;
     15 @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
     22 
     23 void 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 
     35 void 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 
     57 // rdar://8843638
     58 
     59 @interface I
     60 - (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}}
     64 @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)
     74 - (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}}
     82 @end
     83 
     84 // rdar://8861761
     85 
     86 @interface B
     87 + (id)alloc;
     88 - (id)initWithInt: (int) i;
     89 - (id)myInit __attribute__((objc_method_family(init)));
     90 @end
     91 
     92 void rdar8861761() {
     93   B *o1 = [[B alloc] initWithInt:0];
     94   B *o2 = [B alloc];
     95   [o2 initWithInt:0]; // expected-warning {{expression result unused}}
     96   B *o3 = [[B alloc] myInit];
     97   [[B alloc] myInit]; // expected-warning {{expression result unused}}
     98 }
     99 
    100 // rdar://8925835
    101 @interface rdar8925835
    102 - (void)foo:(void (^)(unsigned captureCount, I * const capturedStrings[captureCount]))block;
    103 @end
    104 
    105 void test5() {
    106   extern void test5_helper(__autoreleasing id *);
    107   id x;
    108 
    109   // Okay because of magic temporaries.
    110   test5_helper(&x);
    111 
    112   __autoreleasing id *a = &x; // expected-error {{initializing '__autoreleasing id *' with an expression of type '__strong id *' changes retain/release properties of pointer}}
    113 
    114   a = &x; // expected-error {{assigning '__strong id *' to '__autoreleasing id *' changes retain/release properties of pointer}}
    115 
    116   extern void test5_helper2(id const *);
    117   test5_helper2(&x);
    118 
    119   extern void test5_helper3(__weak id *); // expected-note {{passing argument to parameter here}}
    120   test5_helper3(&x); // expected-error {{passing '__strong id *' to parameter of type '__weak id *' changes retain/release properties of pointer}}
    121 }
    122 
    123 // rdar://problem/8937869
    124 void test6(unsigned cond) {
    125   switch (cond) {
    126   case 0:
    127     ;
    128     id x; // expected-note {{jump bypasses initialization of retaining variable}}
    129 
    130   case 1: // expected-error {{switch case is in protected scope}}
    131     break;
    132   }
    133 }
    134 
    135 @class NSError;
    136 void test7(void) {
    137   extern void test7_helper(NSError **);
    138   NSError *err;
    139   test7_helper(&err);
    140 }
    141 void test7_weak(void) {
    142   extern void test7_helper(NSError **);
    143   __weak NSError *err;
    144   test7_helper(&err);
    145 }
    146 void test7_unsafe(void) {
    147   extern void test7_helper(NSError **); // expected-note {{passing argument to parameter here}}
    148   __unsafe_unretained NSError *err;
    149   test7_helper(&err); // expected-error {{passing 'NSError *__unsafe_unretained *' to parameter of type 'NSError *__autoreleasing *' changes retain/release properties of pointer}}
    150 }
    151 
    152 @class Test8_incomplete;
    153 @interface Test8_complete @end;
    154 @interface Test8_super @end;
    155 @interface Test8 : Test8_super
    156 - (id) init00;
    157 - (id) init01; // expected-note {{declaration in interface}} \
    158                // expected-note{{overridden method}}
    159 - (id) init02; // expected-note{{overridden method}}
    160 - (id) init03; // covariance
    161 - (id) init04; // covariance
    162 - (id) init05; // expected-note{{overridden method}}
    163 
    164 - (void) init10; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
    165 - (void) init11;
    166 - (void) init12;
    167 - (void) init13; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
    168 - (void) init14; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
    169 - (void) init15;
    170 
    171 // These should be invalid to actually call.
    172 - (Test8_incomplete*) init20;
    173 - (Test8_incomplete*) init21; // expected-note {{declaration in interface}}
    174 - (Test8_incomplete*) init22;
    175 - (Test8_incomplete*) init23;
    176 - (Test8_incomplete*) init24;
    177 - (Test8_incomplete*) init25;
    178 
    179 - (Test8_super*) init30; // id exception to covariance
    180 - (Test8_super*) init31; // expected-note {{declaration in interface}} \
    181                          // expected-note{{overridden method}}
    182 - (Test8_super*) init32; // expected-note{{overridden method}}
    183 - (Test8_super*) init33;
    184 - (Test8_super*) init34; // covariance
    185 - (Test8_super*) init35; // expected-note{{overridden method}}
    186 
    187 - (Test8*) init40; // id exception to covariance
    188 - (Test8*) init41; // expected-note {{declaration in interface}} \
    189                    // expected-note{{overridden method}}
    190 - (Test8*) init42; // expected-note{{overridden method}}
    191 - (Test8*) init43; // this should be a warning, but that's a general language thing, not an ARC thing
    192 - (Test8*) init44;
    193 - (Test8*) init45; // expected-note{{overridden method}}
    194 
    195 - (Test8_complete*) init50; // expected-error {{init methods must return a type related to the receiver type}}
    196 - (Test8_complete*) init51; // expected-error {{init methods must return a type related to the receiver type}}
    197 - (Test8_complete*) init52; // expected-error {{init methods must return a type related to the receiver type}}
    198 - (Test8_complete*) init53; // expected-error {{init methods must return a type related to the receiver type}}
    199 - (Test8_complete*) init54; // expected-error {{init methods must return a type related to the receiver type}}
    200 - (Test8_complete*) init55; // expected-error {{init methods must return a type related to the receiver type}}
    201 @end
    202 @implementation Test8
    203 - (id) init00 { return 0; }
    204 - (id) init10 { return 0; } // expected-error {{method implementation does not match its declaration}}
    205 - (id) init20 { return 0; }
    206 - (id) init30 { return 0; }
    207 - (id) init40 { return 0; }
    208 - (id) init50 { return 0; }
    209 
    210 - (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}} \
    211                    // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'void'}}
    212 - (void) init11 {}
    213 - (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}}
    214 - (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}} \
    215                    // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'void'}}
    216 - (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}} \
    217                    // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'void'}}
    218 - (void) init51 {}
    219 
    220 - (Test8_incomplete*) init02 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
    221                                            // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_incomplete *'}}
    222 - (Test8_incomplete*) init12 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
    223 - (Test8_incomplete*) init22 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
    224 - (Test8_incomplete*) init32 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
    225                                            // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_incomplete *'}}
    226 - (Test8_incomplete*) init42 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
    227                                            // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_incomplete *'}}
    228 - (Test8_incomplete*) init52 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
    229 
    230 - (Test8_super*) init03 { return 0; }
    231 - (Test8_super*) init13 { return 0; } // expected-error {{method implementation does not match its declaration}}
    232 - (Test8_super*) init23 { return 0; }
    233 - (Test8_super*) init33 { return 0; }
    234 - (Test8_super*) init43 { return 0; }
    235 - (Test8_super*) init53 { return 0; }
    236 
    237 - (Test8*) init04 { return 0; }
    238 - (Test8*) init14 { return 0; } // expected-error {{method implementation does not match its declaration}}
    239 - (Test8*) init24 { return 0; }
    240 - (Test8*) init34 { return 0; }
    241 - (Test8*) init44 { return 0; }
    242 - (Test8*) init54 { return 0; }
    243 
    244 - (Test8_complete*) init05 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
    245                                          // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_complete *'}}
    246 - (Test8_complete*) init15 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
    247 - (Test8_complete*) init25 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
    248 - (Test8_complete*) init35 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
    249                                          // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_complete *'}}
    250 - (Test8_complete*) init45 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
    251                                          // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_complete *'}}
    252 - (Test8_complete*) init55 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
    253 @end
    254 
    255 @class Test9_incomplete;
    256 @interface Test9
    257 - (Test9_incomplete*) init1; // expected-error {{init methods must return a type related to the receiver type}}
    258 - (Test9_incomplete*) init2;
    259 @end
    260 id test9(Test9 *v) {
    261   return [v init1];
    262 }
    263 
    264 // Test that the inference rules are different for fast enumeration variables.
    265 void test10(id collection) {
    266   for (id x in collection) {
    267     __strong id *ptr = &x; // expected-warning {{initializing '__strong id *' with an expression of type 'const __strong id *' discards qualifiers}}
    268   }
    269 
    270   for (__strong id x in collection) {
    271     __weak id *ptr = &x; // expected-error {{initializing '__weak id *' with an expression of type '__strong id *' changes retain/release properties of pointer}}
    272   }
    273 }
    274 
    275 // rdar://problem/9078626
    276 #define nil ((void*) 0)
    277 void test11(id op, void *vp) {
    278   _Bool b;
    279   b = (op == nil);
    280   b = (nil == op);
    281 
    282   b = (vp == nil);
    283   b = (nil == vp);
    284 
    285   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}}
    286   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}}
    287 }
    288 
    289 void test12(id collection) {
    290   for (id x in collection) {
    291     x = 0; // expected-error {{fast enumeration variables can't be modified in ARC by default; declare the variable __strong to allow this}}
    292   }
    293 
    294   for (const id x in collection) {
    295     x = 0; // expected-error {{read-only variable is not assignable}}
    296   }
    297 
    298   for (__strong id x in collection) {
    299     x = 0;
    300   }
    301 }
    302 
    303 @interface Test13
    304 - (id) init0;
    305 - (void) noninit;
    306 @end
    307 @implementation Test13
    308 - (id) init0 {
    309   self = 0;
    310 }
    311 - (void) noninit {
    312   self = 0; // expected-error {{cannot assign to 'self' outside of a method in the init family}}
    313 }
    314 @end
    315 
    316 // <rdar://problem/10274056>
    317 @interface Test13_B
    318 - (id) consumesSelf __attribute__((ns_consumes_self));
    319 @end
    320 @implementation Test13_B
    321 - (id) consumesSelf {
    322   self = 0; // no-warning
    323 }
    324 @end
    325 
    326 // rdar://problem/9172151
    327 @class Test14A, Test14B;
    328 void test14() {
    329   extern void test14_consume(id *);
    330   extern int test14_cond(void);
    331   extern float test14_nowriteback(id __autoreleasing const *); // expected-note{{passing argument to parameter here}}
    332 
    333   Test14A *a;
    334   Test14B *b;
    335   id i;
    336   id cla[10];
    337   id vla[test14_cond() + 10];
    338 
    339   test14_consume((__strong id*) &a);
    340   test14_consume((test14_cond() ? (__strong id*) &b : &i));
    341   test14_consume(test14_cond() ? 0 : &a);
    342   test14_consume(test14_cond() ? (void*) 0 : (&a));
    343   test14_consume(cla); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}}
    344   test14_consume(vla); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}}
    345   test14_consume(&cla[5]); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}}
    346 
    347   __strong id *test14_indirect(void);
    348   test14_consume(test14_indirect()); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
    349 
    350   extern id test14_global;
    351   test14_consume(&test14_global); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
    352 
    353   extern __strong id *test14_global_ptr;
    354   test14_consume(test14_global_ptr); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
    355 
    356   static id static_local;
    357   test14_consume(&static_local); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
    358 
    359   __weak id* wip;
    360   test14_nowriteback(&static_local); // okay, not a write-back.
    361   test14_nowriteback(wip); // expected-error{{passing '__weak id *' to parameter of type '__autoreleasing id const *' changes retain/release properties of pointer}}
    362 }
    363 
    364 void test15() {
    365   __block __autoreleasing id x; // expected-error {{__block variables cannot have __autoreleasing ownership}}
    366 }
    367 
    368 struct Test16;
    369 @interface Test16a
    370 - (void) test16_0: (int) x;
    371 - (int) test16_1: (int) x; // expected-note {{one possibility}}
    372 - (int) test16_2: (int) x; // expected-note {{one possibility}}
    373 - (id) test16_3: (int) x __attribute__((ns_returns_retained)); // expected-note {{one possibility}}
    374 - (void) test16_4: (int) x __attribute__((ns_consumes_self)); // expected-note {{one possibility}}
    375 - (void) test16_5: (id) __attribute__((ns_consumed)) x; // expected-note {{one possibility}}
    376 - (void) test16_6: (id) x;
    377 @end
    378 
    379 @interface Test16b 
    380 - (void) test16_0: (int) x;
    381 - (int) test16_1: (char*) x; // expected-note {{also found}}
    382 - (char*) test16_2: (int) x; // expected-note {{also found}}
    383 - (id) test16_3: (int) x; // expected-note {{also found}}
    384 - (void) test16_4: (int) x; // expected-note {{also found}}
    385 - (void) test16_5: (id) x; // expected-note {{also found}}
    386 - (void) test16_6: (struct Test16 *) x;
    387 @end
    388 
    389 void test16(void) {
    390   id v;
    391   [v test16_0: 0];
    392   [v test16_1: 0]; // expected-error {{multiple methods named 'test16_1:' found with mismatched result, parameter type or attributes}}
    393   [v test16_2: 0]; // expected-error {{multiple methods named}}
    394   [v test16_3: 0]; // expected-error {{multiple methods named}}
    395   [v test16_4: 0]; // expected-error {{multiple methods named}}
    396   [v test16_5: 0]; // expected-error {{multiple methods named}}
    397   [v test16_6: 0];
    398 }
    399 
    400 @class Test17; // expected-note 2{{forward declaration of class here}}
    401 @protocol Test17p
    402 - (void) test17;
    403 + (void) test17;
    404 @end
    405 void test17(void) {
    406   Test17 *v0;
    407   [v0 test17]; // expected-error {{receiver type 'Test17' for instance message is a forward declaration}}
    408 
    409   Test17<Test17p> *v1;
    410   [v1 test17]; // expected-error {{receiver type 'Test17<Test17p>' for instance message is a forward declaration}}
    411 
    412   [Test17 test17]; // expected-error {{receiver 'Test17' for class message is a forward declaration}}
    413 }
    414 
    415 void test18(void) {
    416   id x;
    417   [x test18]; // expected-error {{instance method 'test18' not found ; did you mean 'test17'?}}
    418 }
    419 
    420 extern struct Test19 *test19a;
    421 struct Test19 *const test19b = 0;
    422 void test19(void) {
    423   id x;
    424   x = (id) test19a; // expected-error {{bridged cast}} \
    425   // expected-note{{use __bridge to convert directly (no change in ownership)}} \
    426   // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'struct Test19 *' into ARC}}
    427   x = (id) test19b; // expected-error {{bridged cast}} \
    428   // expected-note{{use __bridge to convert directly (no change in ownership)}} \
    429   // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'struct Test19 *' into ARC}}
    430 }
    431 
    432 // rdar://problem/8951453
    433 static __thread id test20_implicit; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
    434 static __thread __strong id test20_strong; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
    435 static __thread __weak id test20_weak; // expected-error {{thread-local variable has non-trivial ownership: type is '__weak id'}}
    436 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}}
    437 static __thread __unsafe_unretained id test20_unsafe;
    438 void test20(void) {
    439   static __thread id test20_implicit; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
    440   static __thread __strong id test20_strong; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}}
    441   static __thread __weak id test20_weak; // expected-error {{thread-local variable has non-trivial ownership: type is '__weak id'}}
    442   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}}
    443   static __thread __unsafe_unretained id test20_unsafe;
    444 }
    445 
    446 // rdar://9310049
    447 _Bool fn(id obj) {
    448     return (_Bool)obj;
    449 }
    450 
    451 // Check casting w/ ownership qualifiers.
    452 void test21() {
    453   __strong id *sip;
    454   (void)(__weak id *)sip; // expected-error{{casting '__strong id *' to type '__weak id *' changes retain/release properties of pointer}}
    455   (void)(__weak const id *)sip; // expected-error{{casting '__strong id *' to type '__weak id const *' changes retain/release properties of pointer}}
    456   (void)(__autoreleasing id *)sip; // expected-error{{casting '__strong id *' to type '__autoreleasing id *' changes retain/release properties of pointer}}
    457   (void)(__autoreleasing const id *)sip; // okay
    458 }
    459 
    460 // rdar://problem/9340462
    461 void test22(id x[]) { // expected-error {{must explicitly describe intended ownership of an object array parameter}}
    462 }
    463 
    464 // rdar://problem/9400219
    465 void test23(void) {
    466   void *ptr;
    467   ptr = @"foo";
    468   ptr = (ptr ? @"foo" : 0);
    469   ptr = (ptr ? @"foo" : @"bar");
    470 }
    471 
    472 id test24(void) {
    473   extern void test24_helper(void);
    474   return test24_helper(), (void*) 0;
    475 }
    476 
    477 // rdar://9400841
    478 @interface Base
    479 @property (assign) id content;
    480 @end
    481 
    482 @interface Foo : Base
    483 -(void)test;
    484 @end
    485 
    486 @implementation Foo
    487 -(void)test {
    488 	super.content = 0;
    489 }
    490 @end
    491 
    492 // <rdar://problem/9398437>
    493 void test25(Class *classes) {
    494   Class *other_classes;
    495   test25(other_classes);
    496 }
    497 
    498 void 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 // rdar://9525555
    519 @interface  Test27 {
    520   __weak id _myProp1;
    521   id myProp2;
    522 }
    523 @property id x;
    524 @property (readonly) id ro;
    525 @property (readonly) id custom_ro;
    526 @property int y;
    527 
    528 @property (readonly) __weak id myProp1;
    529 @property (readonly) id myProp2;
    530 @property (readonly) __strong id myProp3;
    531 @end
    532 
    533 @implementation Test27
    534 @synthesize x;
    535 @synthesize ro;
    536 @synthesize y;
    537 
    538 @synthesize myProp1 = _myProp1;
    539 @synthesize myProp2;
    540 @synthesize myProp3;
    541 
    542 -(id)custom_ro { return 0; }
    543 @end
    544 
    545 // rdar://9569264
    546 @interface Test28
    547 @property (nonatomic, assign) __strong id a; // expected-error {{unsafe_unretained property 'a' may not also be declared __strong}}
    548 @end
    549 
    550 @interface Test28 ()
    551 @property (nonatomic, assign) __strong id b; // expected-error {{unsafe_unretained property 'b' may not also be declared __strong}}
    552 @end
    553 
    554 @implementation Test28
    555 @synthesize a;
    556 @synthesize b;
    557 @end
    558 
    559 // rdar://9573962
    560 typedef struct Bark Bark;
    561 @interface Test29
    562 @property Bark* P;
    563 @end
    564 
    565 @implementation Test29
    566 @synthesize P;
    567 - (id)Meth { 
    568   Bark** f = &P; 
    569   return 0; 
    570 }
    571 @end
    572 
    573 // rdar://9495837
    574 @interface Test30
    575 + (id) new;
    576 - (void)Meth;
    577 @end
    578 
    579 @implementation Test30
    580 + (id) new { return 0; }
    581 - (void) Meth {
    582   __weak id x = [Test30 new]; // expected-warning {{assigning retained object to weak variable}}
    583   id __unsafe_unretained u = [Test30 new]; // expected-warning {{assigning retained object to unsafe_unretained variable}}
    584   id y = [Test30 new];
    585   x = [Test30 new]; // expected-warning {{assigning retained object to weak variable}}
    586   u = [Test30 new]; // expected-warning {{assigning retained object to unsafe_unretained variable}}
    587   y = [Test30 new];
    588 }
    589 @end
    590 
    591 // rdar://9411838
    592 @protocol PTest31 @end
    593 
    594 int Test31() {
    595     Class cls;
    596     id ids;
    597     id<PTest31> pids;
    598     Class<PTest31> pcls;
    599 
    600     int i =  (ids->isa ? 1 : 0); // expected-error {{member reference base type 'id' is not a structure or union}}
    601     int j = (pids->isa ? 1 : 0); // expected-error {{member reference base type 'id<PTest31>' is not a structure or union}}
    602     int k = (pcls->isa ? i : j); // expected-error {{member reference base type 'Class<PTest31>' is not a structure or union}}
    603     return cls->isa ? i : j; // expected-error {{member reference base type 'Class' is not a structure or union}}
    604 }
    605 
    606 // rdar://9612030
    607 @interface ITest32 {
    608 @public
    609  id ivar;
    610 }
    611 @end
    612 
    613 id Test32(__weak ITest32 *x) {
    614   __weak ITest32 *y;
    615   x->ivar = 0; // expected-error {{dereferencing a __weak pointer is not allowed}}
    616   return y ? y->ivar     // expected-error {{dereferencing a __weak pointer is not allowed}}
    617            : (*x).ivar;  // expected-error {{dereferencing a __weak pointer is not allowed}}
    618 }
    619 
    620 // rdar://9619861
    621 extern int printf(const char*, ...);
    622 typedef long intptr_t;
    623 
    624 int Test33(id someid) {
    625   printf( "Hello%ld", (intptr_t)someid);
    626   return (int)someid;
    627 }
    628 
    629 // rdar://9636091
    630 @interface I34
    631 @property (nonatomic, retain) id newName __attribute__((ns_returns_not_retained)) ;
    632 
    633 @property (nonatomic, retain) id newName1 __attribute__((ns_returns_not_retained)) ;
    634 - (id) newName1 __attribute__((ns_returns_not_retained));
    635 
    636 @property (nonatomic, retain) id newName2 __attribute__((ns_returns_not_retained)); // expected-note {{roperty declared here}}
    637 - (id) newName2;   // expected-warning {{property declared as returning non-retained objects; getter returning retained objects}}
    638 @end
    639 
    640 @implementation I34
    641 @synthesize newName;
    642 
    643 @synthesize newName1;
    644 - (id) newName1 { return 0; }
    645 
    646 @synthesize newName2;
    647 @end
    648 
    649 void test35(void) {
    650   extern void test36_helper(id*);
    651   id x;
    652   __strong id *xp = 0;
    653 
    654   test36_helper(&x);
    655   test36_helper(xp); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
    656 
    657   // rdar://problem/9665710
    658   __block id y;
    659   test36_helper(&y);
    660   ^{ test36_helper(&y); }();
    661 
    662   __strong int non_objc_type; // expected-warning {{'__strong' only applies to Objective-C object or block pointer types}} 
    663 }
    664 
    665 void test36(int first, ...) {
    666   // <rdar://problem/9758798>
    667   __builtin_va_list arglist;
    668   __builtin_va_start(arglist, first);
    669   id obj = __builtin_va_arg(arglist, id);
    670   __builtin_va_end(arglist);
    671 }
    672 
    673 @class Test37; // expected-note{{forward declaration of class here}}
    674 void test37(Test37 *c) {
    675   for (id y in c) { // expected-error {{collection expression type 'Test37' is a forward declaration}}
    676     (void) y;
    677   }
    678 
    679   (void)sizeof(id*); // no error.
    680 }
    681 
    682 // rdar://problem/9887979
    683 @interface Test38
    684 @property int value;
    685 @end
    686 void test38() {
    687   extern Test38 *test38_helper(void);
    688   switch (test38_helper().value) {
    689   case 0:
    690   case 1:
    691     ;
    692   }
    693 }
    694 
    695 // rdar://10186536
    696 @class NSColor;
    697 void _NSCalc(NSColor* color, NSColor* bezelColors[]) __attribute__((unavailable("not available in automatic reference counting mode")));
    698 
    699 void _NSCalcBeze(NSColor* color, NSColor* bezelColors[]); // expected-error {{must explicitly describe intended ownership of an object array parameter}}
    700 
    701 // rdar://9970739
    702 @interface RestaurantTableViewCell
    703 - (void) restaurantLocation;
    704 @end
    705 
    706 @interface Radar9970739
    707 - (void) Meth;
    708 @end
    709 
    710 @implementation Radar9970739
    711 - (void) Meth { 
    712   RestaurantTableViewCell *cell;
    713   [cell restaurantLocatoin]; // expected-error {{no visible @interface for 'RestaurantTableViewCell' declares the selector 'restaurantLocatoin'}}
    714 }
    715 @end
    716 
    717 // rdar://11814185
    718 @interface Radar11814185
    719 @property (nonatomic, weak)  Radar11814185* picker1;
    720 + alloc;
    721 - init;
    722 @end
    723 
    724 @implementation Radar11814185
    725 
    726 @synthesize picker1;
    727 
    728 - (void)viewDidLoad
    729 {
    730     picker1 = [[Radar11814185 alloc] init]; // expected-warning {{assigning retained object to weak variable; object will be released after assignment}}
    731     self.picker1 = [[Radar11814185 alloc] init]; // expected-warning {{assigning retained object to weak property; object will be released after assignment}}
    732 }
    733 
    734 + alloc { return 0; }
    735 - init { return 0; }
    736 @end
    737 
    738 // <rdar://problem/12569201>.  Warn on cases of initializing a weak variable
    739 // with an Objective-C object literal.
    740 void rdar12569201(id key, id value) {
    741     // Declarations.
    742     __weak id x = @"foo"; // no-warning
    743     __weak id y = @{ key : value }; // expected-warning {{assigning dictionary literal to a weak variable; object will be released after assignment}}
    744     __weak id z = @[ value ]; // expected-warning {{assigning array literal to a weak variable; object will be released after assignment}}
    745     __weak id b = ^() {}; // expected-warning {{assigning block literal to a weak variable; object will be released after assignment}}
    746     __weak id n = @42; // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
    747     __weak id e = @(42); // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
    748     __weak id m = @(41 + 1); // expected-warning {{assigning boxed expression to a weak variable; object will be released after assignment}}
    749     
    750     // Assignments.
    751     y = @{ key : value }; // expected-warning {{assigning dictionary literal to a weak variable; object will be released after assignment}}
    752     z = @[ value ]; // expected-warning {{assigning array literal to a weak variable; object will be released after assignment}}
    753     b = ^() {}; // expected-warning {{assigning block literal to a weak variable; object will be released after assignment}}
    754     n = @42; // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
    755     e = @(42); // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
    756     m = @(41 + 1); // expected-warning {{assigning boxed expression to a weak variable; object will be released after assignment}}
    757 }
    758 
    759 @interface C
    760 - (void)method:(id[])objects; // expected-error{{must explicitly describe intended ownership of an object array parameter}}
    761 @end
    762 
    763 // rdar://13752880
    764 @interface NSMutableArray : NSArray @end
    765 
    766 typedef __strong NSMutableArray * PSNS;
    767 
    768 void test(NSArray *x) {
    769   NSMutableArray *y = x; // expected-warning {{incompatible pointer types initializing 'NSMutableArray *' with an expression of type 'NSArray *'}}
    770   __strong NSMutableArray *y1 = x; // expected-warning {{incompatible pointer types initializing 'NSMutableArray *' with an expression of type 'NSArray *'}}
    771   PSNS y2 = x; // expected-warning {{incompatible pointer types initializing 'NSMutableArray *' with an expression of type 'NSArray *'}}
    772 }
    773