• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 
3 struct A {};
4 
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 
good_const_cast_test(ccvpcvpp var)22 char ***good_const_cast_test(ccvpcvpp var)
23 {
24   // Cast away deep consts and volatiles.
25   char ***var2 = const_cast<cppp>(var);
26   char ***const &var3 = var2;
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 array to array reference.
32   const int ar[100] = {0};
33   int (&rar)[100] = const_cast<iarr>(ar);
34   // Array decay. Intentionally without qualifier change.
35   int *pi = const_cast<int*>(ar);
36   f fp = 0;
37   // Don't misidentify fn** as a function pointer.
38   f *fpp = const_cast<f*>(&fp);
39   int const A::* const A::*icapcap = 0;
40   int A::* A::* iapap = const_cast<int A::* A::*>(icapcap);
41 
42   return var4;
43 }
44 
bad_const_cast_test(char const * volatile * const volatile * var)45 short *bad_const_cast_test(char const *volatile *const volatile *var)
46 {
47   // Different pointer levels.
48   char **var2 = const_cast<char**>(var); // expected-error {{const_cast from 'const char *volatile *const volatile *' to 'char **' is not allowed}}
49   // Different final type.
50   short ***var3 = const_cast<short***>(var); // expected-error {{const_cast from 'const char *volatile *const volatile *' to 'short ***' is not allowed}}
51   // Rvalue to reference.
52   char ***&var4 = const_cast<cpppr>(&var2); // expected-error {{const_cast from rvalue to reference type 'cpppr'}}
53   // Non-pointer.
54   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}}
55   const int *ar[100] = {0};
56   // Not even lenient g++ accepts this.
57   int *(*rar)[100] = const_cast<int *(*)[100]>(&ar); // expected-error {{const_cast from 'const int *(*)[100]' to 'int *(*)[100]' is not allowed}}
58   f fp1 = 0;
59   // Function pointers.
60   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}}
61   void (A::*mfn)() = 0;
62   (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}}
63   return **var3;
64 }
65