xref: /llvm-project/clang/test/SemaCXX/constructor-initializer.cpp (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1 // RUN: %clang_cc1 -Wreorder -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -Wreorder -fsyntax-only -verify -std=c++98 %s
3 // RUN: %clang_cc1 -Wreorder -fsyntax-only -verify -std=c++11 %s
4 
5 class A {
6   int m;
7 public:
A()8    A() : A::m(17) { } // expected-error {{member initializer 'm' does not name a non-static data member or base class}}
9    A(int);
10 };
11 
12 class B : public A {
13 public:
B()14   B() : A(), m(1), n(3.14) { }
15 
16 private:
17   int m;
18   float n;
19 };
20 
21 
22 class C : public virtual B {
23 public:
C()24   C() : B() { }
25 };
26 
27 class D : public C {
28 public:
D()29   D() : B(), C() { }
30 };
31 
32 class E : public D, public B {  // expected-warning{{direct base 'B' is inaccessible due to ambiguity:\n    class E -> D -> C -> B\n    class E -> B}}
33 public:
E()34   E() : B(), D() { } // expected-error{{base class initializer 'B' names both a direct base class and an inherited virtual base class}}
35 };
36 
37 
38 typedef int INT;
39 
40 class F : public B {
41 public:
42   int B;
43 
F()44   F() : B(17),
45         m(17), // expected-error{{member initializer 'm' does not name a non-static data member or base class}}
46         INT(17) // expected-error{{constructor initializer 'INT' (aka 'int') does not name a class}}
47   {
48   }
49 };
50 
51 class G : A {
52   G() : A(10); // expected-error{{expected '{'}}
53 };
54 
f()55 void f() : a(242) { } // expected-error{{only constructors take base initializers}}
56 
57 class H : A {
58   H();
59 };
60 
H()61 H::H() : A(10) { }
62 
63 
64 class  X {};
65 class Y {};
66 
67 struct S : Y, virtual X {
68   S ();
69 };
70 
71 struct Z : S {
ZZ72   Z() : X(), S(), E()  {} // expected-error {{type 'E' is not a direct or virtual base of 'Z'}}
73 };
74 
75 class U {
76   union { int a; char* p; };
77   union { int b; double d; };
78 
U()79   U() :  a(1), // expected-note {{previous initialization is here}}
80          p(0), // expected-error {{initializing multiple members of union}}
81          d(1.0)  {}
82 };
83 
84 struct V {};
85 struct Base {};
86 struct Base1 {};
87 
88 struct Derived : Base, Base1, virtual V {
89   Derived ();
90 };
91 
92 struct Current : Derived {
93   int Derived;
CurrentCurrent94   Current() : Derived(1), ::Derived(), // expected-warning {{initializer order does not match the declaration order}} \
95                                        // expected-note {{field 'Derived' will be initialized after base '::Derived'}} \
96                                        // expected-note {{base class '::Derived' will be initialized after base 'Derived::V'}}
97               ::Derived::Base(),       // expected-error {{type '::Derived::Base' is not a direct or virtual base of 'Current'}}
98               Derived::Base1(),        // expected-error {{type 'Derived::Base1' is not a direct or virtual base of 'Current'}}
99               Derived::V(),
100               ::NonExisting(),      // expected-error {{member initializer 'NonExisting' does not name a non-static data member or}}
101               INT::NonExisting() {} // expected-error {{'INT' (aka 'int') is not a class, namespace, or enumeration}} \
102                                                   // expected-error {{member initializer 'NonExisting' does not name a non-static data member or}}
103 };
104 
105 struct M {              // expected-note 2 {{candidate constructor (the implicit copy constructor)}}
106 #if __cplusplus >= 201103L // C++11 or later
107 // expected-note@-2 2 {{candidate constructor (the implicit move constructor) not viable}}
108 #endif
109 // expected-note@-4 2 {{'M' declared here}}
110   M(int i, int j);      // expected-note 2 {{candidate constructor}}
111 };
112 
113 struct N : M  {
NN114   N() : M(1),        // expected-error {{no matching constructor for initialization of 'M'}}
115         m1(100) {  } // expected-error {{no matching constructor for initialization of 'M'}}
116   M m1;
117 };
118 
119 struct P : M  {
PP120   P()  {  } // expected-error {{constructor for 'P' must explicitly initialize the base class 'M' which does not have a default constructor}} \
121             // expected-error {{member 'm'}}
122   M m; // expected-note {{member is declared here}}
123 };
124 
125 struct Q {
QQ126   Q() : f1(1,2),       // expected-error {{excess elements in scalar initializer}}
127         pf(0.0)  { }   // expected-error {{cannot initialize a member subobject of type 'float *' with an rvalue of type 'double'}}
128   float f1;
129 
130   float *pf;
131 };
132 
133 // A silly class used to demonstrate field-is-uninitialized in constructors with
134 // multiple params.
IntParam(int i)135 int IntParam(int i) { return 0; };
TwoInOne(TwoInOne a,TwoInOne b)136 class TwoInOne { public: TwoInOne(TwoInOne a, TwoInOne b) {} };
137 class InitializeUsingSelfTest {
138   bool A;
139   char* B;
140   int C;
141   TwoInOne D;
142   int E;
InitializeUsingSelfTest(int F)143   InitializeUsingSelfTest(int F)
144       : A(A),  // expected-warning {{field 'A' is uninitialized when used here}}
145         B((((B)))),  // expected-warning {{field 'B' is uninitialized when used here}}
146         C(A && InitializeUsingSelfTest::C),  // expected-warning {{field 'C' is uninitialized when used here}}
147         D(D,  // expected-warning {{field 'D' is uninitialized when used here}}
148           D), // expected-warning {{field 'D' is uninitialized when used here}}
149         E(IntParam(E)) {} // expected-warning {{field 'E' is uninitialized when used here}}
150 };
151 
IntWrapper(int & i)152 int IntWrapper(int &i) { return 0; };
153 class InitializeUsingSelfExceptions {
154   int A;
155   int B;
156   int C;
157   void *P;
InitializeUsingSelfExceptions(int B)158   InitializeUsingSelfExceptions(int B)
159       : 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.
160         B(B),  // Not a warning; B is a local variable.
161         C(sizeof(C)),  // sizeof doesn't reference contents, do not warn
162         P(&P) {} // address-of doesn't reference contents (the pointer may be dereferenced in the same expression but it would be rare; and weird)
163 };
164 
165 class CopyConstructorTest {
166   bool A, B, C;
CopyConstructorTest(const CopyConstructorTest & rhs)167   CopyConstructorTest(const CopyConstructorTest& rhs)
168       : A(rhs.A),
169         B(B),  // expected-warning {{field 'B' is uninitialized when used here}}
170         C(rhs.C || C) { }  // expected-warning {{field 'C' is uninitialized when used here}}
171 };
172 
173 // Make sure we aren't marking default constructors when we shouldn't be.
174 template<typename T>
175 struct NDC {
176   T &ref;
177 
NDCNDC178   NDC() { }
NDCNDC179   NDC(T &ref) : ref(ref) { }
180 };
181 
182 struct X0 : NDC<int> {
X0X0183   X0(int &ref) : NDC<int>(ref), ndc(ref) { }
184 
185   NDC<int> ndc;
186 };
187 
188 namespace Test0 {
189 
190 struct A { A(); };
191 
192 struct B {
BTest0::B193   B() { }
194   const A a;
195 };
196 
197 }
198 
199 namespace Test1 {
200   struct A {
201     enum Kind { Foo } Kind;
ATest1::A202     A() : Kind(Foo) {}
203   };
204 }
205 
206 namespace Test2 {
207 
208 struct A {
209   A(const A&);
210 };
211 
212 struct B : virtual A { };
213 
214   struct C : A, B { }; // expected-warning{{direct base 'A' is inaccessible due to ambiguity:\n    struct Test2::C -> A\n    struct Test2::C -> B -> A}}
215 
f(C c)216 C f(C c) {
217   return c;
218 }
219 
220 }
221 
222 // Don't build implicit initializers for anonymous union fields when we already
223 // have an explicit initializer for another field in the union.
224 namespace PR7402 {
225   struct S {
226     union {
227       void* ptr_;
228       struct { int i_; };
229     };
230 
SPR7402::S231     template <typename T> S(T) : ptr_(0) { }
232   };
233 
f()234   void f() {
235     S s(3);
236   }
237 }
238 
239 // Don't crash. Lots of questionable recovery here;  errors can change.
240 namespace test3 {
241   class A : public std::exception {}; // expected-error {{undeclared identifier}} expected-error {{expected class name}}
242   // expected-note@-1 {{candidate constructor (the implicit copy constructor) not viable}}
243 #if __cplusplus >= 201103L // C++11 or later
244   // expected-note@-3 {{candidate constructor (the implicit move constructor) not viable}}
245 #endif
246   // expected-note@-5 {{candidate constructor (the implicit default constructor) not viable}}
247 
248   class B : public A {
249   public:
B(const String & s,int e=0)250     B(const String& s, int e=0) // expected-error {{unknown type name}}
251       : A(e), m_String(s) , m_ErrorStr(__null) {} // expected-error {{no matching constructor}} \
252       expected-error {{member initializer 'm_String' does not name}} \
253       expected-error {{member initializer 'm_ErrorStr' does not name}}
B(const B & e)254     B(const B& e)
255       : A(e), m_String(e.m_String), m_ErrorStr(__null) { // expected-error 2{{does not name}} \
256       // expected-error {{no member named 'm_String' in 'test3::B'}}
257     }
258   };
259 }
260 
261 // PR8075
262 namespace PR8075 {
263 
264 struct S1 {
265   enum { FOO = 42 };
266   static const int bar = 42;
267   static int baz();
268   S1(int);
269 };
270 
271 const int S1::bar;
272 
273 struct S2 {
274   S1 s1;
S2PR8075::S2275   S2() : s1(s1.FOO) {}
276 };
277 
278 struct S3 {
279   S1 s1;
S3PR8075::S3280   S3() : s1(s1.bar) {}
281 };
282 
283 struct S4 {
284   S1 s1;
S4PR8075::S4285   S4() : s1(s1.baz()) {}
286 };
287 
288 }
289 
290 namespace PR12049 {
291   int function();
292 
293   class Class
294   {
295   public:
296       Class() : member(function() {} // expected-note {{to match this '('}}
297 
298       int member; // expected-error {{expected ')'}}
299   };
300 }
301 
302 namespace PR14073 {
303   struct S1 { union { int n; }; S1() : n(n) {} };  // expected-warning {{field 'n' is uninitialized when used here}}
304   struct S2 { union { union { int n; }; char c; }; S2() : n(n) {} };  // expected-warning {{field 'n' is uninitialized when used here}}
305   struct S3 { struct { int n; }; S3() : n(n) {} };  // expected-warning {{field 'n' is uninitialized when used here}}
306 }
307 
308 namespace PR10758 {
309 struct A;
310 struct B {
311   B (A const &); // expected-note 2 {{candidate constructor not viable: no known conversion from 'const B' to 'const A &' for 1st argument}}
312   B (B &); // expected-note 2 {{candidate constructor not viable: 1st argument ('const B') would lose const qualifier}}
313 };
314 struct A {
315   A (B); // expected-note 2 {{passing argument to parameter here}}
316 };
317 
318 B f(B const &b) {
319   return b; // expected-error {{no matching constructor for initialization of 'B'}}
320 }
321 
322 A f2(const B &b) {
323   return b; // expected-error {{no matching constructor for initialization of 'B'}}
324 }
325 }
326