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