userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
assert.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/utils/assert.hpp
4
/// @brief Assertion macros UASSERT, UASSERT_MSG, UINVARIANT and AbortWithStacktrace() function
5
/// @ingroup userver_universal
6
7
#
include
<
string_view
>
8
9
#
include
<
userver
/
utils
/
impl
/
source_location
.
hpp
>
10
11
USERVER_NAMESPACE_BEGIN
12
13
namespace
utils
{
14
15
namespace
impl {
16
17
// NOLINTNEXTLINE(readability-identifier-naming)
18
[[noreturn]]
void
UASSERT_failed(
19
std::string_view expr,
20
const
char
* file,
21
unsigned
int
line,
22
const
char
* function,
23
std::string_view msg
24
)
noexcept
;
25
26
[[noreturn]]
void
LogAndThrowInvariantError(
27
std::string_view condition,
28
std::string_view message,
29
utils
::impl::SourceLocation source_location
30
);
31
32
#
ifdef
NDEBUG
33
inline
constexpr
bool
kEnableAssert =
false
;
34
#
else
35
inline
constexpr
bool
kEnableAssert =
true
;
36
#
endif
37
38
extern
bool
dump_stacktrace_on_assert_failure;
39
40
}
// namespace impl
41
42
/// @brief Function that prints the stacktrace with message and aborts the program execution.
43
///
44
/// Mostly useful for placing in dead code branches or in 'should-never-happen-and-theres-no-way-to-restore' places.
45
[[
noreturn
]]
void
AbortWithStacktrace(std::string_view message)
noexcept
;
46
47
}
// namespace utils
48
49
USERVER_NAMESPACE_END
50
51
/// @brief Assertion macro for that aborts execution in DEBUG builds with a
52
/// message `msg` and does nothing in release builds
53
///
54
/// @hideinitializer
55
// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
56
#
define
UASSERT_MSG
(
expr
,
msg
)
57
do
{
58
if
(
USERVER_NAMESPACE
::
utils
::
impl
::
kEnableAssert
)
{
59
if
(
expr
)
{
60
}
else
{
61
USERVER_NAMESPACE
::
utils
::
impl
::
UASSERT_failed
(
#
expr
,
__FILE__
,
__LINE__
,
__func__
,
msg
)
;
62
}
63
}
64
}
while
(
0
)
65
66
/// @brief Assertion macro that aborts execution in DEBUG builds and does
67
/// nothing in release builds
68
///
69
/// @hideinitializer
70
// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
71
#
define
UASSERT
(
expr
)
UASSERT_MSG
(
expr
,
std
::
string_view
{
}
)
72
73
/// @brief Asserts in debug builds, throws utils::InvariantError in release
74
///
75
/// @hideinitializer
76
// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
77
#
define
UINVARIANT
(
condition
,
message
)
78
do
{
79
if
(
condition
)
{
80
}
else
{
81
if
constexpr
(
USERVER_NAMESPACE
::
utils
::
impl
::
kEnableAssert
)
{
82
USERVER_NAMESPACE
::
utils
::
impl
::
UASSERT_failed
(
#
condition
,
__FILE__
,
__LINE__
,
__func__
,
message
)
;
83
}
else
{
84
USERVER_NAMESPACE
::
utils
::
impl
::
LogAndThrowInvariantError
(
85
#
condition
,
86
message
,
87
USERVER_NAMESPACE
::
utils
::
impl
::
SourceLocation
::
Current
(
)
88
)
;
89
}
90
}
91
}
while
(
0
)
userver
utils
assert.hpp
Generated on
for userver by
Doxygen
1.17.0