Ted Kremenek | 0a65f94 | 2011-03-17 03:06:07 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -Wuninitialized -Wconditional-uninitialized -fsyntax-only -fblocks %s -verify |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 2 | |
Ted Kremenek | 540dda6 | 2011-08-23 20:30:50 +0000 | [diff] [blame] | 3 | typedef __typeof(sizeof(int)) size_t; |
| 4 | void *malloc(size_t); |
| 5 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 6 | int test1() { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 7 | int x; // expected-note{{initialize the variable 'x' to silence this warning}} |
Chandler Carruth | f04eb2d | 2011-04-08 06:33:38 +0000 | [diff] [blame] | 8 | return x; // expected-warning{{variable 'x' is uninitialized when used here}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 9 | } |
| 10 | |
| 11 | int test2() { |
| 12 | int x = 0; |
| 13 | return x; // no-warning |
| 14 | } |
| 15 | |
| 16 | int test3() { |
| 17 | int x; |
| 18 | x = 0; |
| 19 | return x; // no-warning |
| 20 | } |
| 21 | |
| 22 | int test4() { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 23 | int x; // expected-note{{initialize the variable 'x' to silence this warning}} |
Chandler Carruth | f04eb2d | 2011-04-08 06:33:38 +0000 | [diff] [blame] | 24 | ++x; // expected-warning{{variable 'x' is uninitialized when used here}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 25 | return x; |
| 26 | } |
| 27 | |
| 28 | int test5() { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 29 | int x, y; // expected-note{{initialize the variable 'y' to silence this warning}} |
Chandler Carruth | f04eb2d | 2011-04-08 06:33:38 +0000 | [diff] [blame] | 30 | x = y; // expected-warning{{variable 'y' is uninitialized when used here}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 31 | return x; |
| 32 | } |
| 33 | |
| 34 | int test6() { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 35 | int x; // expected-note{{initialize the variable 'x' to silence this warning}} |
Chandler Carruth | f04eb2d | 2011-04-08 06:33:38 +0000 | [diff] [blame] | 36 | x += 2; // expected-warning{{variable 'x' is uninitialized when used here}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 37 | return x; |
| 38 | } |
| 39 | |
| 40 | int test7(int y) { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 41 | int x; // expected-note{{initialize the variable 'x' to silence this warning}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 42 | if (y) |
| 43 | x = 1; |
Chandler Carruth | 584b9d6 | 2011-04-08 06:47:15 +0000 | [diff] [blame] | 44 | return x; // expected-warning{{variable 'x' may be uninitialized when used here}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | int test8(int y) { |
| 48 | int x; |
| 49 | if (y) |
| 50 | x = 1; |
| 51 | else |
| 52 | x = 0; |
Ted Kremenek | 609e317 | 2011-02-02 23:35:53 +0000 | [diff] [blame] | 53 | return x; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | int test9(int n) { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 57 | int x; // expected-note{{initialize the variable 'x' to silence this warning}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 58 | for (unsigned i = 0 ; i < n; ++i) { |
| 59 | if (i == n - 1) |
| 60 | break; |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 61 | x = 1; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 62 | } |
Chandler Carruth | 584b9d6 | 2011-04-08 06:47:15 +0000 | [diff] [blame] | 63 | return x; // expected-warning{{variable 'x' may be uninitialized when used here}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | int test10(unsigned n) { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 67 | int x; // expected-note{{initialize the variable 'x' to silence this warning}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 68 | for (unsigned i = 0 ; i < n; ++i) { |
| 69 | x = 1; |
| 70 | } |
Chandler Carruth | 584b9d6 | 2011-04-08 06:47:15 +0000 | [diff] [blame] | 71 | return x; // expected-warning{{variable 'x' may be uninitialized when used here}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | int test11(unsigned n) { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 75 | int x; // expected-note{{initialize the variable 'x' to silence this warning}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 76 | for (unsigned i = 0 ; i <= n; ++i) { |
| 77 | x = 1; |
| 78 | } |
Chandler Carruth | 584b9d6 | 2011-04-08 06:47:15 +0000 | [diff] [blame] | 79 | return x; // expected-warning{{variable 'x' may be uninitialized when used here}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void test12(unsigned n) { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 83 | for (unsigned i ; n ; ++i) ; // expected-warning{{variable 'i' is uninitialized when used here}} expected-note{{initialize the variable 'i' to silence this warning}} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | int test13() { |
| 87 | static int i; |
| 88 | return i; // no-warning |
| 89 | } |
| 90 | |
Ted Kremenek | c104e53 | 2011-01-18 04:53:25 +0000 | [diff] [blame] | 91 | // Simply don't crash on this test case. |
| 92 | void test14() { |
| 93 | const char *p = 0; |
| 94 | for (;;) {} |
| 95 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 96 | |
Ted Kremenek | 9e76172 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 97 | void test15() { |
| 98 | int x = x; // no-warning: signals intended lack of initialization. |
| 99 | } |
| 100 | |
| 101 | int test15b() { |
| 102 | // Warn here with the self-init, since it does result in a use of |
| 103 | // an unintialized variable and this is the root cause. |
| 104 | int x = x; // expected-warning {{variable 'x' is uninitialized when used within its own initialization}} |
| 105 | return x; |
Ted Kremenek | c104e53 | 2011-01-18 04:53:25 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // Don't warn in the following example; shows dataflow confluence. |
| 109 | char *test16_aux(); |
| 110 | void test16() { |
| 111 | char *p = test16_aux(); |
| 112 | for (unsigned i = 0 ; i < 100 ; i++) |
| 113 | p[i] = 'a'; // no-warning |
| 114 | } |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 115 | |
| 116 | void test17() { |
| 117 | // Don't warn multiple times about the same uninitialized variable |
| 118 | // along the same path. |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 119 | int *x; // expected-note{{initialize the variable 'x' to silence this warning}} |
Chandler Carruth | f04eb2d | 2011-04-08 06:33:38 +0000 | [diff] [blame] | 120 | *x = 1; // expected-warning{{variable 'x' is uninitialized when used here}} |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 121 | *x = 1; // no-warning |
| 122 | } |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 123 | |
| 124 | int test18(int x, int y) { |
| 125 | int z; |
| 126 | if (x && y && (z = 1)) { |
| 127 | return z; // no-warning |
| 128 | } |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | int test19_aux1(); |
| 133 | int test19_aux2(); |
| 134 | int test19_aux3(int *x); |
| 135 | int test19() { |
| 136 | int z; |
| 137 | if (test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z)) |
| 138 | return z; // no-warning |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | int test20() { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 143 | int z; // expected-note{{initialize the variable 'z' to silence this warning}} |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 144 | if ((test19_aux1() + test19_aux2() && test19_aux1()) || test19_aux3(&z)) |
Chandler Carruth | 584b9d6 | 2011-04-08 06:47:15 +0000 | [diff] [blame] | 145 | return z; // expected-warning{{variable 'z' may be uninitialized when used here}} |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | int test21(int x, int y) { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 150 | int z; // expected-note{{initialize the variable 'z' to silence this warning}} |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 151 | if ((x && y) || test19_aux3(&z) || test19_aux2()) |
Chandler Carruth | 584b9d6 | 2011-04-08 06:47:15 +0000 | [diff] [blame] | 152 | return z; // expected-warning{{variable 'z' may be uninitialized when used here}} |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | int test22() { |
| 157 | int z; |
| 158 | while (test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z)) |
| 159 | return z; // no-warning |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | int test23() { |
| 164 | int z; |
| 165 | for ( ; test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z) ; ) |
| 166 | return z; // no-warning |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | // The basic uninitialized value analysis doesn't have enough path-sensitivity |
| 171 | // to catch initializations relying on control-dependencies spanning multiple |
| 172 | // conditionals. This possibly can be handled by making the CFG itself |
| 173 | // represent such control-dependencies, but it is a niche case. |
| 174 | int test24(int flag) { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 175 | unsigned val; // expected-note{{initialize the variable 'val' to silence this warning}} |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 176 | if (flag) |
| 177 | val = 1; |
| 178 | if (!flag) |
| 179 | val = 1; |
Chandler Carruth | 584b9d6 | 2011-04-08 06:47:15 +0000 | [diff] [blame] | 180 | return val; // expected-warning{{variable 'val' may be uninitialized when used here}} |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Ted Kremenek | dcfb360 | 2011-01-21 22:49:49 +0000 | [diff] [blame] | 183 | float test25() { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 184 | float x; // expected-note{{initialize the variable 'x' to silence this warning}} |
Chandler Carruth | f04eb2d | 2011-04-08 06:33:38 +0000 | [diff] [blame] | 185 | return x; // expected-warning{{variable 'x' is uninitialized when used here}} |
Ted Kremenek | dcfb360 | 2011-01-21 22:49:49 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | typedef int MyInt; |
| 189 | MyInt test26() { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 190 | MyInt x; // expected-note{{initialize the variable 'x' to silence this warning}} |
Chandler Carruth | f04eb2d | 2011-04-08 06:33:38 +0000 | [diff] [blame] | 191 | return x; // expected-warning{{variable 'x' is uninitialized when used here}} |
Ted Kremenek | dcfb360 | 2011-01-21 22:49:49 +0000 | [diff] [blame] | 192 | } |
Ted Kremenek | 9660803 | 2011-01-23 17:53:04 +0000 | [diff] [blame] | 193 | |
| 194 | // Test handling of sizeof(). |
| 195 | int test27() { |
| 196 | struct test_27 { int x; } *y; |
| 197 | return sizeof(y->x); // no-warning |
| 198 | } |
| 199 | |
| 200 | int test28() { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 201 | int len; // expected-note{{initialize the variable 'len' to silence this warning}} |
Chandler Carruth | f04eb2d | 2011-04-08 06:33:38 +0000 | [diff] [blame] | 202 | return sizeof(int[len]); // expected-warning{{variable 'len' is uninitialized when used here}} |
Ted Kremenek | 9660803 | 2011-01-23 17:53:04 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 205 | void test29() { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 206 | int x; // expected-note{{initialize the variable 'x' to silence this warning}} |
Chandler Carruth | f04eb2d | 2011-04-08 06:33:38 +0000 | [diff] [blame] | 207 | (void) ^{ (void) x; }; // expected-warning{{variable 'x' is uninitialized when captured by block}} |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void test30() { |
| 211 | static int x; // no-warning |
| 212 | (void) ^{ (void) x; }; |
| 213 | } |
| 214 | |
| 215 | void test31() { |
| 216 | __block int x; // no-warning |
| 217 | (void) ^{ (void) x; }; |
| 218 | } |
| 219 | |
| 220 | int test32_x; |
| 221 | void test32() { |
| 222 | (void) ^{ (void) test32_x; }; // no-warning |
| 223 | } |
| 224 | |
Ted Kremenek | dd0f794 | 2011-01-26 04:49:43 +0000 | [diff] [blame] | 225 | void test_33() { |
| 226 | int x; // no-warning |
| 227 | (void) x; |
| 228 | } |
| 229 | |
| 230 | int test_34() { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 231 | int x; // expected-note{{initialize the variable 'x' to silence this warning}} |
Ted Kremenek | dd0f794 | 2011-01-26 04:49:43 +0000 | [diff] [blame] | 232 | (void) x; |
Chandler Carruth | f04eb2d | 2011-04-08 06:33:38 +0000 | [diff] [blame] | 233 | return x; // expected-warning{{variable 'x' is uninitialized when used here}} |
Ted Kremenek | dd0f794 | 2011-01-26 04:49:43 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Ted Kremenek | 40900ee | 2011-01-27 02:29:34 +0000 | [diff] [blame] | 236 | // Test that this case doesn't crash. |
| 237 | void test35(int x) { |
| 238 | __block int y = 0; |
| 239 | ^{ y = (x == 0); }(); |
| 240 | } |
| 241 | |
Ted Kremenek | 96554fd | 2011-01-27 18:51:39 +0000 | [diff] [blame] | 242 | // Test handling of indirect goto. |
| 243 | void test36() |
| 244 | { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 245 | void **pc; // expected-note{{initialize the variable 'pc' to silence this warning}} |
Ted Kremenek | 96554fd | 2011-01-27 18:51:39 +0000 | [diff] [blame] | 246 | void *dummy[] = { &&L1, &&L2 }; |
| 247 | L1: |
Chandler Carruth | d837c0d | 2011-07-22 05:27:52 +0000 | [diff] [blame] | 248 | goto *pc; // expected-warning{{variable 'pc' is uninitialized when used here}} |
Ted Kremenek | 96554fd | 2011-01-27 18:51:39 +0000 | [diff] [blame] | 249 | L2: |
| 250 | goto *pc; |
| 251 | } |
| 252 | |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 253 | // Test && nested in ||. |
| 254 | int test37_a(); |
| 255 | int test37_b(); |
| 256 | int test37() |
| 257 | { |
| 258 | int identifier; |
| 259 | if ((test37_a() && (identifier = 1)) || |
| 260 | (test37_b() && (identifier = 2))) { |
| 261 | return identifier; // no-warning |
| 262 | } |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | // Test merging of path-specific dataflow values (without asserting). |
| 267 | int test38(int r, int x, int y) |
| 268 | { |
| 269 | int z; |
| 270 | return ((r < 0) || ((r == 0) && (x < y))); |
| 271 | } |
| 272 | |
Ted Kremenek | f3f5379 | 2011-03-15 03:17:01 +0000 | [diff] [blame] | 273 | int test39(int x) { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 274 | int y; // expected-note{{initialize the variable 'y' to silence this warning}} |
Chandler Carruth | f04eb2d | 2011-04-08 06:33:38 +0000 | [diff] [blame] | 275 | int z = x + y; // expected-warning {{variable 'y' is uninitialized when used here}} |
Ted Kremenek | f3f5379 | 2011-03-15 03:17:01 +0000 | [diff] [blame] | 276 | return z; |
| 277 | } |
| 278 | |
| 279 | |
| 280 | int test40(int x) { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 281 | int y; // expected-note{{initialize the variable 'y' to silence this warning}} |
Chandler Carruth | f04eb2d | 2011-04-08 06:33:38 +0000 | [diff] [blame] | 282 | return x ? 1 : y; // expected-warning {{variable 'y' is uninitialized when used here}} |
Ted Kremenek | f3f5379 | 2011-03-15 03:17:01 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | int test41(int x) { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 286 | int y; // expected-note{{initialize the variable 'y' to silence this warning}} |
Ted Kremenek | f3f5379 | 2011-03-15 03:17:01 +0000 | [diff] [blame] | 287 | if (x) y = 1; // no-warning |
Chandler Carruth | 584b9d6 | 2011-04-08 06:47:15 +0000 | [diff] [blame] | 288 | return y; // expected-warning {{variable 'y' may be uninitialized when used here}} |
Ted Kremenek | f3f5379 | 2011-03-15 03:17:01 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | void test42() { |
| 292 | int a; |
| 293 | a = 30; // no-warning |
| 294 | } |
| 295 | |
| 296 | void test43_aux(int x); |
| 297 | void test43(int i) { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 298 | int x; // expected-note{{initialize the variable 'x' to silence this warning}} |
Ted Kremenek | f3f5379 | 2011-03-15 03:17:01 +0000 | [diff] [blame] | 299 | for (i = 0 ; i < 10; i++) |
Chandler Carruth | d837c0d | 2011-07-22 05:27:52 +0000 | [diff] [blame] | 300 | test43_aux(x++); // expected-warning {{variable 'x' is uninitialized when used here}} |
Ted Kremenek | f3f5379 | 2011-03-15 03:17:01 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | void test44(int i) { |
| 304 | int x = i; |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 305 | int y; // expected-note{{initialize the variable 'y' to silence this warning}} |
Ted Kremenek | f3f5379 | 2011-03-15 03:17:01 +0000 | [diff] [blame] | 306 | for (i = 0; i < 10; i++ ) { |
| 307 | test43_aux(x++); // no-warning |
Chandler Carruth | d837c0d | 2011-07-22 05:27:52 +0000 | [diff] [blame] | 308 | x += y; // expected-warning {{variable 'y' is uninitialized when used here}} |
Ted Kremenek | f3f5379 | 2011-03-15 03:17:01 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
| 312 | int test45(int j) { |
| 313 | int x = 1, y = x + 1; |
| 314 | if (y) // no-warning |
| 315 | return x; |
| 316 | return y; |
| 317 | } |
| 318 | |
| 319 | void test46() |
| 320 | { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 321 | int i; // expected-note{{initialize the variable 'i' to silence this warning}} |
Chandler Carruth | f04eb2d | 2011-04-08 06:33:38 +0000 | [diff] [blame] | 322 | int j = i ? : 1; // expected-warning {{variable 'i' is uninitialized when used here}} |
Ted Kremenek | f3f5379 | 2011-03-15 03:17:01 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | void *test47(int *i) |
| 326 | { |
| 327 | return i ? : 0; // no-warning |
| 328 | } |
| 329 | |
| 330 | void *test49(int *i) |
| 331 | { |
| 332 | int a; |
| 333 | return &a ? : i; // no-warning |
| 334 | } |
| 335 | |
| 336 | void test50() |
| 337 | { |
| 338 | char c[1 ? : 2]; // no-warning |
| 339 | } |
| 340 | |
Ted Kremenek | bc8b44c | 2011-03-31 22:32:41 +0000 | [diff] [blame] | 341 | int test51(void) |
| 342 | { |
| 343 | __block int a; |
| 344 | ^(void) { |
| 345 | a = 42; |
| 346 | }(); |
| 347 | return a; // no-warning |
| 348 | } |
| 349 | |
Ted Kremenek | e6c2803 | 2011-05-10 22:10:35 +0000 | [diff] [blame] | 350 | // FIXME: This is a false positive, but it tests logical operations in switch statements. |
| 351 | int test52(int a, int b) { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 352 | int x; // expected-note {{initialize the variable 'x' to silence this warning}} |
Ted Kremenek | e6c2803 | 2011-05-10 22:10:35 +0000 | [diff] [blame] | 353 | switch (a || b) { // expected-warning {{switch condition has boolean value}} |
| 354 | case 0: |
| 355 | x = 1; |
| 356 | break; |
| 357 | case 1: |
| 358 | x = 2; |
| 359 | break; |
| 360 | } |
| 361 | return x; // expected-warning {{variable 'x' may be uninitialized when used here}} |
| 362 | } |
Chandler Carruth | 8435069 | 2011-07-16 22:27:02 +0000 | [diff] [blame] | 363 | |
Ted Kremenek | d626ec4 | 2011-07-19 20:33:49 +0000 | [diff] [blame] | 364 | void test53() { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 365 | int x; // expected-note {{initialize the variable 'x' to silence this warning}} |
Ted Kremenek | 62d126e | 2011-07-19 21:41:51 +0000 | [diff] [blame] | 366 | int y = (x); // expected-warning {{variable 'x' is uninitialized when used here}} |
Ted Kremenek | d626ec4 | 2011-07-19 20:33:49 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Chandler Carruth | 8435069 | 2011-07-16 22:27:02 +0000 | [diff] [blame] | 369 | // This CFG caused the uninitialized values warning to inf-loop. |
| 370 | extern int PR10379_g(); |
| 371 | void PR10379_f(int *len) { |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 372 | int new_len; // expected-note{{initialize the variable 'new_len' to silence this warning}} |
Chandler Carruth | 8435069 | 2011-07-16 22:27:02 +0000 | [diff] [blame] | 373 | for (int i = 0; i < 42 && PR10379_g() == 0; i++) { |
| 374 | if (PR10379_g() == 1) |
| 375 | continue; |
| 376 | if (PR10379_g() == 2) |
| 377 | PR10379_f(&new_len); |
| 378 | else if (PR10379_g() == 3) |
| 379 | PR10379_f(&new_len); |
| 380 | *len += new_len; // expected-warning {{variable 'new_len' may be uninitialized when used here}} |
| 381 | } |
| 382 | } |
Ted Kremenek | 540dda6 | 2011-08-23 20:30:50 +0000 | [diff] [blame] | 383 | |
| 384 | // Test that sizeof(VLA) doesn't trigger a warning. |
| 385 | void test_vla_sizeof(int x) { |
| 386 | double (*memory)[2][x] = malloc(sizeof(*memory)); // no-warning |
| 387 | } |
| 388 | |
Ted Kremenek | 6f27542 | 2011-09-02 19:39:26 +0000 | [diff] [blame] | 389 | // Test absurd case of deadcode + use of blocks. This previously was a false positive |
| 390 | // due to an analysis bug. |
| 391 | int test_block_and_dead_code() { |
| 392 | __block int x; |
| 393 | ^{ x = 1; }(); |
| 394 | if (0) |
| 395 | return x; |
| 396 | return x; // no-warning |
| 397 | } |
| 398 | |
Ted Kremenek | c5f740e | 2011-10-07 00:42:48 +0000 | [diff] [blame] | 399 | // This previously triggered an infinite loop in the analysis. |
| 400 | void PR11069(int a, int b) { |
| 401 | unsigned long flags; |
| 402 | for (;;) { |
| 403 | if (a && !b) |
| 404 | break; |
| 405 | } |
| 406 | for (;;) { |
| 407 | // This does not trigger a warning because it isn't a real use. |
| 408 | (void)(flags); // no-warning |
| 409 | } |
| 410 | } |
| 411 | |
Ted Kremenek | aa2176b | 2011-10-07 00:52:56 +0000 | [diff] [blame] | 412 | // Test uninitialized value used in loop condition. |
| 413 | void rdar9432305(float *P) { |
| 414 | int i; // expected-note {{initialize the variable 'i' to silence this warning}} |
| 415 | for (; i < 10000; ++i) // expected-warning {{variable 'i' is uninitialized when used here}} |
| 416 | P[i] = 0.0f; |
| 417 | } |
Ted Kremenek | c5f740e | 2011-10-07 00:42:48 +0000 | [diff] [blame] | 418 | |