83 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -verify %s2// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s3// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s4 5struct A {};6 7// See if aliasing can confuse this baby.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 24char ***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 modes45 // expected-warning@-2 {{rvalue references are a C++11 extension}}46#endif47 return var4;48}49 50short *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 extern const int *aub[];62 // const_cast looks through arrays as of DR330.63 (void) const_cast<int *(*)[100]>(&ar); // ok64 (void) const_cast<int *(*)[]>(&aub); // ok65 // ... but the array bound must exactly match.66 (void) const_cast<int *(*)[]>(&ar); // expected-error {{const_cast from 'const int *(*)[100]' to 'int *(*)[]' is not allowed}}67 (void) const_cast<int *(*)[99]>(&ar); // expected-error {{const_cast from 'const int *(*)[100]' to 'int *(*)[99]' is not allowed}}68 (void) const_cast<int *(*)[100]>(&aub); // expected-error {{const_cast from 'const int *(*)[]' to 'int *(*)[100]' is not allowed}}69 f fp1 = 0;70 // Function pointers.71 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}}72 void (A::*mfn)() = 0;73 (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}}74 (void)const_cast<int&&>(0); // expected-error {{const_cast from rvalue to reference type 'int &&'}}75#if __cplusplus <= 199711L // C++03 or earlier modes76 // expected-warning@-2 {{rvalue references are a C++11 extension}}77#endif78 return **var3;79}80 81template <typename T>82char *PR21845() { return const_cast<char *>((void)T::x); } // expected-error {{const_cast from 'void' to 'char *' is not allowed}}83