assert.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #pragma once
  2. #include <util/system/compiler.h>
  3. #include <util/system/src_root.h>
  4. #include <util/generic/strbuf.h>
  5. namespace NYT {
  6. ////////////////////////////////////////////////////////////////////////////////
  7. namespace NDetail {
  8. [[noreturn]]
  9. void AssertTrapImpl(
  10. TStringBuf trapType,
  11. TStringBuf expr,
  12. TStringBuf file,
  13. int line,
  14. TStringBuf function);
  15. } // namespace NDetail
  16. #ifdef __GNUC__
  17. #define YT_BUILTIN_TRAP() __builtin_trap()
  18. #else
  19. #define YT_BUILTIN_TRAP() std::terminate()
  20. #endif
  21. #define YT_ASSERT_TRAP(trapType, expr) \
  22. ::NYT::NDetail::AssertTrapImpl(TStringBuf(trapType), TStringBuf(expr), __SOURCE_FILE_IMPL__.As<TStringBuf>(), __LINE__, TStringBuf(__FUNCTION__)); \
  23. Y_UNREACHABLE() \
  24. #ifdef NDEBUG
  25. #define YT_ASSERT(expr) \
  26. do { \
  27. if (false) { \
  28. (void) (expr); \
  29. } \
  30. } while (false)
  31. #else
  32. #define YT_ASSERT(expr) \
  33. do { \
  34. if (Y_UNLIKELY(!(expr))) { \
  35. YT_ASSERT_TRAP("YT_ASSERT", #expr); \
  36. } \
  37. } while (false)
  38. #endif
  39. //! Same as |YT_ASSERT| but evaluates and checks the expression in both release and debug mode.
  40. #define YT_VERIFY(expr) \
  41. do { \
  42. if (Y_UNLIKELY(!(expr))) { \
  43. YT_ASSERT_TRAP("YT_VERIFY", #expr); \
  44. } \
  45. } while (false)
  46. //! Fatal error code marker. Abnormally terminates the current process.
  47. #ifdef YT_COMPILING_UDF
  48. #define YT_ABORT() __YT_BUILTIN_ABORT()
  49. #else
  50. #define YT_ABORT() \
  51. do { \
  52. YT_ASSERT_TRAP("YT_ABORT", ""); \
  53. } while (false)
  54. #endif
  55. //! Unimplemented code marker. Abnormally terminates the current process.
  56. #define YT_UNIMPLEMENTED() \
  57. do { \
  58. YT_ASSERT_TRAP("YT_UNIMPLEMENTED", ""); \
  59. } while (false)
  60. ////////////////////////////////////////////////////////////////////////////////
  61. } // namespace NYT