Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/src/common/assert.h
4223 views
1
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <[email protected]>
2
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
3
4
#pragma once
5
6
#include "types.h"
7
8
void Y_OnAssertFailed(const char* szMessage, const char* szFunction, const char* szFile, unsigned uLine);
9
[[noreturn]] void Y_OnPanicReached(const char* szMessage, const char* szFunction, const char* szFile, unsigned uLine);
10
11
#define Assert(expr) \
12
do \
13
{ \
14
if (!(expr)) \
15
Y_OnAssertFailed("Assertion failed: '" #expr "'", __FUNCTION__, __FILE__, __LINE__); \
16
} while (0)
17
#define AssertMsg(expr, msg) \
18
do \
19
{ \
20
if (!(expr)) \
21
Y_OnAssertFailed("Assertion failed: '" msg "'", __FUNCTION__, __FILE__, __LINE__); \
22
} while (0)
23
24
#if defined(_DEBUG) || defined(_DEVEL)
25
#define DebugAssert(expr) \
26
do \
27
{ \
28
if (!(expr)) \
29
Y_OnAssertFailed("Debug assertion failed: '" #expr "'", __FUNCTION__, __FILE__, __LINE__); \
30
} while (0)
31
#define DebugAssertMsg(expr, msg) \
32
do \
33
{ \
34
if (!(expr)) \
35
Y_OnAssertFailed("Debug assertion failed: '" msg "'", __FUNCTION__, __FILE__, __LINE__); \
36
} while (0)
37
#else
38
#define DebugAssert(expr)
39
#define DebugAssertMsg(expr, msg)
40
#endif
41
42
// Panics the application, displaying an error message.
43
#define Panic(Message) Y_OnPanicReached("Panic triggered: '" Message "'", __FUNCTION__, __FILE__, __LINE__)
44
45
// Kills the application, indicating a pure function call that should not have happened.
46
#define PureCall() Y_OnPanicReached("PureCall encountered", __FUNCTION__, __FILE__, __LINE__)
47
48
#if defined(_DEBUG) || defined(_DEVEL)
49
// Kills the application, indicating that code that was never supposed to be reached has been executed.
50
#define UnreachableCode() Y_OnPanicReached("Unreachable code reached", __FUNCTION__, __FILE__, __LINE__)
51
#else
52
#define UnreachableCode() ASSUME(false)
53
#endif
54
55
// Helper for switch cases.
56
#define DefaultCaseIsUnreachable() \
57
default: \
58
UnreachableCode(); \
59
break;
60
61