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; |
John McCall | 7002f4c | 2010-04-09 19:03:51 +0000 | [diff] [blame] | 4 | public: |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 5 | 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] | 6 | A(int); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 7 | }; |
| 8 | |
| 9 | class B : public A { |
| 10 | public: |
| 11 | B() : A(), m(1), n(3.14) { } |
| 12 | |
| 13 | private: |
| 14 | int m; |
| 15 | float n; |
| 16 | }; |
| 17 | |
| 18 | |
| 19 | class C : public virtual B { |
| 20 | public: |
| 21 | C() : B() { } |
| 22 | }; |
| 23 | |
| 24 | class D : public C { |
| 25 | public: |
| 26 | D() : B(), C() { } |
| 27 | }; |
| 28 | |
| 29 | class E : public D, public B { |
| 30 | public: |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 31 | E() : B(), D() { } // expected-error{{base class initializer '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] | 32 | }; |
| 33 | |
| 34 | |
| 35 | typedef int INT; |
| 36 | |
| 37 | class F : public B { |
| 38 | public: |
| 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}} |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 43 | 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] | 44 | { |
| 45 | } |
| 46 | }; |
Douglas Gregor | 3f08d18 | 2008-11-10 16:59:40 +0000 | [diff] [blame] | 47 | |
| 48 | class G : A { |
| 49 | G() : A(10); // expected-error{{expected '{'}} |
| 50 | }; |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 51 | |
| 52 | void f() : a(242) { } // expected-error{{only constructors take base initializers}} |
| 53 | |
| 54 | class H : A { |
| 55 | H(); |
| 56 | }; |
| 57 | |
| 58 | H::H() : A(10) { } |
| 59 | |
Fariborz Jahanian | 9da7201 | 2009-06-30 17:34:52 +0000 | [diff] [blame] | 60 | |
| 61 | class X {}; |
| 62 | class Y {}; |
| 63 | |
| 64 | struct S : Y, virtual X { |
| 65 | S (); |
| 66 | }; |
| 67 | |
| 68 | struct Z : S { |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 69 | Z() : X(), S(), E() {} // expected-error {{type 'E' is not a direct or virtual base of 'Z'}} |
Fariborz Jahanian | 9da7201 | 2009-06-30 17:34:52 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
Fariborz Jahanian | 5ac3dfc | 2009-06-30 21:52:59 +0000 | [diff] [blame] | 72 | class U { |
| 73 | union { int a; char* p; }; |
| 74 | union { int b; double d; }; |
| 75 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 76 | U() : a(1), // expected-note {{previous initialization is here}} |
David Blaikie | 6fe2965 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 77 | p(0), // expected-error {{initializing multiple members of union}} |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 78 | d(1.0) {} |
Fariborz Jahanian | 5ac3dfc | 2009-06-30 21:52:59 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 81 | struct V {}; |
| 82 | struct Base {}; |
| 83 | struct Base1 {}; |
| 84 | |
| 85 | struct Derived : Base, Base1, virtual V { |
| 86 | Derived (); |
| 87 | }; |
| 88 | |
| 89 | struct Current : Derived { |
| 90 | int Derived; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 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'}} |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 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'}} |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 95 | Derived::V(), |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 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}} \ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | // 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] | 99 | }; |
Fariborz Jahanian | 87595e4 | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 100 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 101 | struct M { // expected-note 2 {{candidate constructor (the implicit copy constructor)}} \ |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 102 | // expected-note {{declared here}} \ |
| 103 | // expected-note {{declared here}} |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 104 | M(int i, int j); // expected-note 2 {{candidate constructor}} |
Fariborz Jahanian | 87595e4 | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | struct N : M { |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 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'}} |
Fariborz Jahanian | 87595e4 | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 110 | M m1; |
| 111 | }; |
| 112 | |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 113 | struct P : M { |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 114 | P() { } // expected-error {{constructor for 'P' must explicitly initialize the base class 'M' which does not have a default constructor}} \ |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 115 | // expected-error {{member 'm'}} |
| 116 | M m; // expected-note {{member is declared here}} |
Fariborz Jahanian | 87595e4 | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
Fariborz Jahanian | 7252f51 | 2009-07-24 20:28:49 +0000 | [diff] [blame] | 119 | struct Q { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 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'}} |
Fariborz Jahanian | 7252f51 | 2009-07-24 20:28:49 +0000 | [diff] [blame] | 122 | float f1; |
| 123 | |
| 124 | float *pf; |
| 125 | }; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 126 | |
| 127 | // A silly class used to demonstrate field-is-uninitialized in constructors with |
| 128 | // multiple params. |
Richard Trieu | de5e75c | 2012-06-14 23:11:34 +0000 | [diff] [blame] | 129 | int IntParam(int i) { return 0; }; |
John McCall | 7002f4c | 2010-04-09 19:03:51 +0000 | [diff] [blame] | 130 | class TwoInOne { public: TwoInOne(TwoInOne a, TwoInOne b) {} }; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 131 | class InitializeUsingSelfTest { |
| 132 | bool A; |
| 133 | char* B; |
| 134 | int C; |
| 135 | TwoInOne D; |
Richard Trieu | de5e75c | 2012-06-14 23:11:34 +0000 | [diff] [blame] | 136 | int E; |
| 137 | InitializeUsingSelfTest(int F) |
Hans Wennborg | 7821e07 | 2012-09-21 08:58:33 +0000 | [diff] [blame] | 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}} |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
Richard Trieu | de5e75c | 2012-06-14 23:11:34 +0000 | [diff] [blame] | 146 | int IntWrapper(int &i) { return 0; }; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 147 | class InitializeUsingSelfExceptions { |
| 148 | int A; |
| 149 | int B; |
Argyrios Kyrtzidis | ff8819b | 2010-09-21 10:47:20 +0000 | [diff] [blame] | 150 | int C; |
| 151 | void *P; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 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. |
Argyrios Kyrtzidis | ff8819b | 2010-09-21 10:47:20 +0000 | [diff] [blame] | 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) |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | class CopyConstructorTest { |
| 160 | bool A, B, C; |
| 161 | CopyConstructorTest(const CopyConstructorTest& rhs) |
| 162 | : A(rhs.A), |
Hans Wennborg | 7821e07 | 2012-09-21 08:58:33 +0000 | [diff] [blame] | 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}} |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 165 | }; |
Douglas Gregor | c07a494 | 2009-11-15 08:51:10 +0000 | [diff] [blame] | 166 | |
| 167 | // Make sure we aren't marking default constructors when we shouldn't be. |
| 168 | template<typename T> |
| 169 | struct NDC { |
| 170 | T &ref; |
| 171 | |
| 172 | NDC() { } |
| 173 | NDC(T &ref) : ref(ref) { } |
| 174 | }; |
| 175 | |
| 176 | struct X0 : NDC<int> { |
| 177 | X0(int &ref) : NDC<int>(ref), ndc(ref) { } |
| 178 | |
| 179 | NDC<int> ndc; |
| 180 | }; |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 181 | |
| 182 | namespace Test0 { |
| 183 | |
| 184 | struct A { A(); }; |
| 185 | |
| 186 | struct B { |
| 187 | B() { } |
| 188 | const A a; |
| 189 | }; |
| 190 | |
| 191 | } |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 192 | |
Anders Carlsson | 9fd682b | 2010-04-25 01:00:05 +0000 | [diff] [blame] | 193 | namespace Test1 { |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 194 | struct A { |
| 195 | enum Kind { Foo } Kind; |
| 196 | A() : Kind(Foo) {} |
| 197 | }; |
| 198 | } |
Anders Carlsson | 9fd682b | 2010-04-25 01:00:05 +0000 | [diff] [blame] | 199 | |
| 200 | namespace Test2 { |
| 201 | |
| 202 | struct A { |
| 203 | A(const A&); |
| 204 | }; |
| 205 | |
| 206 | struct B : virtual A { }; |
| 207 | struct C : A, B { }; |
| 208 | |
| 209 | C f(C c) { |
| 210 | return c; |
| 211 | } |
| 212 | |
| 213 | } |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 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. |
| 217 | namespace 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 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 231 | |
| 232 | // <rdar://problem/8308215>: don't crash. |
| 233 | // Lots of questionable recovery here; errors can change. |
| 234 | namespace test3 { |
Eli Friedman | 06808f1 | 2012-08-08 04:39:56 +0000 | [diff] [blame] | 235 | class A : public std::exception {}; // expected-error {{undeclared identifier}} expected-error {{expected class name}} expected-note 2 {{candidate}} |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 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) |
Eli Friedman | 06808f1 | 2012-08-08 04:39:56 +0000 | [diff] [blame] | 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'}} |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 242 | } |
| 243 | }; |
| 244 | } |
Anders Carlsson | 175ffbf | 2010-10-06 02:43:25 +0000 | [diff] [blame] | 245 | |
| 246 | // PR8075 |
| 247 | namespace PR8075 { |
| 248 | |
| 249 | struct S1 { |
| 250 | enum { FOO = 42 }; |
| 251 | static const int bar = 42; |
| 252 | static int baz(); |
| 253 | S1(int); |
| 254 | }; |
| 255 | |
| 256 | const int S1::bar; |
| 257 | |
| 258 | struct S2 { |
| 259 | S1 s1; |
| 260 | S2() : s1(s1.FOO) {} |
| 261 | }; |
| 262 | |
| 263 | struct S3 { |
| 264 | S1 s1; |
| 265 | S3() : s1(s1.bar) {} |
| 266 | }; |
| 267 | |
| 268 | struct S4 { |
| 269 | S1 s1; |
| 270 | S4() : s1(s1.baz()) {} |
| 271 | }; |
| 272 | |
| 273 | } |
Eli Friedman | e9ee382 | 2012-02-22 04:49:04 +0000 | [diff] [blame] | 274 | |
| 275 | namespace 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 | } |