assert.h 1.8 KB

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