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