[Clang] replace 'bitfield' with 'bit-field' for consistency (#117881)
[llvm-project.git] / clang / test / SemaCXX / functional-cast.cpp
blobfcfaff5bf854a7103f0b571c5a731c17b550ad49
1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2 // REQUIRES: LP64
4 // ------------ not interpreted as C-style cast ------------
6 struct SimpleValueInit {
7 int i;
8 };
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 --------------------
34 struct A {};
36 // ----------- const_cast --------------
38 typedef char c;
39 typedef c *cp;
40 typedef cp *cpp;
41 typedef cpp *cppp;
42 typedef cppp &cpppr;
43 typedef const cppp &cpppcr;
44 typedef const char cc;
45 typedef cc *ccp;
46 typedef volatile ccp ccvp;
47 typedef ccvp *ccvpp;
48 typedef const volatile ccvpp ccvpcvp;
49 typedef ccvpcvp *ccvpcvpp;
50 typedef int iar[100];
51 typedef iar &iarr;
52 typedef int (*f)(int);
54 void t_cc()
56 ccvpcvpp var = 0;
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.
66 typedef int *intp;
67 int *pi = intp(ar);
68 f fp = 0;
69 // Don't misidentify fn** as a function pointer.
70 typedef f *fp_t;
71 f *fpp = fp_t(&fp);
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); };
94 struct Co3 { };
95 struct Co4 { Co4(Co3); operator Co3(); };
97 // Explicit implicits
98 void t_529_2()
100 int i = 1;
101 (void)float(i);
102 double d = 1.0;
103 (void)float(d);
104 (void)int(d);
105 (void)char(i);
106 typedef unsigned long ulong;
107 (void)ulong(i);
108 (void)int(En1);
109 (void)double(En1);
110 typedef int &intr;
111 (void)intr(i);
112 typedef const int &cintr;
113 (void)cintr(i);
115 int ar[1];
116 typedef const int *cintp;
117 (void)cintp(ar);
118 typedef void (*pfvv)();
119 (void)pfvv(t_529_2);
121 typedef void *voidp;
122 (void)voidp(0);
123 (void)voidp((int*)0);
124 typedef volatile const void *vcvoidp;
125 (void)vcvoidp((const int*)0);
126 typedef A *Ap;
127 (void)Ap((B*)0);
128 typedef A &Ar;
129 (void)Ar(*((B*)0)); // expected-warning {{binding dereferenced null pointer to reference has undefined behavior}}
130 typedef const B *cBp;
131 (void)cBp((C1*)0);
132 typedef B &Br;
133 (void)Br(*((C1*)0)); // expected-warning {{binding dereferenced null pointer to reference has undefined behavior}}
134 (void)Ap((D*)0);
135 typedef const A &cAr;
136 (void)cAr(*((D*)0)); // expected-warning {{binding dereferenced null pointer to reference has undefined behavior}}
137 typedef int B::*Bmp;
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
144 (void)int(Co1());
145 (void)Co2(1);
146 (void)Co3((Co4)(Co3()));
148 // Bad code below
149 //(void)(A*)((H*)0); // {{static_cast from 'struct H *' to 'struct A *' is not allowed}}
152 // Anything to void
153 void t_529_4()
155 void(1);
156 (void(t_529_4));
159 // Static downcasts
160 void t_529_5_8()
162 typedef B *Bp;
163 (void)Bp((A*)0);
164 typedef B &Br;
165 (void)Br(*((A*)0));
166 typedef const G1 *cG1p;
167 (void)cG1p((A*)0);
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
172 typedef E *Ep;
173 (void)Ep((A*)0); // access control ignored
174 typedef E &Er;
175 (void)Er(*((A*)0)); // access control ignored
177 // Bad code below
179 typedef C1 *C1p;
180 (void)C1p((A*)0); // expected-error {{cannot cast 'A *' to 'C1p' (aka 'C1 *') via virtual base 'B'}}
181 typedef C1 &C1r;
182 (void)C1r(*((A*)0)); // expected-error {{cannot cast 'A' to 'C1r' (aka 'C1 &') via virtual base 'B'}}
183 typedef D *Dp;
184 (void)Dp((A*)0); // expected-error {{cannot cast 'A *' to 'Dp' (aka 'D *') via virtual base 'B'}}
185 typedef D &Dr;
186 (void)Dr(*((A*)0)); // expected-error {{cannot cast 'A' to 'Dr' (aka 'D &') via virtual base 'B'}}
187 typedef H *Hp;
188 (void)Hp((A*)0); // expected-error {{ambiguous cast from base 'A' to derived 'H':\n A -> B -> G1 -> struct H\n A -> B -> G2 -> struct H}}
189 typedef H &Hr;
190 (void)Hr(*((A*)0)); // expected-error {{ambiguous cast from base 'A' to derived 'H':\n A -> B -> G1 -> struct H\n A -> B -> G2 -> struct H}}
192 // TODO: Test DR427. This requires user-defined conversions, though.
195 // Enum conversions
196 void t_529_7()
198 (void)Enum(1);
199 (void)Enum(1.0);
200 (void)Onom(En1);
202 // Bad code below
204 (void)Enum((int*)0); // expected-error {{functional-style cast from 'int *' to 'Enum' is not allowed}}
207 // Void pointer to object pointer
208 void t_529_10()
210 typedef int *intp;
211 (void)intp((void*)0);
212 typedef const A *cAp;
213 (void)cAp((void*)0);
214 (void)intp((const void*)0); // const_cast appended
217 // Member pointer upcast.
218 void t_529_9()
220 typedef int A::*Amp;
221 (void)Amp((int B::*)0);
223 // Bad code below
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()
237 typedef void *voidp;
238 void *vp = voidp(testval);
239 long l = long(vp);
240 typedef float *floatp;
241 (void)floatp(l);
242 fnptr fnp = fnptr(l);
243 (void)char(fnp); // expected-error {{cast from pointer to smaller type 'char' loses information}}
244 (void)long(fnp);
247 void pointer_conversion()
249 int *p1 = 0;
250 typedef float *floatp;
251 float *p2 = floatp(p1);
252 typedef structure *structurep;
253 structure *p3 = structurep(p2);
254 typedef int **ppint;
255 typedef ppint *pppint;
256 ppint *deep = pppint(p3);
257 typedef fnptr fnptrp;
258 (void)fnptrp(deep);
261 void constness()
263 int ***const ipppc = 0;
264 typedef int const *icp_t;
265 int const *icp = icp_t(ipppc);
266 typedef int *intp;
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);
271 (void)icp_t(ip);
272 typedef int const *const *const *intcpcpcp;
273 (void)intcpcpcp(ipppc);
276 void fnptrs()
278 typedef int (*fnptr2)(int);
279 fnptr fp = 0;
280 (void)fnptr2(fp);
281 typedef void *voidp;
282 void *vp = voidp(fp);
283 (void)fnptr(vp);
286 void refs()
288 long l = 0;
289 typedef char &charr;
290 char &c = charr(l);
291 // Bad: from rvalue
292 typedef int &intr;
293 (void)intr(&c); // expected-error {{functional-style cast from rvalue to reference type 'intr' (aka 'int &')}}
296 void memptrs()
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}}