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