1*eb8650a7SLouis Dionne //===----------------------------------------------------------------------===//
2e434b34fSJonathan Roelofs //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e434b34fSJonathan Roelofs //
7e434b34fSJonathan Roelofs //===----------------------------------------------------------------------===//
8e434b34fSJonathan Roelofs
9e434b34fSJonathan Roelofs /*
10e434b34fSJonathan Roelofs This test checks that adjustedPtr is correct as there exist offsets in this
11e434b34fSJonathan Roelofs object for the various subobjects, all of which have a unique id_ to
12e434b34fSJonathan Roelofs check against. It also checks that virtual bases work properly
13e434b34fSJonathan Roelofs */
14e434b34fSJonathan Roelofs
158c61114cSLouis Dionne // UNSUPPORTED: no-exceptions
1657e446daSAsiri Rathnayake
17d9eb6c7cSLouis Dionne // Compilers emit warnings about exceptions of type 'Child' being caught by
18d9eb6c7cSLouis Dionne // an earlier handler of type 'Base'. Congrats, you've just diagnosed the
19d9eb6c7cSLouis Dionne // behavior under test.
20d9eb6c7cSLouis Dionne // ADDITIONAL_COMPILE_FLAGS: -Wno-exceptions
218d313927SLouis Dionne
22e434b34fSJonathan Roelofs #include <exception>
23e434b34fSJonathan Roelofs #include <stdlib.h>
24e434b34fSJonathan Roelofs #include <assert.h>
25e434b34fSJonathan Roelofs
26e434b34fSJonathan Roelofs struct B
27e434b34fSJonathan Roelofs {
28e434b34fSJonathan Roelofs static int count;
29e434b34fSJonathan Roelofs int id_;
BB30e434b34fSJonathan Roelofs explicit B(int id) : id_(id) {count++;}
BB31e434b34fSJonathan Roelofs B(const B& a) : id_(a.id_) {count++;}
~BB32e434b34fSJonathan Roelofs ~B() {count--;}
33e434b34fSJonathan Roelofs };
34e434b34fSJonathan Roelofs
35e434b34fSJonathan Roelofs int B::count = 0;
36e434b34fSJonathan Roelofs
37e434b34fSJonathan Roelofs struct C1
38e434b34fSJonathan Roelofs : virtual B
39e434b34fSJonathan Roelofs {
40e434b34fSJonathan Roelofs static int count;
41e434b34fSJonathan Roelofs int id_;
C1C142e434b34fSJonathan Roelofs explicit C1(int id) : B(id-2), id_(id) {count++;}
C1C143e434b34fSJonathan Roelofs C1(const C1& a) : B(a.id_-2), id_(a.id_) {count++;}
~C1C144e434b34fSJonathan Roelofs ~C1() {count--;}
45e434b34fSJonathan Roelofs };
46e434b34fSJonathan Roelofs
47e434b34fSJonathan Roelofs int C1::count = 0;
48e434b34fSJonathan Roelofs
49e434b34fSJonathan Roelofs struct C2
50e434b34fSJonathan Roelofs : virtual private B
51e434b34fSJonathan Roelofs {
52e434b34fSJonathan Roelofs static int count;
53e434b34fSJonathan Roelofs int id_;
C2C254e434b34fSJonathan Roelofs explicit C2(int id) : B(id-2), id_(id) {count++;}
C2C255e434b34fSJonathan Roelofs C2(const C2& a) : B(a.id_-2), id_(a.id_) {count++;}
~C2C256e434b34fSJonathan Roelofs ~C2() {count--;}
57e434b34fSJonathan Roelofs };
58e434b34fSJonathan Roelofs
59e434b34fSJonathan Roelofs int C2::count = 0;
60e434b34fSJonathan Roelofs
61e434b34fSJonathan Roelofs struct A
62e434b34fSJonathan Roelofs : C1, C2
63e434b34fSJonathan Roelofs {
64e434b34fSJonathan Roelofs static int count;
65e434b34fSJonathan Roelofs int id_;
AA6678621e40SEric Fiselier explicit A(int id) : B(id+3), C1(id-1), C2(id-2), id_(id) {count++;}
AA6778621e40SEric Fiselier A(const A& a) : B(a.id_+3), C1(a.id_-1), C2(a.id_-2), id_(a.id_) {count++;}
~AA68e434b34fSJonathan Roelofs ~A() {count--;}
69e434b34fSJonathan Roelofs };
70e434b34fSJonathan Roelofs
71e434b34fSJonathan Roelofs int A::count = 0;
72e434b34fSJonathan Roelofs
738d313927SLouis Dionne A global_a(5);
74e434b34fSJonathan Roelofs
f1()75e434b34fSJonathan Roelofs void f1()
76e434b34fSJonathan Roelofs {
778d313927SLouis Dionne throw &global_a;
78e434b34fSJonathan Roelofs assert(false);
79e434b34fSJonathan Roelofs }
80e434b34fSJonathan Roelofs
f2()81e434b34fSJonathan Roelofs void f2()
82e434b34fSJonathan Roelofs {
83e434b34fSJonathan Roelofs try
84e434b34fSJonathan Roelofs {
85e434b34fSJonathan Roelofs f1();
86e434b34fSJonathan Roelofs assert(false);
87e434b34fSJonathan Roelofs }
88e434b34fSJonathan Roelofs catch (const A* a) // can catch A
89e434b34fSJonathan Roelofs {
90e434b34fSJonathan Roelofs assert(a->id_ == 5);
91e434b34fSJonathan Roelofs assert(static_cast<const C1*>(a)->id_ == 4);
92e434b34fSJonathan Roelofs assert(static_cast<const C2*>(a)->id_ == 3);
93e434b34fSJonathan Roelofs assert(static_cast<const B*>(a)->id_ == 8);
94e434b34fSJonathan Roelofs throw;
95e434b34fSJonathan Roelofs }
96e434b34fSJonathan Roelofs catch (const C1*)
97e434b34fSJonathan Roelofs {
98e434b34fSJonathan Roelofs assert(false);
99e434b34fSJonathan Roelofs }
100e434b34fSJonathan Roelofs catch (const C2*)
101e434b34fSJonathan Roelofs {
102e434b34fSJonathan Roelofs assert(false);
103e434b34fSJonathan Roelofs }
104e434b34fSJonathan Roelofs catch (const B*)
105e434b34fSJonathan Roelofs {
106e434b34fSJonathan Roelofs assert(false);
107e434b34fSJonathan Roelofs }
108e434b34fSJonathan Roelofs }
109e434b34fSJonathan Roelofs
f3()110e434b34fSJonathan Roelofs void f3()
111e434b34fSJonathan Roelofs {
112e434b34fSJonathan Roelofs try
113e434b34fSJonathan Roelofs {
114e434b34fSJonathan Roelofs f2();
115e434b34fSJonathan Roelofs assert(false);
116e434b34fSJonathan Roelofs }
117e434b34fSJonathan Roelofs catch (const B* a) // can catch B
118e434b34fSJonathan Roelofs {
119e434b34fSJonathan Roelofs assert(static_cast<const B*>(a)->id_ == 8);
120e434b34fSJonathan Roelofs throw;
121e434b34fSJonathan Roelofs }
122e434b34fSJonathan Roelofs catch (const C1* c1)
123e434b34fSJonathan Roelofs {
124e434b34fSJonathan Roelofs assert(false);
125e434b34fSJonathan Roelofs }
126e434b34fSJonathan Roelofs catch (const C2*)
127e434b34fSJonathan Roelofs {
128e434b34fSJonathan Roelofs assert(false);
129e434b34fSJonathan Roelofs }
130e434b34fSJonathan Roelofs }
131e434b34fSJonathan Roelofs
f4()132e434b34fSJonathan Roelofs void f4()
133e434b34fSJonathan Roelofs {
134e434b34fSJonathan Roelofs try
135e434b34fSJonathan Roelofs {
136e434b34fSJonathan Roelofs f3();
137e434b34fSJonathan Roelofs assert(false);
138e434b34fSJonathan Roelofs }
139e434b34fSJonathan Roelofs catch (const C2* c2) // can catch C2
140e434b34fSJonathan Roelofs {
141e434b34fSJonathan Roelofs assert(c2->id_ == 3);
142e434b34fSJonathan Roelofs throw;
143e434b34fSJonathan Roelofs }
144e434b34fSJonathan Roelofs catch (const B* a)
145e434b34fSJonathan Roelofs {
146e434b34fSJonathan Roelofs assert(false);
147e434b34fSJonathan Roelofs }
148e434b34fSJonathan Roelofs catch (const C1*)
149e434b34fSJonathan Roelofs {
150e434b34fSJonathan Roelofs assert(false);
151e434b34fSJonathan Roelofs }
152e434b34fSJonathan Roelofs }
153e434b34fSJonathan Roelofs
f5()154e434b34fSJonathan Roelofs void f5()
155e434b34fSJonathan Roelofs {
156e434b34fSJonathan Roelofs try
157e434b34fSJonathan Roelofs {
158e434b34fSJonathan Roelofs f4();
159e434b34fSJonathan Roelofs assert(false);
160e434b34fSJonathan Roelofs }
161e434b34fSJonathan Roelofs catch (const C1* c1) // can catch C1
162e434b34fSJonathan Roelofs {
163e434b34fSJonathan Roelofs assert(c1->id_ == 4);
164e434b34fSJonathan Roelofs assert(static_cast<const B*>(c1)->id_ == 8);
165e434b34fSJonathan Roelofs throw;
166e434b34fSJonathan Roelofs }
167e434b34fSJonathan Roelofs catch (const B* a)
168e434b34fSJonathan Roelofs {
169e434b34fSJonathan Roelofs assert(false);
170e434b34fSJonathan Roelofs }
171e434b34fSJonathan Roelofs catch (const C2*)
172e434b34fSJonathan Roelofs {
173e434b34fSJonathan Roelofs assert(false);
174e434b34fSJonathan Roelofs }
175e434b34fSJonathan Roelofs }
176e434b34fSJonathan Roelofs
main(int,char **)177504bc07dSLouis Dionne int main(int, char**)
178e434b34fSJonathan Roelofs {
179e434b34fSJonathan Roelofs try
180e434b34fSJonathan Roelofs {
181e434b34fSJonathan Roelofs f5();
182e434b34fSJonathan Roelofs assert(false);
183e434b34fSJonathan Roelofs }
184e434b34fSJonathan Roelofs catch (...)
185e434b34fSJonathan Roelofs {
186e434b34fSJonathan Roelofs }
187504bc07dSLouis Dionne
188504bc07dSLouis Dionne return 0;
189e434b34fSJonathan Roelofs }
190