Daniel Dunbar | 8fbe78f | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Charles Li | 542f04c | 2015-11-11 19:34:47 +0000 | [diff] [blame] | 2 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s |
| 3 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s |
Douglas Gregor | 08d918a | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 4 | |
Sebastian Redl | b44ab5f | 2009-01-26 22:19:12 +0000 | [diff] [blame] | 5 | struct A {}; |
| 6 | |
Douglas Gregor | 08d918a | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 7 | // See if aliasing can confuse this baby. |
| 8 | typedef char c; |
| 9 | typedef c *cp; |
| 10 | typedef cp *cpp; |
| 11 | typedef cpp *cppp; |
| 12 | typedef cppp &cpppr; |
| 13 | typedef const cppp &cpppcr; |
| 14 | typedef const char cc; |
| 15 | typedef cc *ccp; |
| 16 | typedef volatile ccp ccvp; |
| 17 | typedef ccvp *ccvpp; |
| 18 | typedef const volatile ccvpp ccvpcvp; |
| 19 | typedef ccvpcvp *ccvpcvpp; |
| 20 | typedef int iar[100]; |
| 21 | typedef iar &iarr; |
| 22 | typedef int (*f)(int); |
| 23 | |
| 24 | char ***good_const_cast_test(ccvpcvpp var) |
| 25 | { |
| 26 | // Cast away deep consts and volatiles. |
| 27 | char ***var2 = const_cast<cppp>(var); |
Sebastian Redl | 5c00982 | 2008-10-29 19:45:21 +0000 | [diff] [blame] | 28 | char ***const &var3 = var2; |
Douglas Gregor | 08d918a | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 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); |
Chandler Carruth | 585fb1e | 2009-12-29 08:05:19 +0000 | [diff] [blame] | 33 | // Const array to array reference. |
Douglas Gregor | 08d918a | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 34 | const int ar[100] = {0}; |
Chandler Carruth | 585fb1e | 2009-12-29 08:05:19 +0000 | [diff] [blame] | 35 | int (&rar)[100] = const_cast<iarr>(ar); |
Douglas Gregor | 08d918a | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 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); |
Sebastian Redl | b44ab5f | 2009-01-26 22:19:12 +0000 | [diff] [blame] | 41 | int const A::* const A::*icapcap = 0; |
| 42 | int A::* A::* iapap = const_cast<int A::* A::*>(icapcap); |
Charles Li | 542f04c | 2015-11-11 19:34:47 +0000 | [diff] [blame] | 43 | (void)const_cast<A&&>(A()); |
| 44 | #if __cplusplus <= 199711L // C++03 or earlier modes |
| 45 | // expected-warning@-2 {{rvalue references are a C++11 extension}} |
| 46 | #endif |
Douglas Gregor | 08d918a | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 47 | return var4; |
| 48 | } |
| 49 | |
| 50 | short *bad_const_cast_test(char const *volatile *const volatile *var) |
| 51 | { |
| 52 | // Different pointer levels. |
Chris Lattner | 53fa049 | 2010-09-05 00:04:01 +0000 | [diff] [blame] | 53 | char **var2 = const_cast<char**>(var); // expected-error {{const_cast from 'const char *volatile *const volatile *' to 'char **' is not allowed}} |
Douglas Gregor | 08d918a | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 54 | // Different final type. |
Chris Lattner | 53fa049 | 2010-09-05 00:04:01 +0000 | [diff] [blame] | 55 | short ***var3 = const_cast<short***>(var); // expected-error {{const_cast from 'const char *volatile *const volatile *' to 'short ***' is not allowed}} |
Douglas Gregor | 08d918a | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 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 | // Not even lenient g++ accepts this. |
Chris Lattner | 53fa049 | 2010-09-05 00:04:01 +0000 | [diff] [blame] | 62 | int *(*rar)[100] = const_cast<int *(*)[100]>(&ar); // expected-error {{const_cast from 'const int *(*)[100]' to 'int *(*)[100]' is not allowed}} |
Douglas Gregor | 08d918a | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 63 | f fp1 = 0; |
| 64 | // Function pointers. |
Chris Lattner | 810d330 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 65 | 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 | b44ab5f | 2009-01-26 22:19:12 +0000 | [diff] [blame] | 66 | void (A::*mfn)() = 0; |
Alp Toker | 6ed7251 | 2013-12-14 01:07:05 +0000 | [diff] [blame] | 67 | (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}} |
Charles Li | 542f04c | 2015-11-11 19:34:47 +0000 | [diff] [blame] | 68 | (void)const_cast<int&&>(0); // expected-error {{const_cast from rvalue to reference type 'int &&'}} |
| 69 | #if __cplusplus <= 199711L // C++03 or earlier modes |
| 70 | // expected-warning@-2 {{rvalue references are a C++11 extension}} |
| 71 | #endif |
Douglas Gregor | 08d918a | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 72 | return **var3; |
| 73 | } |
David Majnemer | e64941f | 2014-12-16 00:46:30 +0000 | [diff] [blame] | 74 | |
| 75 | template <typename T> |
| 76 | char *PR21845() { return const_cast<char *>((void)T::x); } // expected-error {{const_cast from 'void' to 'char *' is not allowed}} |