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// UNSUPPORTED: no-exceptions
10
11// Compilers emit warnings about exceptions of type 'Child' being caught by
12// an earlier handler of type 'Base'. Congrats, you've just diagnosed the
13// behavior under test.
14// ADDITIONAL_COMPILE_FLAGS: -Wno-exceptions
15
16// The fix for PR17222 made it in the dylib for macOS 10.10
17// XFAIL: stdlib=apple-libc++ && target={{.+}}-apple-macosx10.9
18
19#include <cassert>
20#include <stdint.h>
21
22#if __cplusplus < 201103L
23#define DISABLE_NULLPTR_TESTS
24#endif
25
26struct A {};
27A a;
28const A ca = A();
29
30void test1 ()
31{
32 try
33 {
34 throw &a;
35 assert(false);
36 }
37 catch ( const A* )
38 {
39 }
40 catch ( A *)
41 {
42 assert (false);
43 }
44}
45
46void test2 ()
47{
48 try
49 {
50 throw &a;
51 assert(false);
52 }
53 catch ( A* )
54 {
55 }
56 catch ( const A *)
57 {
58 assert (false);
59 }
60}
61
62void test3 ()
63{
64 try
65 {
66 throw &ca;
67 assert(false);
68 }
69 catch ( const A* )
70 {
71 }
72 catch ( A *)
73 {
74 assert (false);
75 }
76}
77
78void test4 ()
79{
80 try
81 {
82 throw &ca;
83 assert(false);
84 }
85 catch ( A *)
86 {
87 assert (false);
88 }
89 catch ( const A* )
90 {
91 }
92}
93
94struct base1 {int x;};
95struct base2 {int x;};
96struct derived : base1, base2 {};
97
98void test5 ()
99{
100 try
101 {
102 throw (derived*)0;
103 assert(false);
104 }
105 catch (base2 *p) {
106 assert (p == 0);
107 }
108 catch (...)
109 {
110 assert (false);
111 }
112}
113
114void test6 ()
115{
116#if !defined(DISABLE_NULLPTR_TESTS)
117 try
118 {
119 throw nullptr;
120 assert(false);
121 }
122 catch (base2 *p) {
123 assert (p == nullptr);
124 }
125 catch (...)
126 {
127 assert (false);
128 }
129#endif
130}
131
132void test7 ()
133{
134 try
135 {
136 throw (derived*)12;
137 assert(false);
138 }
139 catch (base2 *p) {
140 assert ((uintptr_t)p == 12+sizeof(base1));
141 }
142 catch (...)
143 {
144 assert (false);
145 }
146}
147
148
149struct vBase {};
150struct vDerived : virtual public vBase {};
151
152void test8 ()
153{
154 vDerived derived;
155 try
156 {
157 throw &derived;
158 assert(false);
159 }
160 catch (vBase *p) {
161 assert(p != 0);
162 }
163 catch (...)
164 {
165 assert (false);
166 }
167}
168
169void test9 ()
170{
171#if !defined(DISABLE_NULLPTR_TESTS)
172 try
173 {
174 throw nullptr;
175 assert(false);
176 }
177 catch (vBase *p) {
178 assert(p == 0);
179 }
180 catch (...)
181 {
182 assert (false);
183 }
184#endif
185}
186
187void test10 ()
188{
189 try
190 {
191 throw (vDerived*)0;
192 assert(false);
193 }
194 catch (vBase *p) {
195 assert(p == 0);
196 }
197 catch (...)
198 {
199 assert (false);
200 }
201}
202
203int main(int, char**)
204{
205 test1();
206 test2();
207 test3();
208 test4();
209 test5();
210 test6();
211 test7();
212 test8();
213 test9();
214 test10();
215
216 return 0;
217}
218

source code of libcxxabi/test/catch_ptr_02.pass.cpp