summaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/assert/assert.h
blob: 6430f136ffcd27b19127df89bdd95be525e4476a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#pragma once

#include <util/system/compiler.h>
#include <util/system/src_root.h>

#include <util/generic/strbuf.h>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

namespace NDetail {

[[noreturn]]
void AssertTrapImpl(
    TStringBuf trapType,
    TStringBuf expr,
    TStringBuf description,
    TStringBuf file,
    int line,
    TStringBuf function);

} // namespace NDetail

#ifdef __GNUC__
    #define YT_BUILTIN_TRAP()  __builtin_trap()
#else
    #define YT_BUILTIN_TRAP()  std::terminate()
#endif

#define YT_ASSERT_TRAP_IMPL(trapType, expr, description) \
    ::NYT::NDetail::AssertTrapImpl(                      \
        TStringBuf(trapType),                            \
        TStringBuf(expr),                                \
        TStringBuf(description),                         \
        __SOURCE_FILE_IMPL__.As<TStringBuf>(),           \
        __LINE__,                                        \
        TStringBuf(__FUNCTION__));                       \
    Y_UNREACHABLE()

#define YT_ASSERT_TRAP_IMPL_EXTRA_ARG(trapType, expr, description, _) YT_ASSERT_TRAP_IMPL(trapType, expr, description)

#define YT_ASSERT_TRAP(trapType, expr, ...) \
    YT_ASSERT_TRAP_IMPL##__VA_OPT__(_EXTRA_ARG)(trapType, expr,  __VA_OPT__(__VA_ARGS__,) "")

//! If |expr| is evaluated to false, abnormally terminates the current process in debug mode.
//! Does nothing in release mode.
//! Accepts an optional string argument that will be printed in error with #expr.
#ifdef NDEBUG
    #define YT_ASSERT(expr, ...) \
        do { \
            if (false) { \
                (void) (expr); \
                __VA_OPT__((void)(__VA_ARGS__)); \
            } \
        } while (false)
#else
    #define YT_ASSERT(expr, ...) \
        do { \
            if (Y_UNLIKELY(!(expr))) { \
                YT_ASSERT_TRAP("YT_ASSERT", #expr __VA_OPT__(, __VA_ARGS__)); \
            } \
        } while (false)
#endif

//! Same as |YT_ASSERT| but evaluates and checks the expression in both release and debug mode.
//! Accepts an optional string argument that will be printed in error message with #expr.
#define YT_VERIFY(expr, ...) \
    do { \
        if (Y_UNLIKELY(!(expr))) { \
            YT_ASSERT_TRAP("YT_VERIFY", #expr __VA_OPT__(, __VA_ARGS__)); \
        } \
    } while (false)

//! Behaves as |YT_ASSERT| in debug mode and as |Y_ASSUME| in release.
#ifdef NDEBUG
    #define YT_ASSUME(expr) Y_ASSUME(expr)
#else
    #define YT_ASSUME(expr) YT_ASSERT(expr)
#endif

//! Behaves as |YT_ASSERT(false)| in debug mode and as |Y_UNREACHABLE| in release.
#ifdef NDEBUG
    #define YT_UNREACHABLE() Y_UNREACHABLE()
#else
    #define YT_UNREACHABLE() YT_ASSERT(false)
#endif

//! Fatal error code marker. Abnormally terminates the current process.
//! Accepts an optional string argument that will be printed in error message.
#ifdef YT_COMPILING_UDF
    #define YT_ABORT(...) __YT_BUILTIN_ABORT()
#else
    #define YT_ABORT(...) \
        do { \
            YT_ASSERT_TRAP("YT_ABORT", "" __VA_OPT__(, __VA_ARGS__)); \
        } while (false)
#endif

//! Unimplemented code marker. Abnormally terminates the current process.
//! Accepts an optional string argument that will be printed in error message.
#define YT_UNIMPLEMENTED(...) \
    do { \
        YT_ASSERT_TRAP("YT_UNIMPLEMENTED", "" __VA_OPT__(, __VA_ARGS__)); \
    } while (false)

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT