blob: 43d8796ce0919e18ae363bc508e93efc1f975864 [file] [log] [blame]
Ted Kremenekade31952011-03-12 06:14:28 +00001// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -analyze -analyzer-checker=deadcode.DeadStores -verify -Wno-unreachable-code %s
Ted Kremenekade31952011-03-12 06:14:28 +00002// 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 Stump0979d802009-07-22 22:56:04 +00004
Ted Kremenek43f19e32009-12-15 04:12:12 +00005//===----------------------------------------------------------------------===//
6// Basic dead store checking (but in C++ mode).
7//===----------------------------------------------------------------------===//
8
Mike Stump0979d802009-07-22 22:56:04 +00009int j;
Ted Kremenek852274d2009-12-16 03:18:58 +000010void test1() {
Mike Stump0979d802009-07-22 22:56:04 +000011 int x = 4;
12
Ted Kremenekf4e532b2011-02-12 00:17:19 +000013 x = x + 1; // expected-warning{{never read}}
Mike Stump0979d802009-07-22 22:56:04 +000014
15 switch (j) {
16 case 1:
17 throw 1;
18 (void)x;
19 break;
20 }
21}
Ted Kremenek43f19e32009-12-15 04:12:12 +000022
23//===----------------------------------------------------------------------===//
24// Dead store checking involving constructors.
25//===----------------------------------------------------------------------===//
26
Ted Kremenek852274d2009-12-16 03:18:58 +000027class Test2 {
Ted Kremenek43f19e32009-12-15 04:12:12 +000028 int &x;
29public:
Ted Kremenek852274d2009-12-16 03:18:58 +000030 Test2(int &y) : x(y) {}
31 ~Test2() { ++x; }
Ted Kremenek43f19e32009-12-15 04:12:12 +000032};
33
Ted Kremenek852274d2009-12-16 03:18:58 +000034int test2(int x) {
35 { Test2 a(x); } // no-warning
Ted Kremenek43f19e32009-12-15 04:12:12 +000036 return x;
37}
Ted Kremenek852274d2009-12-16 03:18:58 +000038
39//===----------------------------------------------------------------------===//
Ted Kremenek604d9392009-12-23 04:11:44 +000040// Dead store checking involving CXXTemporaryExprs
41//===----------------------------------------------------------------------===//
42
43namespace 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
61void test2_b() {
62 TestTemp::row<const char*> x; // no-warning
63}
64
65//===----------------------------------------------------------------------===//
Ted Kremenek852274d2009-12-16 03:18:58 +000066// Test references.
67//===----------------------------------------------------------------------===//
68
69void test3_a(int x) {
Ted Kremenekf4e532b2011-02-12 00:17:19 +000070 x = x + 1; // expected-warning{{never read}}
Ted Kremenek852274d2009-12-16 03:18:58 +000071}
72
73void test3_b(int &x) {
Ted Kremenekf4e532b2011-02-12 00:17:19 +000074 x = x + 1; // no-warninge
Ted Kremenek852274d2009-12-16 03:18:58 +000075}
76
77void 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
84void test3_d(int &x) {
85 int &y = x;
86 ++y; // no-warning
87}
88
89void test3_e(int &x) {
90 int &y = x;
91}
92
Ted Kremeneka0063422010-06-25 22:48:52 +000093//===----------------------------------------------------------------------===//
94// Dead stores involving 'new'
95//===----------------------------------------------------------------------===//
96
97static void test_new(unsigned n) {
98 char **p = new char* [n]; // expected-warning{{never read}}
99}
100
Ted Kremenek14cc9452011-01-20 17:09:48 +0000101//===----------------------------------------------------------------------===//
102// Dead stores in namespaces.
103//===----------------------------------------------------------------------===//
104
105namespace 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