assert.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. //! Behaves as |YT_ASSERT| in debug mode and as |Y_ASSUME| in release.
  47. #ifdef NDEBUG
  48. #define YT_ASSUME(expr) Y_ASSUME(expr)
  49. #else
  50. #define YT_ASSUME(expr) YT_ASSERT(expr)
  51. #endif
  52. //! Behaves as |YT_ASSERT(false)| in debug mode and as |Y_UNREACHABLE| in release.
  53. #ifdef NDEBUG
  54. #define YT_UNREACHABLE() Y_UNREACHABLE()
  55. #else
  56. #define YT_UNREACHABLE() YT_ASSERT(false)
  57. #endif
  58. //! Fatal error code marker. Abnormally terminates the current process.
  59. #ifdef YT_COMPILING_UDF
  60. #define YT_ABORT() __YT_BUILTIN_ABORT()
  61. #else
  62. #define YT_ABORT() \
  63. do { \
  64. YT_ASSERT_TRAP("YT_ABORT", ""); \
  65. } while (false)
  66. #endif
  67. //! Unimplemented code marker. Abnormally terminates the current process.
  68. #define YT_UNIMPLEMENTED() \
  69. do { \
  70. YT_ASSERT_TRAP("YT_UNIMPLEMENTED", ""); \
  71. } while (false)
  72. ////////////////////////////////////////////////////////////////////////////////
  73. } // namespace NYT