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
|
221
|
9 // XFAIL: gcc-7, gcc-8, gcc-9
|
|
10
|
|
11 // PR33425 and PR33487 are not fixed until the dylib shipped with macOS 10.15
|
|
12 // XFAIL: use_system_cxx_lib && x86_64-apple-macosx10.14
|
|
13
|
|
14 // PR33439 isn't fixed until the dylib shipped with macOS 10.14
|
|
15 // XFAIL: use_system_cxx_lib && x86_64-apple-macosx10.13
|
|
16 // XFAIL: use_system_cxx_lib && x86_64-apple-macosx10.12
|
|
17 // XFAIL: use_system_cxx_lib && x86_64-apple-macosx10.11
|
|
18 // XFAIL: use_system_cxx_lib && x86_64-apple-macosx10.10
|
|
19 // XFAIL: use_system_cxx_lib && x86_64-apple-macosx10.9
|
|
20
|
150
|
21 #include <cassert>
|
|
22
|
|
23 // This test explicitly tests dynamic cast with types that have inaccessible
|
|
24 // bases.
|
|
25 #if defined(__clang__)
|
221
|
26 # pragma clang diagnostic ignored "-Winaccessible-base"
|
|
27 #elif defined(__GNUC__) && (__GNUC__ >= 10)
|
|
28 # pragma GCC diagnostic ignored "-Winaccessible-base"
|
150
|
29 #endif
|
|
30
|
|
31 typedef char Pad1[43981];
|
|
32 typedef char Pad2[34981];
|
|
33 typedef char Pad3[93481];
|
|
34 typedef char Pad4[13489];
|
|
35 typedef char Pad5[81349];
|
|
36 typedef char Pad6[34819];
|
|
37 typedef char Pad7[3489];
|
|
38
|
|
39 namespace t1
|
|
40 {
|
|
41
|
|
42 // PR33425
|
|
43 struct C3 { virtual ~C3() {} Pad1 _; };
|
|
44 struct C5 : protected virtual C3 { Pad2 _; };
|
|
45 struct C6 : virtual C5 { Pad3 _; };
|
|
46 struct C7 : virtual C3 { Pad4 _; };
|
|
47 struct C9 : C6, C7 { Pad5 _; };
|
|
48
|
|
49 C9 c9;
|
|
50 C3 *c3 = &c9;
|
|
51
|
|
52 void test()
|
|
53 {
|
|
54 assert(dynamic_cast<C3*>(c3) == static_cast<C3*>(&c9));
|
|
55 assert(dynamic_cast<C5*>(c3) == static_cast<C5*>(&c9));
|
|
56 assert(dynamic_cast<C6*>(c3) == static_cast<C6*>(&c9));
|
|
57 assert(dynamic_cast<C7*>(c3) == static_cast<C7*>(&c9));
|
|
58 assert(dynamic_cast<C9*>(c3) == static_cast<C9*>(&c9));
|
|
59 }
|
|
60
|
|
61 } // t1
|
|
62
|
|
63 namespace t2
|
|
64 {
|
|
65
|
|
66 // PR33425
|
|
67 struct Src { virtual ~Src() {} Pad1 _; };
|
|
68 struct Mask : protected virtual Src { Pad2 _; };
|
|
69 struct Dest : Mask { Pad3 _; };
|
|
70 struct Root : Dest, virtual Src { Pad4 _; };
|
|
71
|
|
72 Root root;
|
|
73 Src *src = &root;
|
|
74
|
|
75 void test()
|
|
76 {
|
|
77 assert(dynamic_cast<Src*>(src) == static_cast<Src*>(&root));
|
|
78 assert(dynamic_cast<Mask*>(src) == static_cast<Mask*>(&root));
|
|
79 assert(dynamic_cast<Dest*>(src) == static_cast<Dest*>(&root));
|
|
80 assert(dynamic_cast<Root*>(src) == static_cast<Root*>(&root));
|
|
81 }
|
|
82
|
|
83 } // t2
|
|
84
|
|
85 namespace t3
|
|
86 {
|
|
87
|
|
88 // PR33487
|
|
89 struct Class1 { virtual ~Class1() {} Pad1 _; };
|
|
90 struct Shared : virtual Class1 { Pad2 _; };
|
|
91 struct Class6 : virtual Shared { Pad3 _; };
|
|
92 struct Left : Class6 { Pad4 _; };
|
|
93 struct Right : Class6 { Pad5 _; };
|
|
94 struct Main : Left, Right { Pad6 _; };
|
|
95
|
|
96 Main m;
|
|
97 Class1 *c1 = &m;
|
|
98
|
|
99 void test()
|
|
100 {
|
|
101 assert(dynamic_cast<Class1*>(c1) == static_cast<Class1*>(&m));
|
|
102 assert(dynamic_cast<Shared*>(c1) == static_cast<Shared*>(&m));
|
|
103 assert(dynamic_cast<Class6*>(c1) == 0);
|
|
104 assert(dynamic_cast<Left*>(c1) == static_cast<Left*>(&m));
|
|
105 assert(dynamic_cast<Right*>(c1) == static_cast<Right*>(&m));
|
|
106 assert(dynamic_cast<Main*>(c1) == static_cast<Main*>(&m));
|
|
107 }
|
|
108
|
|
109 } // t3
|
|
110
|
|
111 namespace t4
|
|
112 {
|
|
113
|
|
114 // PR33439
|
|
115 struct C2 { virtual ~C2() {} Pad1 _; };
|
|
116 struct C3 { virtual ~C3() {} Pad2 _; };
|
|
117 struct C4 : C3 { Pad3 _; };
|
|
118 struct C8 : C2, virtual C4 { Pad4 _; };
|
|
119 struct C9 : C4, C8 { Pad5 _; };
|
|
120
|
|
121 C9 c9;
|
|
122 C2 *c2 = &c9;
|
|
123
|
|
124 void test()
|
|
125 {
|
|
126 assert(dynamic_cast<C2*>(c2) == static_cast<C2*>(&c9));
|
|
127 assert(dynamic_cast<C3*>(c2) == 0);
|
|
128 assert(dynamic_cast<C4*>(c2) == 0);
|
|
129 assert(dynamic_cast<C8*>(c2) == static_cast<C8*>(&c9));
|
|
130 assert(dynamic_cast<C9*>(c2) == static_cast<C9*>(&c9));
|
|
131 }
|
|
132
|
|
133 } // t4
|
|
134
|
|
135 namespace t5
|
|
136 {
|
|
137
|
|
138 // PR33439
|
|
139 struct Dummy { virtual ~Dummy() {} Pad1 _; };
|
|
140 struct Src { virtual ~Src() {} Pad2 _; };
|
|
141 struct Dest : Dummy { Pad3 _; };
|
|
142 struct A1 : Dest { Pad4 _; };
|
|
143 struct A2 : Dest { Pad5 _; };
|
|
144 struct Root : Src, A1, A2 { Pad6 _; };
|
|
145
|
|
146 Root root;
|
|
147 Src *src = &root;
|
|
148
|
|
149 void test()
|
|
150 {
|
|
151 assert(dynamic_cast<Dummy*>(src) == 0);
|
|
152 assert(dynamic_cast<Src*>(src) == static_cast<Src*>(&root));
|
|
153 assert(dynamic_cast<Dest*>(src) == 0);
|
|
154 assert(dynamic_cast<A1*>(src) == static_cast<A1*>(&root));
|
|
155 assert(dynamic_cast<A2*>(src) == static_cast<A2*>(&root));
|
|
156 }
|
|
157
|
|
158 } // t5
|
|
159
|
221
|
160 int main(int, char**)
|
150
|
161 {
|
|
162 t1::test();
|
|
163 t2::test();
|
|
164 t3::test();
|
|
165 t4::test();
|
|
166 t5::test();
|
221
|
167
|
|
168 return 0;
|
150
|
169 }
|