1 // RUN: %clang_cc1 -fsyntax-only -verify %s
6 // ----------- const_cast --------------
13 typedef const cppp
&cpppcr
;
14 typedef const char cc
;
16 typedef volatile ccp ccvp
;
18 typedef const volatile ccvpp ccvpcvp
;
19 typedef ccvpcvp
*ccvpcvpp
;
22 typedef int (*f
)(int);
27 // Cast away deep consts and volatiles.
28 char ***var2
= (cppp
)(var
);
29 char ***const &var3
= var2
;
30 // Const reference to reference.
31 char ***&var4
= (cpppr
)(var3
);
32 // Drop reference. Intentionally without qualifier change.
33 char *** var5
= (cppp
)(var4
);
34 const int ar
[100] = {0};
35 // Array decay. Intentionally without qualifier change.
38 // Don't misidentify fn** as a function pointer.
40 int const A::* const A::*icapcap
= 0;
41 int A::* A::* iapap
= (int A::* A::*)(icapcap
);
44 // ----------- static_cast -------------
46 struct B
: public A
{}; // Single public base.
47 struct C1
: public virtual B
{}; // Single virtual base.
48 struct C2
: public virtual B
{};
49 struct D
: public C1
, public C2
{}; // Diamond
50 struct E
: private A
{}; // Single private base.
51 struct F
: public C1
{}; // Single path to B with virtual.
52 struct G1
: public B
{};
53 struct G2
: public B
{};
54 struct H
: public G1
, public G2
{}; // Ambiguous path to B.
56 enum Enum
{ En1
, En2
};
57 enum Onom
{ On1
, On2
};
59 struct Co1
{ operator int(); };
60 struct Co2
{ Co2(int); };
62 struct Co4
{ Co4(Co3
); operator Co3(); };
73 (void)(unsigned long)(i
);
77 (void)(const int&)(i
);
80 (void)(const int*)(ar
);
81 (void)(void (*)())(t_529_2
);
84 (void)(void*)((int*)0);
85 (void)(volatile const void*)((const int*)0);
87 (void)(A
&)(*((B
*)0)); // expected-warning {{binding dereferenced null pointer to reference has undefined behavior}}
88 (void)(const B
*)((C1
*)0);
89 (void)(B
&)(*((C1
*)0)); // expected-warning {{binding dereferenced null pointer to reference has undefined behavior}}
91 (void)(const A
&)(*((D
*)0)); // expected-warning {{binding dereferenced null pointer to reference has undefined behavior}}
92 (void)(int B::*)((int A::*)0);
93 (void)(void (B::*)())((void (A::*)())0);
94 (void)(A
*)((E
*)0); // C-style cast ignores access control
95 (void)(void*)((const int*)0); // const_cast appended
99 (void)(Co3
)((Co4
)(Co3()));
102 //(void)(A*)((H*)0); // {{static_cast from 'struct H *' to 'struct A *' is not allowed}}
116 (void)(B
&)(*((A
*)0));
117 (void)(const G1
*)((A
*)0);
118 (void)(const G1
&)(*((A
*)0));
119 (void)(B
*)((const A
*)0); // const_cast appended
120 (void)(B
&)(*((const A
*)0)); // const_cast appended
121 (void)(E
*)((A
*)0); // access control ignored
122 (void)(E
&)(*((A
*)0)); // access control ignored
126 (void)(C1
*)((A
*)0); // expected-error {{cannot cast 'A *' to 'C1 *' via virtual base 'B'}}
127 (void)(C1
&)(*((A
*)0)); // expected-error {{cannot cast 'A' to 'C1 &' via virtual base 'B'}}
128 (void)(D
*)((A
*)0); // expected-error {{cannot cast 'A *' to 'D *' via virtual base 'B'}}
129 (void)(D
&)(*((A
*)0)); // expected-error {{cannot cast 'A' to 'D &' via virtual base 'B'}}
130 (void)(H
*)((A
*)0); // expected-error {{ambiguous cast from base 'A' to derived 'H':\n A -> B -> G1 -> struct H\n A -> B -> G2 -> struct H}}
131 (void)(H
&)(*((A
*)0)); // expected-error {{ambiguous cast from base 'A' to derived 'H':\n A -> B -> G1 -> struct H\n A -> B -> G2 -> struct H}}
133 // TODO: Test DR427. This requires user-defined conversions, though.
145 (void)(Enum
)((int*)0); // expected-error {{C-style cast from 'int *' to 'Enum' is not allowed}}
148 // Void pointer to object pointer
151 (void)(int*)((void*)0);
152 (void)(const A
*)((void*)0);
153 (void)(int*)((const void*)0); // const_cast appended
156 // Member pointer upcast.
159 (void)(int A::*)((int B::*)0);
162 (void)(int A::*)((int H::*)0); // expected-error {{ambiguous conversion from pointer to member of derived class 'H' to pointer to member of base class 'A':}}
163 (void)(int A::*)((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}}
166 // -------- reinterpret_cast -----------
168 enum test
{ testval
= 1 };
169 struct structure
{ int m
; };
170 typedef void (*fnptr
)();
172 // Test conversion between pointer and integral types, as in p3 and p4.
173 void integral_conversion()
175 void *vp
= (void*)(testval
);
178 fnptr fnp
= (fnptr
)(l
);
179 (void)(char)(fnp
); // expected-error {{cast from pointer to smaller type 'char' loses information}}
182 (void)(bool)((void*)0);
183 (void)(bool)((int*)0);
184 (void)(char)((void*)0); // expected-error {{cast from pointer to smaller type 'char' loses information}}
185 (void)(char)((int*)0); // expected-error {{cast from pointer to smaller type 'char' loses information}}
188 void pointer_conversion()
191 float *p2
= (float*)(p1
);
192 structure
*p3
= (structure
*)(p2
);
194 ppint
*deep
= (ppint
*)(p3
);
195 (void)(fnptr
*)(deep
);
200 int ***const ipppc
= 0;
201 int const *icp
= (int const*)(ipppc
);
202 (void)(int*)(icp
); // const_cast appended
203 int const *const **icpcpp
= (int const* const**)(ipppc
); // const_cast appended
204 int *ip
= (int*)(icpcpp
);
205 (void)(int const*)(ip
);
206 (void)(int const* const* const*)(ipppc
);
211 typedef int (*fnptr2
)(int);
214 void *vp
= (void*)(fp
);
221 char &c
= (char&)(l
);
223 (void)(int&)(&c
); // expected-error {{C-style cast from rvalue to reference type 'int &'}}
228 const int structure::*psi
= 0;
229 (void)(const float structure::*)(psi
);
230 (void)(int structure::*)(psi
); // const_cast appended
232 void (structure::*psf
)() = 0;
233 (void)(int (structure::*)())(psf
);
235 (void)(void (structure::*)())(psi
); // expected-error-re {{C-style cast from 'const int structure::*' to 'void (structure::*)(){{( __attribute__\(\(thiscall\)\))?}}' is not allowed}}
236 (void)(int structure::*)(psf
); // expected-error-re {{C-style cast from 'void (structure::*)(){{( __attribute__\(\(thiscall\)\))?}}' to 'int structure::*' is not allowed}}