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