Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -faccess-control %s |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2 | struct A {}; |
| 3 | struct B : public A {}; // Single public base. |
| 4 | struct C1 : public virtual B {}; // Single virtual base. |
| 5 | struct C2 : public virtual B {}; |
| 6 | struct D : public C1, public C2 {}; // Diamond |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 7 | struct E : private A {}; // Single private base. expected-note 3 {{declared private here}} |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 8 | struct F : public C1 {}; // Single path to B with virtual. |
| 9 | struct G1 : public B {}; |
| 10 | struct G2 : public B {}; |
| 11 | struct H : public G1, public G2 {}; // Ambiguous path to B. |
| 12 | |
| 13 | enum Enum { En1, En2 }; |
| 14 | enum Onom { On1, On2 }; |
| 15 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 16 | struct Co1 { operator int(); }; |
| 17 | struct Co2 { Co2(int); }; |
| 18 | struct Co3 { }; |
| 19 | struct Co4 { Co4(Co3); operator Co3(); }; |
| 20 | |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 21 | // Explicit implicits |
| 22 | void t_529_2() |
| 23 | { |
| 24 | int i = 1; |
| 25 | (void)static_cast<float>(i); |
| 26 | double d = 1.0; |
| 27 | (void)static_cast<float>(d); |
| 28 | (void)static_cast<int>(d); |
| 29 | (void)static_cast<char>(i); |
| 30 | (void)static_cast<unsigned long>(i); |
| 31 | (void)static_cast<int>(En1); |
| 32 | (void)static_cast<double>(En1); |
| 33 | (void)static_cast<int&>(i); |
| 34 | (void)static_cast<const int&>(i); |
| 35 | |
| 36 | int ar[1]; |
| 37 | (void)static_cast<const int*>(ar); |
| 38 | (void)static_cast<void (*)()>(t_529_2); |
| 39 | |
| 40 | (void)static_cast<void*>(0); |
| 41 | (void)static_cast<void*>((int*)0); |
| 42 | (void)static_cast<volatile const void*>((const int*)0); |
| 43 | (void)static_cast<A*>((B*)0); |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 44 | (void)static_cast<A&>(*((B*)0)); |
| 45 | (void)static_cast<const B*>((C1*)0); |
| 46 | (void)static_cast<B&>(*((C1*)0)); |
| 47 | (void)static_cast<A*>((D*)0); |
| 48 | (void)static_cast<const A&>(*((D*)0)); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 49 | (void)static_cast<int B::*>((int A::*)0); |
| 50 | (void)static_cast<void (B::*)()>((void (A::*)())0); |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 51 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 52 | (void)static_cast<int>(Co1()); |
| 53 | (void)static_cast<Co2>(1); |
| 54 | (void)static_cast<Co3>(static_cast<Co4>(Co3())); |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 55 | |
| 56 | // Bad code below |
| 57 | |
| 58 | (void)static_cast<void*>((const int*)0); // expected-error {{static_cast from 'int const *' to 'void *' is not allowed}} |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 59 | (void)static_cast<A*>((E*)0); // expected-error {{private base class 'struct A'}} |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 60 | (void)static_cast<A*>((H*)0); // expected-error {{ambiguous conversion}} |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 61 | (void)static_cast<int>((int*)0); // expected-error {{static_cast from 'int *' to 'int' is not allowed}} |
| 62 | (void)static_cast<A**>((B**)0); // expected-error {{static_cast from 'struct B **' to 'struct A **' is not allowed}} |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 63 | (void)static_cast<char&>(i); // expected-error {{non-const lvalue reference to type 'char' cannot be initialized with a value of type 'int'}} |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // Anything to void |
| 67 | void t_529_4() |
| 68 | { |
| 69 | static_cast<void>(1); |
| 70 | static_cast<void>(t_529_4); |
| 71 | } |
| 72 | |
| 73 | // Static downcasts |
| 74 | void t_529_5_8() |
| 75 | { |
| 76 | (void)static_cast<B*>((A*)0); |
| 77 | (void)static_cast<B&>(*((A*)0)); |
| 78 | (void)static_cast<const G1*>((A*)0); |
| 79 | (void)static_cast<const G1&>(*((A*)0)); |
| 80 | |
| 81 | // Bad code below |
| 82 | |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 83 | (void)static_cast<C1*>((A*)0); // expected-error {{cannot cast 'struct A *' to 'struct C1 *' via virtual base 'struct B'}} |
| 84 | (void)static_cast<C1&>(*((A*)0)); // expected-error {{cannot cast 'struct A' to 'struct C1 &' via virtual base 'struct B'}} |
| 85 | (void)static_cast<D*>((A*)0); // expected-error {{cannot cast 'struct A *' to 'struct D *' via virtual base 'struct B'}} |
| 86 | (void)static_cast<D&>(*((A*)0)); // expected-error {{cannot cast 'struct A' to 'struct D &' via virtual base 'struct B'}} |
| 87 | (void)static_cast<B*>((const A*)0); // expected-error {{static_cast from 'struct A const *' to 'struct B *' casts away constness}} |
| 88 | (void)static_cast<B&>(*((const A*)0)); // expected-error {{static_cast from 'struct A const' to 'struct B &' casts away constness}} |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 89 | (void)static_cast<E*>((A*)0); // expected-error {{cannot cast private base class 'struct A' to 'struct E'}} |
| 90 | (void)static_cast<E&>(*((A*)0)); // expected-error {{cannot cast private base class 'struct A' to 'struct E'}} |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 91 | (void)static_cast<H*>((A*)0); // expected-error {{ambiguous cast from base 'struct A' to derived 'struct H':\n struct A -> struct B -> struct G1 -> struct H\n struct A -> struct B -> struct G2 -> struct H}} |
| 92 | (void)static_cast<H&>(*((A*)0)); // expected-error {{ambiguous cast from base 'struct A' to derived 'struct H':\n struct A -> struct B -> struct G1 -> struct H\n struct A -> struct B -> struct G2 -> struct H}} |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 93 | (void)static_cast<E*>((B*)0); // expected-error {{static_cast from 'struct B *' to 'struct E *' is not allowed}} |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 94 | (void)static_cast<E&>(*((B*)0)); // expected-error {{non-const lvalue reference to type 'struct E' cannot be initialized with a value of type 'struct B'}} |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 95 | |
| 96 | // TODO: Test inaccessible base in context where it's accessible, i.e. |
| 97 | // member function and friend. |
| 98 | |
| 99 | // TODO: Test DR427. This requires user-defined conversions, though. |
| 100 | } |
| 101 | |
| 102 | // Enum conversions |
| 103 | void t_529_7() |
| 104 | { |
| 105 | (void)static_cast<Enum>(1); |
| 106 | (void)static_cast<Enum>(1.0); |
| 107 | (void)static_cast<Onom>(En1); |
| 108 | |
| 109 | // Bad code below |
| 110 | |
| 111 | (void)static_cast<Enum>((int*)0); // expected-error {{static_cast from 'int *' to 'enum Enum' is not allowed}} |
| 112 | } |
| 113 | |
| 114 | // Void pointer to object pointer |
| 115 | void t_529_10() |
| 116 | { |
| 117 | (void)static_cast<int*>((void*)0); |
| 118 | (void)static_cast<const A*>((void*)0); |
| 119 | |
| 120 | // Bad code below |
| 121 | |
Sebastian Redl | 4e84935 | 2008-11-05 17:54:26 +0000 | [diff] [blame] | 122 | (void)static_cast<int*>((const void*)0); // expected-error {{static_cast from 'void const *' to 'int *' casts away constness}} |
Argyrios Kyrtzidis | 7c94c4b | 2009-06-03 02:06:50 +0000 | [diff] [blame] | 123 | (void)static_cast<void (*)()>((void*)0); // expected-error {{static_cast from 'void *' to 'void (*)()' is not allowed}} |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 126 | // Member pointer upcast. |
| 127 | void t_529_9() |
| 128 | { |
| 129 | (void)static_cast<int A::*>((int B::*)0); |
| 130 | |
| 131 | // Bad code below |
| 132 | (void)static_cast<int A::*>((int H::*)0); // expected-error {{ambiguous conversion from pointer to member of derived class 'struct H'}} |
| 133 | (void)static_cast<int A::*>((int F::*)0); // expected-error {{conversion from pointer to member of class 'struct F'}} |
| 134 | } |
Sebastian Redl | 5ed66f7 | 2009-10-22 15:07:22 +0000 | [diff] [blame] | 135 | |
| 136 | // PR 5261 - static_cast should instantiate template if possible |
| 137 | namespace pr5261 { |
| 138 | struct base {}; |
| 139 | template<typename E> struct derived : public base {}; |
| 140 | template<typename E> struct outer { |
| 141 | base *pb; |
| 142 | ~outer() { (void)static_cast<derived<E>*>(pb); } |
| 143 | }; |
| 144 | outer<int> EntryList; |
| 145 | } |
Douglas Gregor | 19aeac6 | 2009-11-14 03:27:21 +0000 | [diff] [blame] | 146 | |
| 147 | |
| 148 | // Initialization by constructor |
| 149 | struct X0; |
| 150 | |
| 151 | struct X1 { |
| 152 | X1(); |
| 153 | X1(X1&); |
| 154 | X1(const X0&); |
| 155 | |
| 156 | operator X0() const; |
| 157 | }; |
| 158 | |
| 159 | struct X0 { }; |
| 160 | |
| 161 | void test_ctor_init() { |
| 162 | (void)static_cast<X1>(X1()); |
| 163 | } |
Douglas Gregor | ab15d0e | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 164 | |
| 165 | // Casting away constness |
| 166 | struct X2 { |
| 167 | }; |
| 168 | |
| 169 | struct X3 : X2 { |
| 170 | }; |
| 171 | |
| 172 | struct X4 { |
| 173 | typedef const X3 X3_typedef; |
| 174 | |
| 175 | void f() const { |
| 176 | (void)static_cast<X3_typedef*>(x2); |
| 177 | } |
| 178 | |
| 179 | const X2 *x2; |
| 180 | }; |
Chandler Carruth | c099d9b | 2010-01-31 11:51:51 +0000 | [diff] [blame] | 181 | |
| 182 | // PR5897 - accept static_cast from const void* to const int (*)[1]. |
Chandler Carruth | e1cd337 | 2010-01-31 11:52:52 +0000 | [diff] [blame] | 183 | void PR5897() { (void)static_cast<const int(*)[1]>((const void*)0); } |
Douglas Gregor | 4ce46c2 | 2010-03-07 23:24:59 +0000 | [diff] [blame^] | 184 | |
| 185 | namespace PR6072 { |
| 186 | struct A { }; |
| 187 | struct B : A { void f(int); void f(); }; |
| 188 | struct C : B { }; |
| 189 | struct D { }; |
| 190 | |
| 191 | void f() { |
| 192 | (void)static_cast<void (A::*)()>(&B::f); |
| 193 | (void)static_cast<void (B::*)()>(&B::f); |
| 194 | (void)static_cast<void (C::*)()>(&B::f); |
| 195 | (void)static_cast<void (D::*)()>(&B::f); // expected-error{{static_cast from '<overloaded function type>' to 'void (struct PR6072::D::*)()' is not allowed}} |
| 196 | } |
| 197 | } |