Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
Assert.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/Compiler.h" // SC_COMPILER_DEBUG_BREAK
5#include "../Foundation/LibC.h" // exit
6#include "../Foundation/Platform.h" // SC_CONFIGURATION_DEBUG
7
8namespace SC
9{
10struct SC_COMPILER_EXPORT Assert;
11}
14
17{
18 [[noreturn]] SC_COMPILER_FORCE_INLINE static void unreachable()
19 {
20#if SC_COMPILER_MSVC
21 __assume(false);
22#else
23 __builtin_unreachable();
24#endif
25 }
27 [[noreturn]] static void exit(int code);
28
30 static void printBacktrace(const char* expression, const char* filename, const char* function, int line);
31
32 private:
33 struct Internal;
34};
36
39
42#define SC_ASSERT_RELEASE(e) \
43 if (!(e)) \
44 SC_LANGUAGE_UNLIKELY \
45 { \
46 SC::Assert::printBacktrace(#e, __FILE__, __func__, __LINE__); \
47 SC_COMPILER_DEBUG_BREAK; \
48 SC::Assert::exit(-1); \
49 } \
50 (void)0
51
54#if SC_CONFIGURATION_DEBUG
55#define SC_ASSERT_DEBUG(e) SC_ASSERT_RELEASE(e)
56#else
57#define SC_ASSERT_DEBUG(e) (void)0
58#endif
59
61#define SC_TRUST_RESULT(expression) SC_ASSERT_RELEASE(expression)
62
#define SC_COMPILER_FORCE_INLINE
Macro for forcing inline functions.
Definition Compiler.h:46
Functions and macros to assert, exit() or abort() and capture backtraces.
Definition Assert.h:17
static void printBacktrace(const char *expression, const char *filename, const char *function, int line)
Prints an assertion to standard output.
static void exit(int code)
Exits current process with the given code.