Daniel Dunbar | d7d5f02 | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 1 | // RUN: clang-cc -fsyntax-only -verify %s |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2 | |
| 3 | struct A {}; |
| 4 | struct B : public A {}; // Single public base. |
| 5 | struct C1 : public virtual B {}; // Single virtual base. |
| 6 | struct C2 : public virtual B {}; |
| 7 | struct D : public C1, public C2 {}; // Diamond |
| 8 | struct E : private A {}; // Single private base. |
| 9 | struct F : public C1 {}; // Single path to B with virtual. |
| 10 | struct G1 : public B {}; |
| 11 | struct G2 : public B {}; |
| 12 | struct H : public G1, public G2 {}; // Ambiguous path to B. |
| 13 | |
| 14 | enum Enum { En1, En2 }; |
| 15 | enum Onom { On1, On2 }; |
| 16 | |
| 17 | // Explicit implicits |
| 18 | void t_529_2() |
| 19 | { |
| 20 | int i = 1; |
| 21 | (void)static_cast<float>(i); |
| 22 | double d = 1.0; |
| 23 | (void)static_cast<float>(d); |
| 24 | (void)static_cast<int>(d); |
| 25 | (void)static_cast<char>(i); |
| 26 | (void)static_cast<unsigned long>(i); |
| 27 | (void)static_cast<int>(En1); |
| 28 | (void)static_cast<double>(En1); |
| 29 | (void)static_cast<int&>(i); |
| 30 | (void)static_cast<const int&>(i); |
| 31 | |
| 32 | int ar[1]; |
| 33 | (void)static_cast<const int*>(ar); |
| 34 | (void)static_cast<void (*)()>(t_529_2); |
| 35 | |
| 36 | (void)static_cast<void*>(0); |
| 37 | (void)static_cast<void*>((int*)0); |
| 38 | (void)static_cast<volatile const void*>((const int*)0); |
| 39 | (void)static_cast<A*>((B*)0); |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 40 | (void)static_cast<A&>(*((B*)0)); |
| 41 | (void)static_cast<const B*>((C1*)0); |
| 42 | (void)static_cast<B&>(*((C1*)0)); |
| 43 | (void)static_cast<A*>((D*)0); |
| 44 | (void)static_cast<const A&>(*((D*)0)); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 45 | (void)static_cast<int B::*>((int A::*)0); |
| 46 | (void)static_cast<void (B::*)()>((void (A::*)())0); |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 47 | |
| 48 | // TODO: User-defined conversions |
| 49 | |
| 50 | // Bad code below |
| 51 | |
| 52 | (void)static_cast<void*>((const int*)0); // expected-error {{static_cast from 'int const *' to 'void *' is not allowed}} |
| 53 | //(void)static_cast<A*>((E*)0); // {{static_cast from 'struct E *' to 'struct A *' is not allowed}} |
| 54 | //(void)static_cast<A*>((H*)0); // {{static_cast from 'struct H *' to 'struct A *' is not allowed}} |
| 55 | (void)static_cast<int>((int*)0); // expected-error {{static_cast from 'int *' to 'int' is not allowed}} |
| 56 | (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] | 57 | (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] | 58 | } |
| 59 | |
| 60 | // Anything to void |
| 61 | void t_529_4() |
| 62 | { |
| 63 | static_cast<void>(1); |
| 64 | static_cast<void>(t_529_4); |
| 65 | } |
| 66 | |
| 67 | // Static downcasts |
| 68 | void t_529_5_8() |
| 69 | { |
| 70 | (void)static_cast<B*>((A*)0); |
| 71 | (void)static_cast<B&>(*((A*)0)); |
| 72 | (void)static_cast<const G1*>((A*)0); |
| 73 | (void)static_cast<const G1&>(*((A*)0)); |
| 74 | |
| 75 | // Bad code below |
| 76 | |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 77 | (void)static_cast<C1*>((A*)0); // expected-error {{cannot cast 'struct A *' to 'struct C1 *' via virtual base 'struct B'}} |
| 78 | (void)static_cast<C1&>(*((A*)0)); // expected-error {{cannot cast 'struct A' to 'struct C1 &' via virtual base 'struct B'}} |
| 79 | (void)static_cast<D*>((A*)0); // expected-error {{cannot cast 'struct A *' to 'struct D *' via virtual base 'struct B'}} |
| 80 | (void)static_cast<D&>(*((A*)0)); // expected-error {{cannot cast 'struct A' to 'struct D &' via virtual base 'struct B'}} |
| 81 | (void)static_cast<B*>((const A*)0); // expected-error {{static_cast from 'struct A const *' to 'struct B *' casts away constness}} |
| 82 | (void)static_cast<B&>(*((const A*)0)); // expected-error {{static_cast from 'struct A const' to 'struct B &' casts away constness}} |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 83 | // Accessibility is not yet tested |
| 84 | //(void)static_cast<E*>((A*)0); // {{static_cast from 'struct A *' to 'struct E *' is not allowed}} |
| 85 | //(void)static_cast<E&>(*((A*)0)); // {{static_cast from 'struct A' to 'struct E &' is not allowed}} |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 86 | (void)static_cast<H*>((A*)0); // expected-error {{ambiguous static_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}} |
| 87 | (void)static_cast<H&>(*((A*)0)); // expected-error {{ambiguous static_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] | 88 | (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] | 89 | (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] | 90 | |
| 91 | // TODO: Test inaccessible base in context where it's accessible, i.e. |
| 92 | // member function and friend. |
| 93 | |
| 94 | // TODO: Test DR427. This requires user-defined conversions, though. |
| 95 | } |
| 96 | |
| 97 | // Enum conversions |
| 98 | void t_529_7() |
| 99 | { |
| 100 | (void)static_cast<Enum>(1); |
| 101 | (void)static_cast<Enum>(1.0); |
| 102 | (void)static_cast<Onom>(En1); |
| 103 | |
| 104 | // Bad code below |
| 105 | |
| 106 | (void)static_cast<Enum>((int*)0); // expected-error {{static_cast from 'int *' to 'enum Enum' is not allowed}} |
| 107 | } |
| 108 | |
| 109 | // Void pointer to object pointer |
| 110 | void t_529_10() |
| 111 | { |
| 112 | (void)static_cast<int*>((void*)0); |
| 113 | (void)static_cast<const A*>((void*)0); |
| 114 | |
| 115 | // Bad code below |
| 116 | |
Sebastian Redl | 4e84935 | 2008-11-05 17:54:26 +0000 | [diff] [blame] | 117 | (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^] | 118 | (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] | 119 | } |
| 120 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 121 | // Member pointer upcast. |
| 122 | void t_529_9() |
| 123 | { |
| 124 | (void)static_cast<int A::*>((int B::*)0); |
| 125 | |
| 126 | // Bad code below |
| 127 | (void)static_cast<int A::*>((int H::*)0); // expected-error {{ambiguous conversion from pointer to member of derived class 'struct H'}} |
| 128 | (void)static_cast<int A::*>((int F::*)0); // expected-error {{conversion from pointer to member of class 'struct F'}} |
| 129 | } |