brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.9 KiB · 89f1877 Raw
238 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -verify %s2// REQUIRES: LP643 4struct A {};5 6// ----------- const_cast --------------7 8typedef char c;9typedef c *cp;10typedef cp *cpp;11typedef cpp *cppp;12typedef cppp &cpppr;13typedef const cppp &cpppcr;14typedef const char cc;15typedef cc *ccp;16typedef volatile ccp ccvp;17typedef ccvp *ccvpp;18typedef const volatile ccvpp ccvpcvp;19typedef ccvpcvp *ccvpcvpp;20typedef int iar[100];21typedef iar &iarr;22typedef int (*f)(int);23 24void 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 46struct B : public A {};             // Single public base.47struct C1 : public virtual B {};    // Single virtual base.48struct C2 : public virtual B {};49struct D : public C1, public C2 {}; // Diamond50struct E : private A {};            // Single private base.51struct F : public C1 {};            // Single path to B with virtual.52struct G1 : public B {};53struct G2 : public B {};54struct H : public G1, public G2 {}; // Ambiguous path to B.55 56enum Enum { En1, En2 };57enum Onom { On1, On2 };58 59struct Co1 { operator int(); };60struct Co2 { Co2(int); };61struct Co3 { };62struct Co4 { Co4(Co3); operator Co3(); };63 64// Explicit implicits65void 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 control95  (void)(void*)((const int*)0); // const_cast appended96 97  (void)(int)(Co1());98  (void)(Co2)(1);99  (void)(Co3)((Co4)(Co3()));100 101  // Bad code below102  //(void)(A*)((H*)0); // {{static_cast from 'struct H *' to 'struct A *' is not allowed}}103}104 105// Anything to void106void t_529_4()107{108  (void)(1);109  (void)(t_529_4);110}111 112// Static downcasts113void 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 appended120  (void)(B&)(*((const A*)0)); // const_cast appended121  (void)(E*)((A*)0); // access control ignored122  (void)(E&)(*((A*)0)); // access control ignored123 124  // Bad code below125 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}}132 133  // TODO: Test DR427. This requires user-defined conversions, though.134}135 136// Enum conversions137void t_529_7()138{139  (void)(Enum)(1);140  (void)(Enum)(1.0);141  (void)(Onom)(En1);142 143  // Bad code below144 145  (void)(Enum)((int*)0); // expected-error {{C-style cast from 'int *' to 'Enum' is not allowed}}146}147 148// Void pointer to object pointer149void t_529_10()150{151  (void)(int*)((void*)0);152  (void)(const A*)((void*)0);153  (void)(int*)((const void*)0); // const_cast appended154}155 156// Member pointer upcast.157void t_529_9()158{159  (void)(int A::*)((int B::*)0);160 161  // Bad code below162  (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 168enum test { testval = 1 };169struct structure { int m; };170typedef void (*fnptr)();171 172// Test conversion between pointer and integral types, as in p3 and p4.173void 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  (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}}186}187 188void pointer_conversion()189{190  int *p1 = 0;191  float *p2 = (float*)(p1);192  structure *p3 = (structure*)(p2);193  typedef int **ppint;194  ppint *deep = (ppint*)(p3);195  (void)(fnptr*)(deep);196}197 198void constness()199{200  int ***const ipppc = 0;201  int const *icp = (int const*)(ipppc);202  (void)(int*)(icp); // const_cast appended203  int const *const **icpcpp = (int const* const**)(ipppc); // const_cast appended204  int *ip = (int*)(icpcpp);205  (void)(int const*)(ip);206  (void)(int const* const* const*)(ipppc);207}208 209void fnptrs()210{211  typedef int (*fnptr2)(int);212  fnptr fp = 0;213  (void)(fnptr2)(fp);214  void *vp = (void*)(fp);215  (void)(fnptr)(vp);216}217 218void refs()219{220  long l = 0;221  char &c = (char&)(l);222  // Bad: from rvalue223  (void)(int&)(&c); // expected-error {{C-style cast from rvalue to reference type 'int &'}}224}225 226void memptrs()227{228  const int structure::*psi = 0;229  (void)(const float structure::*)(psi);230  (void)(int structure::*)(psi); // const_cast appended231 232  void (structure::*psf)() = 0;233  (void)(int (structure::*)())(psf);234 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}}237}238