Debian Codegraph @

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

raw
1 // ensure that exceptions thrown inside a custom contract violation handler
2 // are catchable up the call stack
3 // { dg-do run }
4 // { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
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 fun3() {
23 fun();
24 return 2;
25 }
26
27 int main(int, char**) {
28 try {
29 int x = 0;
30 [[ assert: x < 0 ]];
31 } catch(int &ex) {
32 std::cerr << "synth caught direct: " << ex << std::endl;
33 }
34
35 try {
36 fun();
37 } catch(int &ex) {
38 std::cerr << "synth caught indirect: " << ex << std::endl;
39 }
40
41 try {
42 fun3();
43 } catch(int &ex) {
44 std::cerr << "synth caught double indirect: " << ex << std::endl;
45 }
46
47 std::cerr << "end main" << std::endl;
48 return 0;
49 }
50
51 // { dg-skip-if "requires hosted libstdc++ for iostream" { ! hostedlib } }
52 // { dg-output "custom std::handle_contract_violation called: 30 .*/contracts14.C(\n|\r\n|\r)" }
53 // { dg-output "synth caught direct: -30(\n|\r\n|\r)" }
54 // { dg-output "custom std::handle_contract_violation called: 18 .*/contracts14.C(\n|\r\n|\r)" }
55 // { dg-output "synth caught indirect: -18(\n|\r\n|\r)" }
56 // { dg-output "custom std::handle_contract_violation called: 18 .*/contracts14.C(\n|\r\n|\r)" }
57 // { dg-output "synth caught double indirect: -18(\n|\r\n|\r)" }
58 // { dg-output "end main" }
59