blob: b1f16b9c844e431b89255aa2c074b585d9280692 [file] [log] [blame]
Ted Kremenek489f7b62008-07-04 04:38:48 +00001// RUN: clang -warn-dead-stores -verify %s &&
2// RUN: clang -checker-simple -warn-dead-stores -verify %s &&
Ted Kremeneke81da502008-07-02 23:18:22 +00003// RUN: clang -warn-dead-stores -checker-simple -verify %s
4
Ted Kremenek8aefcbf2007-11-19 06:38:23 +00005
Ted Kremenek49a2fd22008-04-14 15:56:17 +00006void f1() {
Ted Kremenekaa395ba2007-11-18 20:06:35 +00007 int k, y;
Ted Kremenek0fdf06e2008-03-19 07:31:52 +00008 int abc=1;
Ted Kremenek1a654b62008-06-20 21:45:25 +00009 long idx=abc+3*5; // expected-warning {{never read}}
Ted Kremenekaa395ba2007-11-18 20:06:35 +000010}
Ted Kremenek8aefcbf2007-11-19 06:38:23 +000011
Ted Kremenek49a2fd22008-04-14 15:56:17 +000012void f2(void *b) {
Ted Kremenek8aefcbf2007-11-19 06:38:23 +000013 char *c = (char*)b; // no-warning
Ted Kremenek1a654b62008-06-20 21:45:25 +000014 char *d = b+1; // expected-warning {{never read}}
Ted Kremenek8aefcbf2007-11-19 06:38:23 +000015 printf("%s", c);
16}
Ted Kremenek74c43a02007-11-20 03:03:00 +000017
Ted Kremenek49a2fd22008-04-14 15:56:17 +000018void f3() {
Ted Kremenek0fdf06e2008-03-19 07:31:52 +000019 int r;
20 if ((r = f()) != 0) { // no-warning
21 int y = r; // no-warning
22 printf("the error is: %d\n", y);
23 }
Ted Kremenek74c43a02007-11-20 03:03:00 +000024}
Ted Kremenek49a2fd22008-04-14 15:56:17 +000025
26void f4(int k) {
27
28 k = 1;
29
30 if (k)
31 f1();
32
Ted Kremenek1a654b62008-06-20 21:45:25 +000033 k = 2; // expected-warning {{never read}}
Ted Kremenek49a2fd22008-04-14 15:56:17 +000034}
Ted Kremenekf87821c2008-04-15 18:37:29 +000035
36void f5() {
37
38 int x = 4; // no-warning
Ted Kremenek1a654b62008-06-20 21:45:25 +000039 int *p = &x; // expected-warning{{never read}}
Ted Kremenekf87821c2008-04-15 18:37:29 +000040
Ted Kremeneka23157e2008-05-05 23:12:21 +000041}
42
43int f6() {
44
45 int x = 4;
Ted Kremenek1a654b62008-06-20 21:45:25 +000046 ++x; // expected-warning{{never read}}
Ted Kremeneka23157e2008-05-05 23:12:21 +000047 return 1;
48}
Ted Kremenek1a654b62008-06-20 21:45:25 +000049
50int f7(int *p) {
51 // This is allowed for defensive programming.
52 p = 0; // no-warning
53 return 1;
54}
55
56int f8(int *p) {
Daniel Dunbar4489fe12008-08-05 00:07:51 +000057 extern int *baz();
Ted Kremenek1a654b62008-06-20 21:45:25 +000058 if (p = baz()) // expected-warning{{Although the value}}
59 return 1;
60 return 0;
61}
62
Ted Kremenek2cfac222008-07-23 21:16:38 +000063int f9() {
64 int x = 4;
65 x = x + 10; // expected-warning{{never read}}
66 return 1;
67}
68
69
70int f10() {
71 int x = 4;
72 x = 10 + x; // expected-warning{{never read}}
73 return 1;
74}
75
Ted Kremenek8b00b6e2008-07-23 23:18:43 +000076int f11() {
77 int x = 4;
Ted Kremenekb0f36322008-07-24 17:01:17 +000078 return ++x; // expected-warning{{never read}}
Ted Kremenek8b00b6e2008-07-23 23:18:43 +000079}
80
Ted Kremenekfc7ff552008-07-25 04:47:34 +000081int f12a(int y) {
82 int x = y; // expected-warning{{never read}}
83 return 1;
84}
85int f12b(int y) {
86 int x __attribute__((unused)) = y; // no-warning
87 return 1;
88}
Ted Kremenek2cfac222008-07-23 21:16:38 +000089
Ted Kremenekefe88f52008-08-06 23:26:31 +000090// Filed with PR 2630. This code should produce no warnings.
91int f13(void)
92{
93 int a = 1;
94 int b, c = b = a + a;
95
96 if (b > 0)
97 return (0);
98
99 return (a + b + c);
100}
101
Ted Kremenekb497ebd2008-09-04 21:52:52 +0000102// Filed with PR 2763.
Ted Kremenek84fa6b92008-09-26 05:52:45 +0000103int f14(int count) {
Ted Kremenekb497ebd2008-09-04 21:52:52 +0000104 int index, nextLineIndex;
105 for (index = 0; index < count; index = nextLineIndex+1) {
106 nextLineIndex = index+1; // no-warning
107 continue;
108 }
109 return index;
110}
Ted Kremenek84fa6b92008-09-26 05:52:45 +0000111
112// Test case for <rdar://problem/6248086>
113void f15(unsigned x, unsigned y) {
114 int count = x * y; // no-warning
115 int z[count];
116}
117