Ted Kremenek | fd6b874 | 2011-01-26 04:49:48 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -Wuninitialized -fsyntax-only -fblocks %s -verify |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 2 | |
| 3 | int test1() { |
Ted Kremenek | dcfb360 | 2011-01-21 22:49:49 +0000 | [diff] [blame] | 4 | int x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}} |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 5 | return x; // expected-note{{variable 'x' is possibly uninitialized when used here}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 6 | } |
| 7 | |
| 8 | int test2() { |
| 9 | int x = 0; |
| 10 | return x; // no-warning |
| 11 | } |
| 12 | |
| 13 | int test3() { |
| 14 | int x; |
| 15 | x = 0; |
| 16 | return x; // no-warning |
| 17 | } |
| 18 | |
| 19 | int test4() { |
Ted Kremenek | dcfb360 | 2011-01-21 22:49:49 +0000 | [diff] [blame] | 20 | int x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}} |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 21 | ++x; // expected-note{{variable 'x' is possibly uninitialized when used here}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 22 | return x; |
| 23 | } |
| 24 | |
| 25 | int test5() { |
Ted Kremenek | dcfb360 | 2011-01-21 22:49:49 +0000 | [diff] [blame] | 26 | int x, y; // expected-warning{{use of uninitialized variable 'y'}} expected-note{{add initialization to silence this warning}} |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 27 | x = y; // expected-note{{variable 'y' is possibly uninitialized when used here}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 28 | return x; |
| 29 | } |
| 30 | |
| 31 | int test6() { |
Ted Kremenek | dcfb360 | 2011-01-21 22:49:49 +0000 | [diff] [blame] | 32 | int x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}} |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 33 | x += 2; // expected-note{{variable 'x' is possibly uninitialized when used here}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 34 | return x; |
| 35 | } |
| 36 | |
| 37 | int test7(int y) { |
Ted Kremenek | dcfb360 | 2011-01-21 22:49:49 +0000 | [diff] [blame] | 38 | int x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 39 | if (y) |
| 40 | x = 1; |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 41 | return x; // expected-note{{variable 'x' is possibly uninitialized when used here}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | int test8(int y) { |
| 45 | int x; |
| 46 | if (y) |
| 47 | x = 1; |
| 48 | else |
| 49 | x = 0; |
| 50 | return x; // no-warning |
| 51 | } |
| 52 | |
| 53 | int test9(int n) { |
Ted Kremenek | dcfb360 | 2011-01-21 22:49:49 +0000 | [diff] [blame] | 54 | int x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 55 | for (unsigned i = 0 ; i < n; ++i) { |
| 56 | if (i == n - 1) |
| 57 | break; |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 58 | x = 1; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 59 | } |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 60 | return x; // expected-note{{variable 'x' is possibly uninitialized when used here}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | int test10(unsigned n) { |
Ted Kremenek | dcfb360 | 2011-01-21 22:49:49 +0000 | [diff] [blame] | 64 | int x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 65 | for (unsigned i = 0 ; i < n; ++i) { |
| 66 | x = 1; |
| 67 | } |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 68 | return x; // expected-note{{variable 'x' is possibly uninitialized when used here}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | int test11(unsigned n) { |
Ted Kremenek | dcfb360 | 2011-01-21 22:49:49 +0000 | [diff] [blame] | 72 | int x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 73 | for (unsigned i = 0 ; i <= n; ++i) { |
| 74 | x = 1; |
| 75 | } |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 76 | return x; //expected-note{{variable 'x' is possibly uninitialized when used here}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void test12(unsigned n) { |
Ted Kremenek | dcfb360 | 2011-01-21 22:49:49 +0000 | [diff] [blame] | 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}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | int test13() { |
| 84 | static int i; |
| 85 | return i; // no-warning |
| 86 | } |
| 87 | |
Ted Kremenek | c104e53 | 2011-01-18 04:53:25 +0000 | [diff] [blame] | 88 | // Simply don't crash on this test case. |
| 89 | void test14() { |
| 90 | const char *p = 0; |
| 91 | for (;;) {} |
| 92 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 93 | |
Ted Kremenek | c104e53 | 2011-01-18 04:53:25 +0000 | [diff] [blame] | 94 | void test15() { |
Ted Kremenek | dcfb360 | 2011-01-21 22:49:49 +0000 | [diff] [blame] | 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}} |
Ted Kremenek | c104e53 | 2011-01-18 04:53:25 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Don't warn in the following example; shows dataflow confluence. |
| 99 | char *test16_aux(); |
| 100 | void test16() { |
| 101 | char *p = test16_aux(); |
| 102 | for (unsigned i = 0 ; i < 100 ; i++) |
| 103 | p[i] = 'a'; // no-warning |
| 104 | } |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 105 | |
| 106 | void test17() { |
| 107 | // Don't warn multiple times about the same uninitialized variable |
| 108 | // along the same path. |
Ted Kremenek | fbb178a | 2011-01-21 19:41:46 +0000 | [diff] [blame] | 109 | int *x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}} |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 110 | *x = 1; // expected-note{{variable 'x' is possibly uninitialized when used here}} |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 111 | *x = 1; // no-warning |
| 112 | } |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 113 | |
| 114 | int 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 | |
| 122 | int test19_aux1(); |
| 123 | int test19_aux2(); |
| 124 | int test19_aux3(int *x); |
| 125 | int 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 | |
| 132 | int test20() { |
Ted Kremenek | dcfb360 | 2011-01-21 22:49:49 +0000 | [diff] [blame] | 133 | int z; // expected-warning{{use of uninitialized variable 'z'}} expected-note{{add initialization to silence this warning}} |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 134 | if ((test19_aux1() + test19_aux2() && test19_aux1()) || test19_aux3(&z)) |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 135 | return z; // expected-note{{variable 'z' is possibly uninitialized when used here}} |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | int test21(int x, int y) { |
Ted Kremenek | dcfb360 | 2011-01-21 22:49:49 +0000 | [diff] [blame] | 140 | int z; // expected-warning{{use of uninitialized variable 'z'}} expected-note{{add initialization to silence this warning}} |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 141 | if ((x && y) || test19_aux3(&z) || test19_aux2()) |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 142 | return z; // expected-note{{variable 'z' is possibly uninitialized when used here}} |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | int 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 | |
| 153 | int 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. |
| 164 | int test24(int flag) { |
Ted Kremenek | dcfb360 | 2011-01-21 22:49:49 +0000 | [diff] [blame] | 165 | unsigned val; // expected-warning{{use of uninitialized variable 'val'}} expected-note{{add initialization to silence this warning}} |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 166 | if (flag) |
| 167 | val = 1; |
| 168 | if (!flag) |
| 169 | val = 1; |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 170 | return val; // expected-note{{variable 'val' is possibly uninitialized when used here}} |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Ted Kremenek | dcfb360 | 2011-01-21 22:49:49 +0000 | [diff] [blame] | 173 | float 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 | |
| 178 | typedef int MyInt; |
| 179 | MyInt 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 | } |
Ted Kremenek | 9660803 | 2011-01-23 17:53:04 +0000 | [diff] [blame] | 183 | |
| 184 | // Test handling of sizeof(). |
| 185 | int test27() { |
| 186 | struct test_27 { int x; } *y; |
| 187 | return sizeof(y->x); // no-warning |
| 188 | } |
| 189 | |
| 190 | int 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 | |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 195 | void test29() { |
| 196 | int x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}} |
| 197 | (void) ^{ (void) x; }; // expected-note{{variable 'x' is possibly uninitialized when captured by block}} |
| 198 | } |
| 199 | |
| 200 | void test30() { |
| 201 | static int x; // no-warning |
| 202 | (void) ^{ (void) x; }; |
| 203 | } |
| 204 | |
| 205 | void test31() { |
| 206 | __block int x; // no-warning |
| 207 | (void) ^{ (void) x; }; |
| 208 | } |
| 209 | |
| 210 | int test32_x; |
| 211 | void test32() { |
| 212 | (void) ^{ (void) test32_x; }; // no-warning |
| 213 | } |
| 214 | |
Ted Kremenek | dd0f794 | 2011-01-26 04:49:43 +0000 | [diff] [blame] | 215 | void test_33() { |
| 216 | int x; // no-warning |
| 217 | (void) x; |
| 218 | } |
| 219 | |
| 220 | int test_34() { |
| 221 | int x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}} |
| 222 | (void) x; |
| 223 | return x; // expected-note{{variable 'x' is possibly uninitialized when used here}} |
| 224 | } |
| 225 | |
Ted Kremenek | 40900ee | 2011-01-27 02:29:34 +0000 | [diff] [blame] | 226 | // Test that this case doesn't crash. |
| 227 | void test35(int x) { |
| 228 | __block int y = 0; |
| 229 | ^{ y = (x == 0); }(); |
| 230 | } |
| 231 | |
Ted Kremenek | 96554fd | 2011-01-27 18:51:39 +0000 | [diff] [blame^] | 232 | // Test handling of indirect goto. |
| 233 | void test36() |
| 234 | { |
| 235 | void **pc; // expected-warning{{use of uninitialized variable 'pc'}} expected-note{{ add initialization to silence this warning}} |
| 236 | void *dummy[] = { &&L1, &&L2 }; |
| 237 | L1: |
| 238 | goto *pc; // expected-note{{variable 'pc' is possibly uninitialized when used here}} |
| 239 | L2: |
| 240 | goto *pc; |
| 241 | } |
| 242 | |