diff options
author | Zancanaro; Carlo <czan8762@plang3.cs.usyd.edu.au> | 2012-09-24 09:58:17 +1000 |
---|---|---|
committer | Zancanaro; Carlo <czan8762@plang3.cs.usyd.edu.au> | 2012-09-24 09:58:17 +1000 |
commit | 222e2a7620e6520ffaf4fc4e69d79c18da31542e (patch) | |
tree | 7bfbc05bfa3b41c8f9d2e56d53a0bc3e310df239 /clang/test/SemaCXX/const-cast.cpp | |
parent | 3d206f03985b50beacae843d880bccdc91a9f424 (diff) |
Add the clang library to the repo (with some of my changes, too).
Diffstat (limited to 'clang/test/SemaCXX/const-cast.cpp')
-rw-r--r-- | clang/test/SemaCXX/const-cast.cpp | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/clang/test/SemaCXX/const-cast.cpp b/clang/test/SemaCXX/const-cast.cpp new file mode 100644 index 0000000..62851f8 --- /dev/null +++ b/clang/test/SemaCXX/const-cast.cpp @@ -0,0 +1,64 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +struct A {}; + +// See if aliasing can confuse this baby. +typedef char c; +typedef c *cp; +typedef cp *cpp; +typedef cpp *cppp; +typedef cppp &cpppr; +typedef const cppp &cpppcr; +typedef const char cc; +typedef cc *ccp; +typedef volatile ccp ccvp; +typedef ccvp *ccvpp; +typedef const volatile ccvpp ccvpcvp; +typedef ccvpcvp *ccvpcvpp; +typedef int iar[100]; +typedef iar &iarr; +typedef int (*f)(int); + +char ***good_const_cast_test(ccvpcvpp var) +{ + // Cast away deep consts and volatiles. + char ***var2 = const_cast<cppp>(var); + char ***const &var3 = var2; + // Const reference to reference. + char ***&var4 = const_cast<cpppr>(var3); + // Drop reference. Intentionally without qualifier change. + char *** var5 = const_cast<cppp>(var4); + // Const array to array reference. + const int ar[100] = {0}; + int (&rar)[100] = const_cast<iarr>(ar); + // Array decay. Intentionally without qualifier change. + int *pi = const_cast<int*>(ar); + f fp = 0; + // Don't misidentify fn** as a function pointer. + f *fpp = const_cast<f*>(&fp); + int const A::* const A::*icapcap = 0; + int A::* A::* iapap = const_cast<int A::* A::*>(icapcap); + + return var4; +} + +short *bad_const_cast_test(char const *volatile *const volatile *var) +{ + // Different pointer levels. + char **var2 = const_cast<char**>(var); // expected-error {{const_cast from 'const char *volatile *const volatile *' to 'char **' is not allowed}} + // Different final type. + short ***var3 = const_cast<short***>(var); // expected-error {{const_cast from 'const char *volatile *const volatile *' to 'short ***' is not allowed}} + // Rvalue to reference. + char ***&var4 = const_cast<cpppr>(&var2); // expected-error {{const_cast from rvalue to reference type 'cpppr'}} + // Non-pointer. + 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}} + const int *ar[100] = {0}; + // Not even lenient g++ accepts this. + int *(*rar)[100] = const_cast<int *(*)[100]>(&ar); // expected-error {{const_cast from 'const int *(*)[100]' to 'int *(*)[100]' is not allowed}} + f fp1 = 0; + // Function pointers. + 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}} + void (A::*mfn)() = 0; + (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}} + return **var3; +} |