icinga2/lib/base/debug.hpp
Johannes Schmidt 4f0a24f8ab Add ABORT() macro for unconditionally failing ASSERTS
This is necessary to stop MSVC complaining that "not all paths return a value", because
it is not able to infer that the expression `false ? 0 : non_returning_function()`
never returns.

In the process of debugging this I've also slightly simplified the other assert macros and
abort function, so they don't need compiler specific preprocessor-paths anymore.
2025-11-19 11:55:49 +01:00

25 lines
658 B
C++

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#ifndef DEBUG_H
#define DEBUG_H
#include "i2-base.hpp"
#ifndef I2_DEBUG
# define ASSERT(expr) ((void)0)
#else /* I2_DEBUG */
# define ASSERT(expr) ((expr) ? void(0) : icinga_assert_fail(#expr, __FILE__, __LINE__))
#endif /* I2_DEBUG */
#define VERIFY(expr) ((expr) ? void(0) : icinga_assert_fail(#expr, __FILE__, __LINE__))
#define ABORT(expr) icinga_assert_fail(#expr, __FILE__, __LINE__)
[[noreturn]] inline void icinga_assert_fail(const char *expr, const char *file, int line) noexcept(true)
{
fprintf(stderr, "%s:%d: assertion failed: %s\n", file, line, expr);
std::abort();
}
#endif /* DEBUG_H */