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