dead-stores.c revision 610a09e409bea151a42dd907768f1e0c4b103f1f
1// RUN: clang -warn-dead-stores -verify %s &&
2// RUN: clang -checker-simple -warn-dead-stores -verify %s &&
3// RUN: clang -warn-dead-stores -checker-simple -verify %s
4
5
6void f1() {
7  int k, y;
8  int abc=1;
9  long idx=abc+3*5; // expected-warning {{never read}}
10}
11
12void f2(void *b) {
13 char *c = (char*)b; // no-warning
14 char *d = b+1; // expected-warning {{never read}}
15 printf("%s", c);
16}
17
18void f3() {
19  int r;
20  if ((r = f()) != 0) { // no-warning
21    int y = r; // no-warning
22    printf("the error is: %d\n", y);
23  }
24}
25
26void f4(int k) {
27
28  k = 1;
29
30  if (k)
31    f1();
32
33  k = 2;  // expected-warning {{never read}}
34}
35
36void f5() {
37
38  int x = 4; // no-warning
39  int *p = &x; // expected-warning{{never read}}
40
41}
42
43int f6() {
44
45  int x = 4;
46  ++x; // expected-warning{{never read}}
47  return 1;
48}
49
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) {
57  extern int *baz();
58  if (p = baz()) // expected-warning{{Although the value}}
59    return 1;
60  return 0;
61}
62
63int f9() {
64  int x = 4;
65  x = x + 10; // expected-warning{{never read}}
66  return 1;
67}
68
69int f10() {
70  int x = 4;
71  x = 10 + x; // expected-warning{{never read}}
72  return 1;
73}
74
75int f11() {
76  int x = 4;
77  return ++x; // expected-warning{{never read}}
78}
79
80int f12a(int y) {
81  int x = y;  // expected-warning{{never read}}
82  return 1;
83}
84int f12b(int y) {
85  int x __attribute__((unused)) = y;  // no-warning
86  return 1;
87}
88
89// Filed with PR 2630.  This code should produce no warnings.
90int f13(void)
91{
92  int a = 1;
93  int b, c = b = a + a;
94
95  if (b > 0)
96    return (0);
97
98  return (a + b + c);
99}
100
101// Filed with PR 2763.
102int f14(int count) {
103  int index, nextLineIndex;
104  for (index = 0; index < count; index = nextLineIndex+1) {
105    nextLineIndex = index+1;  // no-warning
106    continue;
107  }
108  return index;
109}
110
111// Test case for <rdar://problem/6248086>
112void f15(unsigned x, unsigned y) {
113  int count = x * y;   // no-warning
114  int z[count];
115}
116
117int f16(int x) {
118  x = x * 2;
119  x = sizeof(int [x = (x || x + 1) * 2]);  // expected-warning{{Although the value stored to 'x' is used}}
120  return x;
121}
122
123