1 // RUN: %clang_cc1 -std=gnu++11 -Wsometimes-uninitialized -verify %s
2 // RUN: %clang_cc1 -std=gnu++11 -Wsometimes-uninitialized -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
6 int test_if_false(bool b
) {
7 int x
; // expected-note {{variable}}
8 if (b
) // expected-warning {{whenever 'if' condition is false}} \
9 // expected-note {{remove the 'if' if its condition is always true}}
11 return x
; // expected-note {{uninitialized use}}
14 // CHECK: fix-it:"{{.*}}":{8:3-10:5}:""
15 // CHECK: fix-it:"{{.*}}":{7:8-7:8}:" = 0"
18 int test_if_true(bool b
) {
19 int x
; // expected-note {{variable}}
20 if (b
) {} // expected-warning {{whenever 'if' condition is true}} \
21 // expected-note {{remove the 'if' if its condition is always false}}
23 return x
; // expected-note {{uninitialized use}}
26 // CHECK: fix-it:"{{.*}}":{20:3-22:8}:""
27 // CHECK: fix-it:"{{.*}}":{19:8-19:8}:" = 0"
30 int test_while_false(bool b
) {
31 int x
; // expected-note {{variable}}
32 while (b
) { // expected-warning {{whenever 'while' loop exits because its condition is false}} \
33 // expected-note {{remove the condition if it is always true}}
39 return x
; // expected-note {{uninitialized use}}
42 // CHECK: fix-it:"{{.*}}":{32:10-32:11}:"true"
43 // CHECK: fix-it:"{{.*}}":{31:8-31:8}:" = 0"
46 int test_while_true(bool b
) {
47 int x
; // expected-note {{variable}}
48 while (b
) { // expected-warning {{whenever 'while' loop is entered}} \
49 // expected-note {{remove the condition if it is always false}}
51 return x
; // expected-note {{uninitialized use}}
57 // CHECK: fix-it:"{{.*}}":{48:10-48:11}:"false"
58 // CHECK: fix-it:"{{.*}}":{47:8-47:8}:" = 0"
61 int test_do_while_false(bool b
) {
62 int x
; // expected-note {{variable}}
68 } 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}}
73 // CHECK: fix-it:"{{.*}}":{68:12-68:13}:"true"
74 // CHECK: fix-it:"{{.*}}":{62:8-62:8}:" = 0"
77 int test_do_while_true(bool b
) {
78 int x
; // expected-note {{variable}}
82 return x
; // expected-note {{uninitialized use}}
84 } while (b
); // expected-warning {{whenever 'do' loop condition is true}} \
85 // expected-note {{remove the condition if it is always false}}
90 // CHECK: fix-it:"{{.*}}":{84:12-84:13}:"false"
91 // CHECK: fix-it:"{{.*}}":{78:8-78:8}:" = 0"
94 int test_for_false(int k
) {
95 int x
; // expected-note {{variable}}
97 n
< k
; // expected-warning {{whenever 'for' loop exits because its condition is false}} \
98 // expected-note {{remove the condition if it is always true}}
105 return x
; // expected-note {{uninitialized use}}
108 // CHECK: fix-it:"{{.*}}":{97:8-97:13}:""
109 // CHECK: fix-it:"{{.*}}":{95:8-95:8}:" = 0"
112 int test_for_true(int k
) {
113 int x
; // expected-note {{variable}}
116 n
< k
; // expected-warning {{whenever 'for' loop is entered}} \
117 // expected-note {{remove the condition if it is always false}}
120 return x
; // expected-note {{uninitialized use}}
126 // CHECK: fix-it:"{{.*}}":{116:8-116:13}:"false"
127 // CHECK: fix-it:"{{.*}}":{113:8-113:8}:" = 0"
130 int test_for_range_false(int k
) {
131 int arr
[3] = { 1, 2, 3 };
133 for (int &a
: arr
) { // no-warning, condition was not explicitly specified
146 int test_for_range_true(int k
) {
147 int arr
[3] = { 1, 2, 3 };
148 int x
; // expected-note {{variable}}
149 for (int &a
: arr
) { // expected-warning {{variable 'x' is used uninitialized whenever 'for' loop is entered}}
154 return x
; // expected-note {{uninitialized use}}
161 int test_conditional_false(int k
) {
162 int x
; // expected-note {{variable}}
164 maybe() // expected-warning {{whenever '?:' condition is false}} \
165 // expected-note {{remove the '?:' if its condition is always true}}
167 return x
; // expected-note {{uninitialized use}}
170 // CHECK: fix-it:"{{.*}}":{164:7-166:9}:""
171 // CHECK: fix-it:"{{.*}}":{166:14-166:18}:""
172 // CHECK: fix-it:"{{.*}}":{162:8-162:8}:" = 0"
174 int test_conditional_true(int k
) {
175 int x
; // expected-note {{variable}}
177 maybe() // expected-warning {{whenever '?:' condition is true}} \
178 // expected-note {{remove the '?:' if its condition is always false}}
180 return x
; // expected-note {{uninitialized use}}
183 // CHECK: fix-it:"{{.*}}":{177:7-179:13}:""
184 // CHECK: fix-it:"{{.*}}":{175:8-175:8}:" = 0"
187 int test_logical_and_false(int k
) {
188 int x
; // expected-note {{variable}}
189 maybe() // expected-warning {{whenever '&&' condition is false}} \
190 // expected-note {{remove the '&&' if its condition is always true}}
192 return x
; // expected-note {{uninitialized use}}
195 // CHECK: fix-it:"{{.*}}":{189:3-191:10}:""
196 // CHECK: fix-it:"{{.*}}":{188:8-188:8}:" = 0"
199 int test_logical_and_true(int k
) {
200 int x
; // expected-note {{variable}}
201 maybe() // expected-warning {{whenever '&&' condition is true}} \
202 // expected-note {{remove the '&&' if its condition is always false}}
203 && ({ goto skip_init
; 0; });
206 return x
; // expected-note {{uninitialized use}}
209 // CHECK: fix-it:"{{.*}}":{201:3-203:34}:"false"
210 // CHECK: fix-it:"{{.*}}":{200:8-200:8}:" = 0"
213 int test_logical_or_false(int k
) {
214 int x
; // expected-note {{variable}}
215 maybe() // expected-warning {{whenever '||' condition is false}} \
216 // expected-note {{remove the '||' if its condition is always true}}
217 || ({ goto skip_init
; 0; });
220 return x
; // expected-note {{uninitialized use}}
223 // CHECK: fix-it:"{{.*}}":{215:3-217:34}:"true"
224 // CHECK: fix-it:"{{.*}}":{214:8-214:8}:" = 0"
227 int test_logical_or_true(int k
) {
228 int x
; // expected-note {{variable}}
229 maybe() // expected-warning {{whenever '||' condition is true}} \
230 // expected-note {{remove the '||' if its condition is always false}}
232 return x
; // expected-note {{uninitialized use}}
235 // CHECK: fix-it:"{{.*}}":{229:3-231:10}:""
236 // CHECK: fix-it:"{{.*}}":{228:8-228:8}:" = 0"
239 int test_switch_case(int k
) {
240 int x
; // expected-note {{variable}}
245 case 1: // expected-warning {{whenever switch case is taken}}
248 return x
; // expected-note {{uninitialized use}}
251 // CHECK: fix-it:"{{.*}}":{240:8-240:8}:" = 0"
255 int test_switch_default(int k
) {
256 int x
; // expected-note {{variable}}
264 default: // expected-warning {{whenever switch default is taken}}
267 return x
; // expected-note {{uninitialized use}}
270 // CHECK: fix-it:"{{.*}}":{256:8-256:8}:" = 0"
274 int test_switch_suppress_1(int k
) {
284 return x
; // no-warning
291 int test_switch_suppress_2(int k
) {
306 return x
; // no-warning
313 int test_multiple_notes(int k
) {
314 int x
; // expected-note {{variable}}
318 else if (k
== 2) // expected-warning {{whenever 'if' condition is false}} \
319 // expected-note {{remove the 'if' if its condition is always true}}
324 else if (k
== -2) // expected-warning {{whenever 'if' condition is false}} \
325 // expected-note {{remove the 'if' if its condition is always true}}
328 return x
; // expected-note 2{{uninitialized use}}
331 // CHECK: fix-it:"{{.*}}":{324:10-326:7}:""
332 // CHECK: fix-it:"{{.*}}":{318:10-320:7}:""
333 // CHECK: fix-it:"{{.*}}":{314:8-314:8}:" = 0"
335 int test_no_false_positive_1(int k
) {
348 int test_no_false_positive_2() {
362 void test_null_pred_succ() {
363 int x
; // expected-note {{variable}} expected-warning {{used uninitialized whenever function 'test_null_pred_succ' is called}}
366 if (x
) // expected-note {{use}}
374 int 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}}
383 return x
; // expected-note {{uninitialized use occurs here}}
386 // CHECK: fix-it:"{{.*}}":{376:3-380:10}:""
387 // CHECK: fix-it:"{{.*}}":{375:8-375:8}:" = 0"
389 void test_jump_init() {
391 int x
; // expected-note {{variable}} expected-warning {{used uninitialized whenever function 'test_jump_init'}}
393 while (x
) x
= 0; // expected-note {{use}}
397 int x
; // expected-note {{variable}} expected-warning {{used uninitialized whenever function 'PR16054}}
398 while (x
!= 0) { // expected-note {{use}}
403 void test_loop_uninit() {
404 for (int n
= 0; n
< 10; ++n
) {
405 int k
; // expected-warning {{variable 'k' is used uninitialized whenever its declaration is reached}} expected-note {{variable}}
407 k
= k
+ 1; // expected-note {{use}}
412 // FIXME: We should warn here, because the variable is used uninitialized
413 // the first time we encounter the use.
414 void test_loop_with_assignment() {
416 for (int n
= 0; n
< 10; ++n
) {
421 // FIXME: We should warn here, because the variable is used uninitialized
422 // the first time we encounter the use.
423 void test_loop_with_ref_bind() {
425 for (int n
= 0; n
< 10; ++n
) {