blob: 53f057ed0f35f4218ae8c4c92beed99ea1d3ab58 [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;
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00004 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 +00005 A(int);
Douglas Gregor7ad83902008-11-05 04:29:56 +00006};
7
8class B : public A {
9public:
10 B() : A(), m(1), n(3.14) { }
11
12private:
13 int m;
14 float n;
15};
16
17
18class C : public virtual B {
19public:
20 C() : B() { }
21};
22
23class D : public C {
24public:
25 D() : B(), C() { }
26};
27
28class E : public D, public B {
29public:
Fariborz Jahanianb7b6c4c2009-07-29 21:26:28 +000030 E() : B(), D() { } // expected-error{{base class initializer 'class B' names both a direct base class and an inherited virtual base class}}
Douglas Gregor7ad83902008-11-05 04:29:56 +000031};
32
33
34typedef int INT;
35
36class F : public B {
37public:
38 int B;
39
40 F() : B(17),
41 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 +000042 INT(17) // expected-error{{constructor initializer 'INT' (aka 'int') does not name a class}}
Douglas Gregor7ad83902008-11-05 04:29:56 +000043 {
44 }
45};
Douglas Gregor3f08d182008-11-10 16:59:40 +000046
47class G : A {
48 G() : A(10); // expected-error{{expected '{'}}
49};
Anders Carlssona7b35212009-03-25 02:58:17 +000050
51void f() : a(242) { } // expected-error{{only constructors take base initializers}}
52
53class H : A {
54 H();
55};
56
57H::H() : A(10) { }
58
Fariborz Jahanian9da72012009-06-30 17:34:52 +000059
60class X {};
61class Y {};
62
63struct S : Y, virtual X {
64 S ();
65};
66
67struct Z : S {
Fariborz Jahanianeb96e122009-07-09 19:59:47 +000068 Z() : X(), S(), E() {} // expected-error {{type 'class E' is not a direct or virtual base of 'Z'}}
Fariborz Jahanian9da72012009-06-30 17:34:52 +000069};
70
Fariborz Jahanian5ac3dfc2009-06-30 21:52:59 +000071class U {
72 union { int a; char* p; };
73 union { int b; double d; };
74
75 U() : a(1), p(0), d(1.0) {} // expected-error {{multiple initializations given for non-static member 'p'}} \
76 // expected-note {{previous initialization is here}}
77};
78
Fariborz Jahanianbcfad542009-06-30 23:26:25 +000079struct V {};
80struct Base {};
81struct Base1 {};
82
83struct Derived : Base, Base1, virtual V {
84 Derived ();
85};
86
87struct Current : Derived {
88 int Derived;
Fariborz Jahanianeb96e122009-07-09 19:59:47 +000089 Current() : Derived(1), ::Derived(), // expected-warning {{member 'Derived' will be initialized after}} \
90 // expected-note {{base '::Derived'}} \
91 // expected-warning {{base class '::Derived' will be initialized after}}
Fariborz Jahanianbcfad542009-06-30 23:26:25 +000092 ::Derived::Base(), // expected-error {{type '::Derived::Base' is not a direct or virtual base of 'Current'}}
93 Derived::Base1(), // expected-error {{type 'Derived::Base1' is not a direct or virtual base of 'Current'}}
Fariborz Jahanianeb96e122009-07-09 19:59:47 +000094 Derived::V(), // expected-note {{base 'Derived::V'}}
Fariborz Jahanianbcfad542009-06-30 23:26:25 +000095 ::NonExisting(), // expected-error {{member initializer 'NonExisting' does not name a non-static data member or}}
96 INT::NonExisting() {} // expected-error {{expected a class or namespace}} \
Mike Stump1eb44332009-09-09 15:08:12 +000097 // expected-error {{member initializer 'NonExisting' does not name a non-static data member or}}
Fariborz Jahanianbcfad542009-06-30 23:26:25 +000098};
Fariborz Jahanian87595e42009-07-23 23:32:59 +000099
John McCall220ccbf2010-01-13 00:25:19 +0000100struct M { // expected-note 2 {{candidate constructor (the implicit copy constructor)}} \
Eli Friedman49c16da2009-11-09 01:05:47 +0000101 // expected-note {{declared here}} \
102 // expected-note {{declared here}}
John McCallb1622a12010-01-06 09:43:14 +0000103 M(int i, int j); // expected-note 2 {{candidate constructor}}
Fariborz Jahanian87595e42009-07-23 23:32:59 +0000104};
105
106struct N : M {
Mike Stump1eb44332009-09-09 15:08:12 +0000107 N() : M(1), // expected-error {{no matching constructor for initialization of 'M'}}
Fariborz Jahanian87595e42009-07-23 23:32:59 +0000108 m1(100) { } // expected-error {{no matching constructor for initialization of 'm1'}}
109 M m1;
110};
111
Eli Friedman49c16da2009-11-09 01:05:47 +0000112struct P : M {
113 P() { } // expected-error {{base class 'struct M'}} \
114 // expected-error {{member 'm'}}
115 M m; // expected-note {{member is declared here}}
Fariborz Jahanian87595e42009-07-23 23:32:59 +0000116};
117
Fariborz Jahanian7252f512009-07-24 20:28:49 +0000118struct Q {
Mike Stump1eb44332009-09-09 15:08:12 +0000119 Q() : f1(1,2), // expected-error {{Too many arguments for member initializer 'f1'}}
Fariborz Jahanian7252f512009-07-24 20:28:49 +0000120 pf(0.0) { } // expected-error {{incompatible type passing 'double', expected 'float *'}}
121 float f1;
122
123 float *pf;
124};
John McCallb4190042009-11-04 23:02:40 +0000125
126// A silly class used to demonstrate field-is-uninitialized in constructors with
127// multiple params.
128class TwoInOne { TwoInOne(TwoInOne a, TwoInOne b) {} };
129class InitializeUsingSelfTest {
130 bool A;
131 char* B;
132 int C;
133 TwoInOne D;
134 InitializeUsingSelfTest(int E)
135 : A(A), // expected-warning {{field is uninitialized when used here}}
136 B((((B)))), // expected-warning {{field is uninitialized when used here}}
137 C(A && InitializeUsingSelfTest::C), // expected-warning {{field is uninitialized when used here}}
138 D(D, // expected-warning {{field is uninitialized when used here}}
139 D) {} // expected-warning {{field is uninitialized when used here}}
140};
141
142int IntWrapper(int i) { return 0; };
143class InitializeUsingSelfExceptions {
144 int A;
145 int B;
146 InitializeUsingSelfExceptions(int B)
147 : 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.
148 B(B) {} // Not a warning; B is a local variable.
149};
150
151class CopyConstructorTest {
152 bool A, B, C;
153 CopyConstructorTest(const CopyConstructorTest& rhs)
154 : A(rhs.A),
155 B(B), // expected-warning {{field is uninitialized when used here}}
156 C(rhs.C || C) { } // expected-warning {{field is uninitialized when used here}}
157};
Douglas Gregorc07a4942009-11-15 08:51:10 +0000158
159// Make sure we aren't marking default constructors when we shouldn't be.
160template<typename T>
161struct NDC {
162 T &ref;
163
164 NDC() { }
165 NDC(T &ref) : ref(ref) { }
166};
167
168struct X0 : NDC<int> {
169 X0(int &ref) : NDC<int>(ref), ndc(ref) { }
170
171 NDC<int> ndc;
172};