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