dead-stores.c revision 3b58786f85aaa173e122f6eaff0b6efa233d59a2
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 f11b() { 81 int x = 4; 82 return ++x; // no-warning 83} 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