blob: 86d84f0fbfa460187f592745a688cf1524ef6bce [file] [log] [blame]
Ted Kremeneka4a57c12011-03-12 06:14:28 +00001// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -analyze -analyzer-checker=deadcode.DeadStores -verify -Wno-unreachable-code %s
Ted Kremeneka4a57c12011-03-12 06:14:28 +00002// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -analyze -analyzer-store=region -analyzer-constraints=range -analyzer-checker=deadcode.DeadStores -verify -Wno-unreachable-code %s
Mike Stump8dd1b6b2009-07-22 22:56:04 +00003
Ted Kremenek29f38082009-12-15 04:12:12 +00004//===----------------------------------------------------------------------===//
5// Basic dead store checking (but in C++ mode).
6//===----------------------------------------------------------------------===//
7
Mike Stump8dd1b6b2009-07-22 22:56:04 +00008int j;
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00009void test1() {
Mike Stump8dd1b6b2009-07-22 22:56:04 +000010 int x = 4;
11
Ted Kremenekb1c392a2011-02-12 00:17:19 +000012 x = x + 1; // expected-warning{{never read}}
Mike Stump8dd1b6b2009-07-22 22:56:04 +000013
14 switch (j) {
15 case 1:
16 throw 1;
17 (void)x;
18 break;
19 }
20}
Ted Kremenek29f38082009-12-15 04:12:12 +000021
22//===----------------------------------------------------------------------===//
23// Dead store checking involving constructors.
24//===----------------------------------------------------------------------===//
25
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000026class Test2 {
Ted Kremenek29f38082009-12-15 04:12:12 +000027 int &x;
28public:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000029 Test2(int &y) : x(y) {}
30 ~Test2() { ++x; }
Ted Kremenek29f38082009-12-15 04:12:12 +000031};
32
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000033int test2(int x) {
34 { Test2 a(x); } // no-warning
Ted Kremenek29f38082009-12-15 04:12:12 +000035 return x;
36}
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000037
38//===----------------------------------------------------------------------===//
Ted Kremenek857f41c2009-12-23 04:11:44 +000039// Dead store checking involving CXXTemporaryExprs
40//===----------------------------------------------------------------------===//
41
42namespace TestTemp {
43 template<typename _Tp>
44 class pencil {
45 public:
46 ~pencil() throw() {}
47 };
48 template<typename _Tp, typename _Number2> struct _Row_base {
49 _Row_base(const pencil<_Tp>& x) {}
50 };
51 template<typename _Tp, typename _Number2 = TestTemp::pencil<_Tp> >
52 class row : protected _Row_base<_Tp, _Number2> {
53 typedef _Row_base<_Tp, _Number2> _Base;
54 typedef _Number2 pencil_type;
55 public:
56 explicit row(const pencil_type& __a = pencil_type()) : _Base(__a) {}
57 };
58}
59
60void test2_b() {
61 TestTemp::row<const char*> x; // no-warning
62}
63
64//===----------------------------------------------------------------------===//
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000065// Test references.
66//===----------------------------------------------------------------------===//
67
68void test3_a(int x) {
Ted Kremenekb1c392a2011-02-12 00:17:19 +000069 x = x + 1; // expected-warning{{never read}}
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000070}
71
72void test3_b(int &x) {
Ted Kremenekb1c392a2011-02-12 00:17:19 +000073 x = x + 1; // no-warninge
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000074}
75
76void test3_c(int x) {
77 int &y = x;
78 // Shows the limitation of dead stores tracking. The write is really
79 // dead since the value cannot escape the function.
80 ++y; // no-warning
81}
82
83void test3_d(int &x) {
84 int &y = x;
85 ++y; // no-warning
86}
87
88void test3_e(int &x) {
89 int &y = x;
90}
91
Ted Kremenekda42d522010-06-25 22:48:52 +000092//===----------------------------------------------------------------------===//
93// Dead stores involving 'new'
94//===----------------------------------------------------------------------===//
95
96static void test_new(unsigned n) {
97 char **p = new char* [n]; // expected-warning{{never read}}
98}
99
Ted Kremeneka1ec4f32011-01-20 17:09:48 +0000100//===----------------------------------------------------------------------===//
101// Dead stores in namespaces.
102//===----------------------------------------------------------------------===//
103
104namespace foo {
105 int test_4(int x) {
106 x = 2; // expected-warning{{Value stored to 'x' is never read}}
107 x = 2;
108 return x;
109 }
110}
111
Ted Kremenekcadd9f12012-09-06 22:32:48 +0000112//===----------------------------------------------------------------------===//
113// Dead stores in with EH code.
114//===----------------------------------------------------------------------===//
115
116void test_5_Aux();
117int test_5() {
118 int x = 0;
119 try {
120 x = 2; // no-warning
121 test_5_Aux();
122 }
123 catch (int z) {
124 return x + z;
125 }
126 return 1;
127}
128
Ted Kremeneke6ee6712012-11-13 00:12:13 +0000129
130int test_6_aux(unsigned x);
131
132void test_6() {
133 unsigned currDestLen = 0; // no-warning
134 try {
135 while (test_6_aux(currDestLen)) {
136 currDestLen += 2; // no-warning
137 }
138 }
139 catch (void *) {}
140}
141
142void test_6b() {
143 unsigned currDestLen = 0; // no-warning
144 try {
145 while (test_6_aux(currDestLen)) {
146 currDestLen += 2; // expected-warning {{Value stored to 'currDestLen' is never read}}
147 break;
148 }
149 }
150 catch (void *) {}
151}