dead-stores.c revision 58f9e13e87e57236fee4b914eea9be6f92a1c345
123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)// 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 223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)// 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 323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)// 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 423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)// 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 5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// 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 6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)void f1() { 823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) int k, y; // expected-warning{{unused variable 'k'}} expected-warning{{unused variable 'y'}} 946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) int abc=1; 1023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) long idx=abc+3*5; // expected-warning {{never read}} expected-warning{{unused variable 'idx'}} 1123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)} 1223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 13116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid f2(void *b) { 14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch char *c = (char*)b; // no-warning 1523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) char *d = b+1; // expected-warning {{never read}} expected-warning{{unused variable 'd'}} 1623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) printf("%s", c); // expected-warning{{implicitly declaring C library function 'printf' with type 'int (const char *, ...)'}} \ 175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // expected-note{{please include the header <stdio.h> or explicitly provide a declaration for 'printf'}} 185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} 1923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 2003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)int f(); 2123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 22effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid f3() { 23effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch int r; 24effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch if ((r = f()) != 0) { // no-warning 25effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch int y = r; // no-warning 2623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) printf("the error is: %d\n", y); 2723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) } 28116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 29116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 30effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid f4(int k) { 3123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 3223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) k = 1; 3323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 3423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) if (k) 3523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) f1(); 3623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 3746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) k = 2; // expected-warning {{never read}} 3846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)} 3946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 4023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)void f5() { 4123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 4223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) int x = 4; // no-warning 4323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) int *p = &x; // expected-warning{{never read}} expected-warning{{unused variable 'p'}} 4423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 4523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)} 4623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 4723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)int f6() { 4823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 4923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) int x = 4; 5023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) ++x; // expected-warning{{never read}} 51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch return 1; 52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 54116680a4aac90f2aa7413d9095a592090648e557Ben Murdochint f7(int *p) { 5523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) // This is allowed for defensive programming. 56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch p = 0; // no-warning 57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch return 1; 585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} 5903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 60116680a4aac90f2aa7413d9095a592090648e557Ben Murdochint f7b(int *p) { 61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // This is allowed for defensive programming. 62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch p = (0); // no-warning 6323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) return 1; 6423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)} 6523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)int f7c(int *p) { 6703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) // This is allowed for defensive programming. 685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) p = (void*) 0; // no-warning 6923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) return 1; 7023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)} 7123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 72116680a4aac90f2aa7413d9095a592090648e557Ben Murdochint f7d(int *p) { 73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // This is allowed for defensive programming. 74a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch p = (void*) (0); // no-warning 7523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) return 1; 76c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch} 7723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 7823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)int f8(int *p) { 7923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) extern int *baz(); 8023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) if ((p = baz())) // expected-warning{{Although the value}} 8123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) return 1; 82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch return 0; 83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)int f9() { 8623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) int x = 4; 8723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) x = x + 10; // expected-warning{{never read}} 8823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) return 1; 8923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)} 9046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 9146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)int f10() { 9246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) int x = 4; 93116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch x = 10 + x; // expected-warning{{never read}} 9423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) return 1; 9523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)} 96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)int f11() { 9846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) int x = 4; 9946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) return x++; // expected-warning{{never read}} 10046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)} 10146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 1025f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)int f11b() { 1035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) int x = 4; 1045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) return ((((++x)))); // no-warning 1055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} 1065f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 1075f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)int f12a(int y) { 1085f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) int x = y; // expected-warning{{unused variable 'x'}} 1095f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) return 1; 11003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)} 11146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)int f12b(int y) { 11246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) int x __attribute__((unused)) = y; // no-warning 113116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch return 1; 11446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)} 11546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)int f12c(int y) { 11603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) // Allow initialiation of scalar variables by parameters as a form of 1175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // defensive programming. 1185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) int x = y; // no-warning 1195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) x = 1; 12003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) return x; 12103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)} 12203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 1235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Filed with PR 2630. This code should produce no warnings. 1245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)int f13(void) 1255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles){ 1265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) int a = 1; 1275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) int b, c = b = a + a; 1285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 1295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) if (b > 0) 1305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) return (0); 1315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 1325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) return (a + b + c); 1335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} 1345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 1355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Filed with PR 2763. 13646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)int f14(int count) { 13723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) int index, nextLineIndex; 13823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) for (index = 0; index < count; index = nextLineIndex+1) { 13946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) nextLineIndex = index+1; // no-warning 14046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) continue; 14146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) } 142116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch return index; 143116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 144116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 145116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Test case for <rdar://problem/6248086> 14603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)void f15(unsigned x, unsigned y) { 14703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) int count = x * y; // no-warning 14803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) int z[count]; // expected-warning{{unused variable 'z'}} 14903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)} 1505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 151116680a4aac90f2aa7413d9095a592090648e557Ben Murdochint f16(int x) { 152116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch x = x * 2; 15346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) x = sizeof(int [x = (x || x + 1) * 2]) // expected-warning{{Although the value stored to 'x' is used}} expected-warning{{The left operand to '*' is always 1}} 15446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) ? 5 : 8; 15546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) return x; 15646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)} 15746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 15846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Self-assignments should not be flagged as dead stores. 15946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)void f17() { 16046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) int x = 1; 16146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) x = x; 16246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)} 16346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 16446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// <rdar://problem/6506065> 16546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// The values of dead stores are only "consumed" in an enclosing expression 16646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// what that value is actually used. In other words, don't say "Although the 167effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// value stored to 'x' is used...". 168116680a4aac90f2aa7413d9095a592090648e557Ben Murdochint f18() { 16923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) int x = 0; // no-warning 17023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) if (1) 17123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) x = 10; // expected-warning{{Value stored to 'x' is never read}} 17223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) while (1) 173116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 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} 180 181// PR 3514: false positive `dead initialization` warning for init to global 182// http://llvm.org/bugs/show_bug.cgi?id=3514 183extern const int MyConstant; 184int f19(void) { 185 int x = MyConstant; // no-warning 186 x = 1; 187 return x; 188} 189 190int f19b(void) { // This case is the same as f19. 191 const int MyConstant = 0; 192 int x = MyConstant; // no-warning 193 x = 1; 194 return x; 195} 196 197void f20(void) { 198 int x = 1; // no-warning 199#pragma unused(x) 200} 201 202void halt() __attribute__((noreturn)); 203int 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} 213 214int j; 215void 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; 227 int y11 = 4; 228 int y12 = 4; 229 int y13 = 4; 230 int y14 = 4; 231 int y15 = 4; 232 int y16 = 4; 233 int y17 = 4; 234 int y18 = 4; 235 int y19 = 4; 236 int y20 = 4; 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; 249 ++y11; 250 ++y12; 251 ++y13; 252 ++y14; 253 ++y15; 254 ++y16; 255 ++y17; 256 ++y18; 257 ++y19; 258 ++y20; 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)); // expected-warning {{expression result unused}} 304 (void)x; 305 break; 306 case 8: 307 (void)(1 && (y9, ({ return; }), 1)); // expected-warning {{expression result unused}} 308 (void)x; 309 break; 310 case 9: 311 (void)(1 || x); 312 (void)y10; 313 break; 314 case 10: 315 while (0) { 316 (void)x; 317 } 318 (void)y11; 319 break; 320 case 11: 321 while (1) { 322 (void)y12; 323 } 324 (void)x; 325 break; 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; 338 case 14: 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: 351 for (;0;) { 352 (void)x; 353 } 354 (void)y18; 355 break; 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; 364 } 365} 366 367void f23_aux(const char* s); 368void 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 376void f23_pos(int argc, char **argv) { 377 int shouldLog = (argc > 1); // expected-warning{{Value stored to 'shouldLog' during its initialization is never read}} expected-warning{{unused variable 'shouldLog'}} 378 ^{ 379 f23_aux("I did too use it!\n"); 380 }(); 381} 382 383void 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 ^ { 387 int z = x + y; // expected-warning{{Value stored to 'z' during its initialization is never read}} expected-warning{{unused variable 'z'}} 388 }(); 389} 390 391void 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 400int 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 409int 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 418// This example shows that writing to a variable captured by a block means that it might 419// not be dead. 420int 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. 431int 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 439int 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 453// The FOREACH macro in QT uses 'break' statements within statement expressions 454// placed within the increment code of for loops. 455void 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}} expected-warning{{The left operand to '*' is always 1}} 462 } 463} 464 465// <rdar://problem/8320674> NullStmts followed by do...while() can lead to disconnected CFG 466// 467// This previously caused bogus dead-stores warnings because the body of the first do...while was 468// disconnected from the entry of the function. 469typedef struct { float r; float i; } s_rdar8320674; 470typedef struct { s_rdar8320674 x[1]; } s2_rdar8320674; 471 472void rdar8320674(s_rdar8320674 *z, unsigned y, s2_rdar8320674 *st, int m) 473{ 474 s_rdar8320674 * z2; 475 s_rdar8320674 * tw1 = st->x; 476 s_rdar8320674 t; 477 z2 = z + m; 478 do{ 479 ; ; 480 do{ (t).r = (*z2).r*(*tw1).r - (*z2).i*(*tw1).i; (t).i = (*z2).r*(*tw1).i + (*z2).i*(*tw1).r; }while(0); 481 tw1 += y; 482 do { (*z2).r=(*z).r-(t).r; (*z2).i=(*z).i-(t).i; }while(0); 483 do { (*z).r += (t).r; (*z).i += (t).i; }while(0); 484 ++z2; 485 ++z; 486 }while (--m); 487} 488 489