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