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