dead-stores.c revision a316e7b735b12ce6b34961a9dcfaae34f4b08d29
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); // expected-warning{{implicitly declaring C library function 'printf' with type 'int (char const *, ...)'}} \ 16 // expected-note{{please include the header <stdio.h> or explicitly provide a declaration for 'printf'}} 17} 18 19void f3() { 20 int r; 21 if ((r = f()) != 0) { // no-warning 22 int y = r; // no-warning 23 printf("the error is: %d\n", y); 24 } 25} 26 27void f4(int k) { 28 29 k = 1; 30 31 if (k) 32 f1(); 33 34 k = 2; // expected-warning {{never read}} 35} 36 37void f5() { 38 39 int x = 4; // no-warning 40 int *p = &x; // expected-warning{{never read}} 41 42} 43 44int f6() { 45 46 int x = 4; 47 ++x; // expected-warning{{never read}} 48 return 1; 49} 50 51int f7(int *p) { 52 // This is allowed for defensive programming. 53 p = 0; // no-warning 54 return 1; 55} 56 57int f8(int *p) { 58 extern int *baz(); 59 if (p = baz()) // expected-warning{{Although the value}} 60 return 1; 61 return 0; 62} 63 64int f9() { 65 int x = 4; 66 x = x + 10; // expected-warning{{never read}} 67 return 1; 68} 69 70int f10() { 71 int x = 4; 72 x = 10 + x; // expected-warning{{never read}} 73 return 1; 74} 75 76int f11() { 77 int x = 4; 78 return x++; // expected-warning{{never read}} 79} 80 81int f11b() { 82 int x = 4; 83 return ((((++x)))); // no-warning 84} 85 86int f12a(int y) { 87 int x = y; // expected-warning{{never read}} 88 return 1; 89} 90int f12b(int y) { 91 int x __attribute__((unused)) = y; // no-warning 92 return 1; 93} 94 95// Filed with PR 2630. This code should produce no warnings. 96int f13(void) 97{ 98 int a = 1; 99 int b, c = b = a + a; 100 101 if (b > 0) 102 return (0); 103 104 return (a + b + c); 105} 106 107// Filed with PR 2763. 108int f14(int count) { 109 int index, nextLineIndex; 110 for (index = 0; index < count; index = nextLineIndex+1) { 111 nextLineIndex = index+1; // no-warning 112 continue; 113 } 114 return index; 115} 116 117// Test case for <rdar://problem/6248086> 118void f15(unsigned x, unsigned y) { 119 int count = x * y; // no-warning 120 int z[count]; 121} 122 123int f16(int x) { 124 x = x * 2; 125 x = sizeof(int [x = (x || x + 1) * 2]) // expected-warning{{Although the value stored to 'x' is used}} 126 ? 5 : 8; 127 return x; 128} 129 130// Self-assignments should not be flagged as dead stores. 131int f17() { 132 int x = 1; 133 x = x; // no-warning 134} 135 136// <rdar://problem/6506065> 137// The values of dead stores are only "consumed" in an enclosing expression 138// what that value is actually used. In other words, don't say "Although the value stored to 'x' is used...". 139int f18() { 140 int x = 0; // no-warning 141 if (1) 142 x = 10; // expected-warning{{Value stored to 'x' is never read}} 143 while (1) 144 x = 10; // expected-warning{{Value stored to 'x' is never read}} 145 do 146 x = 10; // expected-warning{{Value stored to 'x' is never read}} 147 while (1); 148 149 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'}} 150} 151 152// PR 3514: false positive `dead initialization` warning for init to global 153// http://llvm.org/bugs/show_bug.cgi?id=3514 154extern const int MyConstant; 155int f19(void) { 156 int x = MyConstant; // no-warning 157 x = 1; 158 return x; 159} 160 161int f19b(void) { // FIXME: Should this case be considered the same as f19? 162 const int MyConstant = 0; 163 int x = MyConstant; // expected-warning{{never read}} 164 x = 1; 165 return x; 166} 167