1 //===------------------------- dynamic_cast.pass.cpp ----------------------===//
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
7 //===----------------------------------------------------------------------===//
11 // This test explicitly tests dynamic cast with types that have inaccessible
13 #if defined(__clang__)
14 #pragma clang diagnostic ignored "-Winaccessible-base"
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];
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 _
; };
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
));
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 _
; };
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
));
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 _
; };
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
));
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 _
; };
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
));
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 _
; };
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
));