Common: Add ASSUME() and use for UnreachableCode()

This commit is contained in:
Stenzek 2023-09-03 14:42:37 +10:00
parent 80e3d29ab8
commit c0af478dfd
3 changed files with 20 additions and 5 deletions

View file

@ -12,6 +12,10 @@
#include <tlhelp32.h> #include <tlhelp32.h>
#endif #endif
#ifdef __clang__
#pragma clang diagnostic ignored "-Winvalid-noreturn"
#endif
static std::mutex s_AssertFailedMutex; static std::mutex s_AssertFailedMutex;
static inline void FreezeThreads(void** ppHandle) static inline void FreezeThreads(void** ppHandle)
@ -107,7 +111,7 @@ void Y_OnAssertFailed(const char* szMessage, const char* szFunction, const char*
ResumeThreads(pHandle); ResumeThreads(pHandle);
} }
void Y_OnPanicReached(const char* szMessage, const char* szFunction, const char* szFile, unsigned uLine) [[noreturn]] void Y_OnPanicReached(const char* szMessage, const char* szFunction, const char* szFile, unsigned uLine)
{ {
std::lock_guard<std::mutex> guard(s_AssertFailedMutex); std::lock_guard<std::mutex> guard(s_AssertFailedMutex);

View file

@ -1,10 +1,12 @@
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com> // SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#pragma once #pragma once
#include "types.h"
void Y_OnAssertFailed(const char* szMessage, const char* szFunction, const char* szFile, unsigned uLine); void Y_OnAssertFailed(const char* szMessage, const char* szFunction, const char* szFile, unsigned uLine);
void Y_OnPanicReached(const char* szMessage, const char* szFunction, const char* szFile, unsigned uLine); [[noreturn]] void Y_OnPanicReached(const char* szMessage, const char* szFunction, const char* szFile, unsigned uLine);
#define Assert(expr) \ #define Assert(expr) \
if (!(expr)) \ if (!(expr)) \
@ -28,11 +30,9 @@ void Y_OnPanicReached(const char* szMessage, const char* szFunction, const char*
{ \ { \
Y_OnAssertFailed("Debug assertion failed: '" msg "'", __FUNCTION__, __FILE__, __LINE__); \ Y_OnAssertFailed("Debug assertion failed: '" msg "'", __FUNCTION__, __FILE__, __LINE__); \
} }
#define DebugUnreachableCode() Y_OnPanicReached("Unreachable code reached", __FUNCTION__, __FILE__, __LINE__)
#else #else
#define DebugAssert(expr) #define DebugAssert(expr)
#define DebugAssertMsg(expr, msg) #define DebugAssertMsg(expr, msg)
#define DebugUnreachableCode()
#endif #endif
// Panics the application, displaying an error message. // Panics the application, displaying an error message.
@ -41,8 +41,12 @@ void Y_OnPanicReached(const char* szMessage, const char* szFunction, const char*
// Kills the application, indicating a pure function call that should not have happened. // Kills the application, indicating a pure function call that should not have happened.
#define PureCall() Y_OnPanicReached("PureCall encountered", __FUNCTION__, __FILE__, __LINE__) #define PureCall() Y_OnPanicReached("PureCall encountered", __FUNCTION__, __FILE__, __LINE__)
#ifdef _DEBUG
// Kills the application, indicating that code that was never supposed to be reached has been executed. // Kills the application, indicating that code that was never supposed to be reached has been executed.
#define UnreachableCode() Y_OnPanicReached("Unreachable code reached", __FUNCTION__, __FILE__, __LINE__) #define UnreachableCode() Y_OnPanicReached("Unreachable code reached", __FUNCTION__, __FILE__, __LINE__)
#else
#define UnreachableCode() ASSUME(false)
#endif
// Helper for switch cases. // Helper for switch cases.
#define DefaultCaseIsUnreachable() \ #define DefaultCaseIsUnreachable() \

View file

@ -73,6 +73,13 @@ char (&__countof_ArraySizeHelper(T (&array)[N]))[N];
#define NORETURN_FUNCTION_POINTER __attribute__((noreturn)) #define NORETURN_FUNCTION_POINTER __attribute__((noreturn))
#endif #endif
// __assume, potentially enables optimization.
#ifdef _MSC_VER
#define ASSUME(x) __assume(x)
#else
#define ASSUME(x) do { if (!(x)) __builtin_unreachable(); } while(0)
#endif
// disable warnings that show up at warning level 4 // disable warnings that show up at warning level 4
// TODO: Move to build system instead // TODO: Move to build system instead
#ifdef _MSC_VER #ifdef _MSC_VER