assert.h 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #pragma once
  5. #include "common/logging/log.h"
  6. // Sometimes we want to try to continue even after hitting an assert.
  7. // However touching this file yields a global recompilation as this header is included almost
  8. // everywhere. So let's just move the handling of the failed assert to a single cpp file.
  9. void assert_handle_failure();
  10. // For asserts we'd like to keep all the junk executed when an assert happens away from the
  11. // important code in the function. One way of doing this is to put all the relevant code inside a
  12. // lambda and force the compiler to not inline it. Unfortunately, MSVC seems to have no syntax to
  13. // specify __declspec on lambda functions, so what we do instead is define a noinline wrapper
  14. // template that calls the lambda. This seems to generate an extra instruction at the call-site
  15. // compared to the ideal implementation (which wouldn't support ASSERT_MSG parameters), but is good
  16. // enough for our purposes.
  17. template <typename Fn>
  18. #if defined(_MSC_VER)
  19. [[msvc::noinline]]
  20. #elif defined(__GNUC__)
  21. [[gnu::cold, gnu::noinline]]
  22. #endif
  23. static void
  24. assert_noinline_call(const Fn& fn) {
  25. fn();
  26. assert_handle_failure();
  27. }
  28. #define ASSERT(_a_) \
  29. do \
  30. if (!(_a_)) { \
  31. assert_noinline_call([] { LOG_CRITICAL(Debug, "Assertion Failed!"); }); \
  32. } \
  33. while (0)
  34. #define ASSERT_MSG(_a_, ...) \
  35. do \
  36. if (!(_a_)) { \
  37. assert_noinline_call([&] { LOG_CRITICAL(Debug, "Assertion Failed!\n" __VA_ARGS__); }); \
  38. } \
  39. while (0)
  40. #define UNREACHABLE() assert_noinline_call([] { LOG_CRITICAL(Debug, "Unreachable code!"); })
  41. #define UNREACHABLE_MSG(...) \
  42. assert_noinline_call([&] { LOG_CRITICAL(Debug, "Unreachable code!\n" __VA_ARGS__); })
  43. #ifdef _DEBUG
  44. #define DEBUG_ASSERT(_a_) ASSERT(_a_)
  45. #define DEBUG_ASSERT_MSG(_a_, ...) ASSERT_MSG(_a_, __VA_ARGS__)
  46. #else // not debug
  47. #define DEBUG_ASSERT(_a_) \
  48. do { \
  49. } while (0)
  50. #define DEBUG_ASSERT_MSG(_a_, _desc_, ...) \
  51. do { \
  52. } while (0)
  53. #endif
  54. #define UNIMPLEMENTED() ASSERT_MSG(false, "Unimplemented code!")
  55. #define UNIMPLEMENTED_MSG(...) ASSERT_MSG(false, __VA_ARGS__)
  56. #define UNIMPLEMENTED_IF(cond) ASSERT_MSG(!(cond), "Unimplemented code!")
  57. #define UNIMPLEMENTED_IF_MSG(cond, ...) ASSERT_MSG(!(cond), __VA_ARGS__)
  58. // If the assert is ignored, execute _b_
  59. #define ASSERT_OR_EXECUTE(_a_, _b_) \
  60. do { \
  61. ASSERT(_a_); \
  62. if (!(_a_)) { \
  63. _b_ \
  64. } \
  65. } while (0)
  66. // If the assert is ignored, execute _b_
  67. #define ASSERT_OR_EXECUTE_MSG(_a_, _b_, ...) \
  68. do { \
  69. ASSERT_MSG(_a_, __VA_ARGS__); \
  70. if (!(_a_)) { \
  71. _b_ \
  72. } \
  73. } while (0)