blob: 3233b974d6478f80f80023747a611702eafe55e5 [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 &&
Ted Kremenekc037eac2009-07-10 00:41:58 +00003// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic-old-cast -analyzer-constraints=basic -warn-dead-stores -verify %s &&
Daniel Dunbard7d5f022009-03-24 02:24:46 +00004// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=range -warn-dead-stores -verify %s &&
Ted Kremenekc037eac2009-07-10 00:41:58 +00005// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic-old-cast -analyzer-constraints=range -warn-dead-stores -verify %s &&
Daniel Dunbard7d5f022009-03-24 02:24:46 +00006// 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
Ted Kremenek8aefcbf2007-11-19 06:38:23 +00008
Ted Kremenek49a2fd22008-04-14 15:56:17 +00009void f1() {
Ted Kremenekaa395ba2007-11-18 20:06:35 +000010 int k, y;
Ted Kremenek0fdf06e2008-03-19 07:31:52 +000011 int abc=1;
Ted Kremenek1a654b62008-06-20 21:45:25 +000012 long idx=abc+3*5; // expected-warning {{never read}}
Ted Kremenekaa395ba2007-11-18 20:06:35 +000013}
Ted Kremenek8aefcbf2007-11-19 06:38:23 +000014
Ted Kremenek49a2fd22008-04-14 15:56:17 +000015void f2(void *b) {
Ted Kremenek8aefcbf2007-11-19 06:38:23 +000016 char *c = (char*)b; // no-warning
Ted Kremenek1a654b62008-06-20 21:45:25 +000017 char *d = b+1; // expected-warning {{never read}}
Douglas Gregora316e7b2009-02-14 00:32:47 +000018 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'}}
Ted Kremenek8aefcbf2007-11-19 06:38:23 +000020}
Ted Kremenek74c43a02007-11-20 03:03:00 +000021
Ted Kremenek49a2fd22008-04-14 15:56:17 +000022void f3() {
Ted Kremenek0fdf06e2008-03-19 07:31:52 +000023 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 Kremenek74c43a02007-11-20 03:03:00 +000028}
Ted Kremenek49a2fd22008-04-14 15:56:17 +000029
30void f4(int k) {
31
32 k = 1;
33
34 if (k)
35 f1();
36
Ted Kremenek1a654b62008-06-20 21:45:25 +000037 k = 2; // expected-warning {{never read}}
Ted Kremenek49a2fd22008-04-14 15:56:17 +000038}
Ted Kremenekf87821c2008-04-15 18:37:29 +000039
40void f5() {
41
42 int x = 4; // no-warning
Ted Kremenek1a654b62008-06-20 21:45:25 +000043 int *p = &x; // expected-warning{{never read}}
Ted Kremenekf87821c2008-04-15 18:37:29 +000044
Ted Kremeneka23157e2008-05-05 23:12:21 +000045}
46
47int f6() {
48
49 int x = 4;
Ted Kremenek1a654b62008-06-20 21:45:25 +000050 ++x; // expected-warning{{never read}}
Ted Kremeneka23157e2008-05-05 23:12:21 +000051 return 1;
52}
Ted Kremenek1a654b62008-06-20 21:45:25 +000053
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) {
Daniel Dunbar4489fe12008-08-05 00:07:51 +000061 extern int *baz();
Ted Kremenek1a654b62008-06-20 21:45:25 +000062 if (p = baz()) // expected-warning{{Although the value}}
63 return 1;
64 return 0;
65}
66
Ted Kremenek2cfac222008-07-23 21:16:38 +000067int f9() {
68 int x = 4;
69 x = x + 10; // expected-warning{{never read}}
70 return 1;
71}
72
Ted Kremenek2cfac222008-07-23 21:16:38 +000073int f10() {
74 int x = 4;
75 x = 10 + x; // expected-warning{{never read}}
76 return 1;
77}
78
Ted Kremenek8b00b6e2008-07-23 23:18:43 +000079int f11() {
80 int x = 4;
Ted Kremenek380277e2008-10-15 05:23:41 +000081 return x++; // expected-warning{{never read}}
Ted Kremenek8b00b6e2008-07-23 23:18:43 +000082}
83
Ted Kremenek380277e2008-10-15 05:23:41 +000084int f11b() {
85 int x = 4;
Ted Kremenek7f5fce72009-01-20 00:47:45 +000086 return ((((++x)))); // no-warning
Ted Kremenek380277e2008-10-15 05:23:41 +000087}
88
Ted Kremenekfc7ff552008-07-25 04:47:34 +000089int 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}
Ted Kremenek2cfac222008-07-23 21:16:38 +000097
Ted Kremenekefe88f52008-08-06 23:26:31 +000098// 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
Ted Kremenekb497ebd2008-09-04 21:52:52 +0000110// Filed with PR 2763.
Ted Kremenek84fa6b92008-09-26 05:52:45 +0000111int f14(int count) {
Ted Kremenekb497ebd2008-09-04 21:52:52 +0000112 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}
Ted Kremenek84fa6b92008-09-26 05:52:45 +0000119
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
Ted Kremenek610a09e2008-09-26 22:58:57 +0000126int f16(int x) {
127 x = x * 2;
Ted Kremenekd2025e22008-09-26 23:05:47 +0000128 x = sizeof(int [x = (x || x + 1) * 2]) // expected-warning{{Although the value stored to 'x' is used}}
129 ? 5 : 8;
Ted Kremenek610a09e2008-09-26 22:58:57 +0000130 return x;
131}
132
Ted Kremenek3b587862009-01-09 22:15:01 +0000133// Self-assignments should not be flagged as dead stores.
134int f17() {
135 int x = 1;
136 x = x; // no-warning
137}
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000138
139// <rdar://problem/6506065>
140// The values of dead stores are only "consumed" in an enclosing expression
Mike Stumpcd7bf232009-07-17 01:04:31 +0000141// what that value is actually used. In other words, don't say "Although the
142// value stored to 'x' is used...".
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000143int 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}
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000155
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
Ted Kremenek28433ff2009-03-31 03:34:38 +0000165int f19b(void) { // This case is the same as f19.
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000166 const int MyConstant = 0;
Ted Kremenek28433ff2009-03-31 03:34:38 +0000167 int x = MyConstant; // no-warning
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000168 x = 1;
169 return x;
170}
Ted Kremenek632d1ec2009-03-23 22:30:58 +0000171
172void f20(void) {
173 int x = 1; // no-warning
174#pragma unused(x)
175}
176
Mike Stumpcd7bf232009-07-17 01:04:31 +0000177void 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}
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000188
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;
Mike Stump5f203632009-07-21 00:38:52 +0000202 int y11 = 4;
203 int y12 = 4;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000204 int y13 = 4;
205 int y14 = 4;
206 int y15 = 4;
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000207
208 ++x; // expected-warning{{never read}}
209 ++y1;
210 ++y2;
211 ++y3;
212 ++y4;
213 ++y5;
214 ++y6;
215 ++y7;
216 ++y8;
217 ++y9;
218 ++y10;
Mike Stump5f203632009-07-21 00:38:52 +0000219 ++y11;
220 ++y12;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000221 ++y13;
222 ++y14;
223 ++y15;
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000224
225 switch (j) {
226 case 1:
227 if (0)
228 (void)x;
229 if (1) {
230 (void)y1;
231 return;
232 }
233 (void)x;
234 break;
235 case 2:
236 if (0)
237 (void)x;
238 else {
239 (void)y2;
240 return;
241 }
242 (void)x;
243 break;
244 case 3:
245 if (1) {
246 (void)y3;
247 return;
248 } else
249 (void)x;
250 (void)x;
251 break;
252 case 4:
253 0 ? : ((void)y4, ({ return; }));
254 (void)x;
255 break;
256 case 5:
257 1 ? : (void)x;
258 0 ? (void)x : ((void)y5, ({ return; }));
259 (void)x;
260 break;
261 case 6:
262 1 ? ((void)y6, ({ return; })) : (void)x;
263 (void)x;
264 break;
265 case 7:
266 (void)(0 && x);
267 (void)y7;
268 (void)(0 || (y8, ({ return; }), 1));
269 (void)x;
270 break;
271 case 8:
272 (void)(1 && (y9, ({ return; }), 1));
273 (void)x;
274 break;
275 case 9:
276 (void)(1 || x);
277 (void)y10;
Mike Stump5f203632009-07-21 00:38:52 +0000278 break;
279 case 10:
280 while (1) {
281 (void)y11;
282 }
283 (void)x;
284 break;
285 case 11:
286 while (0) {
287 (void)x;
288 }
289 (void)y12;
290 break;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000291 case 12:
292 for (;;) {
293 (void)y13;
294 }
295 (void)x;
296 break;
297 case 13:
298 for (;1;) {
299 (void)y14;
300 }
301 (void)x;
302 break;
303 case 14:
304 for (;0;) {
305 (void)x;
306 }
307 (void)y15;
308 break;
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000309 }
310}