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