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