Home | History | Annotate | Download | only in SemaCXX
      1 // RUN: %clang_cc1 -fsyntax-only -verify %s
      2 
      3 struct A {};
      4 
      5 // See if aliasing can confuse this baby.
      6 typedef char c;
      7 typedef c *cp;
      8 typedef cp *cpp;
      9 typedef cpp *cppp;
     10 typedef cppp &cpppr;
     11 typedef const cppp &cpppcr;
     12 typedef const char cc;
     13 typedef cc *ccp;
     14 typedef volatile ccp ccvp;
     15 typedef ccvp *ccvpp;
     16 typedef const volatile ccvpp ccvpcvp;
     17 typedef ccvpcvp *ccvpcvpp;
     18 typedef int iar[100];
     19 typedef iar &iarr;
     20 typedef int (*f)(int);
     21 
     22 char ***good_const_cast_test(ccvpcvpp var)
     23 {
     24   // Cast away deep consts and volatiles.
     25   char ***var2 = const_cast<cppp>(var);
     26   char ***const &var3 = var2;
     27   // Const reference to reference.
     28   char ***&var4 = const_cast<cpppr>(var3);
     29   // Drop reference. Intentionally without qualifier change.
     30   char *** var5 = const_cast<cppp>(var4);
     31   // Const array to array reference.
     32   const int ar[100] = {0};
     33   int (&rar)[100] = const_cast<iarr>(ar);
     34   // Array decay. Intentionally without qualifier change.
     35   int *pi = const_cast<int*>(ar);
     36   f fp = 0;
     37   // Don't misidentify fn** as a function pointer.
     38   f *fpp = const_cast<f*>(&fp);
     39   int const A::* const A::*icapcap = 0;
     40   int A::* A::* iapap = const_cast<int A::* A::*>(icapcap);
     41   (void)const_cast<A&&>(A()); // expected-warning {{C++11}}
     42 
     43   return var4;
     44 }
     45 
     46 short *bad_const_cast_test(char const *volatile *const volatile *var)
     47 {
     48   // Different pointer levels.
     49   char **var2 = const_cast<char**>(var); // expected-error {{const_cast from 'const char *volatile *const volatile *' to 'char **' is not allowed}}
     50   // Different final type.
     51   short ***var3 = const_cast<short***>(var); // expected-error {{const_cast from 'const char *volatile *const volatile *' to 'short ***' is not allowed}}
     52   // Rvalue to reference.
     53   char ***&var4 = const_cast<cpppr>(&var2); // expected-error {{const_cast from rvalue to reference type 'cpppr'}}
     54   // Non-pointer.
     55   char v = const_cast<char>(**var2); // expected-error {{const_cast to 'char', which is not a reference, pointer-to-object, or pointer-to-data-member}}
     56   const int *ar[100] = {0};
     57   // Not even lenient g++ accepts this.
     58   int *(*rar)[100] = const_cast<int *(*)[100]>(&ar); // expected-error {{const_cast from 'const int *(*)[100]' to 'int *(*)[100]' is not allowed}}
     59   f fp1 = 0;
     60   // Function pointers.
     61   f fp2 = const_cast<f>(fp1); // expected-error {{const_cast to 'f' (aka 'int (*)(int)'), which is not a reference, pointer-to-object, or pointer-to-data-member}}
     62   void (A::*mfn)() = 0;
     63   (void)const_cast<void (A::*)()>(mfn); // expected-error-re {{const_cast to 'void (A::*)(){{( __attribute__\(\(thiscall\)\))?}}', which is not a reference, pointer-to-object, or pointer-to-data-member}}
     64   (void)const_cast<int&&>(0); // expected-error {{const_cast from rvalue to reference type 'int &&'}} expected-warning {{C++11}}
     65   return **var3;
     66 }
     67