Tom Care | df4ca42 | 2010-07-16 20:41:41 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-store=region -analyzer-constraints=range -fblocks -verify -analyzer-opt-analyze-nested-blocks -analyzer-check-objc-mem -verify %s |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 2 | |
| 3 | // Basic tests |
| 4 | |
| 5 | extern void test(int i); |
Ted Kremenek | 4532931 | 2010-07-17 00:40:32 +0000 | [diff] [blame] | 6 | extern void test_f(float f); |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 7 | |
| 8 | void basic() { |
| 9 | int x = 10, zero = 0, one = 1; |
| 10 | |
| 11 | // x op x |
Ted Kremenek | 3e5637f | 2010-07-27 18:49:08 +0000 | [diff] [blame] | 12 | x = x; // expected-warning {{Assigned value is always the same as the existing value}} |
| 13 | test(x - x); // expected-warning {{Both operands to '-' always have the same value}} |
| 14 | x -= x; // expected-warning {{Both operands to '-=' always have the same value}} |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 15 | x = 10; // no-warning |
Ted Kremenek | 3e5637f | 2010-07-27 18:49:08 +0000 | [diff] [blame] | 16 | test(x / x); // expected-warning {{Both operands to '/' always have the same value}} |
| 17 | x /= x; // expected-warning {{Both operands to '/=' always have the same value}} |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 18 | x = 10; // no-warning |
Ted Kremenek | 3e5637f | 2010-07-27 18:49:08 +0000 | [diff] [blame] | 19 | test(x & x); // expected-warning {{Both operands to '&' always have the same value}} |
| 20 | x &= x; // expected-warning {{Both operands to '&=' always have the same value}} |
| 21 | test(x | x); // expected-warning {{Both operands to '|' always have the same value}} |
| 22 | x |= x; // expected-warning {{Both operands to '|=' always have the same value}} |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 23 | |
| 24 | // x op 1 |
Ted Kremenek | 3e5637f | 2010-07-27 18:49:08 +0000 | [diff] [blame] | 25 | test(x * one); // expected-warning {{The right operand to '*' is always 1}} |
| 26 | x *= one; // expected-warning {{The right operand to '*=' is always 1}} |
| 27 | test(x / one); // expected-warning {{The right operand to '/' is always 1}} |
| 28 | x /= one; // expected-warning {{The right operand to '/=' is always 1}} |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 29 | |
| 30 | // 1 op x |
Ted Kremenek | 3e5637f | 2010-07-27 18:49:08 +0000 | [diff] [blame] | 31 | test(one * x); // expected-warning {{The left operand to '*' is always 1}} |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 32 | |
| 33 | // x op 0 |
Ted Kremenek | 3e5637f | 2010-07-27 18:49:08 +0000 | [diff] [blame] | 34 | test(x + zero); // expected-warning {{The right operand to '+' is always 0}} |
| 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}} |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 42 | |
| 43 | // 0 op x |
Ted Kremenek | 3e5637f | 2010-07-27 18:49:08 +0000 | [diff] [blame] | 44 | test(zero + x); // expected-warning {{The left operand to '+' is always 0}} |
| 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}} |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 53 | } |
Ted Kremenek | 4532931 | 2010-07-17 00:40:32 +0000 | [diff] [blame] | 54 | |
| 55 | void floats(float x) { |
| 56 | test_f(x * 1.0); // no-warning |
| 57 | test_f(x * 1.0F); // no-warning |
| 58 | } |
Tom Care | d85770b | 2010-07-30 21:42:31 +0000 | [diff] [blame^] | 59 | |
| 60 | // Ensure that we don't report false poitives on complex loops |
| 61 | void bailout() { |
| 62 | int unused, result = 4; |
| 63 | int numbers[5] = { 0, 32, 'x', 128, 255 }; |
| 64 | |
| 65 | for (int bg = 0; bg < 5; bg ++) { |
| 66 | result += numbers[bg]; // no-warning |
| 67 | |
| 68 | for (int i = 0; i < 256; i++) { |
| 69 | unused = i; |
| 70 | } |
| 71 | } |
| 72 | } |