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