Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
zassert.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Måns Ansgariusson <mansgariusson@gmail.com>
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
17#ifndef ZEPHYR_INCLUDE_SYS_ZASSERT_H_
18#define ZEPHYR_INCLUDE_SYS_ZASSERT_H_
19
20#include <stdarg.h>
21
23#include <zephyr/toolchain.h>
24
30
31/*
32 * Granular, per-source-file assertions.
33 *
34 * ZASSERT_MODULE() opts a scope into the module-aware ZASSERT() macro. The
35 * resulting assertion level is a compile-time constant, so disabled assertions
36 * (and, below ZASSERT_LEVEL_VERBOSE, the message and its arguments) are folded away by
37 * the optimizer. An individual file may set its level explicitly, overriding the
38 * module default.
39 *
40 * For headers and inline functions, place ZASSERT_MODULE() inside the function
41 * body so the selection does not leak into the includer, or use ZASSERT_TERSE(),
42 * ZASSERT_NORMAL() or ZASSERT_VERBOSE() for an assertion with a fixed level.
43 */
44
46#define ZASSERT_LEVEL_OFF 0
48#define ZASSERT_LEVEL_TERSE 1
50#define ZASSERT_LEVEL_NORMAL 2
52#define ZASSERT_LEVEL_VERBOSE 3
53
54#ifdef __cplusplus
55extern "C" {
56#endif
57
72#ifndef CONFIG_ASSERT_TEST
73FUNC_NORETURN
74#endif
75__printf_like(4, 5)
76void zassert_fail(const char *cond, const char *file, unsigned int line, const char *fmt, ...);
77
82#ifndef CONFIG_ASSERT_TEST
83FUNC_NORETURN
84#endif
85void zassert_post_action(const char *file, unsigned int line);
86
97__printf_like(1, 0)
98void zassert_vprint(const char *fmt, va_list ap);
99
100
107static inline __printf_like(1, 2) void zassert_print(const char *fmt, ...)
108{
109 va_list ap;
110
111 va_start(ap, fmt);
112 zassert_vprint(fmt, ap);
113 va_end(ap);
114}
115
116#ifdef __cplusplus
117}
118#endif
119
120/* @cond INTERNAL_HIDDEN */
121/* Level of a named Kconfig module: CONFIG_ASSERT_MODULE_<module>_LEVEL. */
122#define ZASSERT_IMPL_MODULE_LEVEL(module) UTIL_CAT(UTIL_CAT(CONFIG_ASSERT_MODULE_, module), _LEVEL)
123
124/*
125 * ZASSERT_MODULE(module) -> CONFIG_ASSERT_MODULE_<module>_LEVEL
126 * ZASSERT_MODULE(module, level) -> explicit level
127 */
128#define ZASSERT_IMPL_LEVEL_RESOLVE(...) \
129 GET_ARG_N(2, __VA_ARGS__, ZASSERT_IMPL_MODULE_LEVEL(GET_ARG_N(1, __VA_ARGS__)), _)
130
131#ifndef CONFIG_ASSERT
132#define ZASSERT_IMPL(_level, _test, ...)
133#define ZASSERT_IMPL_MODULE(...)
134#else
135#define ZASSERT_IMPL(_level, _test, ...) \
136 do { \
137 if ((_level) != ZASSERT_LEVEL_OFF && unlikely(!(_test))) { \
138 switch ((_level)) { \
139 case ZASSERT_LEVEL_VERBOSE: \
140 zassert_fail(Z_STRINGIFY(_test), __FILE__, __LINE__, \
141 COND_CODE_1(IS_EMPTY(__VA_ARGS__), \
142 (NULL), (__VA_ARGS__))); \
143 break; \
144 case ZASSERT_LEVEL_NORMAL: \
145 zassert_fail(NULL, __FILE__, __LINE__, NULL); \
146 break; \
147 case ZASSERT_LEVEL_TERSE: \
148 zassert_fail(NULL, NULL, 0, NULL); \
149 break; \
150 default: /* ZASSERT_LEVEL_OFF */ \
151 break; \
152 } \
153 } \
154 } while (false)
155
156#define ZASSERT_IMPL_MODULE(...) \
157 BUILD_ASSERT((ZASSERT_IMPL_LEVEL_RESOLVE(__VA_ARGS__)) >= ZASSERT_LEVEL_OFF && \
158 (ZASSERT_IMPL_LEVEL_RESOLVE(__VA_ARGS__)) <= ZASSERT_LEVEL_VERBOSE,\
159 "Invalid assert level"); \
160 static const int zassert_impl_level __unused = \
161 IS_ENABLED(CONFIG_ASSERT) ? (ZASSERT_IMPL_LEVEL_RESOLVE(__VA_ARGS__)) \
162 : ZASSERT_LEVEL_OFF
163
164#endif /* ifndef CONFIG_ASSERT */
165#define ZASSERT_IMPL_LEVEL(_level, _test, ...) \
166 do { \
167 BUILD_ASSERT((_level) >= ZASSERT_LEVEL_OFF && (_level) <= ZASSERT_LEVEL_VERBOSE,\
168 "Invalid assert level"); \
169 ZASSERT_IMPL(_level, _test, ##__VA_ARGS__); \
170 } while (false)
171/* @endcond */
172
179#define ZASSERT_TERSE(_test, ...) ZASSERT_IMPL_LEVEL(ZASSERT_LEVEL_TERSE, (_test), ##__VA_ARGS__)
180
187#define ZASSERT_NORMAL(_test, ...) ZASSERT_IMPL_LEVEL(ZASSERT_LEVEL_NORMAL, (_test), ##__VA_ARGS__)
188
195#define ZASSERT_VERBOSE(_test, ...) \
196 ZASSERT_IMPL_LEVEL(ZASSERT_LEVEL_VERBOSE, (_test), ##__VA_ARGS__)
197
216#define ZASSERT_MODULE(...) ZASSERT_IMPL_MODULE(__VA_ARGS__)
229#define ZASSERT(_test, ...) ZASSERT_IMPL(zassert_impl_level, _test, ##__VA_ARGS__)
230
232
233#endif /* ZEPHYR_INCLUDE_SYS_ZASSERT_H_ */
FUNC_NORETURN void zassert_fail(const char *cond, const char *file, unsigned int line, const char *fmt,...)
Report a failed assertion and take terminal action.
FUNC_NORETURN void zassert_post_action(const char *file, unsigned int line)
Terminal action taken on assertion failure.
void zassert_vprint(const char *fmt, va_list ap)
Emit assertion text (va_list form).
static void zassert_print(const char *fmt,...)
Emit assertion text (printf like), routed through zassert_vprint().
Definition zassert.h:107
Macros to abstract toolchain specific capabilities.
Macro utilities.