blob: 0c77f892e1a401c6154abc34521d0adadbcfb826 (
plain)
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
|
// RUN: %clang_cc1 -std=gnu++11 -Wsometimes-uninitialized -verify %s
bool maybe();
int test_if_false(bool b) {
int x; // expected-note {{variable}}
if (b) x = 1; // expected-note {{whenever 'if' condition is false}}
return x; // expected-warning {{sometimes uninit}}
}
int test_if_true(bool b) {
int x; // expected-note {{variable}}
if (b) {} // expected-note {{whenever 'if' condition is true}}
else x = 1;
return x; // expected-warning {{sometimes uninit}}
}
int test_while_false(bool b) {
int x; // expected-note {{variable}}
while (b) { // expected-note {{whenever 'while' loop exits because its condition is false}}
if (maybe()) {
x = 1;
break;
}
};
return x; // expected-warning {{sometimes uninit}}
}
int test_while_true(bool b) {
int x; // expected-note {{variable}}
while (b) { // expected-note {{whenever 'while' loop is entered}}
label:
return x; // expected-warning {{sometimes uninit}}
}
x = 0;
goto label;
}
int test_do_while_false(bool b) {
int x; // expected-note {{variable}}
do {
if (maybe()) {
x = 1;
break;
}
} while (b); // expected-note {{whenever 'do' loop exits because its condition is false}}
return x; // expected-warning {{sometimes uninit}}
}
int test_do_while_true(bool b) {
int x; // expected-note {{variable}}
goto label2;
do {
label1:
return x; // expected-warning {{sometimes uninit}}
label2: ;
} while (b); // expected-note {{whenever 'do' loop condition is true}}
x = 0;
goto label1;
}
int test_for_false(int k) {
int x; // expected-note {{variable}}
for (int n = 0;
n < k; // expected-note {{whenever 'for' loop exits because its condition is false}}
++n) {
if (maybe()) {
x = n;
break;
}
}
return x; // expected-warning {{sometimes uninit}}
}
int test_for_true(int k) {
int x; // expected-note {{variable}}
int n = 0;
for (;
n < k; // expected-note {{whenever 'for' loop is entered}}
++n) {
label:
return x; // expected-warning {{sometimes uninit}}
}
x = 1;
goto label;
}
int test_for_range_false(int k) {
int arr[3] = { 1, 2, 3 };
int x; // expected-note {{variable}}
for (int &a : arr) { // expected-note {{whenever 'for' loop exits because its condition is false}}
if (a == k) {
x = &a - arr;
break;
}
}
return x; // expected-warning {{sometimes uninit}}
}
int test_for_range_true(int k) {
int arr[3] = { 1, 2, 3 };
int x; // expected-note {{variable}}
for (int &a : arr) { // expected-note {{whenever 'for' loop is entered}}
goto label;
}
x = 0;
label:
return x; // expected-warning {{sometimes uninit}}
}
int test_conditional_false(int k) {
int x; // expected-note {{variable}}
(void)(
maybe() // expected-note {{whenever '?:' condition is false}}
? x = 1 : 0);
return x; // expected-warning {{sometimes uninit}}
}
int test_conditional_true(int k) {
int x; // expected-note {{variable}}
(void)(
maybe() // expected-note {{whenever '?:' condition is true}}
? 0 : x = 1);
return x; // expected-warning {{sometimes uninit}}
}
int test_logical_and_false(int k) {
int x; // expected-note {{variable}}
maybe() // expected-note {{whenever '&&' condition is false}}
&& (x = 1);
return x; // expected-warning {{sometimes uninit}}
}
int test_logical_and_true(int k) {
int x; // expected-note {{variable}}
maybe() // expected-note {{whenever '&&' condition is true}}
&& ({ goto skip_init; 0; });
x = 1;
skip_init:
return x; // expected-warning {{sometimes uninit}}
}
int test_logical_or_false(int k) {
int x; // expected-note {{variable}}
maybe() // expected-note {{whenever '||' condition is false}}
|| ({ goto skip_init; 0; });
x = 1;
skip_init:
return x; // expected-warning {{sometimes uninit}}
}
int test_logical_or_true(int k) {
int x; // expected-note {{variable}}
maybe() // expected-note {{whenever '||' condition is true}}
|| (x = 1);
return x; // expected-warning {{sometimes uninit}}
}
int test_switch_case(int k) {
int x; // expected-note {{variable}}
switch (k) {
case 0:
x = 0;
break;
case 1: // expected-note {{whenever switch case is taken}}
break;
}
return x; // expected-warning {{sometimes uninit}}
}
int test_switch_default(int k) {
int x; // expected-note {{variable}}
switch (k) {
case 0:
x = 0;
break;
case 1:
x = 1;
break;
default: // expected-note {{whenever switch default is taken}}
break;
}
return x; // expected-warning {{sometimes uninit}}
}
int test_switch_suppress_1(int k) {
int x;
switch (k) {
case 0:
x = 0;
break;
case 1:
x = 1;
break;
}
return x; // no-warning
}
int test_switch_suppress_2(int k) {
int x;
switch (k) {
case 0:
case 1:
switch (k) {
case 0:
return 0;
case 1:
return 1;
}
case 2:
case 3:
x = 1;
}
return x; // no-warning
}
int test_multiple_notes(int k) {
int x; // expected-note {{variable}}
if (k > 0) {
if (k == 5)
x = 1;
else if (k == 2) // expected-note {{whenever 'if' condition is false}}
x = 2;
} else {
if (k == -5)
x = 3;
else if (k == -2) // expected-note {{whenever 'if' condition is false}}
x = 4;
}
return x; // expected-warning {{sometimes uninit}}
}
int test_no_false_positive_1(int k) {
int x;
if (k)
x = 5;
while (!k)
maybe();
return x;
}
int test_no_false_positive_2() {
int x;
bool b = false;
if (maybe()) {
x = 5;
b = true;
}
return b ? x : 0;
}
void test_null_pred_succ() {
int x; // expected-note {{variable}}
if (0) // expected-note {{whenever}}
foo: x = 0;
if (x) // expected-warning {{sometimes uninit}}
goto foo;
}
|