Debian Codegraph @

gcc/testsuite/g++.dg/contracts/contracts17.C

raw
1 // ensure that exceptions thrown inside a custom contract violation handler
2 // are not catchable up the call stack when continue mode is off and the
3 // assert fails in a noexcept function
4 // { dg-do run }
5 // { dg-options "-std=c++2a -fcontracts" }
6 #include <iostream>
7 #include <experimental/contract>
8
9 void handle_contract_violation(const std::experimental::contract_violation &violation) {
10 std::cerr << "custom std::handle_contract_violation called:"
11 << " " << violation.line_number()
12 << " " << violation.file_name()
13 << std::endl;
14 throw -violation.line_number();
15 }
16
17 int fun() noexcept {
18 int x = 0;
19 [[ assert: x < 0 ]];
20 return 0;
21 }
22
23 int main(int, char**) {
24 try {
25 fun();
26 } catch(int &ex) {
27 std::cerr << "synth caught indirect: " << ex << std::endl;
28 }
29
30 return 0;
31 }
32
33 // { dg-skip-if "requires hosted libstdc++ for iostream" { ! hostedlib } }
34 // { dg-output "custom std::handle_contract_violation called: 19 .*/contracts17.C(\n|\r\n|\r)" }
35 // { dg-shouldfail "throwing in noexcept" }
36