1 // RUN: %clang_analyze_cc1 -w -verify %s\
2 // RUN: -analyzer-checker=core,unix.Malloc,cplusplus.NewDeleteLeaks\
3 // RUN: -analyzer-checker=debug.ExprInspection -std=c++11
4 // RUN: %clang_analyze_cc1 -w -verify %s\
5 // RUN: -analyzer-checker=core,unix.Malloc,cplusplus.NewDeleteLeaks\
6 // RUN: -analyzer-checker=debug.ExprInspection -std=c++17
7 // RUN: %clang_analyze_cc1 -w -verify %s\
8 // RUN: -analyzer-checker=core,unix.Malloc,cplusplus.NewDeleteLeaks\
9 // RUN: -analyzer-checker=debug.ExprInspection -std=c++11\
10 // RUN: -DTEST_INLINABLE_ALLOCATORS
11 // RUN: %clang_analyze_cc1 -w -verify %s\
12 // RUN: -analyzer-checker=core,unix.Malloc,cplusplus.NewDeleteLeaks\
13 // RUN: -analyzer-checker=debug.ExprInspection -std=c++17\
14 // RUN: -DTEST_INLINABLE_ALLOCATORS
16 void clang_analyzer_eval(bool);
18 #include "Inputs/system-header-simulator-cxx.h"
27 clang_analyzer_eval(x
== 0); // expected-warning{{TRUE}}
34 DirectMember(int value
) : x(value
) {}
36 int getX() { return x
; }
39 void testDirectMember() {
41 clang_analyzer_eval(obj
.getX() == 3); // expected-warning{{TRUE}}
45 class IndirectMember
{
50 IndirectMember(int value
) : x(value
) {}
52 int getX() { return x
; }
55 void testIndirectMember() {
56 IndirectMember
obj(3);
57 clang_analyzer_eval(obj
.getX() == 3); // expected-warning{{TRUE}}
61 struct DelegatingConstructor
{
63 DelegatingConstructor(int y
) { x
= y
; }
64 DelegatingConstructor() : DelegatingConstructor(42) {}
67 void testDelegatingConstructor() {
68 DelegatingConstructor obj
;
69 clang_analyzer_eval(obj
.x
== 42); // expected-warning{{TRUE}}
74 RefWrapper(int *p
) : x(*p
) {}
75 RefWrapper(int &r
) : x(r
) {}
79 void testReferenceMember() {
81 RefWrapper
X(p
); // expected-warning@-7 {{Dereference of null pointer}}
84 void testReferenceMember2() {
86 RefWrapper
X(*p
); // expected-warning {{Forming reference to null pointer}}
90 extern "C" char *strdup(const char *);
95 StringWrapper(const char *input
) : str(strdup(input
)) {} // no-warning
99 // PR15070 - Constructing a type containing a non-POD array mistakenly
100 // tried to perform a bind instead of relying on the CXXConstructExpr,
101 // which caused a cast<> failure in RegionStore.
102 namespace DefaultConstructorWithCleanups
{
111 Element(Helper h
= Helper());
120 Wrapper::Wrapper() /* initializers synthesized */ {}
124 return w
.arr
[0].value
; // no-warning
128 namespace DefaultMemberInitializers
{
133 Wrapper(int x
) : value(x
) {}
139 clang_analyzer_eval(w1
.value
== 42); // expected-warning{{TRUE}}
142 clang_analyzer_eval(w2
.value
== 50); // expected-warning{{TRUE}}
145 clang_analyzer_eval(w3
.value
== 42); // expected-warning{{TRUE}}
148 struct StringWrapper
{
149 const char s
[4] = "abc";
150 const char *p
= "xyz";
152 StringWrapper(bool) {}
156 StringWrapper
w(true);
157 clang_analyzer_eval(w
.s
[1] == 'b'); // expected-warning{{TRUE}}
158 clang_analyzer_eval(w
.p
[1] == 'y'); // expected-warning{{TRUE}}
162 namespace ReferenceInitialization
{
170 MyStruct(OtherStruct os
);
175 void referenceInitializeLocal() {
176 const MyStruct
&myStruct(5);
177 myStruct
.method(); // no-warning
180 void referenceInitializeMultipleLocals() {
181 const MyStruct
&myStruct1(5), myStruct2(5), &myStruct3(5);
182 myStruct1
.method(); // no-warning
183 myStruct2
.method(); // no-warning
184 myStruct3
.method(); // no-warning
187 void referenceInitializeLocalWithCleanup() {
188 const MyStruct
&myStruct(OtherStruct(5));
189 myStruct
.method(); // no-warning
195 C() : f("}") { } // no-crash
200 namespace CXX_initializer_lists
{
202 C(std::initializer_list
<int *> list
);
204 void testPointerEscapeIntoLists() {
205 C empty
{}; // no-crash
207 // Do not warn that 'x' leaks. It might have been deleted by
208 // the destructor of 'c'.
210 C c
{x
}; // no-warning
213 void testPassListsWithExplicitConstructors() {
214 (void)(std::initializer_list
<int>){12}; // no-crash
218 namespace CXX17_aggregate_construction
{
229 struct D
: public virtual A
{
232 // In C++17, classes B and C are aggregates, so they will be constructed
233 // without actually calling their trivial constructor. Used to crash.
235 B b
= {}; // no-crash
236 const B
&bl
= {}; // no-crash
237 B
&&br
= {}; // no-crash
239 C c
= {}; // no-crash
240 const C
&cl
= {}; // no-crash
241 C
&&cr
= {}; // no-crash
243 D d
= {}; // no-crash
245 #if __cplusplus >= 201703L
246 C cd
= {{}}; // no-crash
247 const C
&cdl
= {{}}; // no-crash
248 C
&&cdr
= {{}}; // no-crash
250 const B
&bll
= {{}}; // no-crash
251 const B
&bcl
= C({{}}); // no-crash
252 B
&&bcr
= C({{}}); // no-crash
255 } // namespace CXX17_aggregate_construction
257 namespace CXX17_transparent_init_list_exprs
{
260 class B
: private A
{};
264 B b
{ boo() }; // no-crash
267 class C
: virtual public A
{};
271 C c
{ coo() }; // no-crash
275 B b
{ foo_recursive() };
277 } // namespace CXX17_transparent_init_list_exprs
279 namespace skip_vbase_initializer_side_effects
{
290 struct B
: virtual A
{
301 clang_analyzer_eval(glob
== 1); // expected-warning{{TRUE}}
303 clang_analyzer_eval(glob
== 1); // expected-warning{{TRUE}}
305 } // namespace skip_vbase_initializer_side_effects
307 namespace dont_skip_vbase_initializers_in_most_derived_class
{
320 struct C
: virtual A
{
323 struct D
: C
, virtual B
{
329 clang_analyzer_eval(A::a
== 0); // expected-warning{{TRUE}}
330 clang_analyzer_eval(B::b
== 2); // expected-warning{{TRUE}}
333 struct E
: virtual B
, C
{
339 clang_analyzer_eval(A::a
== 0); // expected-warning{{TRUE}}
340 clang_analyzer_eval(B::b
== 2); // expected-warning{{TRUE}}
343 struct F
: virtual A
, virtual B
{
352 clang_analyzer_eval(A::a
== 0); // expected-warning{{TRUE}}
353 clang_analyzer_eval(B::b
== 2); // expected-warning{{TRUE}}
356 struct H
: virtual B
, virtual A
{
365 clang_analyzer_eval(A::a
== 0); // expected-warning{{TRUE}}
366 clang_analyzer_eval(B::b
== 2); // expected-warning{{TRUE}}
368 } // namespace dont_skip_vbase_initializers_in_most_derived_class