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