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