/ src / common / assert.h
assert.h
 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/backend.h"
10  #include "common/logging/log.h"
11  
12  // For asserts we'd like to keep all the junk executed when an assert happens away from the
13  // important code in the function. One way of doing this is to put all the relevant code inside a
14  // lambda and force the compiler to not inline it.
15  
16  #define ASSERT(_a_)                                                                                \
17      do                                                                                             \
18          if (!(_a_)) [[unlikely]] {                                                                 \
19              []() CITRA_NO_INLINE CITRA_NO_RETURN {                                                 \
20                  LOG_CRITICAL(Debug, "Assertion Failed!");                                          \
21                  Common::Log::Stop();                                                               \
22                  Crash();                                                                           \
23                  exit(1);                                                                           \
24              }();                                                                                   \
25          }                                                                                          \
26      while (0)
27  
28  #define ASSERT_MSG(_a_, ...)                                                                       \
29      do                                                                                             \
30          if (!(_a_)) [[unlikely]] {                                                                 \
31              [&]() CITRA_NO_INLINE CITRA_NO_RETURN {                                                \
32                  LOG_CRITICAL(Debug, "Assertion Failed!\n" __VA_ARGS__);                            \
33                  Common::Log::Stop();                                                               \
34                  Crash();                                                                           \
35                  exit(1);                                                                           \
36              }();                                                                                   \
37          }                                                                                          \
38      while (0)
39  
40  #define UNREACHABLE()                                                                              \
41      ([]() CITRA_NO_INLINE CITRA_NO_RETURN {                                                        \
42          LOG_CRITICAL(Debug, "Unreachable code!");                                                  \
43          Common::Log::Stop();                                                                       \
44          Crash();                                                                                   \
45          exit(1);                                                                                   \
46      }())
47  
48  #define UNREACHABLE_MSG(...)                                                                       \
49      ([&]() CITRA_NO_INLINE CITRA_NO_RETURN {                                                       \
50          LOG_CRITICAL(Debug, "Unreachable code!\n" __VA_ARGS__);                                    \
51          Common::Log::Stop();                                                                       \
52          Crash();                                                                                   \
53          exit(1);                                                                                   \
54      }())
55  
56  #ifdef _DEBUG
57  #define DEBUG_ASSERT(_a_) ASSERT(_a_)
58  #define DEBUG_ASSERT_MSG(_a_, ...) ASSERT_MSG(_a_, __VA_ARGS__)
59  #else // not debug
60  #define DEBUG_ASSERT(_a_)
61  #define DEBUG_ASSERT_MSG(_a_, _desc_, ...)
62  #endif
63  
64  #define UNIMPLEMENTED() LOG_CRITICAL(Debug, "Unimplemented code!")
65  #define UNIMPLEMENTED_MSG(_a_, ...) LOG_CRITICAL(Debug, _a_, __VA_ARGS__)