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