Fortran: Fix regression caused by r14-10477 [PR59104]
[official-gcc.git] / libstdc++-v3 / testsuite / 18_support / uncaught_exceptions / uncaught_exceptions.cc
blob966c4367eee8c847d2f7d70dffa4c94a5253f4d5
1 // Copyright (C) 2015-2024 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-do run { target c++17 } }
19 // { dg-add-options no_pch }
21 #include <cassert>
22 #include <exception>
23 #include <testsuite_hooks.h>
25 #ifndef __cpp_lib_uncaught_exceptions
26 # error "Feature-test macro for uncaught_exceptions missing"
27 #elif __cpp_lib_uncaught_exceptions != 201411
28 # error "Feature-test macro for uncaught_exceptions has wrong value"
29 #endif
31 struct UncaughtVerifier
33 int expected_count_ = 0;
34 UncaughtVerifier(int expected_count) : expected_count_(expected_count) {}
35 ~UncaughtVerifier()
37 VERIFY(std::uncaught_exceptions() == expected_count_);
41 struct Transaction
43 int initial_count_;
44 bool& result_;
45 Transaction(bool& result)
46 : initial_count_(std::uncaught_exceptions()),
47 result_(result) {}
48 ~Transaction()
50 if (std::uncaught_exceptions() != initial_count_) {
51 result_ = false;
56 void test01()
58 try {
59 UncaughtVerifier uv{0};
60 } catch (...) {
61 UncaughtVerifier uv{0};
65 void test02()
67 try {
68 UncaughtVerifier uv{1};
69 throw 0;
70 } catch (...) {
71 UncaughtVerifier uv{0};
75 void test03()
77 try {
78 struct Wrap
80 UncaughtVerifier uv_{1};
81 ~Wrap() {try {UncaughtVerifier uv2{2}; throw 0;} catch(...) {}}
83 Wrap w;
84 throw 0;
85 } catch (...) {
86 UncaughtVerifier uv{0};
90 void test04()
92 bool result = true;
93 try {
94 Transaction t{result};
95 } catch(...) {
97 VERIFY(result);
100 void test05()
102 bool result = true;
103 try {
104 Transaction t{result};
105 throw 0;
106 } catch(...) {
108 VERIFY(!result);
111 void test06()
113 bool result = true;
114 bool result2 = true;
115 try {
116 struct Wrap
118 bool& result_;
119 Wrap(bool& result) : result_(result) {}
120 ~Wrap()
122 Transaction t{result_};
125 Transaction t{result};
126 Wrap w{result2};
127 throw 0;
128 } catch(...) {
130 VERIFY(!result);
131 VERIFY(result2);
134 void test07()
136 bool result = true;
137 bool result2 = true;
138 try {
139 struct Wrap
141 bool& result_;
142 Wrap(bool& result) : result_(result) {}
143 ~Wrap()
145 try {
146 Transaction t{result_};
147 throw 0;
148 } catch(...) {}
151 Transaction t{result};
152 Wrap w{result2};
153 throw 0;
154 } catch(...) {
156 VERIFY(!result);
157 VERIFY(!result2);
160 int main()
162 test01();
163 test02();
164 test03();
165 test04();
166 test05();
167 test06();
168 test07();