dead-stores.c revision a5495eadc816b29954e22df55aa9a9d6bb41429c
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-old-cast -analyzer-constraints=basic -warn-dead-stores -verify %s && 4// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=range -warn-dead-stores -verify %s && 5// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic-old-cast -analyzer-constraints=range -warn-dead-stores -verify %s && 6// RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=basic -warn-dead-stores -verify %s && 7// RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=range -warn-dead-stores -verify %s 8 9void f1() { 10 int k, y; 11 int abc=1; 12 long idx=abc+3*5; // expected-warning {{never read}} 13} 14 15void f2(void *b) { 16 char *c = (char*)b; // no-warning 17 char *d = b+1; // expected-warning {{never read}} 18 printf("%s", c); // expected-warning{{implicitly declaring C library function 'printf' with type 'int (char const *, ...)'}} \ 19 // expected-note{{please include the header <stdio.h> or explicitly provide a declaration for 'printf'}} 20} 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}} 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 f8(int *p) { 61 extern int *baz(); 62 if (p = baz()) // expected-warning{{Although the value}} 63 return 1; 64 return 0; 65} 66 67int f9() { 68 int x = 4; 69 x = x + 10; // expected-warning{{never read}} 70 return 1; 71} 72 73int f10() { 74 int x = 4; 75 x = 10 + x; // expected-warning{{never read}} 76 return 1; 77} 78 79int f11() { 80 int x = 4; 81 return x++; // expected-warning{{never read}} 82} 83 84int f11b() { 85 int x = 4; 86 return ((((++x)))); // no-warning 87} 88 89int f12a(int y) { 90 int x = y; // expected-warning{{never read}} 91 return 1; 92} 93int f12b(int y) { 94 int x __attribute__((unused)) = y; // no-warning 95 return 1; 96} 97 98// Filed with PR 2630. This code should produce no warnings. 99int f13(void) 100{ 101 int a = 1; 102 int b, c = b = a + a; 103 104 if (b > 0) 105 return (0); 106 107 return (a + b + c); 108} 109 110// Filed with PR 2763. 111int f14(int count) { 112 int index, nextLineIndex; 113 for (index = 0; index < count; index = nextLineIndex+1) { 114 nextLineIndex = index+1; // no-warning 115 continue; 116 } 117 return index; 118} 119 120// Test case for <rdar://problem/6248086> 121void f15(unsigned x, unsigned y) { 122 int count = x * y; // no-warning 123 int z[count]; 124} 125 126int f16(int x) { 127 x = x * 2; 128 x = sizeof(int [x = (x || x + 1) * 2]) // expected-warning{{Although the value stored to 'x' is used}} 129 ? 5 : 8; 130 return x; 131} 132 133// Self-assignments should not be flagged as dead stores. 134void f17() { 135 int x = 1; 136 x = x; // no-warning 137} 138 139// <rdar://problem/6506065> 140// The values of dead stores are only "consumed" in an enclosing expression 141// what that value is actually used. In other words, don't say "Although the 142// value stored to 'x' is used...". 143int f18() { 144 int x = 0; // no-warning 145 if (1) 146 x = 10; // expected-warning{{Value stored to 'x' is never read}} 147 while (1) 148 x = 10; // expected-warning{{Value stored to 'x' is never read}} 149 do 150 x = 10; // expected-warning{{Value stored to 'x' is never read}} 151 while (1); 152 153 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'}} 154} 155 156// PR 3514: false positive `dead initialization` warning for init to global 157// http://llvm.org/bugs/show_bug.cgi?id=3514 158extern const int MyConstant; 159int f19(void) { 160 int x = MyConstant; // no-warning 161 x = 1; 162 return x; 163} 164 165int f19b(void) { // This case is the same as f19. 166 const int MyConstant = 0; 167 int x = MyConstant; // no-warning 168 x = 1; 169 return x; 170} 171 172void f20(void) { 173 int x = 1; // no-warning 174#pragma unused(x) 175} 176 177void halt() __attribute__((noreturn)); 178int f21() { 179 int x = 4; 180 181 ++x; // expected-warning{{never read}} 182 if (1) { 183 halt(); 184 (void)x; 185 } 186 return 1; 187} 188 189int j; 190void f22() { 191 int x = 4; 192 int y1 = 4; 193 int y2 = 4; 194 int y3 = 4; 195 int y4 = 4; 196 int y5 = 4; 197 int y6 = 4; 198 int y7 = 4; 199 int y8 = 4; 200 int y9 = 4; 201 int y10 = 4; 202 int y11 = 4; 203 int y12 = 4; 204 int y13 = 4; 205 int y14 = 4; 206 int y15 = 4; 207 int y16 = 4; 208 int y17 = 4; 209 int y18 = 4; 210 int y19 = 4; 211 int y20 = 4; 212 213 ++x; // expected-warning{{never read}} 214 ++y1; 215 ++y2; 216 ++y3; 217 ++y4; 218 ++y5; 219 ++y6; 220 ++y7; 221 ++y8; 222 ++y9; 223 ++y10; 224 ++y11; 225 ++y12; 226 ++y13; 227 ++y14; 228 ++y15; 229 ++y16; 230 ++y17; 231 ++y18; 232 ++y19; 233 ++y20; 234 235 switch (j) { 236 case 1: 237 if (0) 238 (void)x; 239 if (1) { 240 (void)y1; 241 return; 242 } 243 (void)x; 244 break; 245 case 2: 246 if (0) 247 (void)x; 248 else { 249 (void)y2; 250 return; 251 } 252 (void)x; 253 break; 254 case 3: 255 if (1) { 256 (void)y3; 257 return; 258 } else 259 (void)x; 260 (void)x; 261 break; 262 case 4: 263 0 ? : ((void)y4, ({ return; })); 264 (void)x; 265 break; 266 case 5: 267 1 ? : (void)x; 268 0 ? (void)x : ((void)y5, ({ return; })); 269 (void)x; 270 break; 271 case 6: 272 1 ? ((void)y6, ({ return; })) : (void)x; 273 (void)x; 274 break; 275 case 7: 276 (void)(0 && x); 277 (void)y7; 278 (void)(0 || (y8, ({ return; }), 1)); 279 (void)x; 280 break; 281 case 8: 282 (void)(1 && (y9, ({ return; }), 1)); 283 (void)x; 284 break; 285 case 9: 286 (void)(1 || x); 287 (void)y10; 288 break; 289 case 10: 290 while (0) { 291 (void)x; 292 } 293 (void)y11; 294 break; 295 case 11: 296 while (1) { 297 (void)y12; 298 } 299 (void)x; 300 break; 301 case 12: 302 do { 303 (void)y13; 304 } while (0); 305 (void)y14; 306 break; 307 case 13: 308 do { 309 (void)y15; 310 } while (1); 311 (void)x; 312 break; 313 case 14: 314 for (;;) { 315 (void)y16; 316 } 317 (void)x; 318 break; 319 case 15: 320 for (;1;) { 321 (void)y17; 322 } 323 (void)x; 324 break; 325 case 16: 326 for (;0;) { 327 (void)x; 328 } 329 (void)y18; 330 break; 331 case 17: 332 __builtin_choose_expr(0, (void)x, ((void)y19, ({ return; }))); 333 (void)x; 334 break; 335 case 19: 336 __builtin_choose_expr(1, ((void)y20, ({ return; })), (void)x); 337 (void)x; 338 break; 339 } 340} 341