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