1 // RUN: %clang_analyze_cc1 -fcxx-exceptions -fexceptions -fblocks -std=c++11 \
2 // RUN: -analyzer-checker=deadcode.DeadStores -Wno-unreachable-code \
3 // RUN: -analyzer-config deadcode.DeadStores:WarnForDeadNestedAssignments=false\
4 // RUN: -verify=non-nested %s
6 // RUN: %clang_analyze_cc1 -fcxx-exceptions -fexceptions -fblocks -std=c++11 \
7 // RUN: -analyzer-checker=deadcode.DeadStores \
8 // RUN: -analyzer-config deadcode.DeadStores:WarnForDeadNestedAssignments=false\
9 // RUN: -Wno-unreachable-code -verify=non-nested %s
11 // RUN: %clang_analyze_cc1 -fcxx-exceptions -fexceptions -fblocks -std=c++11 \
12 // RUN: -analyzer-checker=deadcode.DeadStores -Wno-unreachable-code \
13 // RUN: -verify=non-nested,nested %s
15 // RUN: %clang_analyze_cc1 -fcxx-exceptions -fexceptions -fblocks -std=c++17 \
16 // RUN: -analyzer-checker=deadcode.DeadStores -Wno-unreachable-code \
17 // RUN: -verify=non-nested,nested %s
19 //===----------------------------------------------------------------------===//
20 // Basic dead store checking (but in C++ mode).
21 //===----------------------------------------------------------------------===//
27 x
= x
+ 1; // non-nested-warning {{never read}}
38 if ((y
= make_int())) // nested-warning {{Although the value stored}}
41 auto z
= "text"; // non-nested-warning {{never read}}
44 //===----------------------------------------------------------------------===//
45 // Dead store checking involving constructors.
46 //===----------------------------------------------------------------------===//
52 Test2(int &y
) : x(y
) {}
57 { Test2
a(x
); } // no-warning
61 class TestConstructor
{
63 TestConstructor(int &y
);
66 // All these calls might have side effects in the opaque constructor
67 TestConstructor tc1
= x
; // no-warning potential side effects
68 TestConstructor tc2
= TestConstructor(x
); // no-warning potential side effects
69 TestConstructor tc3
= (TestConstructor(x
)); // no-warning potential side effects
70 TestConstructor tc4
= (TestConstructor
)(x
); // no-warning potential side effects
73 //===----------------------------------------------------------------------===//
74 // Dead store checking involving CXXTemporaryExprs
75 //===----------------------------------------------------------------------===//
78 template<typename _Tp
>
83 template<typename _Tp
, typename _Number2
> struct _Row_base
{
84 _Row_base(const pencil
<_Tp
>& x
) {}
86 template<typename _Tp
, typename _Number2
= TestTemp::pencil
<_Tp
> >
87 class row
: protected _Row_base
<_Tp
, _Number2
> {
88 typedef _Row_base
<_Tp
, _Number2
> _Base
;
89 typedef _Number2 pencil_type
;
91 explicit row(const pencil_type
& __a
= pencil_type()) : _Base(__a
) {}
96 TestTemp::row
<const char*> x
; // no-warning
99 //===----------------------------------------------------------------------===//
101 //===----------------------------------------------------------------------===//
103 void test3_a(int x
) {
104 x
= x
+ 1; // non-nested-warning {{never read}}
107 void test3_b(int &x
) {
108 x
= x
+ 1; // no-warning
111 void test3_c(int x
) {
113 // Shows the limitation of dead stores tracking. The write is really dead
114 // since the value cannot escape the function.
118 void test3_d(int &x
) {
123 void test3_e(int &x
) {
127 //===----------------------------------------------------------------------===//
128 // Dead stores involving 'new'
129 //===----------------------------------------------------------------------===//
131 static void test_new(unsigned n
) {
132 char **p
= new char *[n
]; // non-nested-warning {{never read}}
135 //===----------------------------------------------------------------------===//
136 // Dead stores in namespaces.
137 //===----------------------------------------------------------------------===//
141 x
= 2; // non-nested-warning {{Value stored to 'x' is never read}}
147 //===----------------------------------------------------------------------===//
148 // Dead stores in with EH code.
149 //===----------------------------------------------------------------------===//
163 int test_6_aux(unsigned x
);
165 unsigned currDestLen
= 0; // no-warning
167 while (test_6_aux(currDestLen
)) {
168 currDestLen
+= 2; // no-warning
175 unsigned currDestLen
= 0; // no-warning
177 while (test_6_aux(currDestLen
)) {
179 // non-nested-warning@-1 {{Value stored to 'currDestLen' is never read}}
186 void testCXX11Using() {
189 value
= 1; // non-nested-warning {{never read}}
192 //===----------------------------------------------------------------------===//
193 // Dead stores in template instantiations (do not warn).
194 //===----------------------------------------------------------------------===//
196 template <bool f
> int radar13213575_testit(int i
) {
197 int x
= 5+i
; // warning: Value stored to 'x' during its initialization is never read
205 int radar_13213575() {
206 return radar13213575_testit
<true>(5) + radar13213575_testit
<false>(3);
210 void test_block_in_dependent_context(typename
T::some_t someArray
) {
212 int i
= someArray
[0]; // no-warning
216 void test_block_in_non_dependent_context(int *someArray
) {
218 int i
= someArray
[0];
219 // non-nested-warning@-1 {{Value stored to 'i' during its initialization is never read}}
224 //===----------------------------------------------------------------------===//
225 // Dead store checking involving lambdas.
226 //===----------------------------------------------------------------------===//
228 int basicLambda(int i
, int j
) {