1 // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -analyze -analyzer-checker=deadcode.DeadStores -verify -Wno-unreachable-code %s
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
4 
5 //===----------------------------------------------------------------------===//
6 // Basic dead store checking (but in C++ mode).
7 //===----------------------------------------------------------------------===//
8 
9 int j;
10 void test1() {
11   int x = 4;
12 
13   x = x + 1; // expected-warning{{never read}}
14 
15   switch (j) {
16   case 1:
17     throw 1;
18     (void)x;
19     break;
20   }
21 }
22 
23 //===----------------------------------------------------------------------===//
24 // Dead store checking involving constructors.
25 //===----------------------------------------------------------------------===//
26 
27 class Test2 {
28   int &x;
29 public:
30   Test2(int &y) : x(y) {}
31   ~Test2() { ++x; }
32 };
33 
34 int test2(int x) {
35   { Test2 a(x); } // no-warning
36   return x;
37 }
38 
39 //===----------------------------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
66 // Test references.
67 //===----------------------------------------------------------------------===//
68 
69 void test3_a(int x) {
70    x = x + 1; // expected-warning{{never read}}
71 }
72 
73 void test3_b(int &x) {
74   x = x + 1; // no-warninge
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 
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 
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 
113