blob: 015b675d9b86e13fe7c99f8a9ed814a4e6fee33d [file] [log] [blame]
Richard Smith2815e1a2012-05-25 02:17:09 +00001// RUN: %clang_cc1 -std=gnu++11 -Wsometimes-uninitialized -verify %s
Richard Smithbdb97ff2012-05-26 06:20:46 +00002// RUN: %clang_cc1 -std=gnu++11 -Wsometimes-uninitialized -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
Richard Smith2815e1a2012-05-25 02:17:09 +00003
4bool maybe();
5
6int test_if_false(bool b) {
7 int x; // expected-note {{variable}}
Richard Smithbdb97ff2012-05-26 06:20:46 +00008 if (b) // expected-warning {{whenever 'if' condition is false}} \
9 // expected-note {{remove the 'if' if its condition is always true}}
10 x = 1;
11 return x; // expected-note {{uninitialized use}}
Richard Smith2815e1a2012-05-25 02:17:09 +000012}
13
Richard Smithbdb97ff2012-05-26 06:20:46 +000014// CHECK: fix-it:"{{.*}}":{8:3-10:5}:""
15// CHECK: fix-it:"{{.*}}":{7:8-7:8}:" = 0"
16
17
Richard Smith2815e1a2012-05-25 02:17:09 +000018int test_if_true(bool b) {
19 int x; // expected-note {{variable}}
Richard Smithbdb97ff2012-05-26 06:20:46 +000020 if (b) {} // expected-warning {{whenever 'if' condition is true}} \
21 // expected-note {{remove the 'if' if its condition is always false}}
Richard Smith2815e1a2012-05-25 02:17:09 +000022 else x = 1;
Richard Smithbdb97ff2012-05-26 06:20:46 +000023 return x; // expected-note {{uninitialized use}}
Richard Smith2815e1a2012-05-25 02:17:09 +000024}
25
Richard Smithbdb97ff2012-05-26 06:20:46 +000026// CHECK: fix-it:"{{.*}}":{20:3-22:8}:""
27// CHECK: fix-it:"{{.*}}":{19:8-19:8}:" = 0"
28
29
Richard Smith2815e1a2012-05-25 02:17:09 +000030int test_while_false(bool b) {
31 int x; // expected-note {{variable}}
Richard Smithbdb97ff2012-05-26 06:20:46 +000032 while (b) { // expected-warning {{whenever 'while' loop exits because its condition is false}} \
33 // expected-note {{remove the condition if it is always true}}
Richard Smith2815e1a2012-05-25 02:17:09 +000034 if (maybe()) {
35 x = 1;
36 break;
37 }
38 };
Richard Smithbdb97ff2012-05-26 06:20:46 +000039 return x; // expected-note {{uninitialized use}}
Richard Smith2815e1a2012-05-25 02:17:09 +000040}
41
Richard Smithbdb97ff2012-05-26 06:20:46 +000042// CHECK: fix-it:"{{.*}}":{32:10-32:11}:"true"
43// CHECK: fix-it:"{{.*}}":{31:8-31:8}:" = 0"
44
45
Richard Smith2815e1a2012-05-25 02:17:09 +000046int test_while_true(bool b) {
47 int x; // expected-note {{variable}}
Richard Smithbdb97ff2012-05-26 06:20:46 +000048 while (b) { // expected-warning {{whenever 'while' loop is entered}} \
49 // expected-note {{remove the condition if it is always false}}
Richard Smith2815e1a2012-05-25 02:17:09 +000050label:
Richard Smithbdb97ff2012-05-26 06:20:46 +000051 return x; // expected-note {{uninitialized use}}
Richard Smith2815e1a2012-05-25 02:17:09 +000052 }
53 x = 0;
54 goto label;
55}
56
Richard Smithbdb97ff2012-05-26 06:20:46 +000057// CHECK: fix-it:"{{.*}}":{48:10-48:11}:"false"
58// CHECK: fix-it:"{{.*}}":{47:8-47:8}:" = 0"
59
60
Richard Smith2815e1a2012-05-25 02:17:09 +000061int test_do_while_false(bool b) {
62 int x; // expected-note {{variable}}
63 do {
64 if (maybe()) {
65 x = 1;
66 break;
67 }
Richard Smithbdb97ff2012-05-26 06:20:46 +000068 } while (b); // expected-warning {{whenever 'do' loop exits because its condition is false}} \
69 // expected-note {{remove the condition if it is always true}}
70 return x; // expected-note {{uninitialized use}}
Richard Smith2815e1a2012-05-25 02:17:09 +000071}
72
Richard Smithbdb97ff2012-05-26 06:20:46 +000073// CHECK: fix-it:"{{.*}}":{68:12-68:13}:"true"
74// CHECK: fix-it:"{{.*}}":{62:8-62:8}:" = 0"
75
76
Richard Smith2815e1a2012-05-25 02:17:09 +000077int test_do_while_true(bool b) {
78 int x; // expected-note {{variable}}
79goto label2;
80 do {
81label1:
Richard Smithbdb97ff2012-05-26 06:20:46 +000082 return x; // expected-note {{uninitialized use}}
Richard Smith2815e1a2012-05-25 02:17:09 +000083label2: ;
Richard Smithbdb97ff2012-05-26 06:20:46 +000084 } while (b); // expected-warning {{whenever 'do' loop condition is true}} \
85 // expected-note {{remove the condition if it is always false}}
Richard Smith2815e1a2012-05-25 02:17:09 +000086 x = 0;
87 goto label1;
88}
89
Richard Smithbdb97ff2012-05-26 06:20:46 +000090// CHECK: fix-it:"{{.*}}":{84:12-84:13}:"false"
91// CHECK: fix-it:"{{.*}}":{78:8-78:8}:" = 0"
92
93
Richard Smith2815e1a2012-05-25 02:17:09 +000094int test_for_false(int k) {
95 int x; // expected-note {{variable}}
96 for (int n = 0;
Richard Smithbdb97ff2012-05-26 06:20:46 +000097 n < k; // expected-warning {{whenever 'for' loop exits because its condition is false}} \
98 // expected-note {{remove the condition if it is always true}}
Richard Smith2815e1a2012-05-25 02:17:09 +000099 ++n) {
100 if (maybe()) {
101 x = n;
102 break;
103 }
104 }
Richard Smithbdb97ff2012-05-26 06:20:46 +0000105 return x; // expected-note {{uninitialized use}}
Richard Smith2815e1a2012-05-25 02:17:09 +0000106}
107
Richard Smithbdb97ff2012-05-26 06:20:46 +0000108// CHECK: fix-it:"{{.*}}":{97:8-97:13}:""
109// CHECK: fix-it:"{{.*}}":{95:8-95:8}:" = 0"
110
111
Richard Smith2815e1a2012-05-25 02:17:09 +0000112int test_for_true(int k) {
113 int x; // expected-note {{variable}}
114 int n = 0;
115 for (;
Richard Smithbdb97ff2012-05-26 06:20:46 +0000116 n < k; // expected-warning {{whenever 'for' loop is entered}} \
117 // expected-note {{remove the condition if it is always false}}
Richard Smith2815e1a2012-05-25 02:17:09 +0000118 ++n) {
119label:
Richard Smithbdb97ff2012-05-26 06:20:46 +0000120 return x; // expected-note {{uninitialized use}}
Richard Smith2815e1a2012-05-25 02:17:09 +0000121 }
122 x = 1;
123 goto label;
124}
125
Richard Smithbdb97ff2012-05-26 06:20:46 +0000126// CHECK: fix-it:"{{.*}}":{116:8-116:13}:"false"
127// CHECK: fix-it:"{{.*}}":{113:8-113:8}:" = 0"
128
129
Richard Smith2815e1a2012-05-25 02:17:09 +0000130int test_for_range_false(int k) {
131 int arr[3] = { 1, 2, 3 };
Richard Smithbdb97ff2012-05-26 06:20:46 +0000132 int x;
133 for (int &a : arr) { // no-warning, condition was not explicitly specified
Richard Smith2815e1a2012-05-25 02:17:09 +0000134 if (a == k) {
135 x = &a - arr;
136 break;
137 }
138 }
Richard Smithbdb97ff2012-05-26 06:20:46 +0000139 return x;
Richard Smith2815e1a2012-05-25 02:17:09 +0000140}
141
Richard Smithbdb97ff2012-05-26 06:20:46 +0000142
143
144
145
Richard Smith2815e1a2012-05-25 02:17:09 +0000146int test_for_range_true(int k) {
147 int arr[3] = { 1, 2, 3 };
Richard Smithbdb97ff2012-05-26 06:20:46 +0000148 int x;
149 for (int &a : arr) { // no-warning
Richard Smith2815e1a2012-05-25 02:17:09 +0000150 goto label;
151 }
152 x = 0;
153label:
Richard Smithbdb97ff2012-05-26 06:20:46 +0000154 return x;
Richard Smith2815e1a2012-05-25 02:17:09 +0000155}
156
Richard Smithbdb97ff2012-05-26 06:20:46 +0000157
158
159
160
Richard Smith2815e1a2012-05-25 02:17:09 +0000161int test_conditional_false(int k) {
162 int x; // expected-note {{variable}}
163 (void)(
Richard Smithbdb97ff2012-05-26 06:20:46 +0000164 maybe() // expected-warning {{whenever '?:' condition is false}} \
165 // expected-note {{remove the '?:' if its condition is always true}}
Richard Smith2815e1a2012-05-25 02:17:09 +0000166 ? x = 1 : 0);
Richard Smithbdb97ff2012-05-26 06:20:46 +0000167 return x; // expected-note {{uninitialized use}}
Richard Smith2815e1a2012-05-25 02:17:09 +0000168}
169
Richard Smithbdb97ff2012-05-26 06:20:46 +0000170// CHECK: fix-it:"{{.*}}":{164:7-166:9}:""
171// CHECK: fix-it:"{{.*}}":{166:14-166:18}:""
172// CHECK: fix-it:"{{.*}}":{162:8-162:8}:" = 0"
173
Richard Smith2815e1a2012-05-25 02:17:09 +0000174int test_conditional_true(int k) {
175 int x; // expected-note {{variable}}
176 (void)(
Richard Smithbdb97ff2012-05-26 06:20:46 +0000177 maybe() // expected-warning {{whenever '?:' condition is true}} \
178 // expected-note {{remove the '?:' if its condition is always false}}
Richard Smith2815e1a2012-05-25 02:17:09 +0000179 ? 0 : x = 1);
Richard Smithbdb97ff2012-05-26 06:20:46 +0000180 return x; // expected-note {{uninitialized use}}
Richard Smith2815e1a2012-05-25 02:17:09 +0000181}
182
Richard Smithbdb97ff2012-05-26 06:20:46 +0000183// CHECK: fix-it:"{{.*}}":{177:7-179:13}:""
184// CHECK: fix-it:"{{.*}}":{175:8-175:8}:" = 0"
185
186
Richard Smith2815e1a2012-05-25 02:17:09 +0000187int test_logical_and_false(int k) {
188 int x; // expected-note {{variable}}
Richard Smithbdb97ff2012-05-26 06:20:46 +0000189 maybe() // expected-warning {{whenever '&&' condition is false}} \
190 // expected-note {{remove the '&&' if its condition is always true}}
Richard Smith2815e1a2012-05-25 02:17:09 +0000191 && (x = 1);
Richard Smithbdb97ff2012-05-26 06:20:46 +0000192 return x; // expected-note {{uninitialized use}}
Richard Smith2815e1a2012-05-25 02:17:09 +0000193}
194
Argyrios Kyrtzidis5964df12012-12-20 21:05:53 +0000195// CHECK: fix-it:"{{.*}}":{189:3-191:10}:""
Richard Smithbdb97ff2012-05-26 06:20:46 +0000196// CHECK: fix-it:"{{.*}}":{188:8-188:8}:" = 0"
197
198
Richard Smith2815e1a2012-05-25 02:17:09 +0000199int test_logical_and_true(int k) {
200 int x; // expected-note {{variable}}
Richard Smithbdb97ff2012-05-26 06:20:46 +0000201 maybe() // expected-warning {{whenever '&&' condition is true}} \
202 // expected-note {{remove the '&&' if its condition is always false}}
Richard Smith2815e1a2012-05-25 02:17:09 +0000203 && ({ goto skip_init; 0; });
204 x = 1;
205skip_init:
Richard Smithbdb97ff2012-05-26 06:20:46 +0000206 return x; // expected-note {{uninitialized use}}
Richard Smith2815e1a2012-05-25 02:17:09 +0000207}
208
Richard Smithbdb97ff2012-05-26 06:20:46 +0000209// CHECK: fix-it:"{{.*}}":{201:3-203:34}:"false"
210// CHECK: fix-it:"{{.*}}":{200:8-200:8}:" = 0"
211
212
Richard Smith2815e1a2012-05-25 02:17:09 +0000213int test_logical_or_false(int k) {
214 int x; // expected-note {{variable}}
Richard Smithbdb97ff2012-05-26 06:20:46 +0000215 maybe() // expected-warning {{whenever '||' condition is false}} \
216 // expected-note {{remove the '||' if its condition is always true}}
Richard Smith2815e1a2012-05-25 02:17:09 +0000217 || ({ goto skip_init; 0; });
218 x = 1;
219skip_init:
Richard Smithbdb97ff2012-05-26 06:20:46 +0000220 return x; // expected-note {{uninitialized use}}
Richard Smith2815e1a2012-05-25 02:17:09 +0000221}
222
Richard Smithbdb97ff2012-05-26 06:20:46 +0000223// CHECK: fix-it:"{{.*}}":{215:3-217:34}:"true"
224// CHECK: fix-it:"{{.*}}":{214:8-214:8}:" = 0"
225
226
Richard Smith2815e1a2012-05-25 02:17:09 +0000227int test_logical_or_true(int k) {
228 int x; // expected-note {{variable}}
Richard Smithbdb97ff2012-05-26 06:20:46 +0000229 maybe() // expected-warning {{whenever '||' condition is true}} \
230 // expected-note {{remove the '||' if its condition is always false}}
Richard Smith2815e1a2012-05-25 02:17:09 +0000231 || (x = 1);
Richard Smithbdb97ff2012-05-26 06:20:46 +0000232 return x; // expected-note {{uninitialized use}}
Richard Smith2815e1a2012-05-25 02:17:09 +0000233}
234
Argyrios Kyrtzidis5964df12012-12-20 21:05:53 +0000235// CHECK: fix-it:"{{.*}}":{229:3-231:10}:""
Richard Smithbdb97ff2012-05-26 06:20:46 +0000236// CHECK: fix-it:"{{.*}}":{228:8-228:8}:" = 0"
237
238
Richard Smith2815e1a2012-05-25 02:17:09 +0000239int test_switch_case(int k) {
240 int x; // expected-note {{variable}}
241 switch (k) {
242 case 0:
243 x = 0;
244 break;
Richard Smithbdb97ff2012-05-26 06:20:46 +0000245 case 1: // expected-warning {{whenever switch case is taken}}
Richard Smith2815e1a2012-05-25 02:17:09 +0000246 break;
247 }
Richard Smithbdb97ff2012-05-26 06:20:46 +0000248 return x; // expected-note {{uninitialized use}}
Richard Smith2815e1a2012-05-25 02:17:09 +0000249}
250
Richard Smithbdb97ff2012-05-26 06:20:46 +0000251// CHECK: fix-it:"{{.*}}":{240:8-240:8}:" = 0"
252
253
254
Richard Smith2815e1a2012-05-25 02:17:09 +0000255int test_switch_default(int k) {
256 int x; // expected-note {{variable}}
257 switch (k) {
258 case 0:
259 x = 0;
260 break;
261 case 1:
262 x = 1;
263 break;
Richard Smithbdb97ff2012-05-26 06:20:46 +0000264 default: // expected-warning {{whenever switch default is taken}}
Richard Smith2815e1a2012-05-25 02:17:09 +0000265 break;
266 }
Richard Smithbdb97ff2012-05-26 06:20:46 +0000267 return x; // expected-note {{uninitialized use}}
Richard Smith2815e1a2012-05-25 02:17:09 +0000268}
269
Richard Smithbdb97ff2012-05-26 06:20:46 +0000270// CHECK: fix-it:"{{.*}}":{256:8-256:8}:" = 0"
271
272
273
Richard Smith2815e1a2012-05-25 02:17:09 +0000274int test_switch_suppress_1(int k) {
275 int x;
276 switch (k) {
277 case 0:
278 x = 0;
279 break;
280 case 1:
281 x = 1;
282 break;
283 }
284 return x; // no-warning
285}
286
Richard Smithbdb97ff2012-05-26 06:20:46 +0000287
288
289
290
Richard Smith2815e1a2012-05-25 02:17:09 +0000291int test_switch_suppress_2(int k) {
292 int x;
293 switch (k) {
294 case 0:
295 case 1:
296 switch (k) {
297 case 0:
298 return 0;
299 case 1:
300 return 1;
301 }
302 case 2:
303 case 3:
304 x = 1;
305 }
306 return x; // no-warning
307}
308
Richard Smithbdb97ff2012-05-26 06:20:46 +0000309
310
311
312
Richard Smith2815e1a2012-05-25 02:17:09 +0000313int test_multiple_notes(int k) {
314 int x; // expected-note {{variable}}
315 if (k > 0) {
316 if (k == 5)
317 x = 1;
Richard Smithbdb97ff2012-05-26 06:20:46 +0000318 else if (k == 2) // expected-warning {{whenever 'if' condition is false}} \
319 // expected-note {{remove the 'if' if its condition is always true}}
Richard Smith2815e1a2012-05-25 02:17:09 +0000320 x = 2;
321 } else {
322 if (k == -5)
323 x = 3;
Richard Smithbdb97ff2012-05-26 06:20:46 +0000324 else if (k == -2) // expected-warning {{whenever 'if' condition is false}} \
325 // expected-note {{remove the 'if' if its condition is always true}}
Richard Smith2815e1a2012-05-25 02:17:09 +0000326 x = 4;
327 }
Richard Smithbdb97ff2012-05-26 06:20:46 +0000328 return x; // expected-note 2{{uninitialized use}}
Richard Smith2815e1a2012-05-25 02:17:09 +0000329}
330
Richard Smithbdb97ff2012-05-26 06:20:46 +0000331// CHECK: fix-it:"{{.*}}":{324:10-326:7}:""
332// CHECK: fix-it:"{{.*}}":{318:10-320:7}:""
333// CHECK: fix-it:"{{.*}}":{314:8-314:8}:" = 0"
334
Richard Smith2815e1a2012-05-25 02:17:09 +0000335int test_no_false_positive_1(int k) {
336 int x;
337 if (k)
338 x = 5;
339 while (!k)
340 maybe();
341 return x;
342}
343
Richard Smithbdb97ff2012-05-26 06:20:46 +0000344
345
346
347
Richard Smith2815e1a2012-05-25 02:17:09 +0000348int test_no_false_positive_2() {
349 int x;
350 bool b = false;
351 if (maybe()) {
352 x = 5;
353 b = true;
354 }
355 return b ? x : 0;
356}
357
Richard Smithbdb97ff2012-05-26 06:20:46 +0000358
Richard Smith558e8872012-07-13 23:33:44 +0000359// FIXME: In this case, the variable is used uninitialized whenever the
360// function's entry block is reached. Produce a diagnostic saying that
361// the variable is uninitialized the first time it is used.
Richard Smith2815e1a2012-05-25 02:17:09 +0000362void test_null_pred_succ() {
Richard Smith558e8872012-07-13 23:33:44 +0000363 int x;
364 if (0)
Richard Smith2815e1a2012-05-25 02:17:09 +0000365 foo: x = 0;
Richard Smith558e8872012-07-13 23:33:44 +0000366 if (x)
Richard Smith2815e1a2012-05-25 02:17:09 +0000367 goto foo;
368}
Richard Smithbdb97ff2012-05-26 06:20:46 +0000369
Richard Smith558e8872012-07-13 23:33:44 +0000370
371
372
373void foo();
374int PR13360(bool b) {
375 int x; // expected-note {{variable}}
376 if (b) { // expected-warning {{variable 'x' is used uninitialized whenever 'if' condition is true}} expected-note {{remove}}
377 do {
378 foo();
379 } while (0);
380 } else {
381 x = 1;
382 }
383 return x; // expected-note {{uninitialized use occurs here}}
384}
385
386// CHECK: fix-it:"{{.*}}":{376:3-380:10}:""
387// CHECK: fix-it:"{{.*}}":{375:8-375:8}:" = 0"