functional-cast.cpp revision 7002f4c03c2d0544f4e8bea8d3a5636519081e35
1// RUN: %clang_cc1 -fsyntax-only -verify %s 2 3// ------------ not interpreted as C-style cast ------------ 4 5struct SimpleValueInit { 6 int i; 7}; 8 9struct InitViaConstructor { 10 InitViaConstructor(int i = 7); 11}; 12 13struct NoValueInit { // expected-note 2 {{candidate constructor (the implicit copy constructor)}} 14 NoValueInit(int i, int j); // expected-note 2 {{candidate constructor}} 15}; 16 17void test_cxx_functional_value_init() { 18 (void)SimpleValueInit(); 19 (void)InitViaConstructor(); 20 (void)NoValueInit(); // expected-error{{no matching constructor for initialization}} 21} 22 23void 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{{function-style cast to a builtin type can only take one argument}} 27} 28 29 30// ------------------ everything else -------------------- 31 32struct A {}; 33 34// ----------- const_cast -------------- 35 36typedef char c; 37typedef c *cp; 38typedef cp *cpp; 39typedef cpp *cppp; 40typedef cppp &cpppr; 41typedef const cppp &cpppcr; 42typedef const char cc; 43typedef cc *ccp; 44typedef volatile ccp ccvp; 45typedef ccvp *ccvpp; 46typedef const volatile ccvpp ccvpcvp; 47typedef ccvpcvp *ccvpcvpp; 48typedef int iar[100]; 49typedef iar &iarr; 50typedef int (*f)(int); 51 52void 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 77struct B : public A {}; // Single public base. 78struct C1 : public virtual B {}; // Single virtual base. 79struct C2 : public virtual B {}; 80struct D : public C1, public C2 {}; // Diamond 81struct E : private A {}; // Single private base. 82struct F : public C1 {}; // Single path to B with virtual. 83struct G1 : public B {}; 84struct G2 : public B {}; 85struct H : public G1, public G2 {}; // Ambiguous path to B. 86 87enum Enum { En1, En2 }; 88enum Onom { On1, On2 }; 89 90struct Co1 { operator int(); }; 91struct Co2 { Co2(int); }; 92struct Co3 { }; 93struct Co4 { Co4(Co3); operator Co3(); }; 94 95// Explicit implicits 96void 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 151void t_529_4() 152{ 153 void(1); 154 (void(t_529_4)); 155} 156 157// Static downcasts 158void 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 194void 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 206void 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. 216void 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 228enum test { testval = 1 }; 229struct structure { int m; }; 230typedef void (*fnptr)(); 231 232// Test conversion between pointer and integral types, as in p3 and p4. 233void 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 245void 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 259void 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 274void 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 284void 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 294void 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 'int const 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 313void crash_on_invalid_1() 314{ 315 typedef itn Typo; // expected-error {{unknown type name 'itn'}} 316 (void)Typo(1); // used to crash 317} 318