1// RUN: %clang_cc1 -arcmt-check -verify -triple x86_64-apple-darwin10 %s 2// DISABLE: mingw32 3 4#if __has_feature(objc_arc) 5#define NS_AUTOMATED_REFCOUNT_UNAVAILABLE __attribute__((unavailable("not available in automatic reference counting mode"))) 6#else 7#define NS_AUTOMATED_REFCOUNT_UNAVAILABLE 8#endif 9 10typedef const void * CFTypeRef; 11CFTypeRef CFBridgingRetain(id X); 12id CFBridgingRelease(CFTypeRef); 13 14typedef int BOOL; 15typedef unsigned NSUInteger; 16 17@protocol NSObject 18- (id)retain NS_AUTOMATED_REFCOUNT_UNAVAILABLE; 19- (NSUInteger)retainCount NS_AUTOMATED_REFCOUNT_UNAVAILABLE; 20- (oneway void)release NS_AUTOMATED_REFCOUNT_UNAVAILABLE; 21- (id)autorelease NS_AUTOMATED_REFCOUNT_UNAVAILABLE; 22@end 23 24@interface NSObject <NSObject> {} 25- (id)init; 26 27+ (id)new; 28+ (id)alloc; 29- (void)dealloc; 30 31- (void)finalize; 32 33- (id)copy; 34- (id)mutableCopy; 35@end 36 37typedef const struct __CFString * CFStringRef; 38extern const CFStringRef kUTTypePlainText; 39extern const CFStringRef kUTTypeRTF; 40@class NSString; 41@class A; 42 43struct UnsafeS { 44 A *__unsafe_unretained unsafeObj; 45}; 46 47@interface A : NSObject 48- (id)retain; 49- (id)retainCount; 50- (id)autorelease; 51- (id)init; 52- (oneway void)release; 53- (void)dealloc; 54-(void)test; 55-(id)delegate; 56@end 57 58@implementation A 59-(void)test { 60 [super dealloc]; 61} 62-(void)dealloc { 63 [super dealloc]; 64} 65 66- (id)retain { return self; } // expected-error {{ARC forbids implementation}} 67- (id)retainCount { return self; } // expected-error {{ARC forbids implementation}} 68- (id)autorelease { return self; } // expected-error {{ARC forbids implementation}} 69- (oneway void)release { } // expected-error {{ARC forbids implementation}} 70 71-(id)delegate { return self; } 72@end 73 74id global_foo; 75 76void test1(A *a, BOOL b, struct UnsafeS *unsafeS) { 77 [[a delegate] release]; // expected-error {{it is not safe to remove 'retain' message on the result of a 'delegate' message; the object that was passed to 'setDelegate:' may not be properly retained}} \ 78 // expected-error {{ARC forbids explicit message send}} 79 [a.delegate release]; // expected-error {{it is not safe to remove 'retain' message on the result of a 'delegate' message; the object that was passed to 'setDelegate:' may not be properly retained}} \ 80 // expected-error {{ARC forbids explicit message send}} 81 [unsafeS->unsafeObj retain]; // expected-error {{it is not safe to remove 'retain' message on an __unsafe_unretained type}} \ 82 // expected-error {{ARC forbids explicit message send}} 83 id foo = [unsafeS->unsafeObj retain]; // no warning. 84 [global_foo retain]; // expected-error {{it is not safe to remove 'retain' message on a global variable}} \ 85 // expected-error {{ARC forbids explicit message send}} 86 [global_foo release]; // expected-error {{it is not safe to remove 'release' message on a global variable}} \ 87 // expected-error {{ARC forbids explicit message send}} 88 [a dealloc]; 89 [a retain]; 90 [a retainCount]; // expected-error {{ARC forbids explicit message send of 'retainCount'}} 91 [a release]; 92 [a autorelease]; // expected-error {{it is not safe to remove an unused 'autorelease' message; its receiver may be destroyed immediately}} \ 93 // expected-error {{ARC forbids explicit message send}} 94 95 CFStringRef cfstr; 96 NSString *str = (NSString *)cfstr; // expected-error {{cast of C pointer type 'CFStringRef' (aka 'const struct __CFString *') to Objective-C pointer type 'NSString *' requires a bridged cast}} \ 97 // expected-note {{use __bridge to convert directly (no change in ownership)}} \ 98 // expected-note {{use CFBridgingRelease call to transfer ownership of a +1 'CFStringRef' (aka 'const struct __CFString *') into ARC}} \ 99 str = (NSString *)kUTTypePlainText; 100 str = b ? kUTTypeRTF : kUTTypePlainText; 101 str = (NSString *)(b ? kUTTypeRTF : kUTTypePlainText); 102 str = (NSString *)a; // no change. 103 104 SEL s = @selector(retain); // expected-error {{ARC forbids use of 'retain' in a @selector}} 105 s = @selector(release); // expected-error {{ARC forbids use of 'release' in a @selector}} 106 s = @selector(autorelease); // expected-error {{ARC forbids use of 'autorelease' in a @selector}} 107 s = @selector(dealloc); // expected-error {{ARC forbids use of 'dealloc' in a @selector}} 108 109 static id __autoreleasing X1; // expected-error {{global variables cannot have __autoreleasing ownership}} 110} 111 112struct S { 113 A* a; // expected-error {{ARC forbids Objective-C objects in structs or unions}} 114}; 115 116@interface B 117-(id)alloc; 118- (id)initWithInt: (int) i; 119@end 120 121void rdar8861761() { 122 B *o1 = [[B alloc] initWithInt:0]; 123 B *o2 = [B alloc]; 124 [o2 initWithInt:0]; 125} 126 127@interface Test13 128- (id) init0; 129- (void) noninit; 130@end 131@implementation Test13 132- (id) init0 { 133 self = 0; 134} 135- (void) noninit { 136 self = 0; // expected-error {{cannot assign to 'self' outside of a method in the init family}} 137 138 for (id x in collection) { // expected-error {{use of undeclared identifier 'collection'}} 139 x = 0; 140 } 141} 142@end 143 144void * cvt(id arg) 145{ 146 void* voidp_val; 147 (void)(int*)arg; // expected-error {{disallowed}} 148 (void)(id)arg; 149 (void)(__autoreleasing id*)arg; // expected-error {{disallowed}} 150 (void)(id*)arg; // expected-error {{disallowed}} 151 152 (void)(__autoreleasing id**)voidp_val; 153 (void)(void*)voidp_val; 154 (void)(void**)arg; // expected-error {{disallowed}} 155 cvt((void*)arg); // expected-error 2 {{requires a bridged cast}} \ 156 // expected-note 2 {{use __bridge to}} expected-note {{use CFBridgingRelease call}} expected-note {{use CFBridgingRetain call}} 157 cvt(0); 158 (void)(__strong id**)(0); 159 return arg; // expected-error {{requires a bridged cast}} expected-note {{use __bridge}} expected-note {{use CFBridgingRetain call}} 160} 161 162 163void test12(id collection) { 164 for (id x in collection) { 165 x = 0; 166 } 167 168 for (__strong id x in collection) { 169 x = 0; 170 } 171} 172 173void test6(unsigned cond) { 174 // FIXME: Fix this automatically ? 175 switch (cond) { 176 case 0: 177 ; 178 id x; // expected-note {{jump bypasses initialization of retaining variable}} 179 180 case 1: // expected-error {{switch case is in protected scope}} 181 break; 182 } 183} 184 185@class Test8_incomplete; 186@interface Test8_complete @end; 187@interface Test8_super @end; 188@interface Test8 : Test8_super 189- (id) init00; 190- (id) init01; // expected-note {{declaration in interface}} 191- (id) init02; 192- (id) init03; // covariance 193- (id) init04; // covariance 194- (id) init05; 195 196- (void) init10; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}} 197- (void) init11; 198- (void) init12; 199- (void) init13; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}} 200- (void) init14; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}} 201- (void) init15; 202 203// These should be invalid to actually call. 204- (Test8_incomplete*) init20; 205- (Test8_incomplete*) init21; // expected-note {{declaration in interface}} 206- (Test8_incomplete*) init22; 207- (Test8_incomplete*) init23; 208- (Test8_incomplete*) init24; 209- (Test8_incomplete*) init25; 210 211- (Test8_super*) init30; // id exception to covariance 212- (Test8_super*) init31; // expected-note {{declaration in interface}} 213- (Test8_super*) init32; 214- (Test8_super*) init33; 215- (Test8_super*) init34; // covariance 216- (Test8_super*) init35; 217 218- (Test8*) init40; // id exception to covariance 219- (Test8*) init41; // expected-note {{declaration in interface}} 220- (Test8*) init42; 221- (Test8*) init43; // this should be a warning, but that's a general language thing, not an ARC thing 222- (Test8*) init44; 223- (Test8*) init45; 224 225- (Test8_complete*) init50; // expected-error {{init methods must return a type related to the receiver type}} 226- (Test8_complete*) init51; // expected-error {{init methods must return a type related to the receiver type}} 227- (Test8_complete*) init52; // expected-error {{init methods must return a type related to the receiver type}} 228- (Test8_complete*) init53; // expected-error {{init methods must return a type related to the receiver type}} 229- (Test8_complete*) init54; // expected-error {{init methods must return a type related to the receiver type}} 230- (Test8_complete*) init55; // expected-error {{init methods must return a type related to the receiver type}} 231@end 232@implementation Test8 233- (id) init00 { return 0; } 234- (id) init10 { return 0; } // expected-error {{method implementation does not match its declaration}} 235- (id) init20 { return 0; } 236- (id) init30 { return 0; } 237- (id) init40 { return 0; } 238- (id) init50 { return 0; } 239 240- (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}} 241- (void) init11 {} 242- (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}} 243- (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}} 244- (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}} 245- (void) init51 {} 246 247- (Test8_incomplete*) init02 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} 248- (Test8_incomplete*) init12 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} 249- (Test8_incomplete*) init22 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} 250- (Test8_incomplete*) init32 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} 251- (Test8_incomplete*) init42 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} 252- (Test8_incomplete*) init52 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} 253 254- (Test8_super*) init03 { return 0; } 255- (Test8_super*) init13 { return 0; } // expected-error {{method implementation does not match its declaration}} 256- (Test8_super*) init23 { return 0; } 257- (Test8_super*) init33 { return 0; } 258- (Test8_super*) init43 { return 0; } 259- (Test8_super*) init53 { return 0; } 260 261- (Test8*) init04 { return 0; } 262- (Test8*) init14 { return 0; } // expected-error {{method implementation does not match its declaration}} 263- (Test8*) init24 { return 0; } 264- (Test8*) init34 { return 0; } 265- (Test8*) init44 { return 0; } 266- (Test8*) init54 { return 0; } 267 268- (Test8_complete*) init05 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} 269- (Test8_complete*) init15 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} 270- (Test8_complete*) init25 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} 271- (Test8_complete*) init35 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} 272- (Test8_complete*) init45 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} 273- (Test8_complete*) init55 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} 274@end 275 276@class Test9_incomplete; 277@interface Test9 278- (Test9_incomplete*) init1; // expected-error {{init methods must return a type related to the receiver type}} 279- (Test9_incomplete*) init2; 280@end 281id test9(Test9 *v) { 282 return [v init1]; 283} 284 285// rdar://9491791 286void rdar9491791(int p) { 287 switch (p) { 288 case 3:; 289 NSObject *o = [[NSObject alloc] init]; // expected-note {{jump bypasses initialization of retaining variable}} 290 [o release]; 291 break; 292 default: // expected-error {{switch case is in protected scope}} 293 break; 294 } 295} 296 297#define RELEASE_MACRO(x) do { [x release]; } while(1) 298 299// rdar://9504750 300void rdar9504750(id p) { 301 RELEASE_MACRO(p); // expected-error {{ARC forbids explicit message send of 'release'}} 302} 303 304// rdar://8939557 305@interface TestReadonlyProperty : NSObject 306@property(assign,readonly) NSObject *value; 307@end 308 309@implementation TestReadonlyProperty 310@synthesize value; 311- (void)viewDidLoad { 312 value = [NSObject new]; // expected-error {{assigning retained object}} 313} 314@end 315 316// rdar://9601437 317@interface I9601437 { 318 __unsafe_unretained id x; 319} 320-(void)Meth; 321@end 322 323@implementation I9601437 324-(void)Meth { 325 self->x = [NSObject new]; // expected-error {{assigning retained object}} 326} 327@end 328