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