Debian Codegraph @

libstdc++-v3/src/experimental/contract.cc

raw
1 // -*- C++ -*- std::experimental::contract_violation and friends
2
3 // Copyright (C) 2019-2025 Free Software Foundation, Inc.
4 //
5 // This file is part of GCC.
6 //
7 // GCC is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11 //
12 // GCC is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
25
26 #include <experimental/contract>
27 #if _GLIBCXX_HOSTED && _GLIBCXX_VERBOSE
28 # include <iostream>
29 #endif
30
31 __attribute__ ((weak)) void
32 handle_contract_violation (const std::experimental::contract_violation &violation)
33 {
34 #if _GLIBCXX_HOSTED && _GLIBCXX_VERBOSE
35 bool level_default_p = violation.assertion_level() == "default";
36 bool role_default_p = violation.assertion_role() == "default";
37 bool cont_mode_default_p = violation.continuation_mode()
38 == std::experimental::contract_violation_continuation_mode::never_continue;
39
40 const char* modes[]{ "off", "on" }; // Must match enumerators in header.
41 std::cerr << "contract violation in function " << violation.function_name()
42 << " at " << violation.file_name() << ':' << violation.line_number()
43 << ": " << violation.comment();
44
45 const char* delimiter = "\n[";
46
47 if (!level_default_p)
48 {
49 std::cerr << delimiter << "level:" << violation.assertion_level();
50 delimiter = ", ";
51 }
52 if (!role_default_p)
53 {
54 std::cerr << delimiter << "role:" << violation.assertion_role();
55 delimiter = ", ";
56 }
57 if (!cont_mode_default_p)
58 {
59 std::cerr << delimiter << "continue:"
60 << modes[(int)violation.continuation_mode() & 1];
61 delimiter = ", ";
62 }
63
64 if (delimiter[0] == ',')
65 std::cerr << ']';
66
67 std::cerr << std::endl;
68 #endif
69 }
70
71 #if _GLIBCXX_INLINE_VERSION
72 // The compiler expects the contract_violation class to be in an unversioned
73 // namespace, so provide a forwarding function with the expected symbol name.
74 extern "C" void
75 _Z25handle_contract_violationRKNSt12experimental18contract_violationE
76 (const std::experimental::contract_violation &violation)
77 { handle_contract_violation(violation); }
78 #endif