326 lines · cpp
1// RUN: %clang_cc1 -Wreorder -fsyntax-only -verify %s2// RUN: %clang_cc1 -Wreorder -fsyntax-only -verify -std=c++98 %s3// RUN: %clang_cc1 -Wreorder -fsyntax-only -verify -std=c++11 %s4 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 -> D -> C -> B\n class E -> 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 {{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 105struct M { // expected-note 2 {{candidate constructor (the implicit copy constructor)}}106#if __cplusplus >= 201103L // C++11 or later107// expected-note@-2 2 {{candidate constructor (the implicit move constructor) not viable}}108#endif109// expected-note@-4 2 {{'M' declared here}}110 M(int i, int j); // expected-note 2 {{candidate constructor}}111};112 113struct 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 119struct 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 125struct 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 with134// multiple params.135int IntParam(int i) { return 0; };136class TwoInOne { public: TwoInOne(TwoInOne a, TwoInOne b) {} };137class 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 152int IntWrapper(int &i) { return 0; };153class 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 warn162 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 165class 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.174template<typename T>175struct NDC {176 T &ref;177 178 NDC() { }179 NDC(T &ref) : ref(ref) { }180};181 182struct X0 : NDC<int> {183 X0(int &ref) : NDC<int>(ref), ndc(ref) { }184 185 NDC<int> ndc;186};187 188namespace Test0 {189 190struct A { A(); };191 192struct B {193 B() { } 194 const A a;195};196 197}198 199namespace Test1 {200 struct A {201 enum Kind { Foo } Kind;202 A() : Kind(Foo) {}203 };204}205 206namespace Test2 {207 208struct A { 209 A(const A&);210};211 212struct B : virtual A { };213 214 struct C : A, B { }; // expected-warning{{direct base 'A' is inaccessible due to ambiguity:\n struct Test2::C -> A\n struct Test2::C -> B -> A}}215 216C f(C c) {217 return c;218}219 220}221 222// Don't build implicit initializers for anonymous union fields when we already223// have an explicit initializer for another field in the union.224namespace 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// Don't crash. 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 later244 // expected-note@-3 {{candidate constructor (the implicit move constructor) not viable}}245#endif246 // 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}} \252 expected-error {{member initializer 'm_String' does not name}} \253 expected-error {{member initializer 'm_ErrorStr' does not name}}254 B(const B& e)255 : A(e), m_String(e.m_String), m_ErrorStr(__null) { // expected-error 2{{does not name}} \256 // expected-error {{no member named 'm_String' in 'test3::B'}}257 }258 };259}260 261// PR8075262namespace PR8075 {263 264struct S1 {265 enum { FOO = 42 };266 static const int bar = 42;267 static int baz();268 S1(int);269};270 271const int S1::bar;272 273struct S2 {274 S1 s1;275 S2() : s1(s1.FOO) {}276};277 278struct S3 {279 S1 s1;280 S3() : s1(s1.bar) {}281};282 283struct S4 {284 S1 s1;285 S4() : s1(s1.baz()) {}286};287 288}289 290namespace PR12049 {291 int function();292 293 class Class294 {295 public:296 Class() : member(function() {} // expected-note {{to match this '('}}297 298 int member; // expected-error {{expected ')'}}299 };300}301 302namespace PR14073 {303 struct S1 { union { int n; }; S1() : n(n) {} }; // expected-warning {{field 'n' is uninitialized when used here}}304 struct S2 { union { union { int n; }; char c; }; S2() : n(n) {} }; // expected-warning {{field 'n' is uninitialized when used here}}305 struct S3 { struct { int n; }; S3() : n(n) {} }; // expected-warning {{field 'n' is uninitialized when used here}}306}307 308namespace PR10758 {309struct A;310struct B {311 B (A const &); // expected-note 2 {{candidate constructor not viable: no known conversion from 'const B' to 'const A &' for 1st argument}}312 B (B &); // expected-note 2 {{candidate constructor not viable: 1st argument ('const B') would lose const qualifier}}313};314struct A {315 A (B); // expected-note 2 {{passing argument to parameter here}}316};317 318B f(B const &b) {319 return b; // expected-error {{no matching constructor for initialization of 'B'}}320}321 322A f2(const B &b) {323 return b; // expected-error {{no matching constructor for initialization of 'B'}}324}325}326