[clang-cl] Ignore /Wv and /Wv:17 flags
[llvm-project.git] / clang / test / SemaCXX / constructor-initializer.cpp
blob010f90b59cb07ad6d663b3949be0574c4f6e7fa4
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
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);
12 class B : public A {
13 public:
14 B() : A(), m(1), n(3.14) { }
16 private:
17 int m;
18 float n;
22 class C : public virtual B {
23 public:
24 C() : B() { }
27 class D : public C {
28 public:
29 D() : B(), C() { }
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}}
38 typedef int INT;
40 class F : public B {
41 public:
42 int B;
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}}
51 class G : A {
52 G() : A(10); // expected-error{{expected '{'}}
55 void f() : a(242) { } // expected-error{{only constructors take base initializers}}
57 class H : A {
58 H();
61 H::H() : A(10) { }
64 class X {};
65 class Y {};
67 struct S : Y, virtual X {
68 S ();
71 struct Z : S {
72 Z() : X(), S(), E() {} // expected-error {{type 'E' is not a direct or virtual base of 'Z'}}
75 class U {
76 union { int a; char* p; };
77 union { int b; double d; };
79 U() : a(1), // expected-note {{previous initialization is here}}
80 p(0), // expected-error {{initializing multiple members of union}}
81 d(1.0) {}
84 struct V {};
85 struct Base {};
86 struct Base1 {};
88 struct Derived : Base, Base1, virtual V {
89 Derived ();
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}}
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}}
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;
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}}
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;
130 float *pf;
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}}
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)
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}}
173 // Make sure we aren't marking default constructors when we shouldn't be.
174 template<typename T>
175 struct NDC {
176 T &ref;
178 NDC() { }
179 NDC(T &ref) : ref(ref) { }
182 struct X0 : NDC<int> {
183 X0(int &ref) : NDC<int>(ref), ndc(ref) { }
185 NDC<int> ndc;
188 namespace Test0 {
190 struct A { A(); };
192 struct B {
193 B() { }
194 const A a;
199 namespace Test1 {
200 struct A {
201 enum Kind { Foo } Kind;
202 A() : Kind(Foo) {}
206 namespace Test2 {
208 struct A {
209 A(const A&);
212 struct B : virtual A { };
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}}
216 C f(C c) {
217 return c;
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_; };
231 template <typename T> S(T) : ptr_(0) { }
234 void f() {
235 S s(3);
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}}
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'}}
262 // PR8075
263 namespace PR8075 {
265 struct S1 {
266 enum { FOO = 42 };
267 static const int bar = 42;
268 static int baz();
269 S1(int);
272 const int S1::bar;
274 struct S2 {
275 S1 s1;
276 S2() : s1(s1.FOO) {}
279 struct S3 {
280 S1 s1;
281 S3() : s1(s1.bar) {}
284 struct S4 {
285 S1 s1;
286 S4() : s1(s1.baz()) {}
291 namespace PR12049 {
292 int function();
294 class Class
296 public:
297 Class() : member(function() {} // expected-note {{to match this '('}}
299 int member; // expected-error {{expected ')'}}
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}}
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}}
315 struct A {
316 A (B); // expected-note 2 {{passing argument to parameter here}}
319 B f(B const &b) {
320 return b; // expected-error {{no matching constructor for initialization of 'PR10758::B'}}
323 A f2(const B &b) {
324 return b; // expected-error {{no matching constructor for initialization of 'PR10758::B'}}