Ted Kremenek | ade3195 | 2011-03-12 06:14:28 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-store=region -analyzer-constraints=range -fblocks -analyzer-opt-analyze-nested-blocks -analyzer-checker=deadcode.IdempotentOperations -verify %s |
Argyrios Kyrtzidis | 1659acb | 2011-04-04 22:30:01 +0000 | [diff] [blame] | 2 | // RUN: %clang --analyze -Xclang -analyzer-disable-checker=deadcode.DeadStores -fblocks -Xclang -verify %s -o %t |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 3 | |
| 4 | // Basic tests |
| 5 | |
| 6 | extern void test(int i); |
Ted Kremenek | 4532931 | 2010-07-17 00:40:32 +0000 | [diff] [blame] | 7 | extern void test_f(float f); |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 8 | |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 9 | unsigned basic() { |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 10 | int x = 10, zero = 0, one = 1; |
| 11 | |
| 12 | // x op x |
Ted Kremenek | 3e5637f | 2010-07-27 18:49:08 +0000 | [diff] [blame] | 13 | x = x; // expected-warning {{Assigned value is always the same as the existing value}} |
| 14 | test(x - x); // expected-warning {{Both operands to '-' always have the same value}} |
| 15 | x -= x; // expected-warning {{Both operands to '-=' always have the same value}} |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 16 | x = 10; // no-warning |
Ted Kremenek | 3e5637f | 2010-07-27 18:49:08 +0000 | [diff] [blame] | 17 | test(x / x); // expected-warning {{Both operands to '/' always have the same value}} |
| 18 | x /= x; // expected-warning {{Both operands to '/=' always have the same value}} |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 19 | x = 10; // no-warning |
Ted Kremenek | 3e5637f | 2010-07-27 18:49:08 +0000 | [diff] [blame] | 20 | test(x & x); // expected-warning {{Both operands to '&' always have the same value}} |
| 21 | x &= x; // expected-warning {{Both operands to '&=' always have the same value}} |
| 22 | test(x | x); // expected-warning {{Both operands to '|' always have the same value}} |
| 23 | x |= x; // expected-warning {{Both operands to '|=' always have the same value}} |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 24 | |
| 25 | // x op 1 |
Ted Kremenek | 3e5637f | 2010-07-27 18:49:08 +0000 | [diff] [blame] | 26 | test(x * one); // expected-warning {{The right operand to '*' is always 1}} |
| 27 | x *= one; // expected-warning {{The right operand to '*=' is always 1}} |
| 28 | test(x / one); // expected-warning {{The right operand to '/' is always 1}} |
| 29 | x /= one; // expected-warning {{The right operand to '/=' is always 1}} |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 30 | |
| 31 | // 1 op x |
Ted Kremenek | 3e5637f | 2010-07-27 18:49:08 +0000 | [diff] [blame] | 32 | test(one * x); // expected-warning {{The left operand to '*' is always 1}} |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 33 | |
| 34 | // x op 0 |
Ted Kremenek | 3e5637f | 2010-07-27 18:49:08 +0000 | [diff] [blame] | 35 | test(x + zero); // expected-warning {{The right operand to '+' is always 0}} |
| 36 | test(x - zero); // expected-warning {{The right operand to '-' is always 0}} |
| 37 | test(x * zero); // expected-warning {{The right operand to '*' is always 0}} |
| 38 | test(x & zero); // expected-warning {{The right operand to '&' is always 0}} |
| 39 | test(x | zero); // expected-warning {{The right operand to '|' is always 0}} |
| 40 | test(x ^ zero); // expected-warning {{The right operand to '^' is always 0}} |
| 41 | test(x << zero); // expected-warning {{The right operand to '<<' is always 0}} |
| 42 | test(x >> zero); // expected-warning {{The right operand to '>>' is always 0}} |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 43 | |
| 44 | // 0 op x |
Ted Kremenek | 3e5637f | 2010-07-27 18:49:08 +0000 | [diff] [blame] | 45 | test(zero + x); // expected-warning {{The left operand to '+' is always 0}} |
| 46 | test(zero - x); // expected-warning {{The left operand to '-' is always 0}} |
| 47 | test(zero / x); // expected-warning {{The left operand to '/' is always 0}} |
| 48 | test(zero * x); // expected-warning {{The left operand to '*' is always 0}} |
| 49 | test(zero & x); // expected-warning {{The left operand to '&' is always 0}} |
| 50 | test(zero | x); // expected-warning {{The left operand to '|' is always 0}} |
| 51 | test(zero ^ x); // expected-warning {{The left operand to '^' is always 0}} |
| 52 | test(zero << x); // expected-warning {{The left operand to '<<' is always 0}} |
| 53 | test(zero >> x); // expected-warning {{The left operand to '>>' is always 0}} |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 54 | |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 55 | // Overwrite the values so these aren't marked as Pseudoconstants |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 56 | x = 1; |
| 57 | zero = 2; |
| 58 | one = 3; |
| 59 | |
| 60 | return x + zero + one; |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 61 | } |
Ted Kremenek | 4532931 | 2010-07-17 00:40:32 +0000 | [diff] [blame] | 62 | |
| 63 | void floats(float x) { |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 64 | test_f(x * 1.0); // no-warning |
Ted Kremenek | 4532931 | 2010-07-17 00:40:32 +0000 | [diff] [blame] | 65 | test_f(x * 1.0F); // no-warning |
| 66 | } |
Tom Care | d85770b | 2010-07-30 21:42:31 +0000 | [diff] [blame] | 67 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 68 | // Ensure that we don't report false poitives in complex loops |
Tom Care | d85770b | 2010-07-30 21:42:31 +0000 | [diff] [blame] | 69 | void bailout() { |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 70 | int unused = 0, result = 4; |
| 71 | result = result; // expected-warning {{Assigned value is always the same as the existing value}} |
Tom Care | d85770b | 2010-07-30 21:42:31 +0000 | [diff] [blame] | 72 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 73 | for (unsigned bg = 0; bg < 1024; bg ++) { |
| 74 | result = bg * result; // no-warning |
Tom Care | d85770b | 2010-07-30 21:42:31 +0000 | [diff] [blame] | 75 | |
| 76 | for (int i = 0; i < 256; i++) { |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 77 | unused *= i; // no-warning |
Tom Care | d85770b | 2010-07-30 21:42:31 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | } |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 81 | |
Tom Care | 6d0e6ce | 2010-08-27 22:46:32 +0000 | [diff] [blame] | 82 | // Relaxed liveness - check that we don't kill liveness at assignments |
| 83 | typedef unsigned uintptr_t; |
| 84 | void kill_at_assign() { |
| 85 | short array[2]; |
Ted Kremenek | ade3195 | 2011-03-12 06:14:28 +0000 | [diff] [blame] | 86 | uintptr_t x = (uintptr_t) array; |
| 87 | short *p = (short *) x; |
Tom Care | 6d0e6ce | 2010-08-27 22:46:32 +0000 | [diff] [blame] | 88 | |
| 89 | // The following branch should be infeasible. |
| 90 | if (!(p = &array[0])) { // expected-warning{{Assigned value is always the same as the existing value}} |
| 91 | p = 0; |
| 92 | *p = 1; // no-warning |
| 93 | } |
| 94 | } |
| 95 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 96 | // False positive tests |
| 97 | |
| 98 | unsigned false1() { |
Tom Care | 50e8ac2 | 2010-08-16 21:43:52 +0000 | [diff] [blame] | 99 | int a = 10; |
| 100 | return a * (5 - 2 - 3); // no-warning |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | enum testenum { enum1 = 0, enum2 }; |
| 104 | unsigned false2() { |
Tom Care | 50e8ac2 | 2010-08-16 21:43:52 +0000 | [diff] [blame] | 105 | int a = 1234; |
| 106 | return enum1 + a; // no-warning |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 109 | // Self assignments of unused variables are common false positives |
| 110 | unsigned false3(int param, int param2) { |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 111 | param = param; // no-warning |
| 112 | |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 113 | // if a self assigned variable is used later, then it should be reported still |
| 114 | param2 = param2; // expected-warning{{Assigned value is always the same as the existing value}} |
| 115 | |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 116 | unsigned nonparam = 5; |
| 117 | |
| 118 | nonparam = nonparam; // expected-warning{{Assigned value is always the same as the existing value}} |
| 119 | |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 120 | return param2 + nonparam; |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 123 | // Pseudo-constants (vars only read) and constants should not be reported |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 124 | unsigned false4() { |
| 125 | // Trivial constant |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 126 | const int height = 1; |
Tom Care | 8238941 | 2010-08-23 19:57:25 +0000 | [diff] [blame] | 127 | int c = 42; |
| 128 | test(height * c); // no-warning |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 129 | |
Tom Care | 8238941 | 2010-08-23 19:57:25 +0000 | [diff] [blame] | 130 | // Pseudo-constant (never changes after decl) |
| 131 | int width = height; |
| 132 | |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 133 | return width * 10; // no-warning |
| 134 | } |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 135 | |
Tom Care | 967fea6 | 2010-08-25 22:37:26 +0000 | [diff] [blame] | 136 | // Block pseudoconstants |
| 137 | void false4a() { |
| 138 | // Pseudo-constant |
| 139 | __block int a = 1; |
| 140 | int b = 10; |
| 141 | __block int c = 0; |
| 142 | b *= a; // no-warning |
| 143 | |
| 144 | ^{ |
| 145 | // Psuedoconstant block var |
| 146 | test(b * c); // no-warning |
| 147 | |
| 148 | // Non-pseudoconstant block var |
| 149 | int d = 0; |
| 150 | test(b * d); // expected-warning{{The right operand to '*' is always 0}} |
| 151 | d = 5; |
| 152 | test(d); |
| 153 | }(); |
| 154 | |
| 155 | test(a + b); |
| 156 | } |
| 157 | |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 158 | // Static vars are common false positives |
| 159 | int false5() { |
| 160 | static int test = 0; |
| 161 | int a = 56; |
| 162 | a *= test; // no-warning |
| 163 | test++; |
| 164 | return a; |
| 165 | } |
| 166 | |
| 167 | // Non-local storage vars are considered false positives |
| 168 | int globalInt = 1; |
| 169 | int false6() { |
| 170 | int localInt = 23; |
| 171 | |
| 172 | localInt /= globalInt; |
| 173 | |
| 174 | return localInt; |
| 175 | } |
Tom Care | 6216dc0 | 2010-08-30 19:25:43 +0000 | [diff] [blame] | 176 | |
| 177 | // Check that assignments filter out false positives correctly |
| 178 | int false7() { |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 179 | int zero = 0; // pseudo-constant |
Tom Care | 6216dc0 | 2010-08-30 19:25:43 +0000 | [diff] [blame] | 180 | int one = 1; |
| 181 | |
| 182 | int a = 55; |
| 183 | a = a; // expected-warning{{Assigned value is always the same as the existing value}} |
| 184 | a = enum1 * a; // no-warning |
| 185 | |
| 186 | int b = 123; |
| 187 | b = b; // no-warning |
| 188 | |
| 189 | return a; |
| 190 | } |
Tom Care | 84c24ed | 2010-09-07 20:27:56 +0000 | [diff] [blame] | 191 | |
| 192 | // Check truncations do not flag as self-assignments |
| 193 | void false8() { |
| 194 | int a = 10000000; |
| 195 | a = (short)a; // no-warning |
| 196 | test(a); |
| 197 | } |
Ted Kremenek | 02282ac | 2010-09-15 03:13:30 +0000 | [diff] [blame] | 198 | |
| 199 | // This test case previously flagged a warning at 'b == c' because the |
| 200 | // analyzer previously allowed 'UnknownVal' as the index for ElementRegions. |
| 201 | typedef struct RDar8431728_F { |
| 202 | int RDar8431728_A; |
| 203 | unsigned char *RDar8431728_B; |
| 204 | int RDar8431728_E[6]; |
| 205 | } RDar8431728_D; |
| 206 | static inline int RDar8431728_C(RDar8431728_D * s, int n, |
| 207 | unsigned char **RDar8431728_B_ptr) { |
| 208 | int xy, wrap, pred, a, b, c; |
| 209 | |
| 210 | xy = s->RDar8431728_E[n]; |
| 211 | wrap = s->RDar8431728_A; |
| 212 | |
| 213 | a = s->RDar8431728_B[xy - 1]; |
| 214 | b = s->RDar8431728_B[xy - 1 - wrap]; |
| 215 | c = s->RDar8431728_B[xy - wrap]; |
| 216 | |
| 217 | if (b == c) { // no-warning |
| 218 | pred = a; |
| 219 | } else { |
| 220 | pred = c; |
| 221 | } |
| 222 | |
| 223 | *RDar8431728_B_ptr = &s->RDar8431728_B[xy]; |
| 224 | |
| 225 | return pred; |
| 226 | } |
| 227 | |
Ted Kremenek | 74faec2 | 2010-10-29 01:06:54 +0000 | [diff] [blame] | 228 | // <rdar://problem/8601243> - Don't warn on pointer arithmetic. This |
| 229 | // is often idiomatic. |
| 230 | unsigned rdar8601243_aux(unsigned n); |
| 231 | void rdar8601243() { |
| 232 | char arr[100]; |
| 233 | char *start = arr; |
| 234 | start = start + rdar8601243_aux(sizeof(arr) - (arr - start)); // no-warning |
| 235 | (void) start; |
| 236 | } |
| 237 | |