xref: /llvm-project/libcxxabi/test/inherited_exception.pass.cpp (revision eb8650a75793b2bd079d0c8901ff066f129061da)
1*eb8650a7SLouis Dionne //===----------------------------------------------------------------------===//
2e434b34fSJonathan Roelofs //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e434b34fSJonathan Roelofs //
7e434b34fSJonathan Roelofs //===----------------------------------------------------------------------===//
8e434b34fSJonathan Roelofs //
9e434b34fSJonathan Roelofs // This test case checks specifically the cases under C++ ABI 15.3.1, and 15.3.2
10e434b34fSJonathan Roelofs //
11e434b34fSJonathan Roelofs //  C++ ABI 15.3:
12e434b34fSJonathan Roelofs //  A handler is a match for an exception object of type E if
138d313927SLouis Dionne //  >  *  The handler is of type cv T or cv T& and E and T are the same type   <
148d313927SLouis Dionne //  >     (ignoring the top-level cv-qualifiers), or                           <
158d313927SLouis Dionne //  >  *  the handler is of type cv T or cv T& and T is an unambiguous base    <
168d313927SLouis Dionne //  >     class of E, or                                                       <
17e434b34fSJonathan Roelofs //     *  the handler is of type cv1 T* cv2 and E is a pointer type that can
18e434b34fSJonathan Roelofs //        be converted to the type of the handler by either or both of
19e434b34fSJonathan Roelofs //          o  a standard pointer conversion (4.10 [conv.ptr]) not involving
20e434b34fSJonathan Roelofs //             conversions to private or protected or ambiguous classes
21e434b34fSJonathan Roelofs //          o  a qualification conversion
22e434b34fSJonathan Roelofs //     *  the handler is a pointer or pointer to member type and E is
23e434b34fSJonathan Roelofs //        std::nullptr_t
24e434b34fSJonathan Roelofs //
25e434b34fSJonathan Roelofs //===----------------------------------------------------------------------===//
26e434b34fSJonathan Roelofs 
278c61114cSLouis Dionne // UNSUPPORTED: no-exceptions
2857e446daSAsiri Rathnayake 
29d9eb6c7cSLouis Dionne // Compilers emit warnings about exceptions of type 'Child' being caught by
30d9eb6c7cSLouis Dionne // an earlier handler of type 'Base'. Congrats, you've just diagnosed the
31d9eb6c7cSLouis Dionne // behavior under test.
32d9eb6c7cSLouis Dionne // ADDITIONAL_COMPILE_FLAGS: -Wno-exceptions
33f5ff11c4SEric Fiselier 
34e434b34fSJonathan Roelofs #include <assert.h>
35e434b34fSJonathan Roelofs 
36e434b34fSJonathan Roelofs struct Base {
37e434b34fSJonathan Roelofs   int b1;
38e434b34fSJonathan Roelofs };
39e434b34fSJonathan Roelofs 
40e434b34fSJonathan Roelofs struct Base2 {
41e434b34fSJonathan Roelofs   int b2;
42e434b34fSJonathan Roelofs };
43e434b34fSJonathan Roelofs 
44e434b34fSJonathan Roelofs struct Child : public Base, public Base2 {
45e434b34fSJonathan Roelofs   int c;
46e434b34fSJonathan Roelofs };
47e434b34fSJonathan Roelofs 
f1()48e434b34fSJonathan Roelofs void f1() {
49e434b34fSJonathan Roelofs   Child child;
50e434b34fSJonathan Roelofs   child.b1 = 10;
51e434b34fSJonathan Roelofs   child.b2 = 11;
52e434b34fSJonathan Roelofs   child.c = 12;
53e434b34fSJonathan Roelofs   throw child;
54e434b34fSJonathan Roelofs }
55e434b34fSJonathan Roelofs 
f2()56e434b34fSJonathan Roelofs void f2() {
57e434b34fSJonathan Roelofs   Child child;
58e434b34fSJonathan Roelofs   child.b1 = 10;
59e434b34fSJonathan Roelofs   child.b2 = 11;
60e434b34fSJonathan Roelofs   child.c = 12;
61e434b34fSJonathan Roelofs   throw static_cast<Base2&>(child);
62e434b34fSJonathan Roelofs }
63e434b34fSJonathan Roelofs 
f3()64e434b34fSJonathan Roelofs void f3() {
65e434b34fSJonathan Roelofs   static Child child;
66e434b34fSJonathan Roelofs   child.b1 = 10;
67e434b34fSJonathan Roelofs   child.b2 = 11;
68e434b34fSJonathan Roelofs   child.c = 12;
69e434b34fSJonathan Roelofs   throw static_cast<Base2*>(&child);
70e434b34fSJonathan Roelofs }
71e434b34fSJonathan Roelofs 
main(int,char **)72504bc07dSLouis Dionne int main(int, char**)
73e434b34fSJonathan Roelofs {
74e434b34fSJonathan Roelofs     try
75e434b34fSJonathan Roelofs     {
76e434b34fSJonathan Roelofs         f1();
77e434b34fSJonathan Roelofs         assert(false);
78e434b34fSJonathan Roelofs     }
79e434b34fSJonathan Roelofs     catch (const Child& c)
80e434b34fSJonathan Roelofs     {
81e434b34fSJonathan Roelofs         assert(true);
82e434b34fSJonathan Roelofs     }
83e434b34fSJonathan Roelofs     catch (const Base& b)
84e434b34fSJonathan Roelofs     {
85e434b34fSJonathan Roelofs         assert(false);
86e434b34fSJonathan Roelofs     }
87e434b34fSJonathan Roelofs     catch (...)
88e434b34fSJonathan Roelofs     {
89e434b34fSJonathan Roelofs         assert(false);
90e434b34fSJonathan Roelofs     }
91e434b34fSJonathan Roelofs 
92e434b34fSJonathan Roelofs     try
93e434b34fSJonathan Roelofs     {
94e434b34fSJonathan Roelofs         f1();
95e434b34fSJonathan Roelofs         assert(false);
96e434b34fSJonathan Roelofs     }
97e434b34fSJonathan Roelofs     catch (const Base& c)
98e434b34fSJonathan Roelofs     {
99e434b34fSJonathan Roelofs         assert(true);
100e434b34fSJonathan Roelofs     }
101e434b34fSJonathan Roelofs     catch (const Child& b)
102e434b34fSJonathan Roelofs     {
103e434b34fSJonathan Roelofs         assert(false);
104e434b34fSJonathan Roelofs     }
105e434b34fSJonathan Roelofs     catch (...)
106e434b34fSJonathan Roelofs     {
107e434b34fSJonathan Roelofs         assert(false);
108e434b34fSJonathan Roelofs     }
109e434b34fSJonathan Roelofs 
110e434b34fSJonathan Roelofs     try
111e434b34fSJonathan Roelofs     {
112e434b34fSJonathan Roelofs         f1();
113e434b34fSJonathan Roelofs         assert(false);
114e434b34fSJonathan Roelofs     }
115e434b34fSJonathan Roelofs     catch (const Base2& c)
116e434b34fSJonathan Roelofs     {
117e434b34fSJonathan Roelofs         assert(true);
118e434b34fSJonathan Roelofs     }
119e434b34fSJonathan Roelofs     catch (const Child& b)
120e434b34fSJonathan Roelofs     {
121e434b34fSJonathan Roelofs         assert(false);
122e434b34fSJonathan Roelofs     }
123e434b34fSJonathan Roelofs     catch (...)
124e434b34fSJonathan Roelofs     {
125e434b34fSJonathan Roelofs         assert(false);
126e434b34fSJonathan Roelofs     }
127e434b34fSJonathan Roelofs 
128e434b34fSJonathan Roelofs     try
129e434b34fSJonathan Roelofs     {
130e434b34fSJonathan Roelofs         f2();
131e434b34fSJonathan Roelofs         assert(false);
132e434b34fSJonathan Roelofs     }
133e434b34fSJonathan Roelofs     catch (const Child& c)
134e434b34fSJonathan Roelofs     {
135e434b34fSJonathan Roelofs         assert(false);
136e434b34fSJonathan Roelofs     }
137e434b34fSJonathan Roelofs     catch (const Base& b)
138e434b34fSJonathan Roelofs     {
139e434b34fSJonathan Roelofs         assert(false);
140e434b34fSJonathan Roelofs     }
141e434b34fSJonathan Roelofs     catch (const Base2& b)
142e434b34fSJonathan Roelofs     {
143e434b34fSJonathan Roelofs         assert(true);
144e434b34fSJonathan Roelofs     }
145e434b34fSJonathan Roelofs     catch (...)
146e434b34fSJonathan Roelofs     {
147e434b34fSJonathan Roelofs         assert(false);
148e434b34fSJonathan Roelofs     }
149e434b34fSJonathan Roelofs 
150e434b34fSJonathan Roelofs     try
151e434b34fSJonathan Roelofs     {
152e434b34fSJonathan Roelofs         f3();
153e434b34fSJonathan Roelofs         assert(false);
154e434b34fSJonathan Roelofs     }
155e434b34fSJonathan Roelofs     catch (const Base* c)
156e434b34fSJonathan Roelofs     {
157e434b34fSJonathan Roelofs         assert(false);
158e434b34fSJonathan Roelofs     }
159e434b34fSJonathan Roelofs     catch (const Child* b)
160e434b34fSJonathan Roelofs     {
161e434b34fSJonathan Roelofs         assert(false);
162e434b34fSJonathan Roelofs     }
163e434b34fSJonathan Roelofs     catch (const Base2* c)
164e434b34fSJonathan Roelofs     {
165e434b34fSJonathan Roelofs         assert(true);
166e434b34fSJonathan Roelofs     }
167e434b34fSJonathan Roelofs     catch (...)
168e434b34fSJonathan Roelofs     {
169e434b34fSJonathan Roelofs         assert(false);
170e434b34fSJonathan Roelofs     }
171504bc07dSLouis Dionne 
172504bc07dSLouis Dionne     return 0;
173e434b34fSJonathan Roelofs }
174