1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-config c++-inlining=constructors -std=c++11 -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc void clang_analyzer_eval(bool);
4*f4a2713aSLionel Sambuc 
5*f4a2713aSLionel Sambuc class A {
6*f4a2713aSLionel Sambuc   int x;
7*f4a2713aSLionel Sambuc public:
8*f4a2713aSLionel Sambuc   A();
9*f4a2713aSLionel Sambuc };
10*f4a2713aSLionel Sambuc 
A()11*f4a2713aSLionel Sambuc A::A() : x(0) {
12*f4a2713aSLionel Sambuc   clang_analyzer_eval(x == 0); // expected-warning{{TRUE}}
13*f4a2713aSLionel Sambuc }
14*f4a2713aSLionel Sambuc 
15*f4a2713aSLionel Sambuc 
16*f4a2713aSLionel Sambuc class DirectMember {
17*f4a2713aSLionel Sambuc   int x;
18*f4a2713aSLionel Sambuc public:
DirectMember(int value)19*f4a2713aSLionel Sambuc   DirectMember(int value) : x(value) {}
20*f4a2713aSLionel Sambuc 
getX()21*f4a2713aSLionel Sambuc   int getX() { return x; }
22*f4a2713aSLionel Sambuc };
23*f4a2713aSLionel Sambuc 
testDirectMember()24*f4a2713aSLionel Sambuc void testDirectMember() {
25*f4a2713aSLionel Sambuc   DirectMember obj(3);
26*f4a2713aSLionel Sambuc   clang_analyzer_eval(obj.getX() == 3); // expected-warning{{TRUE}}
27*f4a2713aSLionel Sambuc }
28*f4a2713aSLionel Sambuc 
29*f4a2713aSLionel Sambuc 
30*f4a2713aSLionel Sambuc class IndirectMember {
31*f4a2713aSLionel Sambuc   struct {
32*f4a2713aSLionel Sambuc     int x;
33*f4a2713aSLionel Sambuc   };
34*f4a2713aSLionel Sambuc public:
IndirectMember(int value)35*f4a2713aSLionel Sambuc   IndirectMember(int value) : x(value) {}
36*f4a2713aSLionel Sambuc 
getX()37*f4a2713aSLionel Sambuc   int getX() { return x; }
38*f4a2713aSLionel Sambuc };
39*f4a2713aSLionel Sambuc 
testIndirectMember()40*f4a2713aSLionel Sambuc void testIndirectMember() {
41*f4a2713aSLionel Sambuc   IndirectMember obj(3);
42*f4a2713aSLionel Sambuc   clang_analyzer_eval(obj.getX() == 3); // expected-warning{{TRUE}}
43*f4a2713aSLionel Sambuc }
44*f4a2713aSLionel Sambuc 
45*f4a2713aSLionel Sambuc 
46*f4a2713aSLionel Sambuc struct DelegatingConstructor {
47*f4a2713aSLionel Sambuc   int x;
DelegatingConstructorDelegatingConstructor48*f4a2713aSLionel Sambuc   DelegatingConstructor(int y) { x = y; }
DelegatingConstructorDelegatingConstructor49*f4a2713aSLionel Sambuc   DelegatingConstructor() : DelegatingConstructor(42) {}
50*f4a2713aSLionel Sambuc };
51*f4a2713aSLionel Sambuc 
testDelegatingConstructor()52*f4a2713aSLionel Sambuc void testDelegatingConstructor() {
53*f4a2713aSLionel Sambuc   DelegatingConstructor obj;
54*f4a2713aSLionel Sambuc   clang_analyzer_eval(obj.x == 42); // expected-warning{{TRUE}}
55*f4a2713aSLionel Sambuc }
56*f4a2713aSLionel Sambuc 
57*f4a2713aSLionel Sambuc 
58*f4a2713aSLionel Sambuc struct RefWrapper {
RefWrapperRefWrapper59*f4a2713aSLionel Sambuc   RefWrapper(int *p) : x(*p) {}
RefWrapperRefWrapper60*f4a2713aSLionel Sambuc   RefWrapper(int &r) : x(r) {}
61*f4a2713aSLionel Sambuc   int &x;
62*f4a2713aSLionel Sambuc };
63*f4a2713aSLionel Sambuc 
testReferenceMember()64*f4a2713aSLionel Sambuc void testReferenceMember() {
65*f4a2713aSLionel Sambuc   int *p = 0;
66*f4a2713aSLionel Sambuc   RefWrapper X(p); // expected-warning@-7 {{Dereference of null pointer}}
67*f4a2713aSLionel Sambuc }
68*f4a2713aSLionel Sambuc 
testReferenceMember2()69*f4a2713aSLionel Sambuc void testReferenceMember2() {
70*f4a2713aSLionel Sambuc   int *p = 0;
71*f4a2713aSLionel Sambuc   RefWrapper X(*p); // expected-warning {{Forming reference to null pointer}}
72*f4a2713aSLionel Sambuc }
73*f4a2713aSLionel Sambuc 
74*f4a2713aSLionel Sambuc 
75*f4a2713aSLionel Sambuc extern "C" char *strdup(const char *);
76*f4a2713aSLionel Sambuc 
77*f4a2713aSLionel Sambuc class StringWrapper {
78*f4a2713aSLionel Sambuc   char *str;
79*f4a2713aSLionel Sambuc public:
StringWrapper(const char * input)80*f4a2713aSLionel Sambuc   StringWrapper(const char *input) : str(strdup(input)) {} // no-warning
81*f4a2713aSLionel Sambuc };
82*f4a2713aSLionel Sambuc 
83*f4a2713aSLionel Sambuc 
84*f4a2713aSLionel Sambuc // PR15070 - Constructing a type containing a non-POD array mistakenly
85*f4a2713aSLionel Sambuc // tried to perform a bind instead of relying on the CXXConstructExpr,
86*f4a2713aSLionel Sambuc // which caused a cast<> failure in RegionStore.
87*f4a2713aSLionel Sambuc namespace DefaultConstructorWithCleanups {
88*f4a2713aSLionel Sambuc   class Element {
89*f4a2713aSLionel Sambuc   public:
90*f4a2713aSLionel Sambuc     int value;
91*f4a2713aSLionel Sambuc 
92*f4a2713aSLionel Sambuc     class Helper {
93*f4a2713aSLionel Sambuc     public:
94*f4a2713aSLionel Sambuc       ~Helper();
95*f4a2713aSLionel Sambuc     };
96*f4a2713aSLionel Sambuc     Element(Helper h = Helper());
97*f4a2713aSLionel Sambuc   };
98*f4a2713aSLionel Sambuc   class Wrapper {
99*f4a2713aSLionel Sambuc   public:
100*f4a2713aSLionel Sambuc     Element arr[2];
101*f4a2713aSLionel Sambuc 
102*f4a2713aSLionel Sambuc     Wrapper();
103*f4a2713aSLionel Sambuc   };
104*f4a2713aSLionel Sambuc 
Wrapper()105*f4a2713aSLionel Sambuc   Wrapper::Wrapper() /* initializers synthesized */ {}
106*f4a2713aSLionel Sambuc 
test()107*f4a2713aSLionel Sambuc   int test() {
108*f4a2713aSLionel Sambuc     Wrapper w;
109*f4a2713aSLionel Sambuc     return w.arr[0].value; // no-warning
110*f4a2713aSLionel Sambuc   }
111*f4a2713aSLionel Sambuc }
112*f4a2713aSLionel Sambuc 
113*f4a2713aSLionel Sambuc namespace DefaultMemberInitializers {
114*f4a2713aSLionel Sambuc   struct Wrapper {
115*f4a2713aSLionel Sambuc     int value = 42;
116*f4a2713aSLionel Sambuc 
WrapperDefaultMemberInitializers::Wrapper117*f4a2713aSLionel Sambuc     Wrapper() {}
WrapperDefaultMemberInitializers::Wrapper118*f4a2713aSLionel Sambuc     Wrapper(int x) : value(x) {}
WrapperDefaultMemberInitializers::Wrapper119*f4a2713aSLionel Sambuc     Wrapper(bool) {}
120*f4a2713aSLionel Sambuc   };
121*f4a2713aSLionel Sambuc 
test()122*f4a2713aSLionel Sambuc   void test() {
123*f4a2713aSLionel Sambuc     Wrapper w1;
124*f4a2713aSLionel Sambuc     clang_analyzer_eval(w1.value == 42); // expected-warning{{TRUE}}
125*f4a2713aSLionel Sambuc 
126*f4a2713aSLionel Sambuc     Wrapper w2(50);
127*f4a2713aSLionel Sambuc     clang_analyzer_eval(w2.value == 50); // expected-warning{{TRUE}}
128*f4a2713aSLionel Sambuc 
129*f4a2713aSLionel Sambuc     Wrapper w3(false);
130*f4a2713aSLionel Sambuc     clang_analyzer_eval(w3.value == 42); // expected-warning{{TRUE}}
131*f4a2713aSLionel Sambuc   }
132*f4a2713aSLionel Sambuc 
133*f4a2713aSLionel Sambuc   struct StringWrapper {
134*f4a2713aSLionel Sambuc     const char s[4] = "abc";
135*f4a2713aSLionel Sambuc     const char *p = "xyz";
136*f4a2713aSLionel Sambuc 
StringWrapperDefaultMemberInitializers::StringWrapper137*f4a2713aSLionel Sambuc     StringWrapper(bool) {}
138*f4a2713aSLionel Sambuc   };
139*f4a2713aSLionel Sambuc 
testString()140*f4a2713aSLionel Sambuc   void testString() {
141*f4a2713aSLionel Sambuc     StringWrapper w(true);
142*f4a2713aSLionel Sambuc     clang_analyzer_eval(w.s[1] == 'b'); // expected-warning{{TRUE}}
143*f4a2713aSLionel Sambuc     clang_analyzer_eval(w.p[1] == 'y'); // expected-warning{{TRUE}}
144*f4a2713aSLionel Sambuc   }
145*f4a2713aSLionel Sambuc }
146