common/assert: rework ASSERT handling to avoid std::function usage

This commit is contained in:
Liam 2022-06-12 18:11:02 -04:00
parent ebecdd3a74
commit feaf010fa2
2 changed files with 20 additions and 35 deletions

View File

@ -6,13 +6,9 @@
#include "common/settings.h" #include "common/settings.h"
void assert_check_condition(bool cond, std::function<void()>&& on_failure) { void assert_fail_impl() {
if (!cond) [[unlikely]] { if (Settings::values.use_debug_asserts) {
on_failure(); Crash();
if (Settings::values.use_debug_asserts) {
Crash();
}
} }
} }

View File

@ -4,47 +4,36 @@
#pragma once #pragma once
#include <functional>
#include "common/logging/log.h" #include "common/logging/log.h"
// Sometimes we want to try to continue even after hitting an assert. // Sometimes we want to try to continue even after hitting an assert.
// However touching this file yields a global recompilation as this header is included almost // However touching this file yields a global recompilation as this header is included almost
// everywhere. So let's just move the handling of the failed assert to a single cpp file. // everywhere. So let's just move the handling of the failed assert to a single cpp file.
// For asserts we'd like to keep all the junk executed when an assert happens away from the void assert_fail_impl();
// important code in the function. One way of doing this is to put all the relevant code inside a
// lambda and force the compiler to not inline it.
void assert_check_condition(bool cond, std::function<void()>&& on_failure);
[[noreturn]] void unreachable_impl(); [[noreturn]] void unreachable_impl();
#ifdef _MSC_VER
#define YUZU_NO_INLINE __declspec(noinline)
#else
#define YUZU_NO_INLINE __attribute__((noinline))
#endif
#define ASSERT(_a_) \ #define ASSERT(_a_) \
do { \ ([&]() YUZU_NO_INLINE { \
if (std::is_constant_evaluated()) { \ if (!(_a_)) [[unlikely]] { \
if (!(_a_)) { \ LOG_CRITICAL(Debug, "Assertion Failed!"); \
/* Will trigger compile error here */ \ assert_fail_impl(); \
assert_check_condition(bool(_a_), \
[] { LOG_CRITICAL(Debug, "Assertion Failed!"); }); \
} \
} else { \
assert_check_condition(bool(_a_), [] { LOG_CRITICAL(Debug, "Assertion Failed!"); }); \
} \ } \
} while (0) }())
#define ASSERT_MSG(_a_, ...) \ #define ASSERT_MSG(_a_, ...) \
do { \ ([&]() YUZU_NO_INLINE { \
if (std::is_constant_evaluated()) { \ if (!(_a_)) [[unlikely]] { \
if (!(_a_)) { \ LOG_CRITICAL(Debug, "Assertion Failed!\n" __VA_ARGS__); \
/* Will trigger compile error here */ \ assert_fail_impl(); \
assert_check_condition(bool(_a_), \
[] { LOG_CRITICAL(Debug, "Assertion Failed!"); }); \
} \
} else { \
assert_check_condition( \
bool(_a_), [&] { LOG_CRITICAL(Debug, "Assertion Failed!\n" __VA_ARGS__); }); \
} \ } \
} while (0) }())
#define UNREACHABLE() \ #define UNREACHABLE() \
do { \ do { \