blob: d19e8c4f5c6bdf77cfc04f8160d3c3bb11a8577b [file] [log] [blame]
Daniel Dunbard7d5f022009-03-24 02:24:46 +00001// RUN: clang-cc -analyze -warn-dead-stores -verify %s &&
Daniel Dunbard7d5f022009-03-24 02:24:46 +00002// 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
Ted Kremenek8aefcbf2007-11-19 06:38:23 +00006
Ted Kremenek49a2fd22008-04-14 15:56:17 +00007void f1() {
Ted Kremenekaa395ba2007-11-18 20:06:35 +00008 int k, y;
Ted Kremenek0fdf06e2008-03-19 07:31:52 +00009 int abc=1;
Ted Kremenek1a654b62008-06-20 21:45:25 +000010 long idx=abc+3*5; // expected-warning {{never read}}
Ted Kremenekaa395ba2007-11-18 20:06:35 +000011}
Ted Kremenek8aefcbf2007-11-19 06:38:23 +000012
Ted Kremenek49a2fd22008-04-14 15:56:17 +000013void f2(void *b) {
Ted Kremenek8aefcbf2007-11-19 06:38:23 +000014 char *c = (char*)b; // no-warning
Ted Kremenek1a654b62008-06-20 21:45:25 +000015 char *d = b+1; // expected-warning {{never read}}
Douglas Gregora316e7b2009-02-14 00:32:47 +000016 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'}}
Ted Kremenek8aefcbf2007-11-19 06:38:23 +000018}
Ted Kremenek74c43a02007-11-20 03:03:00 +000019
Ted Kremenek49a2fd22008-04-14 15:56:17 +000020void f3() {
Ted Kremenek0fdf06e2008-03-19 07:31:52 +000021 int r;
22 if ((r = f()) != 0) { // no-warning
23 int y = r; // no-warning
24 printf("the error is: %d\n", y);
25 }
Ted Kremenek74c43a02007-11-20 03:03:00 +000026}
Ted Kremenek49a2fd22008-04-14 15:56:17 +000027
28void f4(int k) {
29
30 k = 1;
31
32 if (k)
33 f1();
34
Ted Kremenek1a654b62008-06-20 21:45:25 +000035 k = 2; // expected-warning {{never read}}
Ted Kremenek49a2fd22008-04-14 15:56:17 +000036}
Ted Kremenekf87821c2008-04-15 18:37:29 +000037
38void f5() {
39
40 int x = 4; // no-warning
Ted Kremenek1a654b62008-06-20 21:45:25 +000041 int *p = &x; // expected-warning{{never read}}
Ted Kremenekf87821c2008-04-15 18:37:29 +000042
Ted Kremeneka23157e2008-05-05 23:12:21 +000043}
44
45int f6() {
46
47 int x = 4;
Ted Kremenek1a654b62008-06-20 21:45:25 +000048 ++x; // expected-warning{{never read}}
Ted Kremeneka23157e2008-05-05 23:12:21 +000049 return 1;
50}
Ted Kremenek1a654b62008-06-20 21:45:25 +000051
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) {
Daniel Dunbar4489fe12008-08-05 00:07:51 +000059 extern int *baz();
Ted Kremenek1a654b62008-06-20 21:45:25 +000060 if (p = baz()) // expected-warning{{Although the value}}
61 return 1;
62 return 0;
63}
64
Ted Kremenek2cfac222008-07-23 21:16:38 +000065int f9() {
66 int x = 4;
67 x = x + 10; // expected-warning{{never read}}
68 return 1;
69}
70
Ted Kremenek2cfac222008-07-23 21:16:38 +000071int f10() {
72 int x = 4;
73 x = 10 + x; // expected-warning{{never read}}
74 return 1;
75}
76
Ted Kremenek8b00b6e2008-07-23 23:18:43 +000077int f11() {
78 int x = 4;
Ted Kremenek380277e2008-10-15 05:23:41 +000079 return x++; // expected-warning{{never read}}
Ted Kremenek8b00b6e2008-07-23 23:18:43 +000080}
81
Ted Kremenek380277e2008-10-15 05:23:41 +000082int f11b() {
83 int x = 4;
Ted Kremenek7f5fce72009-01-20 00:47:45 +000084 return ((((++x)))); // no-warning
Ted Kremenek380277e2008-10-15 05:23:41 +000085}
86
Ted Kremenekfc7ff552008-07-25 04:47:34 +000087int 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}
Ted Kremenek2cfac222008-07-23 21:16:38 +000095
Ted Kremenekefe88f52008-08-06 23:26:31 +000096// 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
Ted Kremenekb497ebd2008-09-04 21:52:52 +0000108// Filed with PR 2763.
Ted Kremenek84fa6b92008-09-26 05:52:45 +0000109int f14(int count) {
Ted Kremenekb497ebd2008-09-04 21:52:52 +0000110 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}
Ted Kremenek84fa6b92008-09-26 05:52:45 +0000117
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
Ted Kremenek610a09e2008-09-26 22:58:57 +0000124int f16(int x) {
125 x = x * 2;
Ted Kremenekd2025e22008-09-26 23:05:47 +0000126 x = sizeof(int [x = (x || x + 1) * 2]) // expected-warning{{Although the value stored to 'x' is used}}
127 ? 5 : 8;
Ted Kremenek610a09e2008-09-26 22:58:57 +0000128 return x;
129}
130
Ted Kremenek3b587862009-01-09 22:15:01 +0000131// Self-assignments should not be flagged as dead stores.
Mike Stumpa5495ea2009-07-21 19:01:31 +0000132void f17() {
Ted Kremenek3b587862009-01-09 22:15:01 +0000133 int x = 1;
134 x = x; // no-warning
135}
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000136
137// <rdar://problem/6506065>
138// The values of dead stores are only "consumed" in an enclosing expression
Mike Stumpcd7bf232009-07-17 01:04:31 +0000139// what that value is actually used. In other words, don't say "Although the
140// value stored to 'x' is used...".
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000141int 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}
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000153
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
Ted Kremenek28433ff2009-03-31 03:34:38 +0000163int f19b(void) { // This case is the same as f19.
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000164 const int MyConstant = 0;
Ted Kremenek28433ff2009-03-31 03:34:38 +0000165 int x = MyConstant; // no-warning
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000166 x = 1;
167 return x;
168}
Ted Kremenek632d1ec2009-03-23 22:30:58 +0000169
170void f20(void) {
171 int x = 1; // no-warning
172#pragma unused(x)
173}
174
Mike Stumpcd7bf232009-07-17 01:04:31 +0000175void 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}
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000186
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;
Mike Stump5f203632009-07-21 00:38:52 +0000200 int y11 = 4;
201 int y12 = 4;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000202 int y13 = 4;
203 int y14 = 4;
204 int y15 = 4;
Mike Stump8f9893a2009-07-21 01:27:50 +0000205 int y16 = 4;
206 int y17 = 4;
207 int y18 = 4;
Mike Stump22cd6582009-07-21 01:46:17 +0000208 int y19 = 4;
209 int y20 = 4;
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000210
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;
Mike Stump5f203632009-07-21 00:38:52 +0000222 ++y11;
223 ++y12;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000224 ++y13;
225 ++y14;
226 ++y15;
Mike Stump8f9893a2009-07-21 01:27:50 +0000227 ++y16;
228 ++y17;
229 ++y18;
Mike Stump22cd6582009-07-21 01:46:17 +0000230 ++y19;
231 ++y20;
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000232
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;
Mike Stump5f203632009-07-21 00:38:52 +0000286 break;
287 case 10:
Mike Stump5f203632009-07-21 00:38:52 +0000288 while (0) {
289 (void)x;
290 }
Mike Stump8f9893a2009-07-21 01:27:50 +0000291 (void)y11;
Mike Stump5f203632009-07-21 00:38:52 +0000292 break;
Mike Stump8f9893a2009-07-21 01:27:50 +0000293 case 11:
294 while (1) {
295 (void)y12;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000296 }
297 (void)x;
298 break;
Mike Stump8f9893a2009-07-21 01:27:50 +0000299 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;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000311 case 14:
Mike Stump8f9893a2009-07-21 01:27:50 +0000312 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:
Mike Stumpfefb9f72009-07-21 01:12:51 +0000324 for (;0;) {
325 (void)x;
326 }
Mike Stump8f9893a2009-07-21 01:27:50 +0000327 (void)y18;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000328 break;
Mike Stump22cd6582009-07-21 01:46:17 +0000329 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;
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000337 }
338}