GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
assertions.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_ASSERTIONS_HPP
5#define GREM_CORE_ASSERTIONS_HPP
6
7//==============================================================================
8// Includes
9//==============================================================================
10
11#include <GREM/build_config.hpp>
12
13#if (!defined(GREM_ASSERT) || !defined(GREM_UNREACHABLE))
14#ifdef GREM_USE_RELEASE_ASSERTIONS
15#ifdef GREM_RELEASE_ASSERTIONS_FAST_FAIL
16#if !defined(_MSC_VER) && !defined(__clang__) && !defined(__GNUC__)
17
18#include <cstdlib> // IWYU pragma: keep // std::abort
19
20#endif
21#else
22
23#include <cstdio> // IWYU pragma: keep // stderr, std::fprintf
24#include <cstdlib> // IWYU pragma: keep // std::abort
25#include <source_location> // IWYU pragma: keep // std::source_location
26
27#endif
28#elif !defined(NDEBUG) || !defined(GREM_ASSUME_UNCHECKED_ASSERTIONS_NEVER_FAIL)
29#if !defined(_MSC_VER) && !defined(__clang__) && !defined(__GNUC__)
30
31#include <cstdlib> // IWYU pragma: keep // std::abort
32
33#endif
34#endif
35#endif
36
37//==============================================================================
38// GREM_ASSERT
39//==============================================================================
40
41#ifndef GREM_ASSERT
42#ifdef GREM_USE_RELEASE_ASSERTIONS
43#ifdef GREM_RELEASE_ASSERTIONS_FAST_FAIL
44#ifdef _MSC_VER
45#define GREM_ASSERT(expression) ((static_cast<bool>(expression)) ? (void)0 : __debugbreak())
46#elif defined(__clang__) || defined(__GNUC__)
47#define GREM_ASSERT(expression) ((static_cast<bool>(expression)) ? (void)0 : __builtin_trap())
48#else
49#define GREM_ASSERT(expression) ((static_cast<bool>(expression)) ? (void)0 : std::abort())
50#endif
51#else
52namespace grem {
53namespace detail {
54[[noreturn]] inline void assertionViolation(const char* expressionString, const std::source_location& source) {
55 std::fprintf(stderr, "Assertion failed: %s\n Source: %s:%d:%d\n Function: %s\n", expressionString, source.file_name(), static_cast<int>(source.line()),
56 static_cast<int>(source.column()), source.function_name());
57 std::abort();
58}
59} // namespace detail
60} // namespace grem
61#define GREM_ASSERT(expression) ((static_cast<bool>(expression)) ? (void)0 : grem::detail::assertionViolation(#expression, std::source_location::current()))
62#endif
63#elif defined(NDEBUG)
64#ifdef GREM_ASSUME_UNCHECKED_ASSERTIONS_NEVER_FAIL
65#if defined(__clang__)
66#define GREM_ASSERT(expression) __builtin_assume(expression)
67#elif defined(_MSC_VER)
68#define GREM_ASSERT(expression) __assume(expression)
69#else
70#define GREM_ASSERT(expression) ((void)0)
71#endif
72#else
73#define GREM_ASSERT(expression) ((void)0)
74#endif
75#else
76#include <cassert> // IWYU pragma: keep // assert
77#define GREM_ASSERT(expression) assert(expression)
78#endif
79#endif
80
81//==============================================================================
82// grem::unreachable()
83//==============================================================================
84
85#ifdef __clang__
86#pragma clang diagnostic push
87#pragma clang diagnostic ignored "-Winvalid-noreturn"
88#endif
89
90#if !defined(GREM_UNREACHABLE) && defined(GREM_USE_RELEASE_ASSERTIONS) && !defined(GREM_RELEASE_ASSERTIONS_FAST_FAIL)
91namespace grem {
92namespace detail {
93[[noreturn]] inline void unreachableViolation(const std::source_location& source) {
94 std::fprintf(stderr, "Unreachable violation.\n Source: %s:%d:%d\n Function: %s\n", source.file_name(), static_cast<int>(source.line()), static_cast<int>(source.column()),
95 source.function_name());
96 std::abort();
97}
98} // namespace detail
99} // namespace grem
100#endif
101
102namespace grem {
103
104#ifdef GREM_UNREACHABLE
105[[noreturn]] inline void unreachable() {
106 GREM_UNREACHABLE;
107}
108#elif defined(GREM_USE_RELEASE_ASSERTIONS)
109#ifdef GREM_RELEASE_ASSERTIONS_FAST_FAIL
110[[noreturn]] inline void unreachable() {
111#ifdef _MSC_VER
112 __debugbreak();
113#elif defined(__clang__) || defined(__GNUC__)
114 __builtin_trap();
115#else
116 std::abort();
117#endif
118}
119#else
120[[noreturn]] inline void unreachable(const std::source_location& source = std::source_location::current()) {
121 detail::unreachableViolation(source);
122}
123#endif
124#else
125[[noreturn]] inline void unreachable() {
126#if defined(NDEBUG) && defined(GREM_ASSUME_UNCHECKED_ASSERTIONS_NEVER_FAIL)
127#if defined(__clang__) || defined(__GNUC__)
128 __builtin_unreachable();
129#elif defined(_MSC_VER)
130 __assume(false);
131#endif
132#else
133#ifdef _MSC_VER
134 __debugbreak();
135#elif defined(__clang__) || defined(__GNUC__)
136 __builtin_trap();
137#else
138 std::abort();
139#endif
140#endif
141}
142#endif
143
144} // namespace grem
145
146#ifdef __clang__
147#pragma clang diagnostic pop
148#endif
149
150#endif
Definition aliases.hpp:9
void unreachable()
Definition assertions.hpp:125