-
Notifications
You must be signed in to change notification settings - Fork 100
/
rwkv_error_handling.inc
95 lines (79 loc) · 3.9 KB
/
rwkv_error_handling.inc
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
thread_local enum rwkv_error_flags global_last_error = RWKV_ERROR_NONE;
thread_local bool global_print_errors = true;
inline static enum rwkv_error_flags operator|(enum rwkv_error_flags a, enum rwkv_error_flags b) {
return static_cast<enum rwkv_error_flags>(static_cast<int>(a) | static_cast<int>(b));
}
inline static enum rwkv_error_flags operator|=(enum rwkv_error_flags & a, enum rwkv_error_flags b) {
return a = a | b;
}
// Prints a message to stderr if error printing is enabled globally.
#define RWKV_MSG(...) do { if (global_print_errors) fprintf(stderr, __VA_ARGS__); } while (0)
// Prints a message to stderr if error printing is enabled in the context.
#define RWKV_CTX_MSG(ctx, ...) do { if (ctx->print_errors) fprintf(stderr, __VA_ARGS__); } while (0)
// If the condition x is false, adds ERR_VAL to the last error, and returns RET_VAL.
#define RWKV_ASSERT(ERR_VAL, RET_VAL, x) do { \
if (!(x)) { \
global_last_error |= ERR_VAL; \
RWKV_MSG("\n%s:%d: %s\n", __FILE__, __LINE__, #x); \
RWKV_MAYBE_BREAK; \
return RET_VAL; \
} } while (0)
// If the condition x is false, adds ERR_VAL to the last error, prints a message to stderr, and returns RET_VAL.
#define RWKV_ASSERT_MSG(ERR_VAL, RET_VAL, x, ...) do { \
if (!(x)) { \
global_last_error |= ERR_VAL; \
RWKV_MSG(__VA_ARGS__); \
RWKV_MSG("\n%s:%d: %s\n", __FILE__, __LINE__, #x); \
RWKV_MAYBE_BREAK; \
return RET_VAL; \
} } while (0)
// If the condition x is false, adds ERR_VAL to the ctx's last error, prints a message to stderr, and returns RET_VAL.
#define RWKV_CTX_ASSERT_MSG(ctx, ERR_VAL, RET_VAL, x, ...) do { \
if (!(x)) { \
((struct rwkv_context *) ctx)->last_error |= ERR_VAL; \
RWKV_CTX_MSG(ctx, __VA_ARGS__); \
RWKV_CTX_MSG(ctx, "\n%s:%d: %s\n", __FILE__, __LINE__, #x); \
RWKV_MAYBE_BREAK; \
return RET_VAL; \
} } while (0)
// If the condition x is false, adds ERR_VAL to the ctx's last error, and returns RET_VAL.
#define RWKV_CTX_ASSERT(ctx, ERR_VAL, RET_VAL, x) do { \
if (!(x)) { \
((struct rwkv_context *) ctx)->last_error |= ERR_VAL; \
RWKV_CTX_MSG(ctx, "\n%s:%d: %s\n", __FILE__, __LINE__, #x); \
RWKV_MAYBE_BREAK; \
return RET_VAL; \
} } while (0)
// If the condition x is false, returns RET_VAL.
#define RWKV_ENSURE(RET_VAL, x) do { \
if (!(x)) { \
RWKV_MSG("\n%s:%d: %s\n", __FILE__, __LINE__, #x); \
RWKV_MAYBE_BREAK; \
return RET_VAL; \
} } while (0)
// If the condition x is false, prints a message to stderr, and returns RET_VAL.
#define RWKV_ENSURE_MSG(RET_VAL, x, ...) do { \
if (!(x)) { \
RWKV_MSG(__VA_ARGS__); \
RWKV_MSG("\n%s:%d: %s\n", __FILE__, __LINE__, #x); \
RWKV_MAYBE_BREAK; \
return RET_VAL; \
} } while (0)
// If the condition x is false, prints a message to stderr, and returns RET_VAL.
#define RWKV_CTX_ENSURE_MSG(ctx, RET_VAL, x, ...) do { \
if (!(x)) { \
((struct rwkv_context *) ctx)->last_error |= ERR_VAL; \
RWKV_CTX_MSG(ctx, __VA_ARGS__); \
RWKV_CTX_MSG(ctx, "\n%s:%d: %s\n", __FILE__, __LINE__, #x); \
RWKV_MAYBE_BREAK; \
return RET_VAL; \
} } while (0)
#define RWKV_ASSERT_FALSE_MSG(ERR_VAL, x, ...) RWKV_ASSERT_MSG(ERR_VAL, false, x, __VA_ARGS__)
#define RWKV_ASSERT_NULL_MSG(ERR_VAL, x, ...) RWKV_ASSERT_MSG(ERR_VAL, NULL, x, __VA_ARGS__)
#define RWKV_CTX_ASSERT_FALSE_MSG(ctx, ERR_VAL, x, ...) RWKV_CTX_ASSERT_MSG(ctx, ERR_VAL, false, x, __VA_ARGS__)
#define RWKV_ASSERT_FALSE(ERR_VAL, x) RWKV_ASSERT(ERR_VAL, false, x)
#define RWKV_ASSERT_NULL(ERR_VAL, x) RWKV_ASSERT(ERR_VAL, NULL, x)
#define RWKV_CTX_ASSERT_FALSE(ctx, ERR_VAL, x) RWKV_CTX_ASSERT(ctx, ERR_VAL, false, x)
#define RWKV_ENSURE_OR_FALSE(x) RWKV_ENSURE(false, x)
#define RWKV_ENSURE_OR_NULL(x) RWKV_ENSURE(NULL, x)
#define RWKV_ENSURE_OR_FALSE_MSG(x, ...) RWKV_ENSURE_MSG(false, x, __VA_ARGS__)