libstdc++: Improve output of default contract violation handler [PR107792]
[official-gcc.git] / gcc / testsuite / g++.dg / contracts / contracts15.C
blobef52a0e67f036a59a892dfd0baffee7813a7aaf8
1 // ensure that exceptions thrown inside a custom contract violation handler
2 // are not catchable up the call stack when failing in a noexcept function
3 // { dg-do run }
4 // { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
5 #include <iostream>
6 #include <experimental/contract>
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();
16 int fun() noexcept {
17   int x = 0;
18   [[ assert: x < 0 ]];
19   return 0;
22 int fun3() {
23   fun();
24   return 2;
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   }
35   try {
36     fun();
37   } catch(int &ex) {
38     std::cerr << "synth caught indirect: " << ex << std::endl;
39   }
41   try {
42     fun3();
43   } catch(int &ex) {
44     std::cerr << "synth caught double indirect: " << ex << std::endl;
45   }
47   std::cerr << "end main" << std::endl;
48   return 0;
51 // { dg-output "custom std::handle_contract_violation called: 30 .*/contracts15.C(\n|\r\n|\r)" }
52 // { dg-output "synth caught direct: -30(\n|\r\n|\r)" }
53 // { dg-output "custom std::handle_contract_violation called: 18 .*/contracts15.C(\n|\r\n|\r)" }
54 // { dg-output "terminate called after throwing an instance of .int.(\n|\r\n|\r)" }
55 // { dg-shouldfail "throwing in noexcept" }