1 // Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
2 // Licensed under GPLv2 or any later version
3 // Refer to the license.txt file included.
4
5 #pragma once
6
7 #include <cstdlib>
8 #include "common/common_funcs.h"
9 #include "common/logging/log.h"
10
11 // For asserts we'd like to keep all the junk executed when an assert happens away from the
12 // important code in the function. One way of doing this is to put all the relevant code inside a
13 // lambda and force the compiler to not inline it. Unfortunately, MSVC seems to have no syntax to
14 // specify __declspec on lambda functions, so what we do instead is define a noinline wrapper
15 // template that calls the lambda. This seems to generate an extra instruction at the call-site
16 // compared to the ideal implementation (which wouldn't support ASSERT_MSG parameters), but is good
17 // enough for our purposes.
18 template <typename Fn>
19 #if defined(_MSC_VER)
20 [[msvc::noinline, noreturn]]
21 #elif defined(__GNUC__)
22 [[gnu::cold, gnu::noinline, noreturn]]
23 #endif
24 static void
assert_noinline_call(const Fn & fn)25 assert_noinline_call(const Fn& fn) {
26 fn();
27 Crash();
28 exit(1); // Keeps GCC's mouth shut about this actually returning
29 }
30
31 #define ASSERT(_a_) \
32 if (!(_a_)) { \
33 LOG_CRITICAL(Debug, "Assertion Failed!"); \
34 }
35
36 #define ASSERT_MSG(_a_, ...) \
37 if (!(_a_)) { \
38 LOG_CRITICAL(Debug, "Assertion Failed! " __VA_ARGS__); \
39 }
40
41 #define UNREACHABLE() \
42 { LOG_CRITICAL(Debug, "Unreachable code!"); }
43 #define UNREACHABLE_MSG(...) \
44 { LOG_CRITICAL(Debug, "Unreachable code!\n" __VA_ARGS__); }
45
46 #ifdef _DEBUG
47 #define DEBUG_ASSERT(_a_) ASSERT(_a_)
48 #define DEBUG_ASSERT_MSG(_a_, ...) ASSERT_MSG(_a_, __VA_ARGS__)
49 #else // not debug
50 #define DEBUG_ASSERT(_a_)
51 #define DEBUG_ASSERT_MSG(_a_, _desc_, ...)
52 #endif
53
54 #define UNIMPLEMENTED() ASSERT_MSG(false, "Unimplemented code!")
55 #define UNIMPLEMENTED_MSG(...) ASSERT_MSG(false, __VA_ARGS__)
56
57 #define UNIMPLEMENTED_IF(cond) ASSERT_MSG(!(cond), "Unimplemented code!")
58 #define UNIMPLEMENTED_IF_MSG(cond, ...) ASSERT_MSG(!(cond), __VA_ARGS__)
59
60 // If the assert is ignored, execute _b_
61 #define ASSERT_OR_EXECUTE(_a_, _b_) \
62 do { \
63 ASSERT(_a_); \
64 if (!(_a_)) { \
65 _b_ \
66 } \
67 } while (0)
68
69 // If the assert is ignored, execute _b_
70 #define ASSERT_OR_EXECUTE_MSG(_a_, _b_, ...) \
71 do { \
72 ASSERT_MSG(_a_, __VA_ARGS__); \
73 if (!(_a_)) { \
74 _b_ \
75 } \
76 } while (0)
77