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