blob: ff963a9bce2ade792a55af4b94cff8ee559aedf3 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -Wreorder -fsyntax-only -verify %s
Douglas Gregor7ad83902008-11-05 04:29:56 +00002class A {
3 int m;
John McCall7002f4c2010-04-09 19:03:51 +00004public:
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00005 A() : A::m(17) { } // expected-error {{member initializer 'm' does not name a non-static data member or base class}}
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +00006 A(int);
Douglas Gregor7ad83902008-11-05 04:29:56 +00007};
8
9class B : public A {
10public:
11 B() : A(), m(1), n(3.14) { }
12
13private:
14 int m;
15 float n;
16};
17
18
19class C : public virtual B {
20public:
21 C() : B() { }
22};
23
24class D : public C {
25public:
26 D() : B(), C() { }
27};
28
29class E : public D, public B {
30public:
John McCall7c2342d2010-03-10 11:27:22 +000031 E() : B(), D() { } // expected-error{{base class initializer 'B' names both a direct base class and an inherited virtual base class}}
Douglas Gregor7ad83902008-11-05 04:29:56 +000032};
33
34
35typedef int INT;
36
37class F : public B {
38public:
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}}
Chris Lattnerd0344a42009-02-19 23:45:49 +000043 INT(17) // expected-error{{constructor initializer 'INT' (aka 'int') does not name a class}}
Douglas Gregor7ad83902008-11-05 04:29:56 +000044 {
45 }
46};
Douglas Gregor3f08d182008-11-10 16:59:40 +000047
48class G : A {
49 G() : A(10); // expected-error{{expected '{'}}
50};
Anders Carlssona7b35212009-03-25 02:58:17 +000051
52void f() : a(242) { } // expected-error{{only constructors take base initializers}}
53
54class H : A {
55 H();
56};
57
58H::H() : A(10) { }
59
Fariborz Jahanian9da72012009-06-30 17:34:52 +000060
61class X {};
62class Y {};
63
64struct S : Y, virtual X {
65 S ();
66};
67
68struct Z : S {
John McCall7c2342d2010-03-10 11:27:22 +000069 Z() : X(), S(), E() {} // expected-error {{type 'E' is not a direct or virtual base of 'Z'}}
Fariborz Jahanian9da72012009-06-30 17:34:52 +000070};
71
Fariborz Jahanian5ac3dfc2009-06-30 21:52:59 +000072class U {
73 union { int a; char* p; };
74 union { int b; double d; };
75
John McCall3c3ccdb2010-04-10 09:28:51 +000076 U() : a(1), // expected-note {{previous initialization is here}}
77 p(0), // expected-error {{initializing multiple members of anonymous union}}
78 d(1.0) {}
Fariborz Jahanian5ac3dfc2009-06-30 21:52:59 +000079};
80
Fariborz Jahanianbcfad542009-06-30 23:26:25 +000081struct V {};
82struct Base {};
83struct Base1 {};
84
85struct Derived : Base, Base1, virtual V {
86 Derived ();
87};
88
89struct Current : Derived {
90 int Derived;
John McCalld6ca8da2010-04-10 07:37:23 +000091 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'}}
Fariborz Jahanianbcfad542009-06-30 23:26:25 +000093 ::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'}}
John McCalld6ca8da2010-04-10 07:37:23 +000095 Derived::V(),
Fariborz Jahanianbcfad542009-06-30 23:26:25 +000096 ::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}} \
Mike Stump1eb44332009-09-09 15:08:12 +000098 // expected-error {{member initializer 'NonExisting' does not name a non-static data member or}}
Fariborz Jahanianbcfad542009-06-30 23:26:25 +000099};
Fariborz Jahanian87595e42009-07-23 23:32:59 +0000100
John McCall220ccbf2010-01-13 00:25:19 +0000101struct M { // expected-note 2 {{candidate constructor (the implicit copy constructor)}} \
Eli Friedman49c16da2009-11-09 01:05:47 +0000102 // expected-note {{declared here}} \
103 // expected-note {{declared here}}
John McCallb1622a12010-01-06 09:43:14 +0000104 M(int i, int j); // expected-note 2 {{candidate constructor}}
Fariborz Jahanian87595e42009-07-23 23:32:59 +0000105};
106
107struct N : M {
John McCall7c2342d2010-03-10 11:27:22 +0000108 N() : M(1), // expected-error {{no matching constructor for initialization of 'M'}}
109 m1(100) { } // expected-error {{no matching constructor for initialization of 'M'}}
Fariborz Jahanian87595e42009-07-23 23:32:59 +0000110 M m1;
111};
112
Eli Friedman49c16da2009-11-09 01:05:47 +0000113struct P : M {
John McCall7c2342d2010-03-10 11:27:22 +0000114 P() { } // expected-error {{constructor for 'P' must explicitly initialize the base class 'M' which does not have a default constructor}} \
Eli Friedman49c16da2009-11-09 01:05:47 +0000115 // expected-error {{member 'm'}}
116 M m; // expected-note {{member is declared here}}
Fariborz Jahanian87595e42009-07-23 23:32:59 +0000117};
118
Fariborz Jahanian7252f512009-07-24 20:28:49 +0000119struct Q {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000120 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'}}
Fariborz Jahanian7252f512009-07-24 20:28:49 +0000122 float f1;
123
124 float *pf;
125};
John McCallb4190042009-11-04 23:02:40 +0000126
127// A silly class used to demonstrate field-is-uninitialized in constructors with
128// multiple params.
John McCall7002f4c2010-04-09 19:03:51 +0000129class TwoInOne { public: TwoInOne(TwoInOne a, TwoInOne b) {} };
John McCallb4190042009-11-04 23:02:40 +0000130class 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
143int IntWrapper(int i) { return 0; };
144class 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
152class 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};
Douglas Gregorc07a4942009-11-15 08:51:10 +0000159
160// Make sure we aren't marking default constructors when we shouldn't be.
161template<typename T>
162struct NDC {
163 T &ref;
164
165 NDC() { }
166 NDC(T &ref) : ref(ref) { }
167};
168
169struct X0 : NDC<int> {
170 X0(int &ref) : NDC<int>(ref), ndc(ref) { }
171
172 NDC<int> ndc;
173};
Anders Carlsson114a2972010-04-23 03:07:47 +0000174
175namespace Test0 {
176
177struct A { A(); };
178
179struct B {
180 B() { }
181 const A a;
182};
183
184}
John McCall0d6b1642010-04-23 18:46:30 +0000185
186namespace test1 {
187 struct A {
188 enum Kind { Foo } Kind;
189 A() : Kind(Foo) {}
190 };
191}