repr 0.1
Reconstructable string representations and more
Loading...
Searching...
No Matches
assert.h
Go to the documentation of this file.
1#pragma once
2#include <librepr/feature.h>
3#include "platform.h"
4#include "default.h"
5#include "format.h"
6
7#ifndef LIBREPR_DEBUG
8# if USING(LIBREPR_COMPILER_MSVC)
9# if defined(_DEBUG) && !defined(NDEBUG)
10# define LIBREPR_DEBUG 1
11# endif
12# else
13# if !defined(NDEBUG)
14# define LIBREPR_DEBUG 1
15# endif
16# endif
17#endif // LIBREPR_DEBUG?
18
19#if USING(LIBREPR_PLATFORM_WINDOWS)
20# if USING(LIBREPR_COMPILER_MSVC)
21// _CRT_*_C_HEADER macros
22# include <vcruntime.h>
23# define LIBREPR_CRT_BEGIN_C _CRT_BEGIN_C_HEADER
24# define LIBREPR_CRT_END_C _CRT_END_C_HEADER
25# else
26# define LIBREPR_CRT_BEGIN_C \
27 _Pragma("pack(push, 8)") \
28 extern "C" {
29# define LIBREPR_CRT_END_C } \
30 _Pragma("pack(pop)")
31# endif
32#endif
33#include <assert.h>
34
35// Underlying asserts
36#if USING(LIBREPR_PLATFORM_WINDOWS)
37# define LIBREPR_UND_ASSERT_(...) \
38 _assert((__VA_ARGS__), (__FILE__), unsigned(__LINE__))
39/*
40 * Expose the normal string assertion function.
41 * Windows generally uses `_wassert` for their
42 * `assert`, which we don't want.
43 */
44LIBREPR_CRT_BEGIN_C
45 __declspec(dllimport) void
46 __cdecl _assert( // NOLINT
47 char const* _Message,
48 char const* _File,
49 unsigned _Line
50 );
51LIBREPR_CRT_END_C
52
53#elif USING(LIBREPR_PLATFORM_MACOS)
54# define LIBREPR_UND_ASSERT_(...) \
55 __assert_rtn((__func__), (__FILE__), unsigned(__LINE__), (__VA_ARGS__))
56#else
57# define LIBREPR_UND_ASSERT_(...) \
58 __assert_fail((__VA_ARGS__), __FILE__, __LINE__, __PRETTY_FUNCTION__)
59#endif
60
61// Exposing assertion functions
62#ifdef LIBREPR_DEBUG
64# define LIBREPR_EXASSERT(...) \
65 LIBREPR_UND_ASSERT_((REPR_FORMAT(__VA_ARGS__)).c_str())
67# define LIBREPR_ASSERT(cond, ...) \
68 do { if(!(cond)) [[unlikely]] { LIBREPR_EXASSERT(__VA_ARGS__); } } while(0)
69#else
70# define LIBREPR_EXASSERT(...) ::librepr::detail::unreachable()
71# define LIBREPR_ASSERT(...) (void)(0)
72#endif
73
74#if USING(REPR_HARD_CHECKS)
75# define LIBREPR_SOFT_ASSERT(...) LIBREPR_ASSERT(__VA_ARGS__)
76#else
78# define LIBREPR_SOFT_ASSERT(...) (void)(0)
79#endif
80
81#ifdef __cpp_lib_unreachable
82# define LIBREPR_UNREACHABLE() std::unreachable()
83#elif USING(LIBREPR_COMPILER_MSVC)
84# define LIBREPR_UNREACHABLE() __assume(false)
85#else
86# define LIBREPR_UNREACHABLE() __builtin_unreachable()
87#endif
88
89namespace librepr::detail {
90#ifndef __cpp_lib_unreachable
91 [[noreturn]] inline void unreachable() {
93 }
94#else // Supports unreachable
95 using std::unreachable;
96#endif // __cpp_lib_unreachable
97} // namespace librepr::detail
#define LIBREPR_UNREACHABLE()
Definition assert.h:86
Definition assert.h:89
void unreachable()
Definition assert.h:91
std::string code_for()
Definition repr:39