Home | History | Annotate | Download | only in SemaCXX
      1 // RUN: %clang_cc1 -fsyntax-only -verify %s
      2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
      3 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
      4 
      5 struct A {};
      6 
      7 // See if aliasing can confuse this baby.
      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 char ***good_const_cast_test(ccvpcvpp var)
     25 {
     26   // Cast away deep consts and volatiles.
     27   char ***var2 = const_cast<cppp>(var);
     28   char ***const &var3 = var2;
     29   // Const reference to reference.
     30   char ***&var4 = const_cast<cpppr>(var3);
     31   // Drop reference. Intentionally without qualifier change.
     32   char *** var5 = const_cast<cppp>(var4);
     33   // Const array to array reference.
     34   const int ar[100] = {0};
     35   int (&rar)[100] = const_cast<iarr>(ar);
     36   // Array decay. Intentionally without qualifier change.
     37   int *pi = const_cast<int*>(ar);
     38   f fp = 0;
     39   // Don't misidentify fn** as a function pointer.
     40   f *fpp = const_cast<f*>(&fp);
     41   int const A::* const A::*icapcap = 0;
     42   int A::* A::* iapap = const_cast<int A::* A::*>(icapcap);
     43   (void)const_cast<A&&>(A());
     44 #if __cplusplus <= 199711L // C++03 or earlier modes
     45   // expected-warning@-2 {{rvalue references are a C++11 extension}}
     46 #endif
     47   return var4;
     48 }
     49 
     50 short *bad_const_cast_test(char const *volatile *const volatile *var)
     51 {
     52   // Different pointer levels.
     53   char **var2 = const_cast<char**>(var); // expected-error {{const_cast from 'const char *volatile *const volatile *' to 'char **' is not allowed}}
     54   // Different final type.
     55   short ***var3 = const_cast<short***>(var); // expected-error {{const_cast from 'const char *volatile *const volatile *' to 'short ***' is not allowed}}
     56   // Rvalue to reference.
     57   char ***&var4 = const_cast<cpppr>(&var2); // expected-error {{const_cast from rvalue to reference type 'cpppr'}}
     58   // Non-pointer.
     59   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}}
     60   const int *ar[100] = {0};
     61   // Not even lenient g++ accepts this.
     62   int *(*rar)[100] = const_cast<int *(*)[100]>(&ar); // expected-error {{const_cast from 'const int *(*)[100]' to 'int *(*)[100]' is not allowed}}
     63   f fp1 = 0;
     64   // Function pointers.
     65   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}}
     66   void (A::*mfn)() = 0;
     67   (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}}
     68   (void)const_cast<int&&>(0); // expected-error {{const_cast from rvalue to reference type 'int &&'}}
     69 #if __cplusplus <= 199711L // C++03 or earlier modes
     70   // expected-warning@-2 {{rvalue references are a C++11 extension}}
     71 #endif
     72   return **var3;
     73 }
     74 
     75 template <typename T>
     76 char *PR21845() { return const_cast<char *>((void)T::x); } // expected-error {{const_cast from 'void' to 'char *' is not allowed}}
     77