1 //===------------------------- dynamic_cast.pass.cpp ----------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include <cassert> 11 12 // This test explicitly tests dynamic cast with types that have inaccessible 13 // bases. 14 #if defined(__clang__) 15 #pragma clang diagnostic ignored "-Winaccessible-base" 16 #endif 17 18 typedef char Pad1[43981]; 19 typedef char Pad2[34981]; 20 typedef char Pad3[93481]; 21 typedef char Pad4[13489]; 22 typedef char Pad5[81349]; 23 typedef char Pad6[34819]; 24 typedef char Pad7[3489]; 25 26 namespace t1 27 { 28 29 // PR33425 ~C3t1::C330struct C3 { virtual ~C3() {} Pad1 _; }; 31 struct C5 : protected virtual C3 { Pad2 _; }; 32 struct C6 : virtual C5 { Pad3 _; }; 33 struct C7 : virtual C3 { Pad4 _; }; 34 struct C9 : C6, C7 { Pad5 _; }; 35 36 C9 c9; 37 C3 *c3 = &c9; 38 test()39void test() 40 { 41 assert(dynamic_cast<C3*>(c3) == static_cast<C3*>(&c9)); 42 assert(dynamic_cast<C5*>(c3) == static_cast<C5*>(&c9)); 43 assert(dynamic_cast<C6*>(c3) == static_cast<C6*>(&c9)); 44 assert(dynamic_cast<C7*>(c3) == static_cast<C7*>(&c9)); 45 assert(dynamic_cast<C9*>(c3) == static_cast<C9*>(&c9)); 46 } 47 48 } // t1 49 50 namespace t2 51 { 52 53 // PR33425 ~Srct2::Src54struct Src { virtual ~Src() {} Pad1 _; }; 55 struct Mask : protected virtual Src { Pad2 _; }; 56 struct Dest : Mask { Pad3 _; }; 57 struct Root : Dest, virtual Src { Pad4 _; }; 58 59 Root root; 60 Src *src = &root; 61 test()62void test() 63 { 64 assert(dynamic_cast<Src*>(src) == static_cast<Src*>(&root)); 65 assert(dynamic_cast<Mask*>(src) == static_cast<Mask*>(&root)); 66 assert(dynamic_cast<Dest*>(src) == static_cast<Dest*>(&root)); 67 assert(dynamic_cast<Root*>(src) == static_cast<Root*>(&root)); 68 } 69 70 } // t2 71 72 namespace t3 73 { 74 75 // PR33487 ~Class1t3::Class176struct Class1 { virtual ~Class1() {} Pad1 _; }; 77 struct Shared : virtual Class1 { Pad2 _; }; 78 struct Class6 : virtual Shared { Pad3 _; }; 79 struct Left : Class6 { Pad4 _; }; 80 struct Right : Class6 { Pad5 _; }; 81 struct Main : Left, Right { Pad6 _; }; 82 83 Main m; 84 Class1 *c1 = &m; 85 test()86void test() 87 { 88 assert(dynamic_cast<Class1*>(c1) == static_cast<Class1*>(&m)); 89 assert(dynamic_cast<Shared*>(c1) == static_cast<Shared*>(&m)); 90 assert(dynamic_cast<Class6*>(c1) == 0); 91 assert(dynamic_cast<Left*>(c1) == static_cast<Left*>(&m)); 92 assert(dynamic_cast<Right*>(c1) == static_cast<Right*>(&m)); 93 assert(dynamic_cast<Main*>(c1) == static_cast<Main*>(&m)); 94 } 95 96 } // t3 97 98 namespace t4 99 { 100 101 // PR33439 ~C2t4::C2102struct C2 { virtual ~C2() {} Pad1 _; }; ~C3t4::C3103struct C3 { virtual ~C3() {} Pad2 _; }; 104 struct C4 : C3 { Pad3 _; }; 105 struct C8 : C2, virtual C4 { Pad4 _; }; 106 struct C9 : C4, C8 { Pad5 _; }; 107 108 C9 c9; 109 C2 *c2 = &c9; 110 test()111void test() 112 { 113 assert(dynamic_cast<C2*>(c2) == static_cast<C2*>(&c9)); 114 assert(dynamic_cast<C3*>(c2) == 0); 115 assert(dynamic_cast<C4*>(c2) == 0); 116 assert(dynamic_cast<C8*>(c2) == static_cast<C8*>(&c9)); 117 assert(dynamic_cast<C9*>(c2) == static_cast<C9*>(&c9)); 118 } 119 120 } // t4 121 122 namespace t5 123 { 124 125 // PR33439 ~Dummyt5::Dummy126struct Dummy { virtual ~Dummy() {} Pad1 _; }; ~Srct5::Src127struct Src { virtual ~Src() {} Pad2 _; }; 128 struct Dest : Dummy { Pad3 _; }; 129 struct A1 : Dest { Pad4 _; }; 130 struct A2 : Dest { Pad5 _; }; 131 struct Root : Src, A1, A2 { Pad6 _; }; 132 133 Root root; 134 Src *src = &root; 135 test()136void test() 137 { 138 assert(dynamic_cast<Dummy*>(src) == 0); 139 assert(dynamic_cast<Src*>(src) == static_cast<Src*>(&root)); 140 assert(dynamic_cast<Dest*>(src) == 0); 141 assert(dynamic_cast<A1*>(src) == static_cast<A1*>(&root)); 142 assert(dynamic_cast<A2*>(src) == static_cast<A2*>(&root)); 143 } 144 145 } // t5 146 main()147int main() 148 { 149 t1::test(); 150 t2::test(); 151 t3::test(); 152 t4::test(); 153 t5::test(); 154 } 155