Daniel Dunbar | d427023 | 2009-01-20 23:17:32 +0000 | [diff] [blame^] | 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 |
Ted Kremenek | e81da50 | 2008-07-02 23:18:22 +0000 | [diff] [blame] | 4 | |
Ted Kremenek | 8aefcbf | 2007-11-19 06:38:23 +0000 | [diff] [blame] | 5 | |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 6 | void f1() { |
Ted Kremenek | aa395ba | 2007-11-18 20:06:35 +0000 | [diff] [blame] | 7 | int k, y; |
Ted Kremenek | 0fdf06e | 2008-03-19 07:31:52 +0000 | [diff] [blame] | 8 | int abc=1; |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 9 | long idx=abc+3*5; // expected-warning {{never read}} |
Ted Kremenek | aa395ba | 2007-11-18 20:06:35 +0000 | [diff] [blame] | 10 | } |
Ted Kremenek | 8aefcbf | 2007-11-19 06:38:23 +0000 | [diff] [blame] | 11 | |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 12 | void f2(void *b) { |
Ted Kremenek | 8aefcbf | 2007-11-19 06:38:23 +0000 | [diff] [blame] | 13 | char *c = (char*)b; // no-warning |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 14 | char *d = b+1; // expected-warning {{never read}} |
Ted Kremenek | 8aefcbf | 2007-11-19 06:38:23 +0000 | [diff] [blame] | 15 | printf("%s", c); |
| 16 | } |
Ted Kremenek | 74c43a0 | 2007-11-20 03:03:00 +0000 | [diff] [blame] | 17 | |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 18 | void f3() { |
Ted Kremenek | 0fdf06e | 2008-03-19 07:31:52 +0000 | [diff] [blame] | 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 | } |
Ted Kremenek | 74c43a0 | 2007-11-20 03:03:00 +0000 | [diff] [blame] | 24 | } |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 25 | |
| 26 | void f4(int k) { |
| 27 | |
| 28 | k = 1; |
| 29 | |
| 30 | if (k) |
| 31 | f1(); |
| 32 | |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 33 | k = 2; // expected-warning {{never read}} |
Ted Kremenek | 49a2fd2 | 2008-04-14 15:56:17 +0000 | [diff] [blame] | 34 | } |
Ted Kremenek | f87821c | 2008-04-15 18:37:29 +0000 | [diff] [blame] | 35 | |
| 36 | void f5() { |
| 37 | |
| 38 | int x = 4; // no-warning |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 39 | int *p = &x; // expected-warning{{never read}} |
Ted Kremenek | f87821c | 2008-04-15 18:37:29 +0000 | [diff] [blame] | 40 | |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | int f6() { |
| 44 | |
| 45 | int x = 4; |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 46 | ++x; // expected-warning{{never read}} |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 47 | return 1; |
| 48 | } |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 49 | |
| 50 | int f7(int *p) { |
| 51 | // This is allowed for defensive programming. |
| 52 | p = 0; // no-warning |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | int f8(int *p) { |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 57 | extern int *baz(); |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 58 | if (p = baz()) // expected-warning{{Although the value}} |
| 59 | return 1; |
| 60 | return 0; |
| 61 | } |
| 62 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 63 | int f9() { |
| 64 | int x = 4; |
| 65 | x = x + 10; // expected-warning{{never read}} |
| 66 | return 1; |
| 67 | } |
| 68 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 69 | int f10() { |
| 70 | int x = 4; |
| 71 | x = 10 + x; // expected-warning{{never read}} |
| 72 | return 1; |
| 73 | } |
| 74 | |
Ted Kremenek | 8b00b6e | 2008-07-23 23:18:43 +0000 | [diff] [blame] | 75 | int f11() { |
| 76 | int x = 4; |
Ted Kremenek | 380277e | 2008-10-15 05:23:41 +0000 | [diff] [blame] | 77 | return x++; // expected-warning{{never read}} |
Ted Kremenek | 8b00b6e | 2008-07-23 23:18:43 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Ted Kremenek | 380277e | 2008-10-15 05:23:41 +0000 | [diff] [blame] | 80 | int f11b() { |
| 81 | int x = 4; |
Ted Kremenek | 7f5fce7 | 2009-01-20 00:47:45 +0000 | [diff] [blame] | 82 | return ((((++x)))); // no-warning |
Ted Kremenek | 380277e | 2008-10-15 05:23:41 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Ted Kremenek | fc7ff55 | 2008-07-25 04:47:34 +0000 | [diff] [blame] | 85 | int f12a(int y) { |
| 86 | int x = y; // expected-warning{{never read}} |
| 87 | return 1; |
| 88 | } |
| 89 | int f12b(int y) { |
| 90 | int x __attribute__((unused)) = y; // no-warning |
| 91 | return 1; |
| 92 | } |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 93 | |
Ted Kremenek | efe88f5 | 2008-08-06 23:26:31 +0000 | [diff] [blame] | 94 | // Filed with PR 2630. This code should produce no warnings. |
| 95 | int 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 | |
Ted Kremenek | b497ebd | 2008-09-04 21:52:52 +0000 | [diff] [blame] | 106 | // Filed with PR 2763. |
Ted Kremenek | 84fa6b9 | 2008-09-26 05:52:45 +0000 | [diff] [blame] | 107 | int f14(int count) { |
Ted Kremenek | b497ebd | 2008-09-04 21:52:52 +0000 | [diff] [blame] | 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 | } |
Ted Kremenek | 84fa6b9 | 2008-09-26 05:52:45 +0000 | [diff] [blame] | 115 | |
| 116 | // Test case for <rdar://problem/6248086> |
| 117 | void f15(unsigned x, unsigned y) { |
| 118 | int count = x * y; // no-warning |
| 119 | int z[count]; |
| 120 | } |
| 121 | |
Ted Kremenek | 610a09e | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 122 | int f16(int x) { |
| 123 | x = x * 2; |
Ted Kremenek | d2025e2 | 2008-09-26 23:05:47 +0000 | [diff] [blame] | 124 | x = sizeof(int [x = (x || x + 1) * 2]) // expected-warning{{Although the value stored to 'x' is used}} |
| 125 | ? 5 : 8; |
Ted Kremenek | 610a09e | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 126 | return x; |
| 127 | } |
| 128 | |
Ted Kremenek | 3b58786 | 2009-01-09 22:15:01 +0000 | [diff] [blame] | 129 | // Self-assignments should not be flagged as dead stores. |
| 130 | int f17() { |
| 131 | int x = 1; |
| 132 | x = x; // no-warning |
| 133 | } |
Ted Kremenek | 7f5fce7 | 2009-01-20 00:47:45 +0000 | [diff] [blame] | 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...". |
| 138 | int 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 | } |