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