diff options
Diffstat (limited to 'src/common/assert.h')
| -rw-r--r-- | src/common/assert.h | 55 |
1 files changed, 27 insertions, 28 deletions
diff --git a/src/common/assert.h b/src/common/assert.h index dbfd8abaf..8c927fcc0 100644 --- a/src/common/assert.h +++ b/src/common/assert.h | |||
| @@ -9,44 +9,43 @@ | |||
| 9 | // Sometimes we want to try to continue even after hitting an assert. | 9 | // Sometimes we want to try to continue even after hitting an assert. |
| 10 | // However touching this file yields a global recompilation as this header is included almost | 10 | // However touching this file yields a global recompilation as this header is included almost |
| 11 | // everywhere. So let's just move the handling of the failed assert to a single cpp file. | 11 | // everywhere. So let's just move the handling of the failed assert to a single cpp file. |
| 12 | void assert_handle_failure(); | ||
| 13 | 12 | ||
| 14 | // For asserts we'd like to keep all the junk executed when an assert happens away from the | 13 | void assert_fail_impl(); |
| 15 | // important code in the function. One way of doing this is to put all the relevant code inside a | 14 | [[noreturn]] void unreachable_impl(); |
| 16 | // lambda and force the compiler to not inline it. Unfortunately, MSVC seems to have no syntax to | 15 | |
| 17 | // specify __declspec on lambda functions, so what we do instead is define a noinline wrapper | 16 | #ifdef _MSC_VER |
| 18 | // template that calls the lambda. This seems to generate an extra instruction at the call-site | 17 | #define YUZU_NO_INLINE __declspec(noinline) |
| 19 | // compared to the ideal implementation (which wouldn't support ASSERT_MSG parameters), but is good | 18 | #else |
| 20 | // enough for our purposes. | 19 | #define YUZU_NO_INLINE __attribute__((noinline)) |
| 21 | template <typename Fn> | ||
| 22 | #if defined(_MSC_VER) | ||
| 23 | [[msvc::noinline]] | ||
| 24 | #elif defined(__GNUC__) | ||
| 25 | [[gnu::cold, gnu::noinline]] | ||
| 26 | #endif | 20 | #endif |
| 27 | static void | ||
| 28 | assert_noinline_call(const Fn& fn) { | ||
| 29 | fn(); | ||
| 30 | assert_handle_failure(); | ||
| 31 | } | ||
| 32 | 21 | ||
| 33 | #define ASSERT(_a_) \ | 22 | #define ASSERT(_a_) \ |
| 34 | do \ | 23 | ([&]() YUZU_NO_INLINE { \ |
| 35 | if (!(_a_)) { \ | 24 | if (!(_a_)) [[unlikely]] { \ |
| 36 | assert_noinline_call([] { LOG_CRITICAL(Debug, "Assertion Failed!"); }); \ | 25 | LOG_CRITICAL(Debug, "Assertion Failed!"); \ |
| 26 | assert_fail_impl(); \ | ||
| 37 | } \ | 27 | } \ |
| 38 | while (0) | 28 | }()) |
| 39 | 29 | ||
| 40 | #define ASSERT_MSG(_a_, ...) \ | 30 | #define ASSERT_MSG(_a_, ...) \ |
| 41 | do \ | 31 | ([&]() YUZU_NO_INLINE { \ |
| 42 | if (!(_a_)) { \ | 32 | if (!(_a_)) [[unlikely]] { \ |
| 43 | assert_noinline_call([&] { LOG_CRITICAL(Debug, "Assertion Failed!\n" __VA_ARGS__); }); \ | 33 | LOG_CRITICAL(Debug, "Assertion Failed!\n" __VA_ARGS__); \ |
| 34 | assert_fail_impl(); \ | ||
| 44 | } \ | 35 | } \ |
| 45 | while (0) | 36 | }()) |
| 37 | |||
| 38 | #define UNREACHABLE() \ | ||
| 39 | do { \ | ||
| 40 | LOG_CRITICAL(Debug, "Unreachable code!"); \ | ||
| 41 | unreachable_impl(); \ | ||
| 42 | } while (0) | ||
| 46 | 43 | ||
| 47 | #define UNREACHABLE() assert_noinline_call([] { LOG_CRITICAL(Debug, "Unreachable code!"); }) | ||
| 48 | #define UNREACHABLE_MSG(...) \ | 44 | #define UNREACHABLE_MSG(...) \ |
| 49 | assert_noinline_call([&] { LOG_CRITICAL(Debug, "Unreachable code!\n" __VA_ARGS__); }) | 45 | do { \ |
| 46 | LOG_CRITICAL(Debug, "Unreachable code!\n" __VA_ARGS__); \ | ||
| 47 | unreachable_impl(); \ | ||
| 48 | } while (0) | ||
| 50 | 49 | ||
| 51 | #ifdef _DEBUG | 50 | #ifdef _DEBUG |
| 52 | #define DEBUG_ASSERT(_a_) ASSERT(_a_) | 51 | #define DEBUG_ASSERT(_a_) ASSERT(_a_) |