John McCall | 7002f4c | 2010-04-09 19:03:51 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2 | |
| 3 | struct A {}; |
| 4 | |
| 5 | // ----------- const_cast -------------- |
| 6 | |
| 7 | typedef char c; |
| 8 | typedef c *cp; |
| 9 | typedef cp *cpp; |
| 10 | typedef cpp *cppp; |
| 11 | typedef cppp &cpppr; |
| 12 | typedef const cppp &cpppcr; |
| 13 | typedef const char cc; |
| 14 | typedef cc *ccp; |
| 15 | typedef volatile ccp ccvp; |
| 16 | typedef ccvp *ccvpp; |
| 17 | typedef const volatile ccvpp ccvpcvp; |
| 18 | typedef ccvpcvp *ccvpcvpp; |
| 19 | typedef int iar[100]; |
| 20 | typedef iar &iarr; |
| 21 | typedef int (*f)(int); |
| 22 | |
| 23 | void t_cc() |
| 24 | { |
| 25 | ccvpcvpp var = 0; |
| 26 | // Cast away deep consts and volatiles. |
| 27 | char ***var2 = (cppp)(var); |
| 28 | char ***const &var3 = var2; |
| 29 | // Const reference to reference. |
| 30 | char ***&var4 = (cpppr)(var3); |
| 31 | // Drop reference. Intentionally without qualifier change. |
| 32 | char *** var5 = (cppp)(var4); |
| 33 | const int ar[100] = {0}; |
| 34 | // Array decay. Intentionally without qualifier change. |
| 35 | int *pi = (int*)(ar); |
| 36 | f fp = 0; |
| 37 | // Don't misidentify fn** as a function pointer. |
| 38 | f *fpp = (f*)(&fp); |
| 39 | int const A::* const A::*icapcap = 0; |
| 40 | int A::* A::* iapap = (int A::* A::*)(icapcap); |
| 41 | } |
| 42 | |
| 43 | // ----------- static_cast ------------- |
| 44 | |
| 45 | struct B : public A {}; // Single public base. |
| 46 | struct C1 : public virtual B {}; // Single virtual base. |
| 47 | struct C2 : public virtual B {}; |
| 48 | struct D : public C1, public C2 {}; // Diamond |
| 49 | struct E : private A {}; // Single private base. |
| 50 | struct F : public C1 {}; // Single path to B with virtual. |
| 51 | struct G1 : public B {}; |
| 52 | struct G2 : public B {}; |
| 53 | struct H : public G1, public G2 {}; // Ambiguous path to B. |
| 54 | |
| 55 | enum Enum { En1, En2 }; |
| 56 | enum Onom { On1, On2 }; |
| 57 | |
| 58 | struct Co1 { operator int(); }; |
| 59 | struct Co2 { Co2(int); }; |
| 60 | struct Co3 { }; |
| 61 | struct Co4 { Co4(Co3); operator Co3(); }; |
| 62 | |
| 63 | // Explicit implicits |
| 64 | void t_529_2() |
| 65 | { |
| 66 | int i = 1; |
| 67 | (void)(float)(i); |
| 68 | double d = 1.0; |
| 69 | (void)(float)(d); |
| 70 | (void)(int)(d); |
| 71 | (void)(char)(i); |
| 72 | (void)(unsigned long)(i); |
| 73 | (void)(int)(En1); |
| 74 | (void)(double)(En1); |
| 75 | (void)(int&)(i); |
| 76 | (void)(const int&)(i); |
| 77 | |
| 78 | int ar[1]; |
| 79 | (void)(const int*)(ar); |
| 80 | (void)(void (*)())(t_529_2); |
| 81 | |
| 82 | (void)(void*)(0); |
| 83 | (void)(void*)((int*)0); |
| 84 | (void)(volatile const void*)((const int*)0); |
| 85 | (void)(A*)((B*)0); |
| 86 | (void)(A&)(*((B*)0)); |
| 87 | (void)(const B*)((C1*)0); |
| 88 | (void)(B&)(*((C1*)0)); |
| 89 | (void)(A*)((D*)0); |
| 90 | (void)(const A&)(*((D*)0)); |
| 91 | (void)(int B::*)((int A::*)0); |
| 92 | (void)(void (B::*)())((void (A::*)())0); |
| 93 | (void)(A*)((E*)0); // C-style cast ignores access control |
| 94 | (void)(void*)((const int*)0); // const_cast appended |
| 95 | |
| 96 | (void)(int)(Co1()); |
| 97 | (void)(Co2)(1); |
| 98 | (void)(Co3)((Co4)(Co3())); |
| 99 | |
| 100 | // Bad code below |
| 101 | //(void)(A*)((H*)0); // {{static_cast from 'struct H *' to 'struct A *' is not allowed}} |
| 102 | } |
| 103 | |
| 104 | // Anything to void |
| 105 | void t_529_4() |
| 106 | { |
| 107 | (void)(1); |
| 108 | (void)(t_529_4); |
| 109 | } |
| 110 | |
| 111 | // Static downcasts |
| 112 | void t_529_5_8() |
| 113 | { |
| 114 | (void)(B*)((A*)0); |
| 115 | (void)(B&)(*((A*)0)); |
| 116 | (void)(const G1*)((A*)0); |
| 117 | (void)(const G1&)(*((A*)0)); |
| 118 | (void)(B*)((const A*)0); // const_cast appended |
| 119 | (void)(B&)(*((const A*)0)); // const_cast appended |
| 120 | (void)(E*)((A*)0); // access control ignored |
| 121 | (void)(E&)(*((A*)0)); // access control ignored |
| 122 | |
| 123 | // Bad code below |
| 124 | |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 125 | (void)(C1*)((A*)0); // expected-error {{cannot cast 'A *' to 'C1 *' via virtual base 'B'}} |
| 126 | (void)(C1&)(*((A*)0)); // expected-error {{cannot cast 'A' to 'C1 &' via virtual base 'B'}} |
| 127 | (void)(D*)((A*)0); // expected-error {{cannot cast 'A *' to 'D *' via virtual base 'B'}} |
| 128 | (void)(D&)(*((A*)0)); // expected-error {{cannot cast 'A' to 'D &' via virtual base 'B'}} |
| 129 | (void)(H*)((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}} |
| 130 | (void)(H&)(*((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 | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 131 | |
| 132 | // TODO: Test DR427. This requires user-defined conversions, though. |
| 133 | } |
| 134 | |
| 135 | // Enum conversions |
| 136 | void t_529_7() |
| 137 | { |
| 138 | (void)(Enum)(1); |
| 139 | (void)(Enum)(1.0); |
| 140 | (void)(Onom)(En1); |
| 141 | |
| 142 | // Bad code below |
| 143 | |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 144 | (void)(Enum)((int*)0); // expected-error {{C-style cast from 'int *' to 'Enum' is not allowed}} |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // Void pointer to object pointer |
| 148 | void t_529_10() |
| 149 | { |
| 150 | (void)(int*)((void*)0); |
| 151 | (void)(const A*)((void*)0); |
| 152 | (void)(int*)((const void*)0); // const_cast appended |
| 153 | } |
| 154 | |
| 155 | // Member pointer upcast. |
| 156 | void t_529_9() |
| 157 | { |
| 158 | (void)(int A::*)((int B::*)0); |
| 159 | |
| 160 | // Bad code below |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 161 | (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':}} |
| 162 | (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}} |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // -------- reinterpret_cast ----------- |
| 166 | |
| 167 | enum test { testval = 1 }; |
| 168 | struct structure { int m; }; |
| 169 | typedef void (*fnptr)(); |
| 170 | |
| 171 | // Test conversion between pointer and integral types, as in p3 and p4. |
| 172 | void integral_conversion() |
| 173 | { |
| 174 | void *vp = (void*)(testval); |
| 175 | long l = (long)(vp); |
| 176 | (void)(float*)(l); |
| 177 | fnptr fnp = (fnptr)(l); |
| 178 | (void)(char)(fnp); // expected-error {{cast from pointer to smaller type 'char' loses information}} |
| 179 | (void)(long)(fnp); |
| 180 | } |
| 181 | |
| 182 | void pointer_conversion() |
| 183 | { |
| 184 | int *p1 = 0; |
| 185 | float *p2 = (float*)(p1); |
| 186 | structure *p3 = (structure*)(p2); |
| 187 | typedef int **ppint; |
| 188 | ppint *deep = (ppint*)(p3); |
| 189 | (void)(fnptr*)(deep); |
| 190 | } |
| 191 | |
| 192 | void constness() |
| 193 | { |
| 194 | int ***const ipppc = 0; |
| 195 | int const *icp = (int const*)(ipppc); |
| 196 | (void)(int*)(icp); // const_cast appended |
| 197 | int const *const **icpcpp = (int const* const**)(ipppc); // const_cast appended |
| 198 | int *ip = (int*)(icpcpp); |
| 199 | (void)(int const*)(ip); |
| 200 | (void)(int const* const* const*)(ipppc); |
| 201 | } |
| 202 | |
| 203 | void fnptrs() |
| 204 | { |
| 205 | typedef int (*fnptr2)(int); |
| 206 | fnptr fp = 0; |
| 207 | (void)(fnptr2)(fp); |
| 208 | void *vp = (void*)(fp); |
| 209 | (void)(fnptr)(vp); |
| 210 | } |
| 211 | |
| 212 | void refs() |
| 213 | { |
| 214 | long l = 0; |
| 215 | char &c = (char&)(l); |
| 216 | // Bad: from rvalue |
| 217 | (void)(int&)(&c); // expected-error {{C-style cast from rvalue to reference type 'int &'}} |
| 218 | } |
| 219 | |
| 220 | void memptrs() |
| 221 | { |
| 222 | const int structure::*psi = 0; |
| 223 | (void)(const float structure::*)(psi); |
| 224 | (void)(int structure::*)(psi); // const_cast appended |
| 225 | |
| 226 | void (structure::*psf)() = 0; |
| 227 | (void)(int (structure::*)())(psf); |
| 228 | |
Chris Lattner | 58f9e13 | 2010-09-05 00:04:01 +0000 | [diff] [blame] | 229 | (void)(void (structure::*)())(psi); // expected-error {{C-style cast from 'const int structure::*' to 'void (structure::*)()' is not allowed}} |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 230 | (void)(int structure::*)(psf); // expected-error {{C-style cast from 'void (structure::*)()' to 'int structure::*' is not allowed}} |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 231 | } |