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