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