forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cata_assert.h
38 lines (36 loc) · 1.39 KB
/
cata_assert.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#pragma once
// NOLINTNEXTLINE(cata-header-guard)
// Due to an inability to suppress assert popups when building against mingw-w64
// and running on wine, and to suppress unused variable warnings in release builds,
// we are wrapping the assert macro so that we can substitute functional behavior.
// This copies the semantics of cassert, re-including the file re-defines the macro.
#undef cata_assert
// Might as well handle NDEBUG at the top level instead of just wrapping one variant.
#ifdef NDEBUG
// Use the expression to (hopefully) avoid unused variable warnings, hint compiler with
// unreachable intrinsics, and place the code in decltype to avoid actual evaluation.
#if defined(_MSC_VER)
#define cata_assert(exp) decltype((exp) ? void() : __assume(0))()
#elif defined(__GNUC__) || defined(__clang__)
#define cata_assert(exp) decltype((exp) ? void() : __builtin_unreachable())()
#else
#include <cstdlib>
#define cata_assert(exp) decltype((exp) ? void() : std::abort())()
#endif
#else
#ifdef _WIN32
#include <cstdlib>
#include <cstdio>
#define cata_assert(expression) \
do { \
if( expression ) { \
break; \
} \
fprintf( stderr, "%s at %s:%d: Assertion `%s` failed.\n", __func__, __FILE__, __LINE__, #expression ); \
std::abort(); \
} while( false )
#else
#include <cassert>
#define cata_assert(expression) assert(expression)
#endif // _WIN32
#endif // NDEBUG