blob: 6a88bbaad9b9b5521340a5a7e5ace3e31dc4d10d [file] [log] [blame]
Artem Dergachev9d3a7d82018-03-30 19:21:18 +00001// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -std=c++11 -verify %s
2// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -std=c++17 -verify %s
3
4void clang_analyzer_eval(bool);
5
Artem Dergachev1fe52472018-06-14 01:32:46 +00006namespace variable_functional_cast_crash {
7
8struct A {
9 A(int) {}
10};
11
12void foo() {
13 A a = A(0);
14}
15
16struct B {
17 A a;
18 B(): a(A(0)) {}
19};
20
21} // namespace variable_functional_cast_crash
22
23
Artem Dergacheva84374d2018-06-14 01:40:49 +000024namespace ctor_initializer {
25
26struct S {
27 int x, y, z;
28};
29
30struct T {
31 S s;
32 int w;
33 T(int w): s(), w(w) {}
34};
35
36class C {
37 T t;
38public:
39 C() : t(T(4)) {
40 S s = {1, 2, 3};
41 t.s = s;
42 // FIXME: Should be TRUE in C++11 as well.
43 clang_analyzer_eval(t.w == 4);
44#if __cplusplus >= 201703L
45 // expected-warning@-2{{TRUE}}
46#else
47 // expected-warning@-4{{UNKNOWN}}
48#endif
49 }
50};
51
52} // namespace ctor_initializer
53
54
Artem Dergachev1fe52472018-06-14 01:32:46 +000055namespace address_vector_tests {
56
Artem Dergachev9d3a7d82018-03-30 19:21:18 +000057template <typename T> struct AddressVector {
58 T *buf[10];
59 int len;
60
61 AddressVector() : len(0) {}
62
63 void push(T *t) {
64 buf[len] = t;
65 ++len;
66 }
67};
68
69class ClassWithoutDestructor {
70 AddressVector<ClassWithoutDestructor> &v;
71
72public:
73 ClassWithoutDestructor(AddressVector<ClassWithoutDestructor> &v) : v(v) {
74 v.push(this);
75 }
76
77 ClassWithoutDestructor(ClassWithoutDestructor &&c) : v(c.v) { v.push(this); }
78 ClassWithoutDestructor(const ClassWithoutDestructor &c) : v(c.v) {
79 v.push(this);
80 }
81};
82
83ClassWithoutDestructor make1(AddressVector<ClassWithoutDestructor> &v) {
84 return ClassWithoutDestructor(v);
85}
86ClassWithoutDestructor make2(AddressVector<ClassWithoutDestructor> &v) {
87 return make1(v);
88}
89ClassWithoutDestructor make3(AddressVector<ClassWithoutDestructor> &v) {
90 return make2(v);
91}
92
93void testMultipleReturns() {
94 AddressVector<ClassWithoutDestructor> v;
95 ClassWithoutDestructor c = make3(v);
96
97#if __cplusplus >= 201703L
98 // FIXME: Both should be TRUE.
99 clang_analyzer_eval(v.len == 1); // expected-warning{{TRUE}}
100 clang_analyzer_eval(v.buf[0] == &c); // expected-warning{{FALSE}}
101#else
102 clang_analyzer_eval(v.len == 5); // expected-warning{{TRUE}}
103 clang_analyzer_eval(v.buf[0] != v.buf[1]); // expected-warning{{TRUE}}
104 clang_analyzer_eval(v.buf[1] != v.buf[2]); // expected-warning{{TRUE}}
105 clang_analyzer_eval(v.buf[2] != v.buf[3]); // expected-warning{{TRUE}}
106 clang_analyzer_eval(v.buf[3] != v.buf[4]); // expected-warning{{TRUE}}
107 clang_analyzer_eval(v.buf[4] == &c); // expected-warning{{TRUE}}
108#endif
109}
Artem Dergachev1fe52472018-06-14 01:32:46 +0000110
111} // namespace address_vector_tests