xref: /llvm-project/libcxxabi/test/catch_class_02.pass.cpp (revision eb8650a75793b2bd079d0c8901ff066f129061da)
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 #include <exception>
12 #include <stdlib.h>
13 #include <assert.h>
14 
15 struct B
16 {
17     static int count;
18     int id_;
BB19     explicit B(int id) : id_(id) {count++;}
BB20     B(const B& a) : id_(a.id_) {count++;}
~BB21     ~B() {count--;}
22 };
23 
24 int B::count = 0;
25 
26 struct A
27     : B
28 {
29     static int count;
30     int id_;
AA31     explicit A(int id) : B(id-1), id_(id) {count++;}
AA32     A(const A& a) : B(a.id_-1), id_(a.id_) {count++;}
~AA33     ~A() {count--;}
34 };
35 
36 int A::count = 0;
37 
f1()38 void f1()
39 {
40     assert(A::count == 0);
41     assert(B::count == 0);
42     A a(3);
43     assert(A::count == 1);
44     assert(B::count == 1);
45     throw a;
46     assert(false);
47 }
48 
f2()49 void f2()
50 {
51     try
52     {
53         assert(A::count == 0);
54         f1();
55     assert(false);
56     }
57     catch (A a)
58     {
59         assert(A::count != 0);
60         assert(B::count != 0);
61         assert(a.id_ == 3);
62         throw;
63     }
64     catch (B b)
65     {
66         assert(false);
67     }
68 }
69 
main(int,char **)70 int main(int, char**)
71 {
72     try
73     {
74         f2();
75         assert(false);
76     }
77     catch (const B& b)
78     {
79         assert(B::count != 0);
80         assert(b.id_ == 2);
81     }
82     assert(A::count == 0);
83     assert(B::count == 0);
84 
85     return 0;
86 }
87