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