| Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -Wreorder -fsyntax-only -verify %s | 
| Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2 | class A {  | 
 | 3 |   int m; | 
| Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 4 |    A() : A::m(17) { } // expected-error {{member initializer 'm' does not name a non-static data member or base class}} | 
| Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 5 |    A(int); | 
| Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 6 | }; | 
 | 7 |  | 
 | 8 | class B : public A {  | 
 | 9 | public: | 
 | 10 |   B() : A(), m(1), n(3.14) { } | 
 | 11 |  | 
 | 12 | private: | 
 | 13 |   int m; | 
 | 14 |   float n;   | 
 | 15 | }; | 
 | 16 |  | 
 | 17 |  | 
 | 18 | class C : public virtual B {  | 
 | 19 | public: | 
 | 20 |   C() : B() { } | 
 | 21 | }; | 
 | 22 |  | 
 | 23 | class D : public C {  | 
 | 24 | public: | 
 | 25 |   D() : B(), C() { } | 
 | 26 | }; | 
 | 27 |  | 
 | 28 | class E : public D, public B {  | 
 | 29 | public: | 
| Fariborz Jahanian | b7b6c4c | 2009-07-29 21:26:28 +0000 | [diff] [blame] | 30 |   E() : B(), D() { } // expected-error{{base class initializer 'class B' names both a direct base class and an inherited virtual base class}} | 
| Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 31 | }; | 
 | 32 |  | 
 | 33 |  | 
 | 34 | typedef int INT; | 
 | 35 |  | 
 | 36 | class F : public B {  | 
 | 37 | public: | 
 | 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 Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 42 |         INT(17) // expected-error{{constructor initializer 'INT' (aka 'int') does not name a class}} | 
| Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 43 |   {  | 
 | 44 |   } | 
 | 45 | }; | 
| Douglas Gregor | 3f08d18 | 2008-11-10 16:59:40 +0000 | [diff] [blame] | 46 |  | 
 | 47 | class G : A { | 
 | 48 |   G() : A(10); // expected-error{{expected '{'}} | 
 | 49 | }; | 
| Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 50 |  | 
 | 51 | void f() : a(242) { } // expected-error{{only constructors take base initializers}} | 
 | 52 |  | 
 | 53 | class H : A { | 
 | 54 |   H(); | 
 | 55 | }; | 
 | 56 |  | 
 | 57 | H::H() : A(10) { } | 
 | 58 |  | 
| Fariborz Jahanian | 9da7201 | 2009-06-30 17:34:52 +0000 | [diff] [blame] | 59 |  | 
 | 60 | class  X {}; | 
 | 61 | class Y {}; | 
 | 62 |  | 
 | 63 | struct S : Y, virtual X { | 
 | 64 |   S ();  | 
 | 65 | }; | 
 | 66 |  | 
 | 67 | struct Z : S {  | 
| Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 68 |   Z() : X(), S(), E()  {} // expected-error {{type 'class E' is not a direct or virtual base of 'Z'}} | 
| Fariborz Jahanian | 9da7201 | 2009-06-30 17:34:52 +0000 | [diff] [blame] | 69 | }; | 
 | 70 |  | 
| Fariborz Jahanian | 5ac3dfc | 2009-06-30 21:52:59 +0000 | [diff] [blame] | 71 | class 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 Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 79 | struct V {}; | 
 | 80 | struct Base {}; | 
 | 81 | struct Base1 {}; | 
 | 82 |  | 
 | 83 | struct Derived : Base, Base1, virtual V { | 
 | 84 |   Derived (); | 
 | 85 | }; | 
 | 86 |  | 
 | 87 | struct Current : Derived { | 
 | 88 |   int Derived; | 
| Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 89 |   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 Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 92 |                           ::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 Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 94 |                            Derived::V(), // expected-note {{base 'Derived::V'}} | 
| Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 95 |                            ::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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 |                                                   // expected-error {{member initializer 'NonExisting' does not name a non-static data member or}} | 
| Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 98 | }; | 
| Fariborz Jahanian | 87595e4 | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 99 |  | 
| John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame^] | 100 | struct M {              // expected-note 2 {{candidate constructor (the implicit copy constructor)}} \ | 
| Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 101 |                         // expected-note {{declared here}} \ | 
 | 102 |                         // expected-note {{declared here}} | 
| John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 103 |   M(int i, int j);      // expected-note 2 {{candidate constructor}} | 
| Fariborz Jahanian | 87595e4 | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 104 | }; | 
 | 105 |  | 
 | 106 | struct N : M  { | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 |   N() : M(1),        // expected-error {{no matching constructor for initialization of 'M'}} | 
| Fariborz Jahanian | 87595e4 | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 108 |         m1(100) {  } // expected-error {{no matching constructor for initialization of 'm1'}} | 
 | 109 |   M m1; | 
 | 110 | }; | 
 | 111 |  | 
| Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 112 | struct 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 Jahanian | 87595e4 | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 116 | }; | 
 | 117 |  | 
| Fariborz Jahanian | 7252f51 | 2009-07-24 20:28:49 +0000 | [diff] [blame] | 118 | struct Q { | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 |   Q() : f1(1,2),       // expected-error {{Too many arguments for member initializer 'f1'}} | 
| Fariborz Jahanian | 7252f51 | 2009-07-24 20:28:49 +0000 | [diff] [blame] | 120 |         pf(0.0)  { }   // expected-error {{incompatible type passing 'double', expected 'float *'}} | 
 | 121 |   float f1; | 
 | 122 |  | 
 | 123 |   float *pf; | 
 | 124 | }; | 
| John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 125 |  | 
 | 126 | // A silly class used to demonstrate field-is-uninitialized in constructors with | 
 | 127 | // multiple params. | 
 | 128 | class TwoInOne { TwoInOne(TwoInOne a, TwoInOne b) {} }; | 
 | 129 | class 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 |  | 
 | 142 | int IntWrapper(int i) { return 0; }; | 
 | 143 | class 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 |  | 
 | 151 | class 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 Gregor | c07a494 | 2009-11-15 08:51:10 +0000 | [diff] [blame] | 158 |  | 
 | 159 | // Make sure we aren't marking default constructors when we shouldn't be. | 
 | 160 | template<typename T> | 
 | 161 | struct NDC { | 
 | 162 |   T &ref; | 
 | 163 |    | 
 | 164 |   NDC() { } | 
 | 165 |   NDC(T &ref) : ref(ref) { } | 
 | 166 | }; | 
 | 167 |    | 
 | 168 | struct X0 : NDC<int> { | 
 | 169 |   X0(int &ref) : NDC<int>(ref), ndc(ref) { } | 
 | 170 |    | 
 | 171 |   NDC<int> ndc; | 
 | 172 | }; |