xref: /llvm-project/clang/test/SemaCXX/constructor-initializer.cpp (revision abb04f730e722e2457cabaec14546439f04ac73c)
1 // RUN: %clang_cc1 -Wreorder -fsyntax-only -verify %s
2 class A {
3   int m;
4 public:
5    A() : A::m(17) { } // expected-error {{member initializer 'm' does not name a non-static data member or base class}}
6    A(int);
7 };
8 
9 class B : public A {
10 public:
11   B() : A(), m(1), n(3.14) { }
12 
13 private:
14   int m;
15   float n;
16 };
17 
18 
19 class C : public virtual B {
20 public:
21   C() : B() { }
22 };
23 
24 class D : public C {
25 public:
26   D() : B(), C() { }
27 };
28 
29 class E : public D, public B {
30 public:
31   E() : B(), D() { } // expected-error{{base class initializer 'B' names both a direct base class and an inherited virtual base class}}
32 };
33 
34 
35 typedef int INT;
36 
37 class F : public B {
38 public:
39   int B;
40 
41   F() : B(17),
42         m(17), // expected-error{{member initializer 'm' does not name a non-static data member or base class}}
43         INT(17) // expected-error{{constructor initializer 'INT' (aka 'int') does not name a class}}
44   {
45   }
46 };
47 
48 class G : A {
49   G() : A(10); // expected-error{{expected '{'}}
50 };
51 
52 void f() : a(242) { } // expected-error{{only constructors take base initializers}}
53 
54 class H : A {
55   H();
56 };
57 
58 H::H() : A(10) { }
59 
60 
61 class  X {};
62 class Y {};
63 
64 struct S : Y, virtual X {
65   S ();
66 };
67 
68 struct Z : S {
69   Z() : X(), S(), E()  {} // expected-error {{type 'E' is not a direct or virtual base of 'Z'}}
70 };
71 
72 class U {
73   union { int a; char* p; };
74   union { int b; double d; };
75 
76   U() :  a(1), // expected-note {{previous initialization is here}}
77          p(0), // expected-error {{initializing multiple members of anonymous union}}
78          d(1.0)  {}
79 };
80 
81 struct V {};
82 struct Base {};
83 struct Base1 {};
84 
85 struct Derived : Base, Base1, virtual V {
86   Derived ();
87 };
88 
89 struct Current : Derived {
90   int Derived;
91   Current() : Derived(1), ::Derived(), // expected-warning {{field 'Derived' will be initialized after base '::Derived'}} \
92                                        // expected-warning {{base class '::Derived' will be initialized after base 'Derived::V'}}
93                           ::Derived::Base(), // expected-error {{type '::Derived::Base' is not a direct or virtual base of 'Current'}}
94                            Derived::Base1(), // expected-error {{type 'Derived::Base1' is not a direct or virtual base of 'Current'}}
95                            Derived::V(),
96                            ::NonExisting(), // expected-error {{member initializer 'NonExisting' does not name a non-static data member or}}
97                            INT::NonExisting()  {} // expected-error {{expected a class or namespace}} \
98                                                   // expected-error {{member initializer 'NonExisting' does not name a non-static data member or}}
99 };
100 
101 struct M {              // expected-note 2 {{candidate constructor (the implicit copy constructor)}} \
102                         // expected-note {{declared here}} \
103                         // expected-note {{declared here}}
104   M(int i, int j);      // expected-note 2 {{candidate constructor}}
105 };
106 
107 struct N : M  {
108   N() : M(1),        // expected-error {{no matching constructor for initialization of 'M'}}
109         m1(100) {  } // expected-error {{no matching constructor for initialization of 'M'}}
110   M m1;
111 };
112 
113 struct P : M  {
114   P()  {  } // expected-error {{constructor for 'P' must explicitly initialize the base class 'M' which does not have a default constructor}} \
115             // expected-error {{member 'm'}}
116   M m; // expected-note {{member is declared here}}
117 };
118 
119 struct Q {
120   Q() : f1(1,2),       // expected-error {{excess elements in scalar initializer}}
121         pf(0.0)  { }   // expected-error {{cannot initialize a member subobject of type 'float *' with an rvalue of type 'double'}}
122   float f1;
123 
124   float *pf;
125 };
126 
127 // A silly class used to demonstrate field-is-uninitialized in constructors with
128 // multiple params.
129 class TwoInOne { public: TwoInOne(TwoInOne a, TwoInOne b) {} };
130 class InitializeUsingSelfTest {
131   bool A;
132   char* B;
133   int C;
134   TwoInOne D;
135   InitializeUsingSelfTest(int E)
136       : A(A),  // expected-warning {{field is uninitialized when used here}}
137         B((((B)))),  // expected-warning {{field is uninitialized when used here}}
138         C(A && InitializeUsingSelfTest::C),  // expected-warning {{field is uninitialized when used here}}
139         D(D,  // expected-warning {{field is uninitialized when used here}}
140           D) {}  // expected-warning {{field is uninitialized when used here}}
141 };
142 
143 int IntWrapper(int i) { return 0; };
144 class InitializeUsingSelfExceptions {
145   int A;
146   int B;
147   InitializeUsingSelfExceptions(int B)
148       : A(IntWrapper(A)),  // Due to a conservative implementation, we do not report warnings inside function/ctor calls even though it is possible to do so.
149         B(B) {}  // Not a warning; B is a local variable.
150 };
151 
152 class CopyConstructorTest {
153   bool A, B, C;
154   CopyConstructorTest(const CopyConstructorTest& rhs)
155       : A(rhs.A),
156         B(B),  // expected-warning {{field is uninitialized when used here}}
157         C(rhs.C || C) { }  // expected-warning {{field is uninitialized when used here}}
158 };
159 
160 // Make sure we aren't marking default constructors when we shouldn't be.
161 template<typename T>
162 struct NDC {
163   T &ref;
164 
165   NDC() { }
166   NDC(T &ref) : ref(ref) { }
167 };
168 
169 struct X0 : NDC<int> {
170   X0(int &ref) : NDC<int>(ref), ndc(ref) { }
171 
172   NDC<int> ndc;
173 };
174 
175 namespace Test0 {
176 
177 struct A { A(); };
178 
179 struct B {
180   B() { }
181   const A a;
182 };
183 
184 }
185 
186 namespace Test1 {
187   struct A {
188     enum Kind { Foo } Kind;
189     A() : Kind(Foo) {}
190   };
191 }
192 
193 namespace Test2 {
194 
195 struct A {
196   A(const A&);
197 };
198 
199 struct B : virtual A { };
200 struct C : A, B { };
201 
202 C f(C c) {
203   return c;
204 }
205 
206 }
207 
208 // Don't build implicit initializers for anonymous union fields when we already
209 // have an explicit initializer for another field in the union.
210 namespace PR7402 {
211   struct S {
212     union {
213       void* ptr_;
214       struct { int i_; };
215     };
216 
217     template <typename T> S(T) : ptr_(0) { }
218   };
219 
220   void f() {
221     MyStruct s(3);
222   }
223 }
224