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