John McCall | d1e40d5 | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fobjc-runtime-has-weak -fsyntax-only -fobjc-arc -fblocks -verify %s |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2 | |
| 3 | typedef unsigned long NSUInteger; |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame^] | 4 | typedef const void * CFTypeRef; |
| 5 | CFTypeRef CFBridgingRetain(id X); |
| 6 | id CFBridgingRelease(CFTypeRef); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7 | |
| 8 | void test0(void (*fn)(int), int val) { |
| 9 | fn(val); |
| 10 | } |
| 11 | |
| 12 | @interface A |
| 13 | - (id)retain; |
| 14 | - (id)autorelease; |
| 15 | - (oneway void)release; |
| 16 | - (void)dealloc; |
| 17 | - (NSUInteger)retainCount; |
| 18 | @end |
| 19 | |
| 20 | void test1(A *a) { |
| 21 | SEL s = @selector(retain); // expected-error {{ARC forbids use of 'retain' in a @selector}} |
| 22 | s = @selector(release); // expected-error {{ARC forbids use of 'release' in a @selector}} |
| 23 | s = @selector(autorelease); // expected-error {{ARC forbids use of 'autorelease' in a @selector}} |
| 24 | s = @selector(dealloc); // expected-error {{ARC forbids use of 'dealloc' in a @selector}} |
| 25 | [a dealloc]; // expected-error {{ARC forbids explicit message send of 'dealloc'}} |
| 26 | [a retain]; // expected-error {{ARC forbids explicit message send of 'retain'}} |
| 27 | [a retainCount]; // expected-error {{ARC forbids explicit message send of 'retainCount'}} |
| 28 | [a release]; // expected-error {{ARC forbids explicit message send of 'release'}} |
| 29 | [a autorelease]; // expected-error {{ARC forbids explicit message send of 'autorelease'}} |
| 30 | } |
| 31 | |
| 32 | @interface Test2 : A |
| 33 | - (void) dealloc; |
| 34 | @end |
| 35 | @implementation Test2 |
| 36 | - (void) dealloc { |
| 37 | // This should maybe just be ignored. We're just going to warn about it for now. |
| 38 | [super dealloc]; // expected-error {{ARC forbids explicit message send of 'dealloc'}} |
| 39 | } |
| 40 | @end |
| 41 | |
| 42 | __weak __strong id x; // expected-error {{the type '__strong id' already has retainment attributes}} |
| 43 | |
| 44 | // rdar://8843638 |
| 45 | |
| 46 | @interface I |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 47 | - (id)retain; // expected-note {{method declared here}} |
| 48 | - (id)autorelease; // expected-note {{method declared here}} |
| 49 | - (oneway void)release; // expected-note {{method declared here}} |
| 50 | - (NSUInteger)retainCount; // expected-note {{method declared here}} |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 51 | @end |
| 52 | |
| 53 | @implementation I |
| 54 | - (id)retain{return 0;} // expected-error {{ARC forbids implementation of 'retain'}} |
| 55 | - (id)autorelease{return 0;} // expected-error {{ARC forbids implementation of 'autorelease'}} |
| 56 | - (oneway void)release{} // expected-error {{ARC forbids implementation of 'release'}} |
| 57 | - (NSUInteger)retainCount{ return 0; } // expected-error {{ARC forbids implementation of 'retainCount'}} |
| 58 | @end |
| 59 | |
| 60 | @implementation I(CAT) |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 61 | - (id)retain{return 0;} // expected-error {{ARC forbids implementation of 'retain'}} \ |
| 62 | // expected-warning {{category is implementing a method which will also be implemented by its primary class}} |
| 63 | - (id)autorelease{return 0;} // expected-error {{ARC forbids implementation of 'autorelease'}} \ |
| 64 | // expected-warning {{category is implementing a method which will also be implemented by its primary class}} |
| 65 | - (oneway void)release{} // expected-error {{ARC forbids implementation of 'release'}} \ |
| 66 | // expected-warning {{category is implementing a method which will also be implemented by its primary class}} |
| 67 | - (NSUInteger)retainCount{ return 0; } // expected-error {{ARC forbids implementation of 'retainCount'}} \ |
| 68 | // expected-warning {{category is implementing a method which will also be implemented by its primary class}} |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 69 | @end |
| 70 | |
| 71 | // rdar://8861761 |
| 72 | |
| 73 | @interface B |
| 74 | -(id)alloc; |
| 75 | - (id)initWithInt: (int) i; |
| 76 | @end |
| 77 | |
| 78 | void rdar8861761() { |
| 79 | B *o1 = [[B alloc] initWithInt:0]; |
| 80 | B *o2 = [B alloc]; |
| 81 | [o2 initWithInt:0]; // expected-warning {{expression result unused}} |
| 82 | } |
| 83 | |
| 84 | // rdar://8925835 |
| 85 | @interface rdar8925835 |
| 86 | - (void)foo:(void (^)(unsigned captureCount, I * const capturedStrings[captureCount]))block; |
| 87 | @end |
| 88 | |
| 89 | void test5() { |
| 90 | extern void test5_helper(__autoreleasing id *); |
| 91 | id x; |
| 92 | |
| 93 | // Okay because of magic temporaries. |
| 94 | test5_helper(&x); |
| 95 | |
| 96 | __autoreleasing id *a = &x; // expected-error {{initializing '__autoreleasing id *' with an expression of type '__strong id *' changes retain/release properties of pointer}} |
| 97 | |
| 98 | a = &x; // expected-error {{assigning '__strong id *' to '__autoreleasing id *' changes retain/release properties of pointer}} |
| 99 | |
| 100 | extern void test5_helper2(id const *); |
| 101 | test5_helper2(&x); |
| 102 | |
| 103 | extern void test5_helper3(__weak id *); // expected-note {{passing argument to parameter here}} |
| 104 | test5_helper3(&x); // expected-error {{passing '__strong id *' to parameter of type '__weak id *' changes retain/release properties of pointer}} |
| 105 | } |
| 106 | |
| 107 | // rdar://problem/8937869 |
| 108 | void test6(unsigned cond) { |
| 109 | switch (cond) { |
| 110 | case 0: |
| 111 | ; |
| 112 | id x; // expected-note {{jump bypasses initialization of retaining variable}} |
| 113 | |
| 114 | case 1: // expected-error {{switch case is in protected scope}} |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | @class NSError; |
| 120 | void test7(void) { |
| 121 | extern void test7_helper(NSError **); |
| 122 | NSError *err; |
| 123 | test7_helper(&err); |
| 124 | } |
| 125 | void test7_weak(void) { |
| 126 | extern void test7_helper(NSError **); |
| 127 | __weak NSError *err; |
| 128 | test7_helper(&err); |
| 129 | } |
| 130 | void test7_unsafe(void) { |
| 131 | extern void test7_helper(NSError **); // expected-note {{passing argument to parameter here}} |
| 132 | __unsafe_unretained NSError *err; |
| 133 | test7_helper(&err); // expected-error {{passing 'NSError *__unsafe_unretained *' to parameter of type 'NSError *__autoreleasing *' changes retain/release properties of pointer}} |
| 134 | } |
| 135 | |
| 136 | @class Test8_incomplete; |
| 137 | @interface Test8_complete @end; |
| 138 | @interface Test8_super @end; |
| 139 | @interface Test8 : Test8_super |
| 140 | - (id) init00; |
| 141 | - (id) init01; // expected-note {{declaration in interface}} \ |
| 142 | // expected-note{{overridden method}} |
| 143 | - (id) init02; // expected-note{{overridden method}} |
| 144 | - (id) init03; // covariance |
| 145 | - (id) init04; // covariance |
| 146 | - (id) init05; // expected-note{{overridden method}} |
| 147 | |
| 148 | - (void) init10; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}} |
| 149 | - (void) init11; |
| 150 | - (void) init12; |
| 151 | - (void) init13; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}} |
| 152 | - (void) init14; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}} |
| 153 | - (void) init15; |
| 154 | |
| 155 | // These should be invalid to actually call. |
| 156 | - (Test8_incomplete*) init20; |
| 157 | - (Test8_incomplete*) init21; // expected-note {{declaration in interface}} |
| 158 | - (Test8_incomplete*) init22; |
| 159 | - (Test8_incomplete*) init23; |
| 160 | - (Test8_incomplete*) init24; |
| 161 | - (Test8_incomplete*) init25; |
| 162 | |
| 163 | - (Test8_super*) init30; // id exception to covariance |
| 164 | - (Test8_super*) init31; // expected-note {{declaration in interface}} \ |
| 165 | // expected-note{{overridden method}} |
| 166 | - (Test8_super*) init32; // expected-note{{overridden method}} |
| 167 | - (Test8_super*) init33; |
| 168 | - (Test8_super*) init34; // covariance |
| 169 | - (Test8_super*) init35; // expected-note{{overridden method}} |
| 170 | |
| 171 | - (Test8*) init40; // id exception to covariance |
| 172 | - (Test8*) init41; // expected-note {{declaration in interface}} \ |
| 173 | // expected-note{{overridden method}} |
| 174 | - (Test8*) init42; // expected-note{{overridden method}} |
| 175 | - (Test8*) init43; // this should be a warning, but that's a general language thing, not an ARC thing |
| 176 | - (Test8*) init44; |
| 177 | - (Test8*) init45; // expected-note{{overridden method}} |
| 178 | |
| 179 | - (Test8_complete*) init50; // expected-error {{init methods must return a type related to the receiver type}} |
| 180 | - (Test8_complete*) init51; // expected-error {{init methods must return a type related to the receiver type}} |
| 181 | - (Test8_complete*) init52; // expected-error {{init methods must return a type related to the receiver type}} |
| 182 | - (Test8_complete*) init53; // expected-error {{init methods must return a type related to the receiver type}} |
| 183 | - (Test8_complete*) init54; // expected-error {{init methods must return a type related to the receiver type}} |
| 184 | - (Test8_complete*) init55; // expected-error {{init methods must return a type related to the receiver type}} |
| 185 | @end |
| 186 | @implementation Test8 |
| 187 | - (id) init00 { return 0; } |
| 188 | - (id) init10 { return 0; } // expected-error {{method implementation does not match its declaration}} |
| 189 | - (id) init20 { return 0; } |
| 190 | - (id) init30 { return 0; } |
| 191 | - (id) init40 { return 0; } |
| 192 | - (id) init50 { return 0; } |
| 193 | |
| 194 | - (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}} \ |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 195 | // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'void'}} |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 196 | - (void) init11 {} |
| 197 | - (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}} |
| 198 | - (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}} \ |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 199 | // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'void'}} |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 200 | - (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}} \ |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 201 | // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'void'}} |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 202 | - (void) init51 {} |
| 203 | |
| 204 | - (Test8_incomplete*) init02 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \ |
| 205 | // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_incomplete *'}} |
| 206 | - (Test8_incomplete*) init12 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} |
| 207 | - (Test8_incomplete*) init22 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} |
| 208 | - (Test8_incomplete*) init32 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \ |
| 209 | // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_incomplete *'}} |
| 210 | - (Test8_incomplete*) init42 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \ |
| 211 | // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_incomplete *'}} |
| 212 | - (Test8_incomplete*) init52 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} |
| 213 | |
| 214 | - (Test8_super*) init03 { return 0; } |
| 215 | - (Test8_super*) init13 { return 0; } // expected-error {{method implementation does not match its declaration}} |
| 216 | - (Test8_super*) init23 { return 0; } |
| 217 | - (Test8_super*) init33 { return 0; } |
| 218 | - (Test8_super*) init43 { return 0; } |
| 219 | - (Test8_super*) init53 { return 0; } |
| 220 | |
| 221 | - (Test8*) init04 { return 0; } |
| 222 | - (Test8*) init14 { return 0; } // expected-error {{method implementation does not match its declaration}} |
| 223 | - (Test8*) init24 { return 0; } |
| 224 | - (Test8*) init34 { return 0; } |
| 225 | - (Test8*) init44 { return 0; } |
| 226 | - (Test8*) init54 { return 0; } |
| 227 | |
| 228 | - (Test8_complete*) init05 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \ |
| 229 | // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_complete *'}} |
| 230 | - (Test8_complete*) init15 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} |
| 231 | - (Test8_complete*) init25 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} |
| 232 | - (Test8_complete*) init35 { 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_complete *'}} |
| 234 | - (Test8_complete*) init45 { 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_complete *'}} |
| 236 | - (Test8_complete*) init55 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} |
| 237 | @end |
| 238 | |
| 239 | @class Test9_incomplete; |
| 240 | @interface Test9 |
| 241 | - (Test9_incomplete*) init1; // expected-error {{init methods must return a type related to the receiver type}} |
| 242 | - (Test9_incomplete*) init2; |
| 243 | @end |
| 244 | id test9(Test9 *v) { |
| 245 | return [v init1]; |
| 246 | } |
| 247 | |
| 248 | // Test that the inference rules are different for fast enumeration variables. |
| 249 | void test10(id collection) { |
| 250 | for (id x in collection) { |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 251 | __strong id *ptr = &x; // expected-warning {{initializing '__strong id *' with an expression of type 'const __strong id *' discards qualifiers}} |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | for (__strong id x in collection) { |
| 255 | __weak id *ptr = &x; // expected-error {{initializing '__weak id *' with an expression of type '__strong id *' changes retain/release properties of pointer}} |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // rdar://problem/9078626 |
| 260 | #define nil ((void*) 0) |
| 261 | void test11(id op, void *vp) { |
| 262 | _Bool b; |
| 263 | b = (op == nil); |
| 264 | b = (nil == op); |
| 265 | |
| 266 | b = (vp == nil); |
| 267 | b = (nil == vp); |
| 268 | |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame^] | 269 | 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}} |
| 270 | b = (op == vp); // expected-error {{implicit conversion of C pointer type 'void *' to Objective-C pointer type 'id' requires a bridged cast}} expected-note {{use __bridge}} expected-note {{use CFBridgingRelease call}} |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | void test12(id collection) { |
| 274 | for (id x in collection) { |
| 275 | x = 0; // expected-error {{fast enumeration variables can't be modified in ARC by default; declare the variable __strong to allow this}} |
| 276 | } |
| 277 | |
| 278 | for (const id x in collection) { |
| 279 | x = 0; // expected-error {{read-only variable is not assignable}} |
| 280 | } |
| 281 | |
| 282 | for (__strong id x in collection) { |
| 283 | x = 0; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | @interface Test13 |
| 288 | - (id) init0; |
| 289 | - (void) noninit; |
| 290 | @end |
| 291 | @implementation Test13 |
| 292 | - (id) init0 { |
| 293 | self = 0; |
| 294 | } |
| 295 | - (void) noninit { |
| 296 | self = 0; // expected-error {{cannot assign to 'self' outside of a method in the init family}} |
| 297 | } |
| 298 | @end |
| 299 | |
Ted Kremenek | 3045ce7 | 2011-10-25 04:52:20 +0000 | [diff] [blame] | 300 | // <rdar://problem/10274056> |
| 301 | @interface Test13_B |
| 302 | - (id) consumesSelf __attribute__((ns_consumes_self)); |
| 303 | @end |
| 304 | @implementation Test13_B |
| 305 | - (id) consumesSelf { |
| 306 | self = 0; // no-warning |
| 307 | } |
| 308 | @end |
| 309 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 310 | // rdar://problem/9172151 |
| 311 | @class Test14A, Test14B; |
| 312 | void test14() { |
| 313 | extern void test14_consume(id *); |
| 314 | extern int test14_cond(void); |
| 315 | extern float test14_nowriteback(id __autoreleasing const *); // expected-note{{passing argument to parameter here}} |
| 316 | |
| 317 | Test14A *a; |
| 318 | Test14B *b; |
| 319 | id i; |
| 320 | id cla[10]; |
| 321 | id vla[test14_cond() + 10]; |
| 322 | |
| 323 | test14_consume((__strong id*) &a); |
| 324 | test14_consume((test14_cond() ? (__strong id*) &b : &i)); |
| 325 | test14_consume(test14_cond() ? 0 : &a); |
| 326 | test14_consume(test14_cond() ? (void*) 0 : (&a)); |
| 327 | test14_consume(cla); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}} |
| 328 | test14_consume(vla); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}} |
| 329 | test14_consume(&cla[5]); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}} |
| 330 | |
| 331 | __strong id *test14_indirect(void); |
| 332 | test14_consume(test14_indirect()); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}} |
| 333 | |
| 334 | extern id test14_global; |
| 335 | test14_consume(&test14_global); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}} |
| 336 | |
| 337 | extern __strong id *test14_global_ptr; |
| 338 | test14_consume(test14_global_ptr); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}} |
| 339 | |
| 340 | static id static_local; |
| 341 | test14_consume(&static_local); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}} |
| 342 | |
| 343 | __weak id* wip; |
| 344 | test14_nowriteback(&static_local); // okay, not a write-back. |
| 345 | test14_nowriteback(wip); // expected-error{{passing '__weak id *' to parameter of type '__autoreleasing id const *' changes retain/release properties of pointer}} |
| 346 | } |
| 347 | |
| 348 | void test15() { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 349 | __block __autoreleasing id x; // expected-error {{__block variables cannot have __autoreleasing ownership}} |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | struct Test16; |
| 353 | @interface Test16a |
| 354 | - (void) test16_0: (int) x; |
| 355 | - (int) test16_1: (int) x; // expected-note {{one possibility}} |
| 356 | - (int) test16_2: (int) x; // expected-note {{one possibility}} |
| 357 | - (id) test16_3: (int) x __attribute__((ns_returns_retained)); // expected-note {{one possibility}} |
| 358 | - (void) test16_4: (int) x __attribute__((ns_consumes_self)); // expected-note {{one possibility}} |
| 359 | - (void) test16_5: (id) __attribute__((ns_consumed)) x; // expected-note {{one possibility}} |
| 360 | - (void) test16_6: (id) x; |
| 361 | @end |
| 362 | |
| 363 | @interface Test16b |
| 364 | - (void) test16_0: (int) x; |
| 365 | - (int) test16_1: (char*) x; // expected-note {{also found}} |
| 366 | - (char*) test16_2: (int) x; // expected-note {{also found}} |
| 367 | - (id) test16_3: (int) x; // expected-note {{also found}} |
| 368 | - (void) test16_4: (int) x; // expected-note {{also found}} |
| 369 | - (void) test16_5: (id) x; // expected-note {{also found}} |
| 370 | - (void) test16_6: (struct Test16 *) x; |
| 371 | @end |
| 372 | |
| 373 | void test16(void) { |
| 374 | id v; |
| 375 | [v test16_0: 0]; |
| 376 | [v test16_1: 0]; // expected-error {{multiple methods named 'test16_1:' found with mismatched result, parameter type or attributes}} |
| 377 | [v test16_2: 0]; // expected-error {{multiple methods named}} |
| 378 | [v test16_3: 0]; // expected-error {{multiple methods named}} |
| 379 | [v test16_4: 0]; // expected-error {{multiple methods named}} |
| 380 | [v test16_5: 0]; // expected-error {{multiple methods named}} |
| 381 | [v test16_6: 0]; |
| 382 | } |
| 383 | |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 384 | @class Test17; // expected-note 2{{forward declaration of class here}} |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 385 | @protocol Test17p |
| 386 | - (void) test17; |
| 387 | + (void) test17; |
| 388 | @end |
| 389 | void test17(void) { |
| 390 | Test17 *v0; |
| 391 | [v0 test17]; // expected-error {{receiver type 'Test17' for instance message is a forward declaration}} |
| 392 | |
| 393 | Test17<Test17p> *v1; |
| 394 | [v1 test17]; // expected-error {{receiver type 'Test17<Test17p>' for instance message is a forward declaration}} |
| 395 | |
| 396 | [Test17 test17]; // expected-error {{receiver 'Test17' for class message is a forward declaration}} |
| 397 | } |
| 398 | |
| 399 | void test18(void) { |
| 400 | id x; |
| 401 | [x test18]; // expected-error {{no known instance method for selector 'test18'}} |
| 402 | } |
| 403 | |
| 404 | extern struct Test19 *test19a; |
| 405 | struct Test19 *const test19b = 0; |
| 406 | void test19(void) { |
| 407 | id x; |
| 408 | x = (id) test19a; // expected-error {{bridged cast}} \ |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 409 | // expected-note{{use __bridge to convert directly (no change in ownership)}} \ |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame^] | 410 | // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'struct Test19 *' into ARC}} |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 411 | x = (id) test19b; // expected-error {{bridged cast}} \ |
| 412 | // expected-note{{use __bridge to convert directly (no change in ownership)}} \ |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame^] | 413 | // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'struct Test19 *' into ARC}} |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | // rdar://problem/8951453 |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 417 | static __thread id test20_implicit; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}} |
| 418 | static __thread __strong id test20_strong; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}} |
| 419 | static __thread __weak id test20_weak; // expected-error {{thread-local variable has non-trivial ownership: type is '__weak id'}} |
| 420 | 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}} |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 421 | static __thread __unsafe_unretained id test20_unsafe; |
| 422 | void test20(void) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 423 | static __thread id test20_implicit; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}} |
| 424 | static __thread __strong id test20_strong; // expected-error {{thread-local variable has non-trivial ownership: type is '__strong id'}} |
| 425 | static __thread __weak id test20_weak; // expected-error {{thread-local variable has non-trivial ownership: type is '__weak id'}} |
| 426 | 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}} |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 427 | static __thread __unsafe_unretained id test20_unsafe; |
| 428 | } |
| 429 | |
| 430 | // rdar://9310049 |
| 431 | _Bool fn(id obj) { |
| 432 | return (_Bool)obj; |
| 433 | } |
| 434 | |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 435 | // Check casting w/ ownership qualifiers. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 436 | void test21() { |
| 437 | __strong id *sip; |
| 438 | (void)(__weak id *)sip; // expected-error{{casting '__strong id *' to type '__weak id *' changes retain/release properties of pointer}} |
| 439 | (void)(__weak const id *)sip; // expected-error{{casting '__strong id *' to type '__weak id const *' changes retain/release properties of pointer}} |
| 440 | (void)(__autoreleasing id *)sip; // expected-error{{casting '__strong id *' to type '__autoreleasing id *' changes retain/release properties of pointer}} |
| 441 | (void)(__autoreleasing const id *)sip; // okay |
| 442 | } |
| 443 | |
| 444 | // rdar://problem/9340462 |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 445 | void test22(id x[]) { // expected-error {{must explicitly describe intended ownership of an object array parameter}} |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | // rdar://problem/9400219 |
| 449 | void test23(void) { |
| 450 | void *ptr; |
| 451 | ptr = @"foo"; |
| 452 | ptr = (ptr ? @"foo" : 0); |
| 453 | ptr = (ptr ? @"foo" : @"bar"); |
| 454 | } |
| 455 | |
| 456 | id test24(void) { |
| 457 | extern void test24_helper(void); |
| 458 | return test24_helper(), (void*) 0; |
| 459 | } |
| 460 | |
| 461 | // rdar://9400841 |
| 462 | @interface Base |
| 463 | @property (assign) id content; |
| 464 | @end |
| 465 | |
| 466 | @interface Foo : Base |
| 467 | -(void)test; |
| 468 | @end |
| 469 | |
| 470 | @implementation Foo |
| 471 | -(void)test { |
| 472 | super.content = 0; |
| 473 | } |
| 474 | @end |
| 475 | |
| 476 | // <rdar://problem/9398437> |
| 477 | void test25(Class *classes) { |
| 478 | Class *other_classes; |
| 479 | test25(other_classes); |
| 480 | } |
| 481 | |
| 482 | void test26(id y) { |
| 483 | extern id test26_var1; |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 484 | __sync_swap(&test26_var1, 0, y); // expected-error {{cannot perform atomic operation on a pointer to type '__strong id': type has non-trivial ownership}} |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 485 | |
| 486 | extern __unsafe_unretained id test26_var2; |
| 487 | __sync_swap(&test26_var2, 0, y); |
| 488 | } |
| 489 | |
| 490 | @interface Test26 |
| 491 | - (id) init; |
| 492 | - (id) initWithInt: (int) x; |
| 493 | @end |
| 494 | @implementation Test26 |
| 495 | - (id) init { return self; } |
| 496 | - (id) initWithInt: (int) x { |
| 497 | [self init]; // expected-error {{the result of a delegate init call must be immediately returned or assigned to 'self'}} |
| 498 | return self; |
| 499 | } |
| 500 | @end |
| 501 | |
| 502 | // rdar://9525555 |
Argyrios Kyrtzidis | 473506b | 2011-07-26 21:48:26 +0000 | [diff] [blame] | 503 | @interface Test27 { |
| 504 | __weak id _myProp1; |
| 505 | id myProp2; |
| 506 | } |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 507 | @property id x; |
Fariborz Jahanian | f21a92d | 2011-11-08 20:58:53 +0000 | [diff] [blame] | 508 | @property (readonly) id ro; |
Argyrios Kyrtzidis | 0a68dc7 | 2011-07-12 04:30:16 +0000 | [diff] [blame] | 509 | @property (readonly) id custom_ro; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 510 | @property int y; |
Argyrios Kyrtzidis | 473506b | 2011-07-26 21:48:26 +0000 | [diff] [blame] | 511 | |
Fariborz Jahanian | f21a92d | 2011-11-08 20:58:53 +0000 | [diff] [blame] | 512 | @property (readonly) __weak id myProp1; |
Argyrios Kyrtzidis | 473506b | 2011-07-26 21:48:26 +0000 | [diff] [blame] | 513 | @property (readonly) id myProp2; |
| 514 | @property (readonly) __strong id myProp3; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 515 | @end |
| 516 | |
Argyrios Kyrtzidis | 0a68dc7 | 2011-07-12 04:30:16 +0000 | [diff] [blame] | 517 | @implementation Test27 |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 518 | @synthesize x; |
Fariborz Jahanian | f21a92d | 2011-11-08 20:58:53 +0000 | [diff] [blame] | 519 | @synthesize ro; |
Argyrios Kyrtzidis | 0a68dc7 | 2011-07-12 04:30:16 +0000 | [diff] [blame] | 520 | @synthesize y; |
Argyrios Kyrtzidis | 473506b | 2011-07-26 21:48:26 +0000 | [diff] [blame] | 521 | |
| 522 | @synthesize myProp1 = _myProp1; |
| 523 | @synthesize myProp2; |
| 524 | @synthesize myProp3; |
| 525 | |
Argyrios Kyrtzidis | 0a68dc7 | 2011-07-12 04:30:16 +0000 | [diff] [blame] | 526 | -(id)custom_ro { return 0; } |
| 527 | @end |
| 528 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 529 | // rdar://9569264 |
| 530 | @interface Test28 |
| 531 | @property (nonatomic, assign) __strong id a; // expected-error {{unsafe_unretained property 'a' may not also be declared __strong}} |
| 532 | @end |
| 533 | |
| 534 | @interface Test28 () |
| 535 | @property (nonatomic, assign) __strong id b; // expected-error {{unsafe_unretained property 'b' may not also be declared __strong}} |
| 536 | @end |
| 537 | |
| 538 | @implementation Test28 |
| 539 | @synthesize a; |
| 540 | @synthesize b; |
| 541 | @end |
| 542 | |
| 543 | // rdar://9573962 |
| 544 | typedef struct Bark Bark; |
| 545 | @interface Test29 |
| 546 | @property Bark* P; |
| 547 | @end |
| 548 | |
| 549 | @implementation Test29 |
| 550 | @synthesize P; |
| 551 | - (id)Meth { |
| 552 | Bark** f = &P; |
| 553 | return 0; |
| 554 | } |
| 555 | @end |
| 556 | |
| 557 | // rdar://9495837 |
| 558 | @interface Test30 |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 559 | + (id) new; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 560 | - (void)Meth; |
| 561 | @end |
| 562 | |
| 563 | @implementation Test30 |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 564 | + (id) new { return 0; } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 565 | - (void) Meth { |
Argyrios Kyrtzidis | d26fef0 | 2011-06-22 18:03:53 +0000 | [diff] [blame] | 566 | __weak id x = [Test30 new]; // expected-warning {{assigning retained object to weak variable}} |
| 567 | id __unsafe_unretained u = [Test30 new]; // expected-warning {{assigning retained object to unsafe_unretained variable}} |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 568 | id y = [Test30 new]; |
Argyrios Kyrtzidis | d26fef0 | 2011-06-22 18:03:53 +0000 | [diff] [blame] | 569 | x = [Test30 new]; // expected-warning {{assigning retained object to weak variable}} |
| 570 | u = [Test30 new]; // expected-warning {{assigning retained object to unsafe_unretained variable}} |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 571 | y = [Test30 new]; |
| 572 | } |
| 573 | @end |
| 574 | |
| 575 | // rdar://9411838 |
| 576 | @protocol PTest31 @end |
| 577 | |
| 578 | int Test31() { |
| 579 | Class cls; |
| 580 | id ids; |
| 581 | id<PTest31> pids; |
| 582 | Class<PTest31> pcls; |
| 583 | |
| 584 | int i = (ids->isa ? 1 : 0); // expected-error {{member reference base type 'id' is not a structure or union}} |
| 585 | int j = (pids->isa ? 1 : 0); // expected-error {{member reference base type 'id<PTest31>' is not a structure or union}} |
| 586 | int k = (pcls->isa ? i : j); // expected-error {{member reference base type 'Class<PTest31>' is not a structure or union}} |
| 587 | return cls->isa ? i : j; // expected-error {{member reference base type 'Class' is not a structure or union}} |
| 588 | } |
Fariborz Jahanian | b1f7d24 | 2011-06-16 17:29:56 +0000 | [diff] [blame] | 589 | |
| 590 | // rdar://9612030 |
| 591 | @interface ITest32 { |
| 592 | @public |
| 593 | id ivar; |
| 594 | } |
| 595 | @end |
| 596 | |
| 597 | id Test32(__weak ITest32 *x) { |
| 598 | __weak ITest32 *y; |
| 599 | x->ivar = 0; // expected-error {{dereferencing a __weak pointer is not allowed}} |
| 600 | return y ? y->ivar // expected-error {{dereferencing a __weak pointer is not allowed}} |
| 601 | : (*x).ivar; // expected-error {{dereferencing a __weak pointer is not allowed}} |
| 602 | } |
| 603 | |
Fariborz Jahanian | 8295b7b | 2011-06-22 16:36:45 +0000 | [diff] [blame] | 604 | // rdar://9619861 |
| 605 | extern int printf(const char*, ...); |
| 606 | typedef long intptr_t; |
| 607 | |
| 608 | int Test33(id someid) { |
| 609 | printf( "Hello%ld", (intptr_t)someid); |
| 610 | return (int)someid; |
| 611 | } |
| 612 | |
Fariborz Jahanian | 831fb96 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 613 | // rdar://9636091 |
| 614 | @interface I34 |
| 615 | @property (nonatomic, retain) id newName __attribute__((ns_returns_not_retained)) ; |
| 616 | |
| 617 | @property (nonatomic, retain) id newName1 __attribute__((ns_returns_not_retained)) ; |
| 618 | - (id) newName1 __attribute__((ns_returns_not_retained)); |
| 619 | |
| 620 | @property (nonatomic, retain) id newName2 __attribute__((ns_returns_not_retained)); // expected-note {{roperty declared here}} |
| 621 | - (id) newName2; // expected-warning {{property declared as returning non-retained objects; getter returning retained objects}} |
| 622 | @end |
| 623 | |
| 624 | @implementation I34 |
| 625 | @synthesize newName; |
| 626 | |
| 627 | @synthesize newName1; |
Fariborz Jahanian | 7898005 | 2011-08-01 22:39:49 +0000 | [diff] [blame] | 628 | - (id) newName1 { return 0; } |
Fariborz Jahanian | 831fb96 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 629 | |
Fariborz Jahanian | 7898005 | 2011-08-01 22:39:49 +0000 | [diff] [blame] | 630 | @synthesize newName2; |
Fariborz Jahanian | 831fb96 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 631 | @end |
| 632 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 633 | void test35(void) { |
| 634 | extern void test36_helper(id*); |
| 635 | id x; |
| 636 | __strong id *xp = 0; |
| 637 | |
| 638 | test36_helper(&x); |
| 639 | test36_helper(xp); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}} |
| 640 | |
| 641 | // rdar://problem/9665710 |
| 642 | __block id y; |
| 643 | test36_helper(&y); |
| 644 | ^{ test36_helper(&y); }(); |
Argyrios Kyrtzidis | 05d4876 | 2011-07-01 22:23:09 +0000 | [diff] [blame] | 645 | |
| 646 | __strong int non_objc_type; // expected-warning {{'__strong' only applies to objective-c object or block pointer types}} |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 647 | } |
Douglas Gregor | f96e904 | 2011-07-12 17:28:52 +0000 | [diff] [blame] | 648 | |
| 649 | void test36(int first, ...) { |
| 650 | // <rdar://problem/9758798> |
| 651 | __builtin_va_list arglist; |
| 652 | __builtin_va_start(arglist, first); |
| 653 | id obj = __builtin_va_arg(arglist, id); |
| 654 | __builtin_va_end(arglist); |
| 655 | } |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 656 | |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 657 | @class Test37; // expected-note{{forward declaration of class here}} |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 658 | void test37(Test37 *c) { |
| 659 | for (id y in c) { // expected-error {{collection expression type 'Test37' is a forward declaration}} |
| 660 | (void) y; |
| 661 | } |
Argyrios Kyrtzidis | 5b76f37 | 2011-09-20 23:49:22 +0000 | [diff] [blame] | 662 | |
| 663 | (void)sizeof(id*); // no error. |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 664 | } |
John McCall | a8e0cd8 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 665 | |
| 666 | // rdar://problem/9887979 |
| 667 | @interface Test38 |
| 668 | @property int value; |
| 669 | @end |
| 670 | void test38() { |
| 671 | extern Test38 *test38_helper(void); |
| 672 | switch (test38_helper().value) { |
| 673 | case 0: |
| 674 | case 1: |
| 675 | ; |
| 676 | } |
| 677 | } |
Fariborz Jahanian | 175fb10 | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 678 | |
| 679 | // rdar://10186536 |
| 680 | @class NSColor; |
| 681 | void _NSCalc(NSColor* color, NSColor* bezelColors[]) __attribute__((unavailable("not available in automatic reference counting mode"))); |
| 682 | |
| 683 | void _NSCalcBeze(NSColor* color, NSColor* bezelColors[]); // expected-error {{must explicitly describe intended ownership of an object array parameter}} |
Fariborz Jahanian | a1de8cb | 2011-10-14 17:34:08 +0000 | [diff] [blame] | 684 | |
| 685 | // rdar://9970739 |
| 686 | @interface RestaurantTableViewCell |
| 687 | - (void) restaurantLocation; |
| 688 | @end |
| 689 | |
| 690 | @interface Radar9970739 |
| 691 | - (void) Meth; |
| 692 | @end |
| 693 | |
| 694 | @implementation Radar9970739 |
| 695 | - (void) Meth { |
| 696 | RestaurantTableViewCell *cell; |
| 697 | [cell restaurantLocatoin]; // expected-error {{no visible @interface for 'RestaurantTableViewCell' declares the selector 'restaurantLocatoin'}} |
| 698 | } |
| 699 | @end |
| 700 | |