1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
4 // ------------ not interpreted as C-style cast ------------
6 struct SimpleValueInit
{
10 struct InitViaConstructor
{
11 InitViaConstructor(int i
= 7);
14 struct NoValueInit
{ // expected-note 2 {{candidate constructor (the implicit copy constructor)}} expected-note 2 {{candidate constructor (the implicit move constructor)}}
15 NoValueInit(int i
, int j
); // expected-note 2 {{candidate constructor}}
18 void test_cxx_functional_value_init() {
19 (void)SimpleValueInit();
20 (void)InitViaConstructor();
21 (void)NoValueInit(); // expected-error{{no matching constructor for initialization}}
24 void test_cxx_function_cast_multi() {
25 (void)NoValueInit(0, 0);
26 (void)NoValueInit(0, 0, 0); // expected-error{{no matching constructor for initialization}}
27 (void)int(1, 2); // expected-error{{excess elements in scalar initializer}}
28 (void)int({}, 2); // expected-error{{excess elements in scalar initializer}}
32 // ------------------ everything else --------------------
36 // ----------- const_cast --------------
43 typedef const cppp
&cpppcr
;
44 typedef const char cc
;
46 typedef volatile ccp ccvp
;
48 typedef const volatile ccvpp ccvpcvp
;
49 typedef ccvpcvp
*ccvpcvpp
;
52 typedef int (*f
)(int);
57 // Cast away deep consts and volatiles.
58 char ***var2
= cppp(var
);
59 char ***const &var3
= var2
;
60 // Const reference to reference.
61 char ***&var4
= cpppr(var3
);
62 // Drop reference. Intentionally without qualifier change.
63 char *** var5
= cppp(var4
);
64 const int ar
[100] = {0};
65 // Array decay. Intentionally without qualifier change.
69 // Don't misidentify fn** as a function pointer.
72 int const A::* const A::*icapcap
= 0;
73 typedef int A::* A::*iapap_t
;
74 iapap_t iapap
= iapap_t(icapcap
);
77 // ----------- static_cast -------------
79 struct B
: public A
{}; // Single public base.
80 struct C1
: public virtual B
{}; // Single virtual base.
81 struct C2
: public virtual B
{};
82 struct D
: public C1
, public C2
{}; // Diamond
83 struct E
: private A
{}; // Single private base.
84 struct F
: public C1
{}; // Single path to B with virtual.
85 struct G1
: public B
{};
86 struct G2
: public B
{};
87 struct H
: public G1
, public G2
{}; // Ambiguous path to B.
89 enum Enum
{ En1
, En2
};
90 enum Onom
{ On1
, On2
};
92 struct Co1
{ operator int(); };
93 struct Co2
{ Co2(int); };
95 struct Co4
{ Co4(Co3
); operator Co3(); };
106 typedef unsigned long ulong
;
112 typedef const int &cintr
;
116 typedef const int *cintp
;
118 typedef void (*pfvv
)();
123 (void)voidp((int*)0);
124 typedef volatile const void *vcvoidp
;
125 (void)vcvoidp((const int*)0);
129 (void)Ar(*((B
*)0)); // expected-warning {{binding dereferenced null pointer to reference has undefined behavior}}
130 typedef const B
*cBp
;
133 (void)Br(*((C1
*)0)); // expected-warning {{binding dereferenced null pointer to reference has undefined behavior}}
135 typedef const A
&cAr
;
136 (void)cAr(*((D
*)0)); // expected-warning {{binding dereferenced null pointer to reference has undefined behavior}}
138 (void)Bmp((int A::*)0);
139 typedef void (B::*Bmfp
)();
140 (void)Bmfp((void (A::*)())0);
141 (void)Ap((E
*)0); // functional-style cast ignores access control
142 (void)voidp((const int*)0); // const_cast appended
146 (void)Co3((Co4
)(Co3()));
149 //(void)(A*)((H*)0); // {{static_cast from 'struct H *' to 'struct A *' is not allowed}}
166 typedef const G1
*cG1p
;
168 typedef const G1
&cG1r
;
169 (void)cG1r(*((A
*)0));
170 (void)Bp((const A
*)0); // const_cast appended
171 (void)Br(*((const A
*)0)); // const_cast appended
173 (void)Ep((A
*)0); // access control ignored
175 (void)Er(*((A
*)0)); // access control ignored
180 (void)C1p((A
*)0); // expected-error {{cannot cast 'A *' to 'C1p' (aka 'C1 *') via virtual base 'B'}}
182 (void)C1r(*((A
*)0)); // expected-error {{cannot cast 'A' to 'C1r' (aka 'C1 &') via virtual base 'B'}}
184 (void)Dp((A
*)0); // expected-error {{cannot cast 'A *' to 'Dp' (aka 'D *') via virtual base 'B'}}
186 (void)Dr(*((A
*)0)); // expected-error {{cannot cast 'A' to 'Dr' (aka 'D &') via virtual base 'B'}}
188 (void)Hp((A
*)0); // expected-error {{ambiguous cast from base 'A' to derived 'H':\n struct A -> struct B -> struct G1 -> struct H\n struct A -> struct B -> struct G2 -> struct H}}
190 (void)Hr(*((A
*)0)); // expected-error {{ambiguous cast from base 'A' to derived 'H':\n struct A -> struct B -> struct G1 -> struct H\n struct A -> struct B -> struct G2 -> struct H}}
192 // TODO: Test DR427. This requires user-defined conversions, though.
204 (void)Enum((int*)0); // expected-error {{functional-style cast from 'int *' to 'Enum' is not allowed}}
207 // Void pointer to object pointer
211 (void)intp((void*)0);
212 typedef const A
*cAp
;
214 (void)intp((const void*)0); // const_cast appended
217 // Member pointer upcast.
221 (void)Amp((int B::*)0);
224 (void)Amp((int H::*)0); // expected-error {{ambiguous conversion from pointer to member of derived class 'H' to pointer to member of base class 'A':}}
225 (void)Amp((int F::*)0); // expected-error {{conversion from pointer to member of class 'F' to pointer to member of class 'A' via virtual base 'B' is not allowed}}
228 // -------- reinterpret_cast -----------
230 enum test
{ testval
= 1 };
231 struct structure
{ int m
; };
232 typedef void (*fnptr
)();
234 // Test conversion between pointer and integral types, as in p3 and p4.
235 void integral_conversion()
238 void *vp
= voidp(testval
);
240 typedef float *floatp
;
242 fnptr fnp
= fnptr(l
);
243 (void)char(fnp
); // expected-error {{cast from pointer to smaller type 'char' loses information}}
247 void pointer_conversion()
250 typedef float *floatp
;
251 float *p2
= floatp(p1
);
252 typedef structure
*structurep
;
253 structure
*p3
= structurep(p2
);
255 typedef ppint
*pppint
;
256 ppint
*deep
= pppint(p3
);
257 typedef fnptr fnptrp
;
263 int ***const ipppc
= 0;
264 typedef int const *icp_t
;
265 int const *icp
= icp_t(ipppc
);
267 (void)intp(icp
); // const_cast appended
268 typedef int const *const ** intcpcpp
;
269 intcpcpp icpcpp
= intcpcpp(ipppc
); // const_cast appended
270 int *ip
= intp(icpcpp
);
272 typedef int const *const *const *intcpcpcp
;
273 (void)intcpcpcp(ipppc
);
278 typedef int (*fnptr2
)(int);
282 void *vp
= voidp(fp
);
293 (void)intr(&c
); // expected-error {{functional-style cast from rvalue to reference type 'intr' (aka 'int &')}}
298 const int structure::*psi
= 0;
299 typedef const float structure::*structurecfmp
;
300 (void)structurecfmp(psi
);
301 typedef int structure::*structureimp
;
302 (void)structureimp(psi
); // const_cast appended
304 void (structure::*psf
)() = 0;
305 typedef int (structure::*structureimfp
)();
306 (void)structureimfp(psf
);
308 typedef void (structure::*structurevmfp
)();
309 (void)structurevmfp(psi
); // expected-error-re {{functional-style cast from 'const int structure::*' to 'structurevmfp' (aka 'void (structure::*)(){{( __attribute__\(\(thiscall\)\))?}}') is not allowed}}
310 (void)structureimp(psf
); // expected-error-re {{functional-style cast from 'void (structure::*)(){{( __attribute__\(\(thiscall\)\))?}}' to 'structureimp' (aka 'int structure::*') is not allowed}}
313 // ---------------- misc ------------------
315 void crash_on_invalid_1()
317 typedef itn Typo
; // expected-error {{unknown type name 'itn'}}
318 (void)Typo(1); // used to crash
320 typedef int &int_ref
;
321 (void)int_ref(); // expected-error {{reference to type 'int' requires an initializer}}