arc-repeated-weak.mm
14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
// RUN: %clang_cc1 -fsyntax-only -fobjc-runtime-has-weak -fobjc-arc -fblocks -Wno-objc-root-class -std=c++11 -Warc-repeated-use-of-weak -verify %s
// RUN: %clang_cc1 -fsyntax-only -fobjc-runtime-has-weak -fobjc-weak -fblocks -Wno-objc-root-class -std=c++11 -Warc-repeated-use-of-weak -verify %s
@interface Test {
@public
Test *ivar;
__weak id weakIvar;
}
@property(weak) Test *weakProp;
@property(strong) Test *strongProp;
- (__weak id)implicitProp;
+ (__weak id)weakProp;
@end
extern void use(id);
extern id get();
extern bool condition();
#define nil ((id)0)
void sanity(Test *a) {
use(a.weakProp); // expected-warning{{weak property 'weakProp' is accessed multiple times in this function but may be unpredictably set to nil; assign to a strong variable to keep the object alive}}
use(a.weakProp); // expected-note{{also accessed here}}
use(a.strongProp);
use(a.strongProp); // no-warning
use(a.weakProp); // expected-note{{also accessed here}}
}
void singleUse(Test *a) {
use(a.weakProp); // no-warning
use(a.strongProp); // no-warning
}
void assignsOnly(Test *a) {
a.weakProp = get(); // no-warning
id next = get();
if (next)
a.weakProp = next; // no-warning
a->weakIvar = get(); // no-warning
next = get();
if (next)
a->weakIvar = next; // no-warning
extern __weak id x;
x = get(); // no-warning
next = get();
if (next)
x = next; // no-warning
}
void assignThenRead(Test *a) {
a.weakProp = get(); // expected-note{{also accessed here}}
use(a.weakProp); // expected-warning{{weak property 'weakProp' is accessed multiple times}}
}
void twoVariables(Test *a, Test *b) {
use(a.weakProp); // no-warning
use(b.weakProp); // no-warning
}
void doubleLevelAccess(Test *a) {
use(a.strongProp.weakProp); // expected-warning{{weak property 'weakProp' may be accessed multiple times in this function and may be unpredictably set to nil; assign to a strong variable to keep the object alive}}
use(a.strongProp.weakProp); // expected-note{{also accessed here}}
}
void doubleLevelAccessIvar(Test *a) {
use(a.strongProp.weakProp); // expected-warning{{weak property 'weakProp' may be accessed multiple times}}
use(a.strongProp.weakProp); // expected-note{{also accessed here}}
}
void implicitProperties(Test *a) {
use(a.implicitProp); // expected-warning{{weak implicit property 'implicitProp' is accessed multiple times}}
use(a.implicitProp); // expected-note{{also accessed here}}
}
void classProperties() {
use(Test.weakProp); // expected-warning{{weak implicit property 'weakProp' is accessed multiple times}}
use(Test.weakProp); // expected-note{{also accessed here}}
}
void classPropertiesAreDifferent(Test *a) {
use(Test.weakProp); // no-warning
use(a.weakProp); // no-warning
use(a.strongProp.weakProp); // no-warning
}
void ivars(Test *a) {
use(a->weakIvar); // expected-warning{{weak instance variable 'weakIvar' is accessed multiple times}}
use(a->weakIvar); // expected-note{{also accessed here}}
}
void globals() {
extern __weak id a;
use(a); // expected-warning{{weak variable 'a' is accessed multiple times}}
use(a); // expected-note{{also accessed here}}
}
void messageGetter(Test *a) {
use([a weakProp]); // expected-warning{{weak property 'weakProp' is accessed multiple times}}
use([a weakProp]); // expected-note{{also accessed here}}
}
void messageSetter(Test *a) {
[a setWeakProp:get()]; // no-warning
[a setWeakProp:get()]; // no-warning
}
void messageSetterAndGetter(Test *a) {
[a setWeakProp:get()]; // expected-note{{also accessed here}}
use([a weakProp]); // expected-warning{{weak property 'weakProp' is accessed multiple times}}
}
void mixDotAndMessageSend(Test *a, Test *b) {
use(a.weakProp); // expected-warning{{weak property 'weakProp' is accessed multiple times}}
use([a weakProp]); // expected-note{{also accessed here}}
use([b weakProp]); // expected-warning{{weak property 'weakProp' is accessed multiple times}}
use(b.weakProp); // expected-note{{also accessed here}}
}
void assignToStrongWrongInit(Test *a) {
id val = a.weakProp; // expected-note{{also accessed here}}
use(a.weakProp); // expected-warning{{weak property 'weakProp' is accessed multiple times}}
}
void assignToStrongWrong(Test *a) {
id val;
val = a.weakProp; // expected-note{{also accessed here}}
use(a.weakProp); // expected-warning{{weak property 'weakProp' is accessed multiple times}}
}
void assignToIvarWrong(Test *a) {
a->weakIvar = get(); // expected-note{{also accessed here}}
use(a->weakIvar); // expected-warning{{weak instance variable 'weakIvar' is accessed multiple times}}
}
void assignToGlobalWrong() {
extern __weak id a;
a = get(); // expected-note{{also accessed here}}
use(a); // expected-warning{{weak variable 'a' is accessed multiple times}}
}
void assignToStrongOK(Test *a) {
if (condition()) {
id val = a.weakProp; // no-warning
(void)val;
} else {
id val;
val = a.weakProp; // no-warning
(void)val;
}
}
void assignToStrongConditional(Test *a) {
id val = (condition() ? a.weakProp : a.weakProp); // no-warning
id val2 = a.implicitProp ?: a.implicitProp; // no-warning
}
void testBlock(Test *a) {
use(a.weakProp); // no-warning
use(^{
use(a.weakProp); // expected-warning{{weak property 'weakProp' is accessed multiple times in this block}}
use(a.weakProp); // expected-note{{also accessed here}}
});
}
void assignToStrongWithCasts(Test *a) {
if (condition()) {
Test *val = (Test *)a.weakProp; // no-warning
(void)val;
} else {
id val;
val = (Test *)a.weakProp; // no-warning
(void)val;
}
}
void assignToStrongWithMessages(Test *a) {
if (condition()) {
id val = [a weakProp]; // no-warning
(void)val;
} else {
id val;
val = [a weakProp]; // no-warning
(void)val;
}
}
void assignAfterRead(Test *a) {
// Special exception for a single read before any writes.
if (!a.weakProp) // no-warning
a.weakProp = get(); // no-warning
}
void readOnceWriteMany(Test *a) {
if (!a.weakProp) { // no-warning
a.weakProp = get(); // no-warning
a.weakProp = get(); // no-warning
}
}
void readOnceAfterWrite(Test *a) {
a.weakProp = get(); // expected-note{{also accessed here}}
if (!a.weakProp) { // expected-warning{{weak property 'weakProp' is accessed multiple times in this function}}
a.weakProp = get(); // expected-note{{also accessed here}}
}
}
void readOnceWriteManyLoops(Test *a, Test *b, Test *c, Test *d, Test *e) {
while (condition()) {
if (!a.weakProp) { // expected-warning{{weak property 'weakProp' is accessed multiple times in this function}}
a.weakProp = get(); // expected-note{{also accessed here}}
a.weakProp = get(); // expected-note{{also accessed here}}
}
}
do {
if (!b.weakProp) { // expected-warning{{weak property 'weakProp' is accessed multiple times in this function}}
b.weakProp = get(); // expected-note{{also accessed here}}
b.weakProp = get(); // expected-note{{also accessed here}}
}
} while (condition());
for (id x = get(); x; x = get()) {
if (!c.weakProp) { // expected-warning{{weak property 'weakProp' is accessed multiple times in this function}}
c.weakProp = get(); // expected-note{{also accessed here}}
c.weakProp = get(); // expected-note{{also accessed here}}
}
}
for (id x in get()) {
if (!d.weakProp) { // expected-warning{{weak property 'weakProp' is accessed multiple times in this function}}
d.weakProp = get(); // expected-note{{also accessed here}}
d.weakProp = get(); // expected-note{{also accessed here}}
}
}
int array[] = { 1, 2, 3 };
for (int i : array) {
if (!e.weakProp) { // expected-warning{{weak property 'weakProp' is accessed multiple times in this function}}
e.weakProp = get(); // expected-note{{also accessed here}}
e.weakProp = get(); // expected-note{{also accessed here}}
}
}
}
void readOnlyLoop(Test *a) {
while (condition()) {
use(a.weakProp); // expected-warning{{weak property 'weakProp' is accessed multiple times in this function}}
}
}
void readInIterationLoop() {
for (Test *a in get())
use(a.weakProp); // no-warning
}
void readDoubleLevelAccessInLoop() {
for (Test *a in get()) {
use(a.strongProp.weakProp); // no-warning
}
}
void readParameterInLoop(Test *a) {
for (id unused in get()) {
use(a.weakProp); // expected-warning{{weak property 'weakProp' is accessed multiple times in this function}}
(void)unused;
}
}
void readGlobalInLoop() {
static __weak id a;
for (id unused in get()) {
use(a); // expected-warning{{weak variable 'a' is accessed multiple times in this function}}
(void)unused;
}
}
void doWhileLoop(Test *a) {
do {
use(a.weakProp); // no-warning
} while(0);
}
@interface Test (Methods)
@end
@implementation Test (Methods)
- (void)sanity {
use(self.weakProp); // expected-warning{{weak property 'weakProp' is accessed multiple times in this method but may be unpredictably set to nil; assign to a strong variable to keep the object alive}}
use(self.weakProp); // expected-note{{also accessed here}}
}
- (void)ivars {
use(weakIvar); // expected-warning{{weak instance variable 'weakIvar' is accessed multiple times in this method but may be unpredictably set to nil; assign to a strong variable to keep the object alive}}
use(weakIvar); // expected-note{{also accessed here}}
}
- (void)doubleLevelAccessForSelf {
use(self.strongProp.weakProp); // expected-warning{{weak property 'weakProp' is accessed multiple times}}
use(self.strongProp.weakProp); // expected-note{{also accessed here}}
use(self->ivar.weakProp); // expected-warning{{weak property 'weakProp' is accessed multiple times}}
use(self->ivar.weakProp); // expected-note{{also accessed here}}
use(self->ivar->weakIvar); // expected-warning{{weak instance variable 'weakIvar' is accessed multiple times}}
use(self->ivar->weakIvar); // expected-note{{also accessed here}}
}
- (void)distinctFromOther:(Test *)other {
use(self.strongProp.weakProp); // no-warning
use(other.strongProp.weakProp); // no-warning
use(self->ivar.weakProp); // no-warning
use(other->ivar.weakProp); // no-warning
use(self.strongProp->weakIvar); // no-warning
use(other.strongProp->weakIvar); // no-warning
}
@end
@interface Base1
@end
@interface Sub1 : Base1
@end
@interface Sub1(cat)
-(id)prop;
@end
void test1(Sub1 *s) {
use([s prop]);
use([s prop]);
}
@interface Base1(cat)
@property (weak) id prop;
@end
void test2(Sub1 *s) {
// This does not warn because the "prop" in "Base1(cat)" was introduced
// after the method declaration and we don't find it as overridden.
// Always looking for overridden methods after the method declaration is expensive
// and it's not clear it is worth it currently.
use([s prop]);
use([s prop]);
}
class Wrapper {
Test *a;
public:
void fields() {
use(a.weakProp); // expected-warning{{weak property 'weakProp' is accessed multiple times in this function but may be unpredictably set to nil; assign to a strong variable to keep the object alive}}
use(a.weakProp); // expected-note{{also accessed here}}
}
void distinctFromOther(Test *b, const Wrapper &w) {
use(a.weakProp); // no-warning
use(b.weakProp); // no-warning
use(w.a.weakProp); // no-warning
}
static void doubleLevelAccessField(const Wrapper &x, const Wrapper &y) {
use(x.a.weakProp); // expected-warning{{weak property 'weakProp' may be accessed multiple times}}
use(y.a.weakProp); // expected-note{{also accessed here}}
}
};
// -----------------------
// False positives
// -----------------------
// Most of these would require flow-sensitive analysis to silence correctly.
void assignNil(Test *a) {
if (condition())
a.weakProp = nil; // expected-note{{also accessed here}}
use(a.weakProp); // expected-warning{{weak property 'weakProp' is accessed multiple times}}
}
void branch(Test *a) {
if (condition())
use(a.weakProp); // expected-warning{{weak property 'weakProp' is accessed multiple times}}
else
use(a.weakProp); // expected-note{{also accessed here}}
}
void doubleLevelAccess(Test *a, Test *b) {
use(a.strongProp.weakProp); // expected-warning{{weak property 'weakProp' may be accessed multiple times}}
use(b.strongProp.weakProp); // expected-note{{also accessed here}}
use(a.weakProp.weakProp); // no-warning
}
void doubleLevelAccessIvar(Test *a, Test *b) {
use(a->ivar.weakProp); // expected-warning{{weak property 'weakProp' may be accessed multiple times}}
use(b->ivar.weakProp); // expected-note{{also accessed here}}
use(a.strongProp.weakProp); // no-warning
}
// rdar://13942025
@interface X
@end
@implementation X
- (int) warningAboutWeakVariableInsideTypeof {
__typeof__(self) __weak weakSelf = self;
^(){
__typeof__(weakSelf) blockSelf = weakSelf;
use(blockSelf);
}();
return sizeof(weakSelf);
}
@end
// rdar://19053620
@interface NSNull
+ (NSNull *)null;
@end
@interface INTF @end
@implementation INTF
- (void) Meth : (id) data
{
data = data ?: NSNull.null;
}
@end
// This used to crash in WeakObjectProfileTy::getBaseInfo when getBase() was
// called on an ObjCPropertyRefExpr object whose receiver was an interface.
@class NSString;
@interface NSBundle
+(NSBundle *)foo;
@property (class, strong) NSBundle *foo2;
@property (strong) NSString *prop;
@property(weak) NSString *weakProp;
@end
@interface NSBundle2 : NSBundle
@end
void foo() {
NSString * t = NSBundle.foo.prop;
use(NSBundle.foo.weakProp); // expected-warning{{weak property 'weakProp' may be accessed multiple times}}
use(NSBundle2.foo.weakProp); // expected-note{{also accessed here}}
NSString * t2 = NSBundle.foo2.prop;
use(NSBundle.foo2.weakProp); // expected-warning{{weak property 'weakProp' may be accessed multiple times}}
use(NSBundle2.foo2.weakProp); // expected-note{{also accessed here}}
decltype([NSBundle2.foo2 weakProp]) t3;
decltype(NSBundle2.foo2.weakProp) t4;
__typeof__(NSBundle2.foo2.weakProp) t5;
}
// This used to crash in the constructor of WeakObjectProfileTy when a
// DeclRefExpr was passed that didn't reference a VarDecl.
typedef INTF * INTFPtrTy;
enum E {
e1
};
void foo1() {
INTFPtrTy tmp = (INTFPtrTy)e1;
#if __has_feature(objc_arc)
// expected-error@-2{{cast of 'E' to 'INTFPtrTy' (aka 'INTF *') is disallowed with ARC}}
#endif
}
@class NSString;
static NSString* const kGlobal = @"";
@interface NSDictionary
- (id)objectForKeyedSubscript:(id)key;
@end
@interface WeakProp
@property (weak) NSDictionary *nd;
@end
@implementation WeakProp
-(void)m {
(void)self.nd[@""]; // no warning
}
@end