• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 
3 // ------------ not interpreted as C-style cast ------------
4 
5 struct SimpleValueInit {
6   int i;
7 };
8 
9 struct InitViaConstructor {
10   InitViaConstructor(int i = 7);
11 };
12 
13 struct NoValueInit { // expected-note 2 {{candidate constructor (the implicit copy constructor)}}
14   NoValueInit(int i, int j); // expected-note 2 {{candidate constructor}}
15 };
16 
test_cxx_functional_value_init()17 void test_cxx_functional_value_init() {
18   (void)SimpleValueInit();
19   (void)InitViaConstructor();
20   (void)NoValueInit(); // expected-error{{no matching constructor for initialization}}
21 }
22 
test_cxx_function_cast_multi()23 void test_cxx_function_cast_multi() {
24   (void)NoValueInit(0, 0);
25   (void)NoValueInit(0, 0, 0); // expected-error{{no matching constructor for initialization}}
26   (void)int(1, 2); // expected-error{{excess elements in scalar initializer}}
27 }
28 
29 
30 // ------------------ everything else --------------------
31 
32 struct A {};
33 
34 // ----------- const_cast --------------
35 
36 typedef char c;
37 typedef c *cp;
38 typedef cp *cpp;
39 typedef cpp *cppp;
40 typedef cppp &cpppr;
41 typedef const cppp &cpppcr;
42 typedef const char cc;
43 typedef cc *ccp;
44 typedef volatile ccp ccvp;
45 typedef ccvp *ccvpp;
46 typedef const volatile ccvpp ccvpcvp;
47 typedef ccvpcvp *ccvpcvpp;
48 typedef int iar[100];
49 typedef iar &iarr;
50 typedef int (*f)(int);
51 
t_cc()52 void t_cc()
53 {
54   ccvpcvpp var = 0;
55   // Cast away deep consts and volatiles.
56   char ***var2 = cppp(var);
57   char ***const &var3 = var2;
58   // Const reference to reference.
59   char ***&var4 = cpppr(var3);
60   // Drop reference. Intentionally without qualifier change.
61   char *** var5 = cppp(var4);
62   const int ar[100] = {0};
63   // Array decay. Intentionally without qualifier change.
64   typedef int *intp;
65   int *pi = intp(ar);
66   f fp = 0;
67   // Don't misidentify fn** as a function pointer.
68   typedef f *fp_t;
69   f *fpp = fp_t(&fp);
70   int const A::* const A::*icapcap = 0;
71   typedef int A::* A::*iapap_t;
72   iapap_t iapap = iapap_t(icapcap);
73 }
74 
75 // ----------- static_cast -------------
76 
77 struct B : public A {};             // Single public base.
78 struct C1 : public virtual B {};    // Single virtual base.
79 struct C2 : public virtual B {};
80 struct D : public C1, public C2 {}; // Diamond
81 struct E : private A {};            // Single private base.
82 struct F : public C1 {};            // Single path to B with virtual.
83 struct G1 : public B {};
84 struct G2 : public B {};
85 struct H : public G1, public G2 {}; // Ambiguous path to B.
86 
87 enum Enum { En1, En2 };
88 enum Onom { On1, On2 };
89 
90 struct Co1 { operator int(); };
91 struct Co2 { Co2(int); };
92 struct Co3 { };
93 struct Co4 { Co4(Co3); operator Co3(); };
94 
95 // Explicit implicits
t_529_2()96 void t_529_2()
97 {
98   int i = 1;
99   (void)float(i);
100   double d = 1.0;
101   (void)float(d);
102   (void)int(d);
103   (void)char(i);
104   typedef unsigned long ulong;
105   (void)ulong(i);
106   (void)int(En1);
107   (void)double(En1);
108   typedef int &intr;
109   (void)intr(i);
110   typedef const int &cintr;
111   (void)cintr(i);
112 
113   int ar[1];
114   typedef const int *cintp;
115   (void)cintp(ar);
116   typedef void (*pfvv)();
117   (void)pfvv(t_529_2);
118 
119   typedef void *voidp;
120   (void)voidp(0);
121   (void)voidp((int*)0);
122   typedef volatile const void *vcvoidp;
123   (void)vcvoidp((const int*)0);
124   typedef A *Ap;
125   (void)Ap((B*)0);
126   typedef A &Ar;
127   (void)Ar(*((B*)0));
128   typedef const B *cBp;
129   (void)cBp((C1*)0);
130   typedef B &Br;
131   (void)Br(*((C1*)0));
132   (void)Ap((D*)0);
133   typedef const A &cAr;
134   (void)cAr(*((D*)0));
135   typedef int B::*Bmp;
136   (void)Bmp((int A::*)0);
137   typedef void (B::*Bmfp)();
138   (void)Bmfp((void (A::*)())0);
139   (void)Ap((E*)0); // functional-style cast ignores access control
140   (void)voidp((const int*)0); // const_cast appended
141 
142   (void)int(Co1());
143   (void)Co2(1);
144   (void)Co3((Co4)(Co3()));
145 
146   // Bad code below
147   //(void)(A*)((H*)0); // {{static_cast from 'struct H *' to 'struct A *' is not allowed}}
148 }
149 
150 // Anything to void
t_529_4()151 void t_529_4()
152 {
153   void(1);
154   (void(t_529_4));
155 }
156 
157 // Static downcasts
t_529_5_8()158 void t_529_5_8()
159 {
160   typedef B *Bp;
161   (void)Bp((A*)0);
162   typedef B &Br;
163   (void)Br(*((A*)0));
164   typedef const G1 *cG1p;
165   (void)cG1p((A*)0);
166   typedef const G1 &cG1r;
167   (void)cG1r(*((A*)0));
168   (void)Bp((const A*)0); // const_cast appended
169   (void)Br(*((const A*)0)); // const_cast appended
170   typedef E *Ep;
171   (void)Ep((A*)0); // access control ignored
172   typedef E &Er;
173   (void)Er(*((A*)0)); // access control ignored
174 
175   // Bad code below
176 
177   typedef C1 *C1p;
178   (void)C1p((A*)0); // expected-error {{cannot cast 'A *' to 'C1p' (aka 'C1 *') via virtual base 'B'}}
179   typedef C1 &C1r;
180   (void)C1r(*((A*)0)); // expected-error {{cannot cast 'A' to 'C1r' (aka 'C1 &') via virtual base 'B'}}
181   typedef D *Dp;
182   (void)Dp((A*)0); // expected-error {{cannot cast 'A *' to 'Dp' (aka 'D *') via virtual base 'B'}}
183   typedef D &Dr;
184   (void)Dr(*((A*)0)); // expected-error {{cannot cast 'A' to 'Dr' (aka 'D &') via virtual base 'B'}}
185   typedef H *Hp;
186   (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}}
187   typedef H &Hr;
188   (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}}
189 
190   // TODO: Test DR427. This requires user-defined conversions, though.
191 }
192 
193 // Enum conversions
t_529_7()194 void t_529_7()
195 {
196   (void)Enum(1);
197   (void)Enum(1.0);
198   (void)Onom(En1);
199 
200   // Bad code below
201 
202   (void)Enum((int*)0); // expected-error {{functional-style cast from 'int *' to 'Enum' is not allowed}}
203 }
204 
205 // Void pointer to object pointer
t_529_10()206 void t_529_10()
207 {
208   typedef int *intp;
209   (void)intp((void*)0);
210   typedef const A *cAp;
211   (void)cAp((void*)0);
212   (void)intp((const void*)0); // const_cast appended
213 }
214 
215 // Member pointer upcast.
t_529_9()216 void t_529_9()
217 {
218   typedef int A::*Amp;
219   (void)Amp((int B::*)0);
220 
221   // Bad code below
222   (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':}}
223   (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}}
224 }
225 
226 // -------- reinterpret_cast -----------
227 
228 enum test { testval = 1 };
229 struct structure { int m; };
230 typedef void (*fnptr)();
231 
232 // Test conversion between pointer and integral types, as in p3 and p4.
integral_conversion()233 void integral_conversion()
234 {
235   typedef void *voidp;
236   void *vp = voidp(testval);
237   long l = long(vp);
238   typedef float *floatp;
239   (void)floatp(l);
240   fnptr fnp = fnptr(l);
241   (void)char(fnp); // expected-error {{cast from pointer to smaller type 'char' loses information}}
242   (void)long(fnp);
243 }
244 
pointer_conversion()245 void pointer_conversion()
246 {
247   int *p1 = 0;
248   typedef float *floatp;
249   float *p2 = floatp(p1);
250   typedef structure *structurep;
251   structure *p3 = structurep(p2);
252   typedef int **ppint;
253   typedef ppint *pppint;
254   ppint *deep = pppint(p3);
255   typedef fnptr fnptrp;
256   (void)fnptrp(deep);
257 }
258 
constness()259 void constness()
260 {
261   int ***const ipppc = 0;
262   typedef int const *icp_t;
263   int const *icp = icp_t(ipppc);
264   typedef int *intp;
265   (void)intp(icp); // const_cast appended
266   typedef int const *const ** intcpcpp;
267   intcpcpp icpcpp = intcpcpp(ipppc); // const_cast appended
268   int *ip = intp(icpcpp);
269   (void)icp_t(ip);
270   typedef int const *const *const *intcpcpcp;
271   (void)intcpcpcp(ipppc);
272 }
273 
fnptrs()274 void fnptrs()
275 {
276   typedef int (*fnptr2)(int);
277   fnptr fp = 0;
278   (void)fnptr2(fp);
279   typedef void *voidp;
280   void *vp = voidp(fp);
281   (void)fnptr(vp);
282 }
283 
refs()284 void refs()
285 {
286   long l = 0;
287   typedef char &charr;
288   char &c = charr(l);
289   // Bad: from rvalue
290   typedef int &intr;
291   (void)intr(&c); // expected-error {{functional-style cast from rvalue to reference type 'intr' (aka 'int &')}}
292 }
293 
memptrs()294 void memptrs()
295 {
296   const int structure::*psi = 0;
297   typedef const float structure::*structurecfmp;
298   (void)structurecfmp(psi);
299   typedef int structure::*structureimp;
300   (void)structureimp(psi); // const_cast appended
301 
302   void (structure::*psf)() = 0;
303   typedef int (structure::*structureimfp)();
304   (void)structureimfp(psf);
305 
306   typedef void (structure::*structurevmfp)();
307   (void)structurevmfp(psi); // expected-error {{functional-style cast from 'const int structure::*' to 'structurevmfp' (aka 'void (structure::*)()') is not allowed}}
308   (void)structureimp(psf); // expected-error {{functional-style cast from 'void (structure::*)()' to 'structureimp' (aka 'int structure::*') is not allowed}}
309 }
310 
311 // ---------------- misc ------------------
312 
crash_on_invalid_1()313 void crash_on_invalid_1()
314 {
315   typedef itn Typo; // expected-error {{unknown type name 'itn'}}
316   (void)Typo(1); // used to crash
317 
318   typedef int &int_ref;
319   (void)int_ref(); // expected-error {{reference to type 'int' requires an initializer}}
320 }
321