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