uninit-variables.c revision b88fb027bfe2f85da3a341f42549900bd658ac8b
1b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar// RUN: %clang_cc1 -fsyntax-only -Wuninitialized -Wconditional-uninitialized -fsyntax-only -fblocks %s -verify
2b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
3b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarint test1() {
4b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
59ad441ffec97db647fee3725b3424284fb913e14Howard Hinnant  return x; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
69ad441ffec97db647fee3725b3424284fb913e14Howard Hinnant}
7b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
8b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarint test2() {
9b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  int x = 0;
10b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  return x; // no-warning
11b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
12b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
13b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarint test3() {
14b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  int x;
15b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  x = 0;
16b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  return x; // no-warning
17b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
18b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
19b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarint test4() {
20b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
21b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  ++x; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
22b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  return x;
23b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
24b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
25b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarint test5() {
26b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  int x, y; // expected-note{{variable 'y' is declared here}} expected-note{{add initialization to silence this warning}}
27b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  x = y; // expected-warning{{variable 'y' is possibly uninitialized when used here}}
28b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  return x;
29b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
30b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
31b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarint test6() {
32b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
33b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  x += 2; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
34b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  return x;
35b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
36cff5248a12a36a7225b707b23ac088f5ba214f8aDaniel Dunbar
37b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarint test7(int y) {
38b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
39b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  if (y)
40b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    x = 1;
41b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  return x; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
42b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
43b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
44b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarint test8(int y) {
45b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  int x;
46b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  if (y)
47b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    x = 1;
48b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  else
49b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    x = 0;
50b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  return x;
51b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
52b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
53b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarint test9(int n) {
54b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
55b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  for (unsigned i = 0 ; i < n; ++i) {
56b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    if (i == n - 1)
57b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar      break;
58b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    x = 1;
59b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  }
60b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  return x; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
61b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
62b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
63b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarint test10(unsigned n) {
64b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
65b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  for (unsigned i = 0 ; i < n; ++i) {
66b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    x = 1;
67b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  }
68b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  return x; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
69b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
70b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
71b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarint test11(unsigned n) {
72b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
73b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  for (unsigned i = 0 ; i <= n; ++i) {
74b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    x = 1;
75b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  }
76b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  return x; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
77b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
78b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
79b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarvoid test12(unsigned n) {
80b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  for (unsigned i ; n ; ++i) ; // expected-warning{{variable 'i' is possibly uninitialized when used here}} expected-note{{variable 'i' is declared here}} expected-note{{add initialization to silence this warning}}
81b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
82b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
83b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarint test13() {
84b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  static int i;
85b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  return i; // no-warning
86b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
87b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
88b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar// Simply don't crash on this test case.
89b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarvoid test14() {
90b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  const char *p = 0;
91b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  for (;;) {}
92b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
93b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
94b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarint test15() {
95b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  int x = x; // no-warning: signals intended lack of initialization. \
96b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar             // expected-note{{variable 'x' is declared here}}
97b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  return x; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
98b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
99b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
100b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar// Don't warn in the following example; shows dataflow confluence.
101b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarchar *test16_aux();
102b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarvoid test16() {
103b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  char *p = test16_aux();
104b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  for (unsigned i = 0 ; i < 100 ; i++)
105b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    p[i] = 'a'; // no-warning
106b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
107b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
108b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarvoid test17() {
109b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  // Don't warn multiple times about the same uninitialized variable
110b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  // along the same path.
111b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  int *x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
112b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  *x = 1; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
113b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  *x = 1; // no-warning
114b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
115b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
116b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarint test18(int x, int y) {
117b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  int z;
118b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  if (x && y && (z = 1)) {
119b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    return z; // no-warning
120b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  }
121b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar  return 0;
122b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
123b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
124b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarint test19_aux1();
125b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarint test19_aux2();
126int test19_aux3(int *x);
127int test19() {
128  int z;
129  if (test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z))
130    return z; // no-warning
131  return 0;
132}
133
134int test20() {
135  int z; // expected-note{{variable 'z' is declared here}} expected-note{{add initialization to silence this warning}}
136  if ((test19_aux1() + test19_aux2() && test19_aux1()) || test19_aux3(&z))
137    return z; // expected-warning{{variable 'z' is possibly uninitialized when used here}}
138  return 0;
139}
140
141int test21(int x, int y) {
142  int z; // expected-note{{variable 'z' is declared here}} expected-note{{add initialization to silence this warning}}
143  if ((x && y) || test19_aux3(&z) || test19_aux2())
144    return z; // expected-warning{{variable 'z' is possibly uninitialized when used here}}
145  return 0;
146}
147
148int test22() {
149  int z;
150  while (test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z))
151    return z; // no-warning
152  return 0;
153}
154
155int test23() {
156  int z;
157  for ( ; test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z) ; )
158    return z; // no-warning
159  return 0;
160}
161
162// The basic uninitialized value analysis doesn't have enough path-sensitivity
163// to catch initializations relying on control-dependencies spanning multiple
164// conditionals.  This possibly can be handled by making the CFG itself
165// represent such control-dependencies, but it is a niche case.
166int test24(int flag) {
167  unsigned val; // expected-note{{variable 'val' is declared here}} expected-note{{add initialization to silence this warning}}
168  if (flag)
169    val = 1;
170  if (!flag)
171    val = 1;
172  return val; // expected-warning{{variable 'val' is possibly uninitialized when used here}}
173}
174
175float test25() {
176  float x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
177  return x; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
178}
179
180typedef int MyInt;
181MyInt test26() {
182  MyInt x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
183  return x; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
184}
185
186// Test handling of sizeof().
187int test27() {
188  struct test_27 { int x; } *y;
189  return sizeof(y->x); // no-warning
190}
191
192int test28() {
193  int len; // expected-note{{variable 'len' is declared here}} expected-note{{add initialization to silence this warning}}
194  return sizeof(int[len]); // expected-warning{{variable 'len' is possibly uninitialized when used here}}
195}
196
197void test29() {
198  int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
199  (void) ^{ (void) x; }; // expected-warning{{variable 'x' is possibly uninitialized when captured by block}}
200}
201
202void test30() {
203  static int x; // no-warning
204  (void) ^{ (void) x; };
205}
206
207void test31() {
208  __block int x; // no-warning
209  (void) ^{ (void) x; };
210}
211
212int test32_x;
213void test32() {
214  (void) ^{ (void) test32_x; }; // no-warning
215}
216
217void test_33() {
218  int x; // no-warning
219  (void) x;
220}
221
222int test_34() {
223  int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
224  (void) x;
225  return x; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
226}
227
228// Test that this case doesn't crash.
229void test35(int x) {
230  __block int y = 0;
231  ^{ y = (x == 0); }();
232}
233
234// Test handling of indirect goto.
235void test36()
236{
237  void **pc; // expected-note{{variable 'pc' is declared here}} expected-note{{add initialization to silence this warning}}
238  void *dummy[] = { &&L1, &&L2 };
239 L1:
240    goto *pc; // expected-warning{{variable 'pc' is possibly uninitialized when used here}}
241 L2:
242    goto *pc;
243}
244
245// Test && nested in ||.
246int test37_a();
247int test37_b();
248int test37()
249{
250    int identifier;
251    if ((test37_a() && (identifier = 1)) ||
252        (test37_b() && (identifier = 2))) {
253        return identifier; // no-warning
254    }
255    return 0;
256}
257
258// Test merging of path-specific dataflow values (without asserting).
259int test38(int r, int x, int y)
260{
261  int z;
262  return ((r < 0) || ((r == 0) && (x < y)));
263}
264
265int test39(int x) {
266  int y; // expected-note {{variable 'y' is declared here}} expected-note{{add initialization to silence this warning}}
267  int z = x + y; // expected-warning {{variable 'y' is possibly uninitialized when used here}}
268  return z;
269}
270
271
272int test40(int x) {
273  int y; // expected-note {{variable 'y' is declared here}} expected-note{{add initialization to silence this warning}}
274  return x ? 1 : y; // expected-warning {{variable 'y' is possibly uninitialized when used here}}
275}
276
277int test41(int x) {
278  int y; // expected-note {{variable 'y' is declared here}} expected-note{{add initialization to silence this warning}}
279  if (x) y = 1; // no-warning
280  return y; // expected-warning {{variable 'y' is possibly uninitialized when used here}}
281}
282
283void test42() {
284  int a;
285  a = 30; // no-warning
286}
287
288void test43_aux(int x);
289void test43(int i) {
290  int x; // expected-note {{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
291  for (i = 0 ; i < 10; i++)
292    test43_aux(x++); // expected-warning {{variable 'x' is possibly uninitialized when used here}}
293}
294
295void test44(int i) {
296  int x = i;
297  int y; // expected-note {{variable 'y' is declared here}} expected-note{{add initialization to silence this warning}}
298  for (i = 0; i < 10; i++ ) {
299    test43_aux(x++); // no-warning
300    x += y; // expected-warning {{variable 'y' is possibly uninitialized when used here}}
301  }
302}
303
304int test45(int j) {
305  int x = 1, y = x + 1;
306  if (y) // no-warning
307    return x;
308  return y;
309}
310
311void test46()
312{
313  int i; // expected-note {{variable 'i' is declared here}} expected-note{{add initialization to silence this warning}}
314  int j = i ? : 1; // expected-warning {{variable 'i' is possibly uninitialized when used here}}
315}
316
317void *test47(int *i)
318{
319  return i ? : 0; // no-warning
320}
321
322void *test49(int *i)
323{
324  int a;
325  return &a ? : i; // no-warning
326}
327
328void test50()
329{
330  char c[1 ? : 2]; // no-warning
331}
332
333int test51(void)
334{
335    __block int a;
336    ^(void) {
337      a = 42;
338    }();
339    return a; // no-warning
340}
341
342