Ted Kremenek | cd9902b | 2010-02-05 01:52:40 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-dead-stores -verify -Wno-unreachable-code %s |
Ted Kremenek | 565e465 | 2010-02-05 02:06:54 +0000 | [diff] [blame] | 2 | // RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=basic -analyzer-check-dead-stores -verify -Wno-unreachable-code %s |
| 3 | // RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=range -analyzer-check-dead-stores -verify -Wno-unreachable-code %s |
| 4 | // RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=basic -analyzer-check-dead-stores -verify -Wno-unreachable-code %s |
| 5 | // RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=range -analyzer-check-dead-stores -verify -Wno-unreachable-code %s |
Mike Stump | 0979d80 | 2009-07-22 22:56:04 +0000 | [diff] [blame] | 6 | |
Ted Kremenek | 43f19e3 | 2009-12-15 04:12:12 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
| 8 | // Basic dead store checking (but in C++ mode). |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
Mike Stump | 0979d80 | 2009-07-22 22:56:04 +0000 | [diff] [blame] | 11 | int j; |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 12 | void test1() { |
Mike Stump | 0979d80 | 2009-07-22 22:56:04 +0000 | [diff] [blame] | 13 | int x = 4; |
| 14 | |
| 15 | ++x; // expected-warning{{never read}} |
| 16 | |
| 17 | switch (j) { |
| 18 | case 1: |
| 19 | throw 1; |
| 20 | (void)x; |
| 21 | break; |
| 22 | } |
| 23 | } |
Ted Kremenek | 43f19e3 | 2009-12-15 04:12:12 +0000 | [diff] [blame] | 24 | |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | // Dead store checking involving constructors. |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 29 | class Test2 { |
Ted Kremenek | 43f19e3 | 2009-12-15 04:12:12 +0000 | [diff] [blame] | 30 | int &x; |
| 31 | public: |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 32 | Test2(int &y) : x(y) {} |
| 33 | ~Test2() { ++x; } |
Ted Kremenek | 43f19e3 | 2009-12-15 04:12:12 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 36 | int test2(int x) { |
| 37 | { Test2 a(x); } // no-warning |
Ted Kremenek | 43f19e3 | 2009-12-15 04:12:12 +0000 | [diff] [blame] | 38 | return x; |
| 39 | } |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 40 | |
| 41 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 604d939 | 2009-12-23 04:11:44 +0000 | [diff] [blame] | 42 | // Dead store checking involving CXXTemporaryExprs |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | |
| 45 | namespace TestTemp { |
| 46 | template<typename _Tp> |
| 47 | class pencil { |
| 48 | public: |
| 49 | ~pencil() throw() {} |
| 50 | }; |
| 51 | template<typename _Tp, typename _Number2> struct _Row_base { |
| 52 | _Row_base(const pencil<_Tp>& x) {} |
| 53 | }; |
| 54 | template<typename _Tp, typename _Number2 = TestTemp::pencil<_Tp> > |
| 55 | class row : protected _Row_base<_Tp, _Number2> { |
| 56 | typedef _Row_base<_Tp, _Number2> _Base; |
| 57 | typedef _Number2 pencil_type; |
| 58 | public: |
| 59 | explicit row(const pencil_type& __a = pencil_type()) : _Base(__a) {} |
| 60 | }; |
| 61 | } |
| 62 | |
| 63 | void test2_b() { |
| 64 | TestTemp::row<const char*> x; // no-warning |
| 65 | } |
| 66 | |
| 67 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 68 | // Test references. |
| 69 | //===----------------------------------------------------------------------===// |
| 70 | |
| 71 | void test3_a(int x) { |
| 72 | ++x; // expected-warning{{never read}} |
| 73 | } |
| 74 | |
| 75 | void test3_b(int &x) { |
| 76 | ++x; // no-warninge |
| 77 | } |
| 78 | |
| 79 | void test3_c(int x) { |
| 80 | int &y = x; |
| 81 | // Shows the limitation of dead stores tracking. The write is really |
| 82 | // dead since the value cannot escape the function. |
| 83 | ++y; // no-warning |
| 84 | } |
| 85 | |
| 86 | void test3_d(int &x) { |
| 87 | int &y = x; |
| 88 | ++y; // no-warning |
| 89 | } |
| 90 | |
| 91 | void test3_e(int &x) { |
| 92 | int &y = x; |
| 93 | } |
| 94 | |
Ted Kremenek | a006342 | 2010-06-25 22:48:52 +0000 | [diff] [blame] | 95 | //===----------------------------------------------------------------------===// |
| 96 | // Dead stores involving 'new' |
| 97 | //===----------------------------------------------------------------------===// |
| 98 | |
| 99 | static void test_new(unsigned n) { |
| 100 | char **p = new char* [n]; // expected-warning{{never read}} |
| 101 | } |
| 102 | |