Daniel Dunbar | d7d5f02 | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 1 | // RUN: clang-cc -analyze -warn-dead-stores -verify %s && |
Daniel Dunbar | d7d5f02 | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 2 | // RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=basic -warn-dead-stores -verify %s && |
| 3 | // RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=range -warn-dead-stores -verify %s && |
| 4 | // RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=basic -warn-dead-stores -verify %s && |
| 5 | // RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=range -warn-dead-stores -verify %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 | aa395ba | 2007-11-18 20:06:35 +0000 | [diff] [blame] | 8 | int k, y; |
Ted Kremenek | 0fdf06e | 2008-03-19 07:31:52 +0000 | [diff] [blame] | 9 | int abc=1; |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 10 | long idx=abc+3*5; // expected-warning {{never read}} |
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 | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 15 | char *d = b+1; // expected-warning {{never read}} |
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 | |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 20 | void f3() { |
Ted Kremenek | 0fdf06e | 2008-03-19 07:31:52 +0000 | [diff] [blame] | 21 | int r; |
| 22 | if ((r = f()) != 0) { // no-warning |
| 23 | int y = r; // no-warning |
| 24 | printf("the error is: %d\n", y); |
| 25 | } |
Ted Kremenek | 74c43a0 | 2007-11-20 03:03:00 +0000 | [diff] [blame] | 26 | } |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 27 | |
| 28 | void f4(int k) { |
| 29 | |
| 30 | k = 1; |
| 31 | |
| 32 | if (k) |
| 33 | f1(); |
| 34 | |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 35 | k = 2; // expected-warning {{never read}} |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 36 | } |
Ted Kremenek | f87821c | 2008-04-15 18:37:29 +0000 | [diff] [blame] | 37 | |
| 38 | void f5() { |
| 39 | |
| 40 | int x = 4; // no-warning |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 41 | int *p = &x; // expected-warning{{never read}} |
Ted Kremenek | f87821c | 2008-04-15 18:37:29 +0000 | [diff] [blame] | 42 | |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | int f6() { |
| 46 | |
| 47 | int x = 4; |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 48 | ++x; // expected-warning{{never read}} |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 49 | return 1; |
| 50 | } |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 51 | |
| 52 | int f7(int *p) { |
| 53 | // This is allowed for defensive programming. |
| 54 | p = 0; // no-warning |
| 55 | return 1; |
| 56 | } |
| 57 | |
| 58 | int f8(int *p) { |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 59 | extern int *baz(); |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 60 | if (p = baz()) // expected-warning{{Although the value}} |
| 61 | return 1; |
| 62 | return 0; |
| 63 | } |
| 64 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 65 | int f9() { |
| 66 | int x = 4; |
| 67 | x = x + 10; // expected-warning{{never read}} |
| 68 | return 1; |
| 69 | } |
| 70 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 71 | int f10() { |
| 72 | int x = 4; |
| 73 | x = 10 + x; // expected-warning{{never read}} |
| 74 | return 1; |
| 75 | } |
| 76 | |
Ted Kremenek | 8b00b6e | 2008-07-23 23:18:43 +0000 | [diff] [blame] | 77 | int f11() { |
| 78 | int x = 4; |
Ted Kremenek | 380277e | 2008-10-15 05:23:41 +0000 | [diff] [blame] | 79 | return x++; // expected-warning{{never read}} |
Ted Kremenek | 8b00b6e | 2008-07-23 23:18:43 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Ted Kremenek | 380277e | 2008-10-15 05:23:41 +0000 | [diff] [blame] | 82 | int f11b() { |
| 83 | int x = 4; |
Ted Kremenek | 7f5fce7 | 2009-01-20 00:47:45 +0000 | [diff] [blame] | 84 | return ((((++x)))); // no-warning |
Ted Kremenek | 380277e | 2008-10-15 05:23:41 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Ted Kremenek | fc7ff55 | 2008-07-25 04:47:34 +0000 | [diff] [blame] | 87 | int f12a(int y) { |
| 88 | int x = y; // expected-warning{{never read}} |
| 89 | return 1; |
| 90 | } |
| 91 | int f12b(int y) { |
| 92 | int x __attribute__((unused)) = y; // no-warning |
| 93 | return 1; |
| 94 | } |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 95 | |
Ted Kremenek | efe88f5 | 2008-08-06 23:26:31 +0000 | [diff] [blame] | 96 | // Filed with PR 2630. This code should produce no warnings. |
| 97 | int f13(void) |
| 98 | { |
| 99 | int a = 1; |
| 100 | int b, c = b = a + a; |
| 101 | |
| 102 | if (b > 0) |
| 103 | return (0); |
| 104 | |
| 105 | return (a + b + c); |
| 106 | } |
| 107 | |
Ted Kremenek | b497ebd | 2008-09-04 21:52:52 +0000 | [diff] [blame] | 108 | // Filed with PR 2763. |
Ted Kremenek | 84fa6b9 | 2008-09-26 05:52:45 +0000 | [diff] [blame] | 109 | int f14(int count) { |
Ted Kremenek | b497ebd | 2008-09-04 21:52:52 +0000 | [diff] [blame] | 110 | int index, nextLineIndex; |
| 111 | for (index = 0; index < count; index = nextLineIndex+1) { |
| 112 | nextLineIndex = index+1; // no-warning |
| 113 | continue; |
| 114 | } |
| 115 | return index; |
| 116 | } |
Ted Kremenek | 84fa6b9 | 2008-09-26 05:52:45 +0000 | [diff] [blame] | 117 | |
| 118 | // Test case for <rdar://problem/6248086> |
| 119 | void f15(unsigned x, unsigned y) { |
| 120 | int count = x * y; // no-warning |
| 121 | int z[count]; |
| 122 | } |
| 123 | |
Ted Kremenek | 610a09e | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 124 | int f16(int x) { |
| 125 | x = x * 2; |
Ted Kremenek | d2025e2 | 2008-09-26 23:05:47 +0000 | [diff] [blame] | 126 | x = sizeof(int [x = (x || x + 1) * 2]) // expected-warning{{Although the value stored to 'x' is used}} |
| 127 | ? 5 : 8; |
Ted Kremenek | 610a09e | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 128 | return x; |
| 129 | } |
| 130 | |
Ted Kremenek | 3b58786 | 2009-01-09 22:15:01 +0000 | [diff] [blame] | 131 | // Self-assignments should not be flagged as dead stores. |
Mike Stump | a5495ea | 2009-07-21 19:01:31 +0000 | [diff] [blame] | 132 | void f17() { |
Ted Kremenek | 3b58786 | 2009-01-09 22:15:01 +0000 | [diff] [blame] | 133 | int x = 1; |
| 134 | x = x; // no-warning |
| 135 | } |
Ted Kremenek | 7f5fce7 | 2009-01-20 00:47:45 +0000 | [diff] [blame] | 136 | |
| 137 | // <rdar://problem/6506065> |
| 138 | // The values of dead stores are only "consumed" in an enclosing expression |
Mike Stump | cd7bf23 | 2009-07-17 01:04:31 +0000 | [diff] [blame] | 139 | // what that value is actually used. In other words, don't say "Although the |
| 140 | // value stored to 'x' is used...". |
Ted Kremenek | 7f5fce7 | 2009-01-20 00:47:45 +0000 | [diff] [blame] | 141 | int f18() { |
| 142 | int x = 0; // no-warning |
| 143 | if (1) |
| 144 | x = 10; // expected-warning{{Value stored to 'x' is never read}} |
| 145 | while (1) |
| 146 | x = 10; // expected-warning{{Value stored to 'x' is never read}} |
| 147 | do |
| 148 | x = 10; // expected-warning{{Value stored to 'x' is never read}} |
| 149 | while (1); |
| 150 | |
| 151 | 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'}} |
| 152 | } |
Ted Kremenek | d3098ee | 2009-02-09 18:01:00 +0000 | [diff] [blame] | 153 | |
| 154 | // PR 3514: false positive `dead initialization` warning for init to global |
| 155 | // http://llvm.org/bugs/show_bug.cgi?id=3514 |
| 156 | extern const int MyConstant; |
| 157 | int f19(void) { |
| 158 | int x = MyConstant; // no-warning |
| 159 | x = 1; |
| 160 | return x; |
| 161 | } |
| 162 | |
Ted Kremenek | 28433ff | 2009-03-31 03:34:38 +0000 | [diff] [blame] | 163 | int f19b(void) { // This case is the same as f19. |
Ted Kremenek | d3098ee | 2009-02-09 18:01:00 +0000 | [diff] [blame] | 164 | const int MyConstant = 0; |
Ted Kremenek | 28433ff | 2009-03-31 03:34:38 +0000 | [diff] [blame] | 165 | int x = MyConstant; // no-warning |
Ted Kremenek | d3098ee | 2009-02-09 18:01:00 +0000 | [diff] [blame] | 166 | x = 1; |
| 167 | return x; |
| 168 | } |
Ted Kremenek | 632d1ec | 2009-03-23 22:30:58 +0000 | [diff] [blame] | 169 | |
| 170 | void f20(void) { |
| 171 | int x = 1; // no-warning |
| 172 | #pragma unused(x) |
| 173 | } |
| 174 | |
Mike Stump | cd7bf23 | 2009-07-17 01:04:31 +0000 | [diff] [blame] | 175 | void halt() __attribute__((noreturn)); |
| 176 | int f21() { |
| 177 | int x = 4; |
| 178 | |
| 179 | ++x; // expected-warning{{never read}} |
| 180 | if (1) { |
| 181 | halt(); |
| 182 | (void)x; |
| 183 | } |
| 184 | return 1; |
| 185 | } |
Mike Stump | e5af3ce | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 186 | |
| 187 | int j; |
| 188 | void f22() { |
| 189 | int x = 4; |
| 190 | int y1 = 4; |
| 191 | int y2 = 4; |
| 192 | int y3 = 4; |
| 193 | int y4 = 4; |
| 194 | int y5 = 4; |
| 195 | int y6 = 4; |
| 196 | int y7 = 4; |
| 197 | int y8 = 4; |
| 198 | int y9 = 4; |
| 199 | int y10 = 4; |
Mike Stump | 5f20363 | 2009-07-21 00:38:52 +0000 | [diff] [blame] | 200 | int y11 = 4; |
| 201 | int y12 = 4; |
Mike Stump | fefb9f7 | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 202 | int y13 = 4; |
| 203 | int y14 = 4; |
| 204 | int y15 = 4; |
Mike Stump | 8f9893a | 2009-07-21 01:27:50 +0000 | [diff] [blame] | 205 | int y16 = 4; |
| 206 | int y17 = 4; |
| 207 | int y18 = 4; |
Mike Stump | 22cd658 | 2009-07-21 01:46:17 +0000 | [diff] [blame] | 208 | int y19 = 4; |
| 209 | int y20 = 4; |
Mike Stump | e5af3ce | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 210 | |
| 211 | ++x; // expected-warning{{never read}} |
| 212 | ++y1; |
| 213 | ++y2; |
| 214 | ++y3; |
| 215 | ++y4; |
| 216 | ++y5; |
| 217 | ++y6; |
| 218 | ++y7; |
| 219 | ++y8; |
| 220 | ++y9; |
| 221 | ++y10; |
Mike Stump | 5f20363 | 2009-07-21 00:38:52 +0000 | [diff] [blame] | 222 | ++y11; |
| 223 | ++y12; |
Mike Stump | fefb9f7 | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 224 | ++y13; |
| 225 | ++y14; |
| 226 | ++y15; |
Mike Stump | 8f9893a | 2009-07-21 01:27:50 +0000 | [diff] [blame] | 227 | ++y16; |
| 228 | ++y17; |
| 229 | ++y18; |
Mike Stump | 22cd658 | 2009-07-21 01:46:17 +0000 | [diff] [blame] | 230 | ++y19; |
| 231 | ++y20; |
Mike Stump | e5af3ce | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 232 | |
| 233 | switch (j) { |
| 234 | case 1: |
| 235 | if (0) |
| 236 | (void)x; |
| 237 | if (1) { |
| 238 | (void)y1; |
| 239 | return; |
| 240 | } |
| 241 | (void)x; |
| 242 | break; |
| 243 | case 2: |
| 244 | if (0) |
| 245 | (void)x; |
| 246 | else { |
| 247 | (void)y2; |
| 248 | return; |
| 249 | } |
| 250 | (void)x; |
| 251 | break; |
| 252 | case 3: |
| 253 | if (1) { |
| 254 | (void)y3; |
| 255 | return; |
| 256 | } else |
| 257 | (void)x; |
| 258 | (void)x; |
| 259 | break; |
| 260 | case 4: |
| 261 | 0 ? : ((void)y4, ({ return; })); |
| 262 | (void)x; |
| 263 | break; |
| 264 | case 5: |
| 265 | 1 ? : (void)x; |
| 266 | 0 ? (void)x : ((void)y5, ({ return; })); |
| 267 | (void)x; |
| 268 | break; |
| 269 | case 6: |
| 270 | 1 ? ((void)y6, ({ return; })) : (void)x; |
| 271 | (void)x; |
| 272 | break; |
| 273 | case 7: |
| 274 | (void)(0 && x); |
| 275 | (void)y7; |
| 276 | (void)(0 || (y8, ({ return; }), 1)); |
| 277 | (void)x; |
| 278 | break; |
| 279 | case 8: |
| 280 | (void)(1 && (y9, ({ return; }), 1)); |
| 281 | (void)x; |
| 282 | break; |
| 283 | case 9: |
| 284 | (void)(1 || x); |
| 285 | (void)y10; |
Mike Stump | 5f20363 | 2009-07-21 00:38:52 +0000 | [diff] [blame] | 286 | break; |
| 287 | case 10: |
Mike Stump | 5f20363 | 2009-07-21 00:38:52 +0000 | [diff] [blame] | 288 | while (0) { |
| 289 | (void)x; |
| 290 | } |
Mike Stump | 8f9893a | 2009-07-21 01:27:50 +0000 | [diff] [blame] | 291 | (void)y11; |
Mike Stump | 5f20363 | 2009-07-21 00:38:52 +0000 | [diff] [blame] | 292 | break; |
Mike Stump | 8f9893a | 2009-07-21 01:27:50 +0000 | [diff] [blame] | 293 | case 11: |
| 294 | while (1) { |
| 295 | (void)y12; |
Mike Stump | fefb9f7 | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 296 | } |
| 297 | (void)x; |
| 298 | break; |
Mike Stump | 8f9893a | 2009-07-21 01:27:50 +0000 | [diff] [blame] | 299 | case 12: |
| 300 | do { |
| 301 | (void)y13; |
| 302 | } while (0); |
| 303 | (void)y14; |
| 304 | break; |
| 305 | case 13: |
| 306 | do { |
| 307 | (void)y15; |
| 308 | } while (1); |
| 309 | (void)x; |
| 310 | break; |
Mike Stump | fefb9f7 | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 311 | case 14: |
Mike Stump | 8f9893a | 2009-07-21 01:27:50 +0000 | [diff] [blame] | 312 | for (;;) { |
| 313 | (void)y16; |
| 314 | } |
| 315 | (void)x; |
| 316 | break; |
| 317 | case 15: |
| 318 | for (;1;) { |
| 319 | (void)y17; |
| 320 | } |
| 321 | (void)x; |
| 322 | break; |
| 323 | case 16: |
Mike Stump | fefb9f7 | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 324 | for (;0;) { |
| 325 | (void)x; |
| 326 | } |
Mike Stump | 8f9893a | 2009-07-21 01:27:50 +0000 | [diff] [blame] | 327 | (void)y18; |
Mike Stump | fefb9f7 | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 328 | break; |
Mike Stump | 22cd658 | 2009-07-21 01:46:17 +0000 | [diff] [blame] | 329 | case 17: |
| 330 | __builtin_choose_expr(0, (void)x, ((void)y19, ({ return; }))); |
| 331 | (void)x; |
| 332 | break; |
| 333 | case 19: |
| 334 | __builtin_choose_expr(1, ((void)y20, ({ return; })), (void)x); |
| 335 | (void)x; |
| 336 | break; |
Mike Stump | e5af3ce | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 337 | } |
| 338 | } |