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