FE 0.10.2
Header-only C++ frontend library
Loading...
Searching...
No Matches
assert.h
Go to the documentation of this file.
1#pragma once
2
3#include <cassert>
4#include <cstdlib>
5#include <format>
6#include <stdexcept>
7#include <utility>
8
9namespace fe {
10
11/// Throws a `T` (a `std::logic_error` by default) whose message is `std::format(fmt, args...)`.
12/// Use this for unrecoverable errors that should surface as a proper exception with a formatted message.
13template<class T = std::logic_error, class... Args>
14[[noreturn]] void throwf(std::format_string<Args...> fmt, Args&&... args) {
15 throw T("error: " + std::format(fmt, std::forward<Args>(args)...));
16}
17
18/// @sa https://stackoverflow.com/a/65258501
19#ifdef __GNUC__ // GCC 4.8+, Clang, Intel and other compilers compatible with GCC (-std=c++0x or above)
20[[noreturn]] inline __attribute__((always_inline)) void unreachable() {
21 assert(false);
22 __builtin_unreachable();
23}
24#elif defined(_MSC_VER) // MSVC
25[[noreturn]] __forceinline void unreachable() {
26 assert(false);
27 __assume(false);
28}
29#else // ???
30[[noreturn]] inline void unreachable() {
31 assert(false);
32 std::abort();
33}
34#endif
35
36/// Raise a breakpoint in the debugger.
37#if (defined(__clang__) || defined(__GNUC__)) && (defined(__x86_64__) || defined(__i386__))
38inline void breakpoint() { asm("int3"); }
39#else
40inline void breakpoint() {
41 volatile int* p = nullptr;
42 *p = 42;
43}
44#endif
45
46} // namespace fe
47
48#ifndef NDEBUG
49# define assert_unused(x) assert(x)
50#else
51# define assert_unused(x) ((void)(0 && (x)))
52#endif
Definition arena.h:13
void throwf(std::format_string< Args... > fmt, Args &&... args)
Throws a T (a std::logic_error by default) whose message is std::format(fmt, args....
Definition assert.h:14
void breakpoint()
Raise a breakpoint in the debugger.
Definition assert.h:40
void unreachable()
Definition assert.h:30