blob: 142dba7b13f775ddb90e79a5fabc2d8571ae2702 [file] [log] [blame]
Sebastian Redlef0cb8e2009-07-29 13:50:23 +00001// RUN: clang-cc -fsyntax-only -verify -faccess-control %s
2
3// ------------ not interpreted as C-style cast ------------
Douglas Gregor506ae412009-01-16 18:33:17 +00004
5struct SimpleValueInit {
6 int i;
7};
8
9struct InitViaConstructor {
10 InitViaConstructor(int i = 7);
11};
12
13// FIXME: error messages for implicitly-declared special member
14// function candidates are very poor
Sebastian Redl3cb06922009-02-07 19:52:04 +000015struct NoValueInit { // expected-note 2 {{candidate function}}
16 NoValueInit(int i, int j); // expected-note 2 {{candidate function}}
Douglas Gregor506ae412009-01-16 18:33:17 +000017};
18
19void test_cxx_functional_value_init() {
20 (void)SimpleValueInit();
21 (void)InitViaConstructor();
22 (void)NoValueInit(); // expected-error{{no matching constructor for initialization}}
23}
24
25void test_cxx_function_cast_multi() {
26 (void)NoValueInit(0, 0);
27 (void)NoValueInit(0, 0, 0); // expected-error{{no matching constructor for initialization}}
28 (void)int(1, 2); // expected-error{{function-style cast to a builtin type can only take one argument}}
29}
Sebastian Redlef0cb8e2009-07-29 13:50:23 +000030
31
32// ------------------ everything else --------------------
33
34struct A {};
35
36// ----------- const_cast --------------
37
38typedef char c;
39typedef c *cp;
40typedef cp *cpp;
41typedef cpp *cppp;
42typedef cppp &cpppr;
43typedef const cppp &cpppcr;
44typedef const char cc;
45typedef cc *ccp;
46typedef volatile ccp ccvp;
47typedef ccvp *ccvpp;
48typedef const volatile ccvpp ccvpcvp;
49typedef ccvpcvp *ccvpcvpp;
50typedef int iar[100];
51typedef iar &iarr;
52typedef int (*f)(int);
53
54void t_cc()
55{
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);
75}
76
77// ----------- static_cast -------------
78
79struct B : public A {}; // Single public base.
80struct C1 : public virtual B {}; // Single virtual base.
81struct C2 : public virtual B {};
82struct D : public C1, public C2 {}; // Diamond
83struct E : private A {}; // Single private base.
84struct F : public C1 {}; // Single path to B with virtual.
85struct G1 : public B {};
86struct G2 : public B {};
87struct H : public G1, public G2 {}; // Ambiguous path to B.
88
89enum Enum { En1, En2 };
90enum Onom { On1, On2 };
91
92struct Co1 { operator int(); };
93struct Co2 { Co2(int); };
94struct Co3 { };
95struct Co4 { Co4(Co3); operator Co3(); };
96
97// Explicit implicits
98void t_529_2()
99{
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);
114
115 int ar[1];
116 typedef const int *cintp;
117 (void)cintp(ar);
118 typedef void (*pfvv)();
119 (void)pfvv(t_529_2);
120
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));
130 typedef const B *cBp;
131 (void)cBp((C1*)0);
132 typedef B &Br;
133 (void)Br(*((C1*)0));
134 (void)Ap((D*)0);
135 typedef const A &cAr;
136 (void)cAr(*((D*)0));
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
143
144 (void)int(Co1());
145 (void)Co2(1);
146 (void)Co3((Co4)(Co3()));
147
148 // Bad code below
149 //(void)(A*)((H*)0); // {{static_cast from 'struct H *' to 'struct A *' is not allowed}}
150}
151
152// Anything to void
153void t_529_4()
154{
155 void(1);
156 (void(t_529_4));
157}
158
159// Static downcasts
160void t_529_5_8()
161{
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
176
177 // Bad code below
178
179 typedef C1 *C1p;
180 (void)C1p((A*)0); // expected-error {{cannot cast 'struct A *' to 'C1p' (aka 'struct C1 *') via virtual base 'struct B'}}
181 typedef C1 &C1r;
182 (void)C1r(*((A*)0)); // expected-error {{cannot cast 'struct A' to 'C1r' (aka 'struct C1 &') via virtual base 'struct B'}}
183 typedef D *Dp;
184 (void)Dp((A*)0); // expected-error {{cannot cast 'struct A *' to 'Dp' (aka 'struct D *') via virtual base 'struct B'}}
185 typedef D &Dr;
186 (void)Dr(*((A*)0)); // expected-error {{cannot cast 'struct A' to 'Dr' (aka 'struct D &') via virtual base 'struct B'}}
187 typedef H *Hp;
188 (void)Hp((A*)0); // expected-error {{ambiguous cast from base 'struct A' to derived 'struct H':\n struct A -> struct B -> struct G1 -> struct H\n struct A -> struct B -> struct G2 -> struct H}}
189 typedef H &Hr;
190 (void)Hr(*((A*)0)); // expected-error {{ambiguous cast from base 'struct A' to derived 'struct H':\n struct A -> struct B -> struct G1 -> struct H\n struct A -> struct B -> struct G2 -> struct H}}
191
192 // TODO: Test DR427. This requires user-defined conversions, though.
193}
194
195// Enum conversions
196void t_529_7()
197{
198 (void)Enum(1);
199 (void)Enum(1.0);
200 (void)Onom(En1);
201
202 // Bad code below
203
204 (void)Enum((int*)0); // expected-error {{functional-style cast from 'int *' to 'enum Enum' is not allowed}}
205}
206
207// Void pointer to object pointer
208void t_529_10()
209{
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
215}
216
217// Member pointer upcast.
218void t_529_9()
219{
220 typedef int A::*Amp;
221 (void)Amp((int B::*)0);
222
223 // Bad code below
224 (void)Amp((int H::*)0); // expected-error {{ambiguous conversion from pointer to member of derived class 'struct H'}}
225 (void)Amp((int F::*)0); // expected-error {{conversion from pointer to member of class 'struct F'}}
226}
227
228// -------- reinterpret_cast -----------
229
230enum test { testval = 1 };
231struct structure { int m; };
232typedef void (*fnptr)();
233
234// Test conversion between pointer and integral types, as in p3 and p4.
235void integral_conversion()
236{
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);
245}
246
247void pointer_conversion()
248{
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);
259}
260
261void constness()
262{
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);
274}
275
276void fnptrs()
277{
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);
284}
285
286void refs()
287{
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 &')}}
294}
295
296void memptrs()
297{
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
303
304 void (structure::*psf)() = 0;
305 typedef int (structure::*structureimfp)();
306 (void)structureimfp(psf);
307
308 typedef void (structure::*structurevmfp)();
309 (void)structurevmfp(psi); // expected-error {{functional-style cast from 'int const struct structure::*' to 'structurevmfp' (aka 'void (struct structure::*)()') is not allowed}}
310 (void)structureimp(psf); // expected-error {{functional-style cast from 'void (struct structure::*)()' to 'structureimp' (aka 'int struct structure::*') is not allowed}}
311}
312
313// ---------------- misc ------------------
314
315void crash_on_invalid_1()
316{
317 typedef itn Typo; // expected-error {{unknown type name 'itn'}}
318 (void)Typo(1); // used to crash
319}