blob: 44bf2e2af518238d7257edab6cc3ff49ef361dd9 [file] [log] [blame]
Howard Hinnant4c3bdd62012-02-01 21:25:40 +00001//===------------------------- catch_ptr_02.cpp ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Asiri Rathnayake4174e8b2016-05-31 12:01:32 +000010// UNSUPPORTED: libcxxabi-no-exceptions
11
Howard Hinnant4c3bdd62012-02-01 21:25:40 +000012#include <cassert>
13
Eric Fiselierdf448d82016-06-15 19:07:19 +000014// Clang emits warnings about exceptions of type 'Child' being caught by
15// an earlier handler of type 'Base'. Congrats clang, you've just
16// diagnosed the behavior under test.
17#if defined(__clang__)
18#pragma clang diagnostic ignored "-Wexceptions"
19#endif
20
Eric Fiselierb16496b2015-08-20 01:22:17 +000021#if __cplusplus < 201103L
22#define DISABLE_NULLPTR_TESTS
23#endif
24
Howard Hinnant4c3bdd62012-02-01 21:25:40 +000025struct A {};
26A a;
27const A ca = A();
28
29void test1 ()
30{
31 try
32 {
33 throw &a;
34 assert(false);
35 }
36 catch ( const A* )
37 {
38 }
39 catch ( A *)
40 {
41 assert (false);
42 }
43}
44
45void test2 ()
46{
47 try
48 {
Marshall Clow288859d2014-02-05 18:19:57 +000049 throw &a;
Howard Hinnant4c3bdd62012-02-01 21:25:40 +000050 assert(false);
51 }
52 catch ( A* )
53 {
54 }
55 catch ( const A *)
56 {
57 assert (false);
58 }
59}
60
61void test3 ()
62{
63 try
64 {
65 throw &ca;
66 assert(false);
67 }
68 catch ( const A* )
69 {
70 }
71 catch ( A *)
72 {
73 assert (false);
74 }
75}
76
77void test4 ()
78{
79 try
80 {
81 throw &ca;
82 assert(false);
83 }
84 catch ( A *)
85 {
86 assert (false);
87 }
88 catch ( const A* )
89 {
90 }
91}
92
Marshall Clow288859d2014-02-05 18:19:57 +000093struct base1 {int x;};
94struct base2 {int x;};
95struct derived : base1, base2 {};
96
97void test5 ()
98{
99 try
100 {
101 throw (derived*)0;
102 assert(false);
103 }
104 catch (base2 *p) {
105 assert (p == 0);
106 }
107 catch (...)
108 {
109 assert (false);
110 }
111}
112
113void test6 ()
114{
Eric Fiselierb16496b2015-08-20 01:22:17 +0000115#if !defined(DISABLE_NULLPTR_TESTS)
Marshall Clow288859d2014-02-05 18:19:57 +0000116 try
117 {
118 throw nullptr;
119 assert(false);
120 }
121 catch (base2 *p) {
122 assert (p == nullptr);
123 }
124 catch (...)
125 {
126 assert (false);
127 }
Eric Fiselierb16496b2015-08-20 01:22:17 +0000128#endif
Marshall Clow288859d2014-02-05 18:19:57 +0000129}
130
131void test7 ()
132{
133 try
134 {
135 throw (derived*)12;
136 assert(false);
137 }
138 catch (base2 *p) {
139 assert ((unsigned long)p == 12+sizeof(base1));
140 }
141 catch (...)
142 {
143 assert (false);
144 }
145}
146
147
Marshall Clow98bbf282014-02-06 04:47:02 +0000148struct vBase {};
149struct vDerived : virtual public vBase {};
Marshall Clow288859d2014-02-05 18:19:57 +0000150
151void test8 ()
152{
Eric Fiselier6e50cba2014-11-21 01:53:51 +0000153 vDerived derived;
Marshall Clow288859d2014-02-05 18:19:57 +0000154 try
155 {
Eric Fiselier6e50cba2014-11-21 01:53:51 +0000156 throw &derived;
Marshall Clow288859d2014-02-05 18:19:57 +0000157 assert(false);
158 }
Marshall Clow98bbf282014-02-06 04:47:02 +0000159 catch (vBase *p) {
160 assert(p != 0);
161 }
162 catch (...)
163 {
164 assert (false);
165 }
166}
167
168void test9 ()
169{
Eric Fiselierb16496b2015-08-20 01:22:17 +0000170#if !defined(DISABLE_NULLPTR_TESTS)
Marshall Clow98bbf282014-02-06 04:47:02 +0000171 try
172 {
173 throw nullptr;
174 assert(false);
175 }
176 catch (vBase *p) {
177 assert(p == 0);
178 }
179 catch (...)
180 {
181 assert (false);
182 }
Eric Fiselierb16496b2015-08-20 01:22:17 +0000183#endif
Marshall Clow98bbf282014-02-06 04:47:02 +0000184}
185
186void test10 ()
187{
188 try
189 {
190 throw (vDerived*)0;
191 assert(false);
192 }
193 catch (vBase *p) {
Marshall Clow288859d2014-02-05 18:19:57 +0000194 assert(p == 0);
195 }
196 catch (...)
197 {
198 assert (false);
199 }
200}
201
Howard Hinnant4c3bdd62012-02-01 21:25:40 +0000202int main()
203{
204 test1();
205 test2();
206 test3();
207 test4();
Marshall Clow288859d2014-02-05 18:19:57 +0000208 test5();
209 test6();
210 test7();
Marshall Clow98bbf282014-02-06 04:47:02 +0000211 test8();
212 test9();
213 test10();
Howard Hinnant4c3bdd62012-02-01 21:25:40 +0000214}