Debian Codegraph @

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

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