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