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