Skip to content

Commit 0efe9c4

Browse files
Merge pull request gcc-mirror#105 from NinaRanns/invoke_default_contract_violation_handler
adding Invoke_default_contract_violation_handler
2 parents 80fdb3a + 52aac6a commit 0efe9c4

File tree

4 files changed

+146
-4
lines changed

4 files changed

+146
-4
lines changed

libstdc++-v3/include/std/contracts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ namespace contracts
106106
evaluation_semantic semantic() const noexcept { return _M_evaluation_semantic; }
107107
};
108108

109-
//void invoke_default_contract_violation_handler(const contract_violation&);
109+
void invoke_default_contract_violation_handler(const contract_violation&) noexcept;
110110

111111
} // namespace contracts
112112

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
# include <cxxabi.h>
3030
#endif
3131

32-
__attribute__ ((weak)) void
33-
handle_contract_violation (const std::contracts::contract_violation &violation) noexcept
32+
void __handle_contract_violation(const std::contracts::contract_violation &violation) noexcept
3433
{
3534
#if _GLIBCXX_HOSTED && _GLIBCXX_VERBOSE
3635

@@ -112,11 +111,43 @@ handle_contract_violation (const std::contracts::contract_violation &violation)
112111
#endif
113112
}
114113

114+
namespace std _GLIBCXX_VISIBILITY(default)
115+
{
116+
_GLIBCXX_BEGIN_NAMESPACE_VERSION
117+
118+
namespace contracts
119+
{
120+
121+
void invoke_default_contract_violation_handler(const std::contracts::contract_violation& violation) noexcept
122+
{
123+
return __handle_contract_violation(violation);
124+
}
125+
126+
}
127+
}
128+
129+
__attribute__ ((weak)) void
130+
handle_contract_violation (const std::contracts::contract_violation &violation)
131+
{
132+
return __handle_contract_violation(violation);
133+
}
134+
115135
#if _GLIBCXX_INLINE_VERSION
116136
// The compiler expects the contract_violation class to be in an unversioned
117137
// namespace, so provide a forwarding function with the expected symbol name.
118138
extern "C" void
119-
_Z25handle_contract_violationRKNSt12contract_violationE
139+
_Z25handle_contract_violationRKNSt9contracts18contract_violationE
120140
(const std::contracts::contract_violation &violation)
121141
{ handle_contract_violation(violation); }
142+
143+
extern "C" void
144+
_Z27__handle_contract_violationRKNSt9contracts18contract_violationE
145+
(const std::contracts::contract_violation &violation)
146+
{ __handle_contract_violation(violation); }
147+
148+
extern "C" void
149+
_Z41invoke_default_contract_violation_handlerRKNSt9contracts18contract_violationE
150+
(const std::contracts::contract_violation &violation)
151+
{ invoke_default_contract_violation_handler(violation); }
152+
122153
#endif
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (C) 2020-2025 Free Software Foundation, Inc.
2+
//
3+
// This file is part of the GNU ISO C++ Library. This library is free
4+
// software; you can redistribute it and/or modify it under the
5+
// terms of the GNU General Public License as published by the
6+
// Free Software Foundation; either version 3, or (at your option)
7+
// any later version.
8+
9+
// This library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License along
15+
// with this library; see the file COPYING3. If not see
16+
// <http://www.gnu.org/licenses/>.
17+
18+
// { dg-options "-g0 -fcontracts -fcontracts-nonattr -fcontract-evaluation-semantic=observe" }
19+
// { dg-do run { target c++2a } }
20+
21+
#include <contracts>
22+
#include <testsuite_hooks.h>
23+
24+
bool custom_called = false;
25+
26+
27+
void handle_contract_violation(const std::contracts::contract_violation& v)
28+
{
29+
invoke_default_contract_violation_handler(v);
30+
custom_called = true;
31+
}
32+
33+
void f(int i) pre (i>10) {};
34+
35+
int main()
36+
{
37+
f(0);
38+
VERIFY(custom_called);
39+
}
40+
// { dg-output "contract violation in function f at .*(\n|\r\n|\r)" }
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright (C) 2020-2025 Free Software Foundation, Inc.
2+
//
3+
// This file is part of the GNU ISO C++ Library. This library is free
4+
// software; you can redistribute it and/or modify it under the
5+
// terms of the GNU General Public License as published by the
6+
// Free Software Foundation; either version 3, or (at your option)
7+
// any later version.
8+
9+
// This library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License along
15+
// with this library; see the file COPYING3. If not see
16+
// <http://www.gnu.org/licenses/>.
17+
18+
// { dg-options "-g0 -fcontracts -fcontracts-nonattr -fcontract-evaluation-semantic=observe" }
19+
// { dg-do run { target c++2a } }
20+
21+
#include <contracts>
22+
#include <testsuite_hooks.h>
23+
#include <iostream>
24+
#include <sstream>
25+
26+
27+
struct checking_buf
28+
: public std::streambuf
29+
{
30+
bool written = false;
31+
32+
checking_buf() = default;
33+
34+
virtual int_type
35+
overflow(int_type)
36+
{
37+
written = true;
38+
return int_type();
39+
}
40+
41+
std::streamsize xsputn(const char* s, std::streamsize count)
42+
{
43+
written = true;
44+
return count;
45+
}
46+
47+
};
48+
49+
50+
bool custom_called = false;
51+
52+
53+
void handle_contract_violation(const std::contracts::contract_violation& v)
54+
{
55+
custom_called = true;
56+
}
57+
58+
59+
60+
61+
void f(int i) pre (i>10) {};
62+
63+
int main()
64+
{
65+
checking_buf buf;
66+
std::cerr.rdbuf(&buf);
67+
68+
f(0);
69+
VERIFY(!buf.written);
70+
}
71+

0 commit comments

Comments
 (0)