assert.h (3462B)
1 // SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com> 2 // SPDX-License-Identifier: (GPL-3.0 OR 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 if (!(expr)) \ 13 { \ 14 Y_OnAssertFailed("Assertion failed: '" #expr "'", __FUNCTION__, __FILE__, __LINE__); \ 15 } 16 #define AssertMsg(expr, msg) \ 17 if (!(expr)) \ 18 { \ 19 Y_OnAssertFailed("Assertion failed: '" msg "'", __FUNCTION__, __FILE__, __LINE__); \ 20 } 21 22 #ifdef _DEBUG 23 #define DebugAssert(expr) \ 24 if (!(expr)) \ 25 { \ 26 Y_OnAssertFailed("Debug assertion failed: '" #expr "'", __FUNCTION__, __FILE__, __LINE__); \ 27 } 28 #define DebugAssertMsg(expr, msg) \ 29 if (!(expr)) \ 30 { \ 31 Y_OnAssertFailed("Debug assertion failed: '" msg "'", __FUNCTION__, __FILE__, __LINE__); \ 32 } 33 #else 34 #define DebugAssert(expr) 35 #define DebugAssertMsg(expr, msg) 36 #endif 37 38 // Panics the application, displaying an error message. 39 #define Panic(Message) Y_OnPanicReached("Panic triggered: '" Message "'", __FUNCTION__, __FILE__, __LINE__) 40 41 // Kills the application, indicating a pure function call that should not have happened. 42 #define PureCall() Y_OnPanicReached("PureCall encountered", __FUNCTION__, __FILE__, __LINE__) 43 44 #ifdef _DEBUG 45 // Kills the application, indicating that code that was never supposed to be reached has been executed. 46 #define UnreachableCode() Y_OnPanicReached("Unreachable code reached", __FUNCTION__, __FILE__, __LINE__) 47 #else 48 #define UnreachableCode() ASSUME(false) 49 #endif 50 51 // Helper for switch cases. 52 #define DefaultCaseIsUnreachable() \ 53 default: \ 54 UnreachableCode(); \ 55 break;