dead-stores.c revision d427023c334fe03105d9359711a3df4d6f23b344
1// RUN: clang -analyze -warn-dead-stores -verify %s && 2// RUN: clang -analyze -checker-simple -warn-dead-stores -verify %s && 3// RUN: clang -analyze -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 f11b() { 81 int x = 4; 82 return ((((++x)))); // no-warning 83} 84 85int f12a(int y) { 86 int x = y; // expected-warning{{never read}} 87 return 1; 88} 89int f12b(int y) { 90 int x __attribute__((unused)) = y; // no-warning 91 return 1; 92} 93 94// Filed with PR 2630. This code should produce no warnings. 95int f13(void) 96{ 97 int a = 1; 98 int b, c = b = a + a; 99 100 if (b > 0) 101 return (0); 102 103 return (a + b + c); 104} 105 106// Filed with PR 2763. 107int f14(int count) { 108 int index, nextLineIndex; 109 for (index = 0; index < count; index = nextLineIndex+1) { 110 nextLineIndex = index+1; // no-warning 111 continue; 112 } 113 return index; 114} 115 116// Test case for <rdar://problem/6248086> 117void f15(unsigned x, unsigned y) { 118 int count = x * y; // no-warning 119 int z[count]; 120} 121 122int f16(int x) { 123 x = x * 2; 124 x = sizeof(int [x = (x || x + 1) * 2]) // expected-warning{{Although the value stored to 'x' is used}} 125 ? 5 : 8; 126 return x; 127} 128 129// Self-assignments should not be flagged as dead stores. 130int f17() { 131 int x = 1; 132 x = x; // no-warning 133} 134 135// <rdar://problem/6506065> 136// The values of dead stores are only "consumed" in an enclosing expression 137// what that value is actually used. In other words, don't say "Although the value stored to 'x' is used...". 138int f18() { 139 int x = 0; // no-warning 140 if (1) 141 x = 10; // expected-warning{{Value stored to 'x' is never read}} 142 while (1) 143 x = 10; // expected-warning{{Value stored to 'x' is never read}} 144 do 145 x = 10; // expected-warning{{Value stored to 'x' is never read}} 146 while (1); 147 148 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'}} 149} 150