uninit-variables.c revision 9660803cd332d5c605899435019bb3b37fca3acc
1// RUN: %clang_cc1 -fsyntax-only -Wuninitialized-experimental -fsyntax-only %s -verify 2 3int test1() { 4 int x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}} 5 return x; // expected-note{{variable 'x' is possibly uninitialized when used here}} 6} 7 8int test2() { 9 int x = 0; 10 return x; // no-warning 11} 12 13int test3() { 14 int x; 15 x = 0; 16 return x; // no-warning 17} 18 19int test4() { 20 int x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}} 21 ++x; // expected-note{{variable 'x' is possibly uninitialized when used here}} 22 return x; 23} 24 25int test5() { 26 int x, y; // expected-warning{{use of uninitialized variable 'y'}} expected-note{{add initialization to silence this warning}} 27 x = y; // expected-note{{variable 'y' is possibly uninitialized when used here}} 28 return x; 29} 30 31int test6() { 32 int x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}} 33 x += 2; // expected-note{{variable 'x' is possibly uninitialized when used here}} 34 return x; 35} 36 37int test7(int y) { 38 int x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}} 39 if (y) 40 x = 1; 41 return x; // expected-note{{variable 'x' is possibly uninitialized when used here}} 42} 43 44int test8(int y) { 45 int x; 46 if (y) 47 x = 1; 48 else 49 x = 0; 50 return x; // no-warning 51} 52 53int test9(int n) { 54 int x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}} 55 for (unsigned i = 0 ; i < n; ++i) { 56 if (i == n - 1) 57 break; 58 x = 1; 59 } 60 return x; // expected-note{{variable 'x' is possibly uninitialized when used here}} 61} 62 63int test10(unsigned n) { 64 int x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}} 65 for (unsigned i = 0 ; i < n; ++i) { 66 x = 1; 67 } 68 return x; // expected-note{{variable 'x' is possibly uninitialized when used here}} 69} 70 71int test11(unsigned n) { 72 int x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}} 73 for (unsigned i = 0 ; i <= n; ++i) { 74 x = 1; 75 } 76 return x; //expected-note{{variable 'x' is possibly uninitialized when used here}} 77} 78 79void test12(unsigned n) { 80 for (unsigned i ; n ; ++i) ; // expected-warning{{use of uninitialized variable 'i'}} expected-note{{variable 'i' is possibly uninitialized when used here}}} expected-note{{add initialization to silence this warning}} 81} 82 83int test13() { 84 static int i; 85 return i; // no-warning 86} 87 88// Simply don't crash on this test case. 89void test14() { 90 const char *p = 0; 91 for (;;) {} 92} 93 94void test15() { 95 int x = x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{variable 'x' is possibly uninitialized when used here}} expected-note{{add initialization to silence this warning}} 96} 97 98// Don't warn in the following example; shows dataflow confluence. 99char *test16_aux(); 100void test16() { 101 char *p = test16_aux(); 102 for (unsigned i = 0 ; i < 100 ; i++) 103 p[i] = 'a'; // no-warning 104} 105 106void test17() { 107 // Don't warn multiple times about the same uninitialized variable 108 // along the same path. 109 int *x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}} 110 *x = 1; // expected-note{{variable 'x' is possibly uninitialized when used here}} 111 *x = 1; // no-warning 112} 113 114int test18(int x, int y) { 115 int z; 116 if (x && y && (z = 1)) { 117 return z; // no-warning 118 } 119 return 0; 120} 121 122int test19_aux1(); 123int test19_aux2(); 124int test19_aux3(int *x); 125int test19() { 126 int z; 127 if (test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z)) 128 return z; // no-warning 129 return 0; 130} 131 132int test20() { 133 int z; // expected-warning{{use of uninitialized variable 'z'}} expected-note{{add initialization to silence this warning}} 134 if ((test19_aux1() + test19_aux2() && test19_aux1()) || test19_aux3(&z)) 135 return z; // expected-note{{variable 'z' is possibly uninitialized when used here}} 136 return 0; 137} 138 139int test21(int x, int y) { 140 int z; // expected-warning{{use of uninitialized variable 'z'}} expected-note{{add initialization to silence this warning}} 141 if ((x && y) || test19_aux3(&z) || test19_aux2()) 142 return z; // expected-note{{variable 'z' is possibly uninitialized when used here}} 143 return 0; 144} 145 146int test22() { 147 int z; 148 while (test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z)) 149 return z; // no-warning 150 return 0; 151} 152 153int test23() { 154 int z; 155 for ( ; test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z) ; ) 156 return z; // no-warning 157 return 0; 158} 159 160// The basic uninitialized value analysis doesn't have enough path-sensitivity 161// to catch initializations relying on control-dependencies spanning multiple 162// conditionals. This possibly can be handled by making the CFG itself 163// represent such control-dependencies, but it is a niche case. 164int test24(int flag) { 165 unsigned val; // expected-warning{{use of uninitialized variable 'val'}} expected-note{{add initialization to silence this warning}} 166 if (flag) 167 val = 1; 168 if (!flag) 169 val = 1; 170 return val; // expected-note{{variable 'val' is possibly uninitialized when used here}} 171} 172 173float test25() { 174 float x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}} 175 return x; // expected-note{{variable 'x' is possibly uninitialized when used here}} 176} 177 178typedef int MyInt; 179MyInt test26() { 180 MyInt x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}} 181 return x; // expected-note{{variable 'x' is possibly uninitialized when used here}} 182} 183 184// Test handling of sizeof(). 185int test27() { 186 struct test_27 { int x; } *y; 187 return sizeof(y->x); // no-warning 188} 189 190int test28() { 191 int len; // expected-warning{{use of uninitialized variable 'len'}} expected-note{{add initialization to silence this warning}} 192 return sizeof(int[len]); // expected-note{{variable 'len' is possibly uninitialized when used here}} 193} 194 195