ivars.m
3.79 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
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -analyzer-store=region -fblocks -verify -Wno-objc-root-class -analyzer-config eagerly-assume=false %s
void clang_analyzer_eval(int);
@interface Root {
@public
int uniqueID;
}
- (void)refreshID;
@end
void testInvalidation(Root *obj) {
int savedID = obj->uniqueID;
clang_analyzer_eval(savedID == obj->uniqueID); // expected-warning{{TRUE}}
[obj refreshID];
clang_analyzer_eval(savedID == obj->uniqueID); // expected-warning{{UNKNOWN}}
}
@interface Child : Root
@end
@implementation Child
- (void)testSuperInvalidation {
int savedID = self->uniqueID;
clang_analyzer_eval(savedID == self->uniqueID); // expected-warning{{TRUE}}
[super refreshID];
clang_analyzer_eval(savedID == self->uniqueID); // expected-warning{{UNKNOWN}}
}
@end
@interface ManyIvars {
struct S { int a, b; } s;
int c;
int d;
}
@end
struct S makeS();
@implementation ManyIvars
- (void)testMultipleIvarInvalidation:(int)useConstraints {
if (useConstraints) {
if (s.a != 1) return;
if (s.b != 2) return;
if (c != 3) return;
if (d != 4) return;
return;
} else {
s.a = 1;
s.b = 2;
c = 3;
d = 4;
}
clang_analyzer_eval(s.a == 1); // expected-warning{{TRUE}}
clang_analyzer_eval(s.b == 2); // expected-warning{{TRUE}}
clang_analyzer_eval(c == 3); // expected-warning{{TRUE}}
clang_analyzer_eval(d == 4); // expected-warning{{TRUE}}
d = 0;
clang_analyzer_eval(s.a == 1); // expected-warning{{TRUE}}
clang_analyzer_eval(s.b == 2); // expected-warning{{TRUE}}
clang_analyzer_eval(c == 3); // expected-warning{{TRUE}}
clang_analyzer_eval(d == 0); // expected-warning{{TRUE}}
d = 4;
s = makeS();
clang_analyzer_eval(s.a == 1); // expected-warning{{UNKNOWN}}
clang_analyzer_eval(s.b == 2); // expected-warning{{UNKNOWN}}
clang_analyzer_eval(c == 3); // expected-warning{{TRUE}}
clang_analyzer_eval(d == 4); // expected-warning{{TRUE}}
s.a = 1;
clang_analyzer_eval(s.a == 1); // expected-warning{{TRUE}}
clang_analyzer_eval(s.b == 2); // expected-warning{{UNKNOWN}}
clang_analyzer_eval(c == 3); // expected-warning{{TRUE}}
clang_analyzer_eval(d == 4); // expected-warning{{TRUE}}
}
+ (void)testMultipleIvarInvalidation:(int)useConstraints
forObject:(ManyIvars *)obj {
if (useConstraints) {
if (obj->s.a != 1) return;
if (obj->s.b != 2) return;
if (obj->c != 3) return;
if (obj->d != 4) return;
return;
} else {
obj->s.a = 1;
obj->s.b = 2;
obj->c = 3;
obj->d = 4;
}
clang_analyzer_eval(obj->s.a == 1); // expected-warning{{TRUE}}
clang_analyzer_eval(obj->s.b == 2); // expected-warning{{TRUE}}
clang_analyzer_eval(obj->c == 3); // expected-warning{{TRUE}}
clang_analyzer_eval(obj->d == 4); // expected-warning{{TRUE}}
obj->d = 0;
clang_analyzer_eval(obj->s.a == 1); // expected-warning{{TRUE}}
clang_analyzer_eval(obj->s.b == 2); // expected-warning{{TRUE}}
clang_analyzer_eval(obj->c == 3); // expected-warning{{TRUE}}
clang_analyzer_eval(obj->d == 0); // expected-warning{{TRUE}}
obj->d = 4;
obj->s = makeS();
clang_analyzer_eval(obj->s.a == 1); // expected-warning{{UNKNOWN}}
clang_analyzer_eval(obj->s.b == 2); // expected-warning{{UNKNOWN}}
clang_analyzer_eval(obj->c == 3); // expected-warning{{TRUE}}
clang_analyzer_eval(obj->d == 4); // expected-warning{{TRUE}}
obj->s.a = 1;
clang_analyzer_eval(obj->s.a == 1); // expected-warning{{TRUE}}
clang_analyzer_eval(obj->s.b == 2); // expected-warning{{UNKNOWN}}
clang_analyzer_eval(obj->c == 3); // expected-warning{{TRUE}}
clang_analyzer_eval(obj->d == 4); // expected-warning{{TRUE}}
}
@end
int testNull(Root *obj) {
if (obj) return 0;
int *x = &obj->uniqueID;
return *x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
}