Ted Kremenek | ebd42f4 | 2010-03-18 01:22:39 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -Wunused-variable -analyze -analyzer-experimental-internal-checks -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s |
| 2 | // RUN: %clang_cc1 -Wunused-variable -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=basic -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s |
| 3 | // RUN: %clang_cc1 -Wunused-variable -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=range -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s |
| 4 | // RUN: %clang_cc1 -Wunused-variable -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=basic -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s |
| 5 | // RUN: %clang_cc1 -Wunused-variable -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=range -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s |
Ted Kremenek | 8aefcbf | 2007-11-19 06:38:23 +0000 | [diff] [blame] | 6 | |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 7 | void f1() { |
Ted Kremenek | ebd42f4 | 2010-03-18 01:22:39 +0000 | [diff] [blame] | 8 | int k, y; // expected-warning{{unused variable 'k'}} expected-warning{{unused variable 'y'}} |
Ted Kremenek | 0fdf06e | 2008-03-19 07:31:52 +0000 | [diff] [blame] | 9 | int abc=1; |
Ted Kremenek | ebd42f4 | 2010-03-18 01:22:39 +0000 | [diff] [blame] | 10 | long idx=abc+3*5; // expected-warning {{never read}} expected-warning{{unused variable 'idx'}} |
Ted Kremenek | aa395ba | 2007-11-18 20:06:35 +0000 | [diff] [blame] | 11 | } |
Ted Kremenek | 8aefcbf | 2007-11-19 06:38:23 +0000 | [diff] [blame] | 12 | |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 13 | void f2(void *b) { |
Ted Kremenek | 8aefcbf | 2007-11-19 06:38:23 +0000 | [diff] [blame] | 14 | char *c = (char*)b; // no-warning |
Ted Kremenek | ebd42f4 | 2010-03-18 01:22:39 +0000 | [diff] [blame] | 15 | char *d = b+1; // expected-warning {{never read}} expected-warning{{unused variable 'd'}} |
Douglas Gregor | a316e7b | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 16 | printf("%s", c); // expected-warning{{implicitly declaring C library function 'printf' with type 'int (char const *, ...)'}} \ |
| 17 | // expected-note{{please include the header <stdio.h> or explicitly provide a declaration for 'printf'}} |
Ted Kremenek | 8aefcbf | 2007-11-19 06:38:23 +0000 | [diff] [blame] | 18 | } |
Ted Kremenek | 74c43a0 | 2007-11-20 03:03:00 +0000 | [diff] [blame] | 19 | |
Chris Lattner | e030358 | 2010-01-09 20:43:19 +0000 | [diff] [blame] | 20 | int f(); |
| 21 | |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 22 | void f3() { |
Ted Kremenek | 0fdf06e | 2008-03-19 07:31:52 +0000 | [diff] [blame] | 23 | int r; |
| 24 | if ((r = f()) != 0) { // no-warning |
| 25 | int y = r; // no-warning |
| 26 | printf("the error is: %d\n", y); |
| 27 | } |
Ted Kremenek | 74c43a0 | 2007-11-20 03:03:00 +0000 | [diff] [blame] | 28 | } |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 29 | |
| 30 | void f4(int k) { |
| 31 | |
| 32 | k = 1; |
| 33 | |
| 34 | if (k) |
| 35 | f1(); |
| 36 | |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 37 | k = 2; // expected-warning {{never read}} |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 38 | } |
Ted Kremenek | 93fab7c | 2009-11-22 20:26:21 +0000 | [diff] [blame] | 39 | |
Ted Kremenek | f87821c | 2008-04-15 18:37:29 +0000 | [diff] [blame] | 40 | void f5() { |
| 41 | |
| 42 | int x = 4; // no-warning |
Ted Kremenek | ebd42f4 | 2010-03-18 01:22:39 +0000 | [diff] [blame] | 43 | int *p = &x; // expected-warning{{never read}} expected-warning{{unused variable 'p'}} |
Ted Kremenek | f87821c | 2008-04-15 18:37:29 +0000 | [diff] [blame] | 44 | |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | int f6() { |
| 48 | |
| 49 | int x = 4; |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 50 | ++x; // expected-warning{{never read}} |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 51 | return 1; |
| 52 | } |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 53 | |
| 54 | int f7(int *p) { |
| 55 | // This is allowed for defensive programming. |
| 56 | p = 0; // no-warning |
| 57 | return 1; |
| 58 | } |
| 59 | |
Ted Kremenek | 93fab7c | 2009-11-22 20:26:21 +0000 | [diff] [blame] | 60 | int f7b(int *p) { |
| 61 | // This is allowed for defensive programming. |
| 62 | p = (0); // no-warning |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | int f7c(int *p) { |
| 67 | // This is allowed for defensive programming. |
| 68 | p = (void*) 0; // no-warning |
| 69 | return 1; |
| 70 | } |
| 71 | |
| 72 | int f7d(int *p) { |
| 73 | // This is allowed for defensive programming. |
| 74 | p = (void*) (0); // no-warning |
| 75 | return 1; |
| 76 | } |
| 77 | |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 78 | int f8(int *p) { |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 79 | extern int *baz(); |
John McCall | f66d5cd | 2009-10-13 17:57:23 +0000 | [diff] [blame] | 80 | if ((p = baz())) // expected-warning{{Although the value}} |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 81 | return 1; |
| 82 | return 0; |
| 83 | } |
| 84 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 85 | int f9() { |
| 86 | int x = 4; |
| 87 | x = x + 10; // expected-warning{{never read}} |
| 88 | return 1; |
| 89 | } |
| 90 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 91 | int f10() { |
| 92 | int x = 4; |
| 93 | x = 10 + x; // expected-warning{{never read}} |
| 94 | return 1; |
| 95 | } |
| 96 | |
Ted Kremenek | 8b00b6e | 2008-07-23 23:18:43 +0000 | [diff] [blame] | 97 | int f11() { |
| 98 | int x = 4; |
Ted Kremenek | 380277e | 2008-10-15 05:23:41 +0000 | [diff] [blame] | 99 | return x++; // expected-warning{{never read}} |
Ted Kremenek | 8b00b6e | 2008-07-23 23:18:43 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Ted Kremenek | 380277e | 2008-10-15 05:23:41 +0000 | [diff] [blame] | 102 | int f11b() { |
| 103 | int x = 4; |
Ted Kremenek | 7f5fce7 | 2009-01-20 00:47:45 +0000 | [diff] [blame] | 104 | return ((((++x)))); // no-warning |
Ted Kremenek | 380277e | 2008-10-15 05:23:41 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Ted Kremenek | fc7ff55 | 2008-07-25 04:47:34 +0000 | [diff] [blame] | 107 | int f12a(int y) { |
Ted Kremenek | ebd42f4 | 2010-03-18 01:22:39 +0000 | [diff] [blame] | 108 | int x = y; // expected-warning{{unused variable 'x'}} |
Ted Kremenek | fc7ff55 | 2008-07-25 04:47:34 +0000 | [diff] [blame] | 109 | return 1; |
| 110 | } |
| 111 | int f12b(int y) { |
| 112 | int x __attribute__((unused)) = y; // no-warning |
| 113 | return 1; |
| 114 | } |
Ted Kremenek | ebd42f4 | 2010-03-18 01:22:39 +0000 | [diff] [blame] | 115 | int f12c(int y) { |
| 116 | // Allow initialiation of scalar variables by parameters as a form of |
| 117 | // defensive programming. |
| 118 | int x = y; // no-warning |
| 119 | x = 1; |
| 120 | return x; |
| 121 | } |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | efe88f5 | 2008-08-06 23:26:31 +0000 | [diff] [blame] | 123 | // Filed with PR 2630. This code should produce no warnings. |
| 124 | int f13(void) |
| 125 | { |
| 126 | int a = 1; |
| 127 | int b, c = b = a + a; |
| 128 | |
| 129 | if (b > 0) |
| 130 | return (0); |
| 131 | |
| 132 | return (a + b + c); |
| 133 | } |
| 134 | |
Ted Kremenek | b497ebd | 2008-09-04 21:52:52 +0000 | [diff] [blame] | 135 | // Filed with PR 2763. |
Ted Kremenek | 84fa6b9 | 2008-09-26 05:52:45 +0000 | [diff] [blame] | 136 | int f14(int count) { |
Ted Kremenek | b497ebd | 2008-09-04 21:52:52 +0000 | [diff] [blame] | 137 | int index, nextLineIndex; |
| 138 | for (index = 0; index < count; index = nextLineIndex+1) { |
| 139 | nextLineIndex = index+1; // no-warning |
| 140 | continue; |
| 141 | } |
| 142 | return index; |
| 143 | } |
Ted Kremenek | 84fa6b9 | 2008-09-26 05:52:45 +0000 | [diff] [blame] | 144 | |
| 145 | // Test case for <rdar://problem/6248086> |
| 146 | void f15(unsigned x, unsigned y) { |
| 147 | int count = x * y; // no-warning |
Ted Kremenek | ebd42f4 | 2010-03-18 01:22:39 +0000 | [diff] [blame] | 148 | int z[count]; // expected-warning{{unused variable 'z'}} |
Ted Kremenek | 84fa6b9 | 2008-09-26 05:52:45 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Ted Kremenek | 610a09e | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 151 | int f16(int x) { |
| 152 | x = x * 2; |
Ted Kremenek | d2025e2 | 2008-09-26 23:05:47 +0000 | [diff] [blame] | 153 | x = sizeof(int [x = (x || x + 1) * 2]) // expected-warning{{Although the value stored to 'x' is used}} |
| 154 | ? 5 : 8; |
Ted Kremenek | 610a09e | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 155 | return x; |
| 156 | } |
| 157 | |
Ted Kremenek | 3b58786 | 2009-01-09 22:15:01 +0000 | [diff] [blame] | 158 | // Self-assignments should not be flagged as dead stores. |
Mike Stump | a5495ea | 2009-07-21 19:01:31 +0000 | [diff] [blame] | 159 | void f17() { |
Ted Kremenek | 3b58786 | 2009-01-09 22:15:01 +0000 | [diff] [blame] | 160 | int x = 1; |
| 161 | x = x; // no-warning |
| 162 | } |
Ted Kremenek | 7f5fce7 | 2009-01-20 00:47:45 +0000 | [diff] [blame] | 163 | |
| 164 | // <rdar://problem/6506065> |
| 165 | // The values of dead stores are only "consumed" in an enclosing expression |
Mike Stump | cd7bf23 | 2009-07-17 01:04:31 +0000 | [diff] [blame] | 166 | // what that value is actually used. In other words, don't say "Although the |
| 167 | // value stored to 'x' is used...". |
Ted Kremenek | 7f5fce7 | 2009-01-20 00:47:45 +0000 | [diff] [blame] | 168 | int f18() { |
| 169 | int x = 0; // no-warning |
| 170 | if (1) |
| 171 | x = 10; // expected-warning{{Value stored to 'x' is never read}} |
| 172 | while (1) |
| 173 | x = 10; // expected-warning{{Value stored to 'x' is never read}} |
| 174 | do |
| 175 | x = 10; // expected-warning{{Value stored to 'x' is never read}} |
| 176 | while (1); |
| 177 | |
| 178 | return (x = 10); // expected-warning{{Although the value stored to 'x' is used in the enclosing expression, the value is never actually read from 'x'}} |
| 179 | } |
Ted Kremenek | d3098ee | 2009-02-09 18:01:00 +0000 | [diff] [blame] | 180 | |
| 181 | // PR 3514: false positive `dead initialization` warning for init to global |
| 182 | // http://llvm.org/bugs/show_bug.cgi?id=3514 |
| 183 | extern const int MyConstant; |
| 184 | int f19(void) { |
| 185 | int x = MyConstant; // no-warning |
| 186 | x = 1; |
| 187 | return x; |
| 188 | } |
| 189 | |
Ted Kremenek | 28433ff | 2009-03-31 03:34:38 +0000 | [diff] [blame] | 190 | int f19b(void) { // This case is the same as f19. |
Ted Kremenek | d3098ee | 2009-02-09 18:01:00 +0000 | [diff] [blame] | 191 | const int MyConstant = 0; |
Ted Kremenek | 28433ff | 2009-03-31 03:34:38 +0000 | [diff] [blame] | 192 | int x = MyConstant; // no-warning |
Ted Kremenek | d3098ee | 2009-02-09 18:01:00 +0000 | [diff] [blame] | 193 | x = 1; |
| 194 | return x; |
| 195 | } |
Ted Kremenek | 632d1ec | 2009-03-23 22:30:58 +0000 | [diff] [blame] | 196 | |
| 197 | void f20(void) { |
| 198 | int x = 1; // no-warning |
| 199 | #pragma unused(x) |
| 200 | } |
| 201 | |
Mike Stump | cd7bf23 | 2009-07-17 01:04:31 +0000 | [diff] [blame] | 202 | void halt() __attribute__((noreturn)); |
| 203 | int f21() { |
| 204 | int x = 4; |
| 205 | |
| 206 | ++x; // expected-warning{{never read}} |
| 207 | if (1) { |
| 208 | halt(); |
| 209 | (void)x; |
| 210 | } |
| 211 | return 1; |
| 212 | } |
Mike Stump | e5af3ce | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 213 | |
| 214 | int j; |
| 215 | void f22() { |
| 216 | int x = 4; |
| 217 | int y1 = 4; |
| 218 | int y2 = 4; |
| 219 | int y3 = 4; |
| 220 | int y4 = 4; |
| 221 | int y5 = 4; |
| 222 | int y6 = 4; |
| 223 | int y7 = 4; |
| 224 | int y8 = 4; |
| 225 | int y9 = 4; |
| 226 | int y10 = 4; |
Mike Stump | 5f20363 | 2009-07-21 00:38:52 +0000 | [diff] [blame] | 227 | int y11 = 4; |
| 228 | int y12 = 4; |
Mike Stump | fefb9f7 | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 229 | int y13 = 4; |
| 230 | int y14 = 4; |
| 231 | int y15 = 4; |
Mike Stump | 8f9893a | 2009-07-21 01:27:50 +0000 | [diff] [blame] | 232 | int y16 = 4; |
| 233 | int y17 = 4; |
| 234 | int y18 = 4; |
Mike Stump | 22cd658 | 2009-07-21 01:46:17 +0000 | [diff] [blame] | 235 | int y19 = 4; |
| 236 | int y20 = 4; |
Mike Stump | e5af3ce | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 237 | |
| 238 | ++x; // expected-warning{{never read}} |
| 239 | ++y1; |
| 240 | ++y2; |
| 241 | ++y3; |
| 242 | ++y4; |
| 243 | ++y5; |
| 244 | ++y6; |
| 245 | ++y7; |
| 246 | ++y8; |
| 247 | ++y9; |
| 248 | ++y10; |
Mike Stump | 5f20363 | 2009-07-21 00:38:52 +0000 | [diff] [blame] | 249 | ++y11; |
| 250 | ++y12; |
Mike Stump | fefb9f7 | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 251 | ++y13; |
| 252 | ++y14; |
| 253 | ++y15; |
Mike Stump | 8f9893a | 2009-07-21 01:27:50 +0000 | [diff] [blame] | 254 | ++y16; |
| 255 | ++y17; |
| 256 | ++y18; |
Mike Stump | 22cd658 | 2009-07-21 01:46:17 +0000 | [diff] [blame] | 257 | ++y19; |
| 258 | ++y20; |
Mike Stump | e5af3ce | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 259 | |
| 260 | switch (j) { |
| 261 | case 1: |
| 262 | if (0) |
| 263 | (void)x; |
| 264 | if (1) { |
| 265 | (void)y1; |
| 266 | return; |
| 267 | } |
| 268 | (void)x; |
| 269 | break; |
| 270 | case 2: |
| 271 | if (0) |
| 272 | (void)x; |
| 273 | else { |
| 274 | (void)y2; |
| 275 | return; |
| 276 | } |
| 277 | (void)x; |
| 278 | break; |
| 279 | case 3: |
| 280 | if (1) { |
| 281 | (void)y3; |
| 282 | return; |
| 283 | } else |
| 284 | (void)x; |
| 285 | (void)x; |
| 286 | break; |
| 287 | case 4: |
| 288 | 0 ? : ((void)y4, ({ return; })); |
| 289 | (void)x; |
| 290 | break; |
| 291 | case 5: |
| 292 | 1 ? : (void)x; |
| 293 | 0 ? (void)x : ((void)y5, ({ return; })); |
| 294 | (void)x; |
| 295 | break; |
| 296 | case 6: |
| 297 | 1 ? ((void)y6, ({ return; })) : (void)x; |
| 298 | (void)x; |
| 299 | break; |
| 300 | case 7: |
| 301 | (void)(0 && x); |
| 302 | (void)y7; |
| 303 | (void)(0 || (y8, ({ return; }), 1)); |
| 304 | (void)x; |
| 305 | break; |
| 306 | case 8: |
| 307 | (void)(1 && (y9, ({ return; }), 1)); |
| 308 | (void)x; |
| 309 | break; |
| 310 | case 9: |
| 311 | (void)(1 || x); |
| 312 | (void)y10; |
Mike Stump | 5f20363 | 2009-07-21 00:38:52 +0000 | [diff] [blame] | 313 | break; |
| 314 | case 10: |
Mike Stump | 5f20363 | 2009-07-21 00:38:52 +0000 | [diff] [blame] | 315 | while (0) { |
| 316 | (void)x; |
| 317 | } |
Mike Stump | 8f9893a | 2009-07-21 01:27:50 +0000 | [diff] [blame] | 318 | (void)y11; |
Mike Stump | 5f20363 | 2009-07-21 00:38:52 +0000 | [diff] [blame] | 319 | break; |
Mike Stump | 8f9893a | 2009-07-21 01:27:50 +0000 | [diff] [blame] | 320 | case 11: |
| 321 | while (1) { |
| 322 | (void)y12; |
Mike Stump | fefb9f7 | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 323 | } |
| 324 | (void)x; |
| 325 | break; |
Mike Stump | 8f9893a | 2009-07-21 01:27:50 +0000 | [diff] [blame] | 326 | case 12: |
| 327 | do { |
| 328 | (void)y13; |
| 329 | } while (0); |
| 330 | (void)y14; |
| 331 | break; |
| 332 | case 13: |
| 333 | do { |
| 334 | (void)y15; |
| 335 | } while (1); |
| 336 | (void)x; |
| 337 | break; |
Mike Stump | fefb9f7 | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 338 | case 14: |
Mike Stump | 8f9893a | 2009-07-21 01:27:50 +0000 | [diff] [blame] | 339 | for (;;) { |
| 340 | (void)y16; |
| 341 | } |
| 342 | (void)x; |
| 343 | break; |
| 344 | case 15: |
| 345 | for (;1;) { |
| 346 | (void)y17; |
| 347 | } |
| 348 | (void)x; |
| 349 | break; |
| 350 | case 16: |
Mike Stump | fefb9f7 | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 351 | for (;0;) { |
| 352 | (void)x; |
| 353 | } |
Mike Stump | 8f9893a | 2009-07-21 01:27:50 +0000 | [diff] [blame] | 354 | (void)y18; |
Mike Stump | fefb9f7 | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 355 | break; |
Mike Stump | 22cd658 | 2009-07-21 01:46:17 +0000 | [diff] [blame] | 356 | case 17: |
| 357 | __builtin_choose_expr(0, (void)x, ((void)y19, ({ return; }))); |
| 358 | (void)x; |
| 359 | break; |
| 360 | case 19: |
| 361 | __builtin_choose_expr(1, ((void)y20, ({ return; })), (void)x); |
| 362 | (void)x; |
| 363 | break; |
Mike Stump | e5af3ce | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 364 | } |
| 365 | } |
Ted Kremenek | 3a97634 | 2009-11-26 06:55:36 +0000 | [diff] [blame] | 366 | |
| 367 | void f23_aux(const char* s); |
| 368 | void f23(int argc, char **argv) { |
| 369 | int shouldLog = (argc > 1); // no-warning |
| 370 | ^{ |
| 371 | if (shouldLog) f23_aux("I did too use it!\n"); |
| 372 | else f23_aux("I shouldn't log. Wait.. d'oh!\n"); |
| 373 | }(); |
| 374 | } |
| 375 | |
| 376 | void f23_pos(int argc, char **argv) { |
Ted Kremenek | ebd42f4 | 2010-03-18 01:22:39 +0000 | [diff] [blame] | 377 | int shouldLog = (argc > 1); // expected-warning{{Value stored to 'shouldLog' during its initialization is never read}} expected-warning{{unused variable 'shouldLog'}} |
Ted Kremenek | 3a97634 | 2009-11-26 06:55:36 +0000 | [diff] [blame] | 378 | ^{ |
| 379 | f23_aux("I did too use it!\n"); |
| 380 | }(); |
| 381 | } |
Ted Kremenek | 9a0459c | 2009-12-01 23:04:14 +0000 | [diff] [blame] | 382 | |
| 383 | void f24_A(int y) { |
| 384 | // FIXME: One day this should be reported as dead since 'z = x + y' is dead. |
| 385 | int x = (y > 2); // no-warning |
| 386 | ^ { |
Ted Kremenek | ebd42f4 | 2010-03-18 01:22:39 +0000 | [diff] [blame] | 387 | int z = x + y; // expected-warning{{Value stored to 'z' during its initialization is never read}} expected-warning{{unused variable 'z'}} |
Ted Kremenek | 9a0459c | 2009-12-01 23:04:14 +0000 | [diff] [blame] | 388 | }(); |
| 389 | } |
| 390 | |
| 391 | void f24_B(int y) { |
| 392 | // FIXME: One day this should be reported as dead since 'x' is just overwritten. |
| 393 | __block int x = (y > 2); // no-warning |
| 394 | ^{ |
| 395 | // FIXME: This should eventually be a dead store since it is never read either. |
| 396 | x = 5; // no-warning |
| 397 | }(); |
| 398 | } |
| 399 | |
| 400 | int f24_C(int y) { |
| 401 | // FIXME: One day this should be reported as dead since 'x' is just overwritten. |
| 402 | __block int x = (y > 2); // no-warning |
| 403 | ^{ |
| 404 | x = 5; // no-warning |
| 405 | }(); |
| 406 | return x; |
| 407 | } |
| 408 | |
| 409 | int f24_D(int y) { |
| 410 | __block int x = (y > 2); // no-warning |
| 411 | ^{ |
| 412 | if (y > 4) |
| 413 | x = 5; // no-warning |
| 414 | }(); |
| 415 | return x; |
| 416 | } |
| 417 | |
Ted Kremenek | 74635d8 | 2009-12-03 00:46:16 +0000 | [diff] [blame] | 418 | // This example shows that writing to a variable captured by a block means that it might |
| 419 | // not be dead. |
| 420 | int f25(int y) { |
| 421 | __block int x = (y > 2); |
| 422 | __block int z = 0; |
| 423 | void (^foo)() = ^{ z = x + y; }; |
| 424 | x = 4; // no-warning |
| 425 | foo(); |
| 426 | return z; |
| 427 | } |
| 428 | |
| 429 | // This test is mostly the same as 'f25', but shows that the heuristic of pruning out dead |
| 430 | // stores for variables that are just marked '__block' is overly conservative. |
| 431 | int f25_b(int y) { |
| 432 | // FIXME: we should eventually report a dead store here. |
| 433 | __block int x = (y > 2); |
| 434 | __block int z = 0; |
| 435 | x = 4; // no-warning |
| 436 | return z; |
| 437 | } |
| 438 | |
Ted Kremenek | 2cfe28b | 2010-03-10 00:18:11 +0000 | [diff] [blame] | 439 | int f26_nestedblocks() { |
| 440 | int z; |
| 441 | z = 1; |
| 442 | __block int y = 0; |
| 443 | ^{ |
| 444 | int k; |
| 445 | k = 1; // expected-warning{{Value stored to 'k' is never read}} |
| 446 | ^{ |
| 447 | y = z + 1; |
| 448 | }(); |
| 449 | }(); |
| 450 | return y; |
| 451 | } |
| 452 | |
Ted Kremenek | 3f64a0e | 2010-05-21 20:30:15 +0000 | [diff] [blame^] | 453 | // The FOREACH macro in QT uses 'break' statements within statement expressions |
| 454 | // placed within the increment code of for loops. |
| 455 | void rdar8014335() { |
| 456 | for (int i = 0 ; i != 10 ; ({ break; })) { |
| 457 | for ( ; ; ({ ++i; break; })) ; |
| 458 | // Note that the next value stored to 'i' is never executed |
| 459 | // because the next statement to be executed is the 'break' |
| 460 | // in the increment code of the first loop. |
| 461 | i = i * 3; // expected-warning{{Value stored to 'i' is never read}} |
| 462 | } |
| 463 | } |
| 464 | |