assert.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 <cstdlib>
  6. #include "common/common_funcs.h"
  7. #include "common/logging/log.h"
  8. // For asserts we'd like to keep all the junk executed when an assert happens away from the
  9. // important code in the function. One way of doing this is to put all the relevant code inside a
  10. // lambda and force the compiler to not inline it. Unfortunately, MSVC seems to have no syntax to
  11. // specify __declspec on lambda functions, so what we do instead is define a noinline wrapper
  12. // template that calls the lambda. This seems to generate an extra instruction at the call-site
  13. // compared to the ideal implementation (which wouldn't support ASSERT_MSG parameters), but is good
  14. // enough for our purposes.
  15. template <typename Fn>
  16. #if defined(_MSC_VER)
  17. __declspec(noinline, noreturn)
  18. #elif defined(__GNUC__)
  19. __attribute__((noinline, noreturn, cold))
  20. #endif
  21. static void assert_noinline_call(const Fn& fn) {
  22. fn();
  23. Crash();
  24. exit(1); // Keeps GCC's mouth shut about this actually returning
  25. }
  26. #define ASSERT(_a_) \
  27. do \
  28. if (!(_a_)) { \
  29. assert_noinline_call([] { LOG_CRITICAL(Debug, "Assertion Failed!"); }); \
  30. } \
  31. while (0)
  32. #define ASSERT_MSG(_a_, ...) \
  33. do \
  34. if (!(_a_)) { \
  35. assert_noinline_call([&] { LOG_CRITICAL(Debug, "Assertion Failed!\n" __VA_ARGS__); }); \
  36. } \
  37. while (0)
  38. #define UNREACHABLE() ASSERT_MSG(false, "Unreachable code!")
  39. #define UNREACHABLE_MSG(...) ASSERT_MSG(false, __VA_ARGS__)
  40. #ifdef _DEBUG
  41. #define DEBUG_ASSERT(_a_) ASSERT(_a_)
  42. #define DEBUG_ASSERT_MSG(_a_, ...) ASSERT_MSG(_a_, __VA_ARGS__)
  43. #else // not debug
  44. #define DEBUG_ASSERT(_a_)
  45. #define DEBUG_ASSERT_MSG(_a_, _desc_, ...)
  46. #endif
  47. #define UNIMPLEMENTED() LOG_CRITICAL(Debug, "Unimplemented code!")
  48. #define UNIMPLEMENTED_MSG(...) ASSERT_MSG(false, __VA_ARGS__)