Home | History | Annotate | Download | only in SemaCXX
      1 // RUN: %clang_cc1 -fsyntax-only -verify %s
      2 // REQUIRES: LP64
      3 
      4 struct A {};
      5 
      6 // ----------- const_cast --------------
      7 
      8 typedef char c;
      9 typedef c *cp;
     10 typedef cp *cpp;
     11 typedef cpp *cppp;
     12 typedef cppp &cpppr;
     13 typedef const cppp &cpppcr;
     14 typedef const char cc;
     15 typedef cc *ccp;
     16 typedef volatile ccp ccvp;
     17 typedef ccvp *ccvpp;
     18 typedef const volatile ccvpp ccvpcvp;
     19 typedef ccvpcvp *ccvpcvpp;
     20 typedef int iar[100];
     21 typedef iar &iarr;
     22 typedef int (*f)(int);
     23 
     24 void t_cc()
     25 {
     26   ccvpcvpp var = 0;
     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.
     36   int *pi = (int*)(ar);
     37   f fp = 0;
     38   // Don't misidentify fn** as a function pointer.
     39   f *fpp = (f*)(&fp);
     40   int const A::* const A::*icapcap = 0;
     41   int A::* A::* iapap = (int A::* A::*)(icapcap);
     42 }
     43 
     44 // ----------- static_cast -------------
     45 
     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.
     55 
     56 enum Enum { En1, En2 };
     57 enum Onom { On1, On2 };
     58 
     59 struct Co1 { operator int(); };
     60 struct Co2 { Co2(int); };
     61 struct Co3 { };
     62 struct Co4 { Co4(Co3); operator Co3(); };
     63 
     64 // Explicit implicits
     65 void t_529_2()
     66 {
     67   int i = 1;
     68   (void)(float)(i);
     69   double d = 1.0;
     70   (void)(float)(d);
     71   (void)(int)(d);
     72   (void)(char)(i);
     73   (void)(unsigned long)(i);
     74   (void)(int)(En1);
     75   (void)(double)(En1);
     76   (void)(int&)(i);
     77   (void)(const int&)(i);
     78 
     79   int ar[1];
     80   (void)(const int*)(ar);
     81   (void)(void (*)())(t_529_2);
     82 
     83   (void)(void*)(0);
     84   (void)(void*)((int*)0);
     85   (void)(volatile const void*)((const int*)0);
     86   (void)(A*)((B*)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}}
     90   (void)(A*)((D*)0);
     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
     96 
     97   (void)(int)(Co1());
     98   (void)(Co2)(1);
     99   (void)(Co3)((Co4)(Co3()));
    100 
    101   // Bad code below
    102   //(void)(A*)((H*)0); // {{static_cast from 'struct H *' to 'struct A *' is not allowed}}
    103 }
    104 
    105 // Anything to void
    106 void t_529_4()
    107 {
    108   (void)(1);
    109   (void)(t_529_4);
    110 }
    111 
    112 // Static downcasts
    113 void t_529_5_8()
    114 {
    115   (void)(B*)((A*)0);
    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
    123 
    124   // Bad code below
    125 
    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    struct A -> struct B -> struct G1 -> struct H\n    struct A -> struct B -> struct G2 -> struct H}}
    131   (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}}
    132 
    133   // TODO: Test DR427. This requires user-defined conversions, though.
    134 }
    135 
    136 // Enum conversions
    137 void t_529_7()
    138 {
    139   (void)(Enum)(1);
    140   (void)(Enum)(1.0);
    141   (void)(Onom)(En1);
    142 
    143   // Bad code below
    144 
    145   (void)(Enum)((int*)0); // expected-error {{C-style cast from 'int *' to 'Enum' is not allowed}}
    146 }
    147 
    148 // Void pointer to object pointer
    149 void t_529_10()
    150 {
    151   (void)(int*)((void*)0);
    152   (void)(const A*)((void*)0);
    153   (void)(int*)((const void*)0); // const_cast appended
    154 }
    155 
    156 // Member pointer upcast.
    157 void t_529_9()
    158 {
    159   (void)(int A::*)((int B::*)0);
    160 
    161   // Bad code below
    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}}
    164 }
    165 
    166 // -------- reinterpret_cast -----------
    167 
    168 enum test { testval = 1 };
    169 struct structure { int m; };
    170 typedef void (*fnptr)();
    171 
    172 // Test conversion between pointer and integral types, as in p3 and p4.
    173 void integral_conversion()
    174 {
    175   void *vp = (void*)(testval);
    176   long l = (long)(vp);
    177   (void)(float*)(l);
    178   fnptr fnp = (fnptr)(l);
    179   (void)(char)(fnp); // expected-error {{cast from pointer to smaller type 'char' loses information}}
    180   (void)(long)(fnp);
    181 }
    182 
    183 void pointer_conversion()
    184 {
    185   int *p1 = 0;
    186   float *p2 = (float*)(p1);
    187   structure *p3 = (structure*)(p2);
    188   typedef int **ppint;
    189   ppint *deep = (ppint*)(p3);
    190   (void)(fnptr*)(deep);
    191 }
    192 
    193 void constness()
    194 {
    195   int ***const ipppc = 0;
    196   int const *icp = (int const*)(ipppc);
    197   (void)(int*)(icp); // const_cast appended
    198   int const *const **icpcpp = (int const* const**)(ipppc); // const_cast appended
    199   int *ip = (int*)(icpcpp);
    200   (void)(int const*)(ip);
    201   (void)(int const* const* const*)(ipppc);
    202 }
    203 
    204 void fnptrs()
    205 {
    206   typedef int (*fnptr2)(int);
    207   fnptr fp = 0;
    208   (void)(fnptr2)(fp);
    209   void *vp = (void*)(fp);
    210   (void)(fnptr)(vp);
    211 }
    212 
    213 void refs()
    214 {
    215   long l = 0;
    216   char &c = (char&)(l);
    217   // Bad: from rvalue
    218   (void)(int&)(&c); // expected-error {{C-style cast from rvalue to reference type 'int &'}}
    219 }
    220 
    221 void memptrs()
    222 {
    223   const int structure::*psi = 0;
    224   (void)(const float structure::*)(psi);
    225   (void)(int structure::*)(psi); // const_cast appended
    226 
    227   void (structure::*psf)() = 0;
    228   (void)(int (structure::*)())(psf);
    229 
    230   (void)(void (structure::*)())(psi); // expected-error-re {{C-style cast from 'const int structure::*' to 'void (structure::*)(){{( __attribute__\(\(thiscall\)\))?}}' is not allowed}}
    231   (void)(int structure::*)(psf); // expected-error-re {{C-style cast from 'void (structure::*)(){{( __attribute__\(\(thiscall\)\))?}}' to 'int structure::*' is not allowed}}
    232 }
    233