]> Sergey Matveev's repositories - bfs.git/blob - src/diag.h
Formatting fixes
[bfs.git] / src / diag.h
1 // Copyright © Tavian Barnes <tavianator@tavianator.com>
2 // SPDX-License-Identifier: 0BSD
3
4 /**
5  * Diagnostic messages.
6  */
7
8 #ifndef BFS_DIAG_H
9 #define BFS_DIAG_H
10
11 #include "config.h"
12 #include "ctx.h"
13 #include <stdarg.h>
14
15 /**
16  * static_assert() with an optional second argument.
17  */
18 #if __STDC_VERSION__ >= 202311L
19 #  define bfs_static_assert static_assert
20 #else
21 #  define bfs_static_assert(...) bfs_static_assert_(__VA_ARGS__, #__VA_ARGS__, )
22 #  define bfs_static_assert_(expr, msg, ...) _Static_assert(expr, msg)
23 #endif
24
25 /**
26  * A source code location.
27  */
28 struct bfs_loc {
29         const char *file;
30         int line;
31         const char *func;
32 };
33
34 #define BFS_LOC_INIT { .file = __FILE__, .line = __LINE__, .func = __func__ }
35
36 /**
37  * Get the current source code location.
38  */
39 #if __STDC_VERSION__ >= 202311L
40 #  define bfs_location() (&(static const struct bfs_loc)BFS_LOC_INIT)
41 #else
42 #  define bfs_location() (&(const struct bfs_loc)BFS_LOC_INIT)
43 #endif
44
45 /**
46  * Print a message to standard error and abort.
47  */
48 BFS_FORMATTER(2, 3)
49 noreturn void bfs_abortf(const struct bfs_loc *loc, const char *format, ...);
50
51 /**
52  * Unconditional abort with a message.
53  */
54 #define bfs_abort(...) bfs_abortf(bfs_location(), __VA_ARGS__)
55
56 /**
57  * Abort in debug builds; no-op in release builds.
58  */
59 #ifdef NDEBUG
60 #  define bfs_bug(...) ((void)0)
61 #else
62 #  define bfs_bug bfs_abort
63 #endif
64
65 /**
66  * Unconditional assert.
67  */
68 #define bfs_verify(...) \
69         bfs_verify_(#__VA_ARGS__, __VA_ARGS__, "", "")
70
71 #define bfs_verify_(str, cond, format, ...) \
72         ((cond) ? (void)0 : bfs_abort( \
73                 sizeof(format) > 1 \
74                         ? "%.0s" format "%s%s" \
75                         : "Assertion failed: `%s`%s", \
76                 str, __VA_ARGS__))
77
78 /**
79  * Assert in debug builds; no-op in release builds.
80  */
81 #ifdef NDEBUG
82 #  define bfs_assert(...) ((void)0)
83 #else
84 #  define bfs_assert bfs_verify
85 #endif
86
87 struct bfs_expr;
88
89 /**
90  * Like perror(), but decorated like bfs_error().
91  */
92 void bfs_perror(const struct bfs_ctx *ctx, const char *str);
93
94 /**
95  * Shorthand for printing error messages.
96  */
97 BFS_FORMATTER(2, 3)
98 void bfs_error(const struct bfs_ctx *ctx, const char *format, ...);
99
100 /**
101  * Shorthand for printing warning messages.
102  *
103  * @return Whether a warning was printed.
104  */
105 BFS_FORMATTER(2, 3)
106 bool bfs_warning(const struct bfs_ctx *ctx, const char *format, ...);
107
108 /**
109  * Shorthand for printing debug messages.
110  *
111  * @return Whether a debug message was printed.
112  */
113 BFS_FORMATTER(3, 4)
114 bool bfs_debug(const struct bfs_ctx *ctx, enum debug_flags flag, const char *format, ...);
115
116 /**
117  * bfs_error() variant that takes a va_list.
118  */
119 BFS_FORMATTER(2, 0)
120 void bfs_verror(const struct bfs_ctx *ctx, const char *format, va_list args);
121
122 /**
123  * bfs_warning() variant that takes a va_list.
124  */
125 BFS_FORMATTER(2, 0)
126 bool bfs_vwarning(const struct bfs_ctx *ctx, const char *format, va_list args);
127
128 /**
129  * bfs_debug() variant that takes a va_list.
130  */
131 BFS_FORMATTER(3, 0)
132 bool bfs_vdebug(const struct bfs_ctx *ctx, enum debug_flags flag, const char *format, va_list args);
133
134 /**
135  * Print the error message prefix.
136  */
137 void bfs_error_prefix(const struct bfs_ctx *ctx);
138
139 /**
140  * Print the warning message prefix.
141  */
142 bool bfs_warning_prefix(const struct bfs_ctx *ctx);
143
144 /**
145  * Print the debug message prefix.
146  */
147 bool bfs_debug_prefix(const struct bfs_ctx *ctx, enum debug_flags flag);
148
149 /**
150  * Highlight parts of the command line in an error message.
151  */
152 void bfs_argv_error(const struct bfs_ctx *ctx, const bool args[]);
153
154 /**
155  * Highlight parts of an expression in an error message.
156  */
157 void bfs_expr_error(const struct bfs_ctx *ctx, const struct bfs_expr *expr);
158
159 /**
160  * Highlight parts of the command line in a warning message.
161  */
162 bool bfs_argv_warning(const struct bfs_ctx *ctx, const bool args[]);
163
164 /**
165  * Highlight parts of an expression in a warning message.
166  */
167 bool bfs_expr_warning(const struct bfs_ctx *ctx, const struct bfs_expr *expr);
168
169 #endif // BFS_DIAG_H