From c0af478dfd07ae267dd957a490ffe9f2cc234d1b Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sun, 3 Sep 2023 14:42:37 +1000 Subject: [PATCH] Common: Add ASSUME() and use for UnreachableCode() --- src/common/assert.cpp | 6 +++++- src/common/assert.h | 12 ++++++++---- src/common/types.h | 7 +++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/common/assert.cpp b/src/common/assert.cpp index 9a959b315..60e5b1d47 100644 --- a/src/common/assert.cpp +++ b/src/common/assert.cpp @@ -12,6 +12,10 @@ #include #endif +#ifdef __clang__ +#pragma clang diagnostic ignored "-Winvalid-noreturn" +#endif + static std::mutex s_AssertFailedMutex; static inline void FreezeThreads(void** ppHandle) @@ -107,7 +111,7 @@ void Y_OnAssertFailed(const char* szMessage, const char* szFunction, const char* 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 guard(s_AssertFailedMutex); diff --git a/src/common/assert.h b/src/common/assert.h index 51589d0fb..3455dfc78 100644 --- a/src/common/assert.h +++ b/src/common/assert.h @@ -1,10 +1,12 @@ -// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin +// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) #pragma once +#include "types.h" + 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) \ 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__); \ } -#define DebugUnreachableCode() Y_OnPanicReached("Unreachable code reached", __FUNCTION__, __FILE__, __LINE__) #else #define DebugAssert(expr) #define DebugAssertMsg(expr, msg) -#define DebugUnreachableCode() #endif // 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. #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. #define UnreachableCode() Y_OnPanicReached("Unreachable code reached", __FUNCTION__, __FILE__, __LINE__) +#else +#define UnreachableCode() ASSUME(false) +#endif // Helper for switch cases. #define DefaultCaseIsUnreachable() \ diff --git a/src/common/types.h b/src/common/types.h index 5f78668a6..0d0d11eb7 100644 --- a/src/common/types.h +++ b/src/common/types.h @@ -73,6 +73,13 @@ char (&__countof_ArraySizeHelper(T (&array)[N]))[N]; #define NORETURN_FUNCTION_POINTER __attribute__((noreturn)) #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 // TODO: Move to build system instead #ifdef _MSC_VER