Tom Care | 1fafd1d | 2010-08-06 22:23:07 +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 -analyzer-check-idempotent-operations -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 | |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame^] | 8 | unsigned basic() { |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 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 | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame^] | 53 | |
| 54 | // Overwrite the values so these aren't marked as psuedoconstants |
| 55 | x = 1; |
| 56 | zero = 2; |
| 57 | one = 3; |
| 58 | |
| 59 | return x + zero + one; |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 60 | } |
Ted Kremenek | 4532931 | 2010-07-17 00:40:32 +0000 | [diff] [blame] | 61 | |
| 62 | void floats(float x) { |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 63 | test_f(x * 1.0); // no-warning |
Ted Kremenek | 4532931 | 2010-07-17 00:40:32 +0000 | [diff] [blame] | 64 | test_f(x * 1.0F); // no-warning |
| 65 | } |
Tom Care | d85770b | 2010-07-30 21:42:31 +0000 | [diff] [blame] | 66 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 67 | // Ensure that we don't report false poitives in complex loops |
Tom Care | d85770b | 2010-07-30 21:42:31 +0000 | [diff] [blame] | 68 | void bailout() { |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 69 | int unused = 0, result = 4; |
| 70 | 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] | 71 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 72 | for (unsigned bg = 0; bg < 1024; bg ++) { |
| 73 | result = bg * result; // no-warning |
Tom Care | d85770b | 2010-07-30 21:42:31 +0000 | [diff] [blame] | 74 | |
| 75 | for (int i = 0; i < 256; i++) { |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 76 | unused *= i; // no-warning |
Tom Care | d85770b | 2010-07-30 21:42:31 +0000 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | } |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 80 | |
| 81 | // False positive tests |
| 82 | |
| 83 | unsigned false1() { |
Tom Care | 50e8ac2 | 2010-08-16 21:43:52 +0000 | [diff] [blame] | 84 | int a = 10; |
| 85 | return a * (5 - 2 - 3); // no-warning |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | enum testenum { enum1 = 0, enum2 }; |
| 89 | unsigned false2() { |
Tom Care | 50e8ac2 | 2010-08-16 21:43:52 +0000 | [diff] [blame] | 90 | int a = 1234; |
| 91 | return enum1 + a; // no-warning |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame^] | 94 | // Self assignments of parameters are common false positives |
| 95 | unsigned false3(int param) { |
| 96 | param = param; // no-warning |
| 97 | |
| 98 | unsigned nonparam = 5; |
| 99 | |
| 100 | nonparam = nonparam; // expected-warning{{Assigned value is always the same as the existing value}} |
| 101 | |
| 102 | return nonparam; |
| 103 | } |
| 104 | |
| 105 | // Psuedo-constants (vars only read) and constants should not be reported |
| 106 | unsigned false4() { |
| 107 | // Trivial constant |
| 108 | const int height = 1; // no-warning |
| 109 | // Psuedo-constant (never changes after decl) |
| 110 | int width = height; // no-warning |
| 111 | |
| 112 | return width * 10; // no-warning |
| 113 | } |