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