dead-stores.c revision 632d1ece2715b1c8a0c99e1b8b487f4c148c89d5
1// RUN: clang -analyze -warn-dead-stores -verify %s && 2// RUN: clang -analyze -checker-simple -analyzer-store=basic -analyzer-constraints=basic -warn-dead-stores -verify %s && 3// RUN: clang -analyze -checker-simple -analyzer-store=basic -analyzer-constraints=range -warn-dead-stores -verify %s && 4// RUN: clang -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=basic -warn-dead-stores -verify %s && 5// RUN: clang -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=range -warn-dead-stores -verify %s && 6// RUN: clang -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=basic -warn-dead-stores -verify %s && 7// RUN: clang -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=range -warn-dead-stores -verify %s 8 9void f1() { 10 int k, y; 11 int abc=1; 12 long idx=abc+3*5; // expected-warning {{never read}} 13} 14 15void f2(void *b) { 16 char *c = (char*)b; // no-warning 17 char *d = b+1; // expected-warning {{never read}} 18 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'}} 20} 21 22void f3() { 23 int r; 24 if ((r = f()) != 0) { // no-warning 25 int y = r; // no-warning 26 printf("the error is: %d\n", y); 27 } 28} 29 30void f4(int k) { 31 32 k = 1; 33 34 if (k) 35 f1(); 36 37 k = 2; // expected-warning {{never read}} 38} 39 40void f5() { 41 42 int x = 4; // no-warning 43 int *p = &x; // expected-warning{{never read}} 44 45} 46 47int f6() { 48 49 int x = 4; 50 ++x; // expected-warning{{never read}} 51 return 1; 52} 53 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) { 61 extern int *baz(); 62 if (p = baz()) // expected-warning{{Although the value}} 63 return 1; 64 return 0; 65} 66 67int f9() { 68 int x = 4; 69 x = x + 10; // expected-warning{{never read}} 70 return 1; 71} 72 73int f10() { 74 int x = 4; 75 x = 10 + x; // expected-warning{{never read}} 76 return 1; 77} 78 79int f11() { 80 int x = 4; 81 return x++; // expected-warning{{never read}} 82} 83 84int f11b() { 85 int x = 4; 86 return ((((++x)))); // no-warning 87} 88 89int 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} 97 98// 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 110// Filed with PR 2763. 111int f14(int count) { 112 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} 119 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 126int f16(int x) { 127 x = x * 2; 128 x = sizeof(int [x = (x || x + 1) * 2]) // expected-warning{{Although the value stored to 'x' is used}} 129 ? 5 : 8; 130 return x; 131} 132 133// Self-assignments should not be flagged as dead stores. 134int f17() { 135 int x = 1; 136 x = x; // no-warning 137} 138 139// <rdar://problem/6506065> 140// The values of dead stores are only "consumed" in an enclosing expression 141// what that value is actually used. In other words, don't say "Although the value stored to 'x' is used...". 142int f18() { 143 int x = 0; // no-warning 144 if (1) 145 x = 10; // expected-warning{{Value stored to 'x' is never read}} 146 while (1) 147 x = 10; // expected-warning{{Value stored to 'x' is never read}} 148 do 149 x = 10; // expected-warning{{Value stored to 'x' is never read}} 150 while (1); 151 152 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'}} 153} 154 155// PR 3514: false positive `dead initialization` warning for init to global 156// http://llvm.org/bugs/show_bug.cgi?id=3514 157extern const int MyConstant; 158int f19(void) { 159 int x = MyConstant; // no-warning 160 x = 1; 161 return x; 162} 163 164int f19b(void) { // FIXME: Should this case be considered the same as f19? 165 const int MyConstant = 0; 166 int x = MyConstant; // expected-warning{{never read}} 167 x = 1; 168 return x; 169} 170 171void f20(void) { 172 int x = 1; // no-warning 173#pragma unused(x) 174} 175 176