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