dead-stores.c revision f66d5cd8926541099def3cb187d56eff60d3be99
1// RUN: clang-cc -analyze -warn-dead-stores -verify %s && 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 6 7void f1() { 8 int k, y; 9 int abc=1; 10 long idx=abc+3*5; // expected-warning {{never read}} 11} 12 13void f2(void *b) { 14 char *c = (char*)b; // no-warning 15 char *d = b+1; // expected-warning {{never read}} 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'}} 18} 19 20void f3() { 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 } 26} 27 28void f4(int k) { 29 30 k = 1; 31 32 if (k) 33 f1(); 34 35 k = 2; // expected-warning {{never read}} 36} 37 38void f5() { 39 40 int x = 4; // no-warning 41 int *p = &x; // expected-warning{{never read}} 42 43} 44 45int f6() { 46 47 int x = 4; 48 ++x; // expected-warning{{never read}} 49 return 1; 50} 51 52int f7(int *p) { 53 // This is allowed for defensive programming. 54 p = 0; // no-warning 55 return 1; 56} 57 58int f8(int *p) { 59 extern int *baz(); 60 if ((p = baz())) // expected-warning{{Although the value}} 61 return 1; 62 return 0; 63} 64 65int f9() { 66 int x = 4; 67 x = x + 10; // expected-warning{{never read}} 68 return 1; 69} 70 71int f10() { 72 int x = 4; 73 x = 10 + x; // expected-warning{{never read}} 74 return 1; 75} 76 77int f11() { 78 int x = 4; 79 return x++; // expected-warning{{never read}} 80} 81 82int f11b() { 83 int x = 4; 84 return ((((++x)))); // no-warning 85} 86 87int f12a(int y) { 88 int x = y; // expected-warning{{never read}} 89 return 1; 90} 91int f12b(int y) { 92 int x __attribute__((unused)) = y; // no-warning 93 return 1; 94} 95 96// Filed with PR 2630. This code should produce no warnings. 97int 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 108// Filed with PR 2763. 109int f14(int count) { 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} 117 118// Test case for <rdar://problem/6248086> 119void f15(unsigned x, unsigned y) { 120 int count = x * y; // no-warning 121 int z[count]; 122} 123 124int f16(int x) { 125 x = x * 2; 126 x = sizeof(int [x = (x || x + 1) * 2]) // expected-warning{{Although the value stored to 'x' is used}} 127 ? 5 : 8; 128 return x; 129} 130 131// Self-assignments should not be flagged as dead stores. 132void f17() { 133 int x = 1; 134 x = x; // no-warning 135} 136 137// <rdar://problem/6506065> 138// The values of dead stores are only "consumed" in an enclosing expression 139// what that value is actually used. In other words, don't say "Although the 140// value stored to 'x' is used...". 141int 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} 153 154// PR 3514: false positive `dead initialization` warning for init to global 155// http://llvm.org/bugs/show_bug.cgi?id=3514 156extern const int MyConstant; 157int f19(void) { 158 int x = MyConstant; // no-warning 159 x = 1; 160 return x; 161} 162 163int f19b(void) { // This case is the same as f19. 164 const int MyConstant = 0; 165 int x = MyConstant; // no-warning 166 x = 1; 167 return x; 168} 169 170void f20(void) { 171 int x = 1; // no-warning 172#pragma unused(x) 173} 174 175void halt() __attribute__((noreturn)); 176int 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} 186 187int j; 188void 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; 200 int y11 = 4; 201 int y12 = 4; 202 int y13 = 4; 203 int y14 = 4; 204 int y15 = 4; 205 int y16 = 4; 206 int y17 = 4; 207 int y18 = 4; 208 int y19 = 4; 209 int y20 = 4; 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; 222 ++y11; 223 ++y12; 224 ++y13; 225 ++y14; 226 ++y15; 227 ++y16; 228 ++y17; 229 ++y18; 230 ++y19; 231 ++y20; 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; 286 break; 287 case 10: 288 while (0) { 289 (void)x; 290 } 291 (void)y11; 292 break; 293 case 11: 294 while (1) { 295 (void)y12; 296 } 297 (void)x; 298 break; 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; 311 case 14: 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: 324 for (;0;) { 325 (void)x; 326 } 327 (void)y18; 328 break; 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; 337 } 338} 339