Zephyr Project API  3.1.0
A Scalable Open Source RTOS
__assert.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2014 Wind River Systems, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_SYS___ASSERT_H_
8#define ZEPHYR_INCLUDE_SYS___ASSERT_H_
9
10#include <stdbool.h>
11#include <zephyr/toolchain.h>
12
13#ifdef CONFIG_ASSERT
14#ifndef __ASSERT_ON
15#define __ASSERT_ON CONFIG_ASSERT_LEVEL
16#endif
17#endif
18
19#ifdef CONFIG_FORCE_NO_ASSERT
20#undef __ASSERT_ON
21#define __ASSERT_ON 0
22#endif
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28/* Wrapper around printk to avoid including printk.h in assert.h */
29void assert_print(const char *fmt, ...);
30
31#ifdef __cplusplus
32}
33#endif
34
35#if defined(CONFIG_ASSERT_VERBOSE)
36#define __ASSERT_PRINT(fmt, ...) assert_print(fmt, ##__VA_ARGS__)
37#else /* CONFIG_ASSERT_VERBOSE */
38#define __ASSERT_PRINT(fmt, ...)
39#endif /* CONFIG_ASSERT_VERBOSE */
40
41#ifdef CONFIG_ASSERT_NO_MSG_INFO
42#define __ASSERT_MSG_INFO(fmt, ...)
43#else /* CONFIG_ASSERT_NO_MSG_INFO */
44#define __ASSERT_MSG_INFO(fmt, ...) __ASSERT_PRINT("\t" fmt "\n", ##__VA_ARGS__)
45#endif /* CONFIG_ASSERT_NO_MSG_INFO */
46
47#if !defined(CONFIG_ASSERT_NO_COND_INFO) && !defined(CONFIG_ASSERT_NO_FILE_INFO)
48#define __ASSERT_LOC(test) \
49 __ASSERT_PRINT("ASSERTION FAIL [%s] @ %s:%d\n", \
50 Z_STRINGIFY(test), \
51 __FILE__, __LINE__)
52#endif
53
54#if defined(CONFIG_ASSERT_NO_COND_INFO) && !defined(CONFIG_ASSERT_NO_FILE_INFO)
55#define __ASSERT_LOC(test) \
56 __ASSERT_PRINT("ASSERTION FAIL @ %s:%d\n", \
57 __FILE__, __LINE__)
58#endif
59
60#if !defined(CONFIG_ASSERT_NO_COND_INFO) && defined(CONFIG_ASSERT_NO_FILE_INFO)
61#define __ASSERT_LOC(test) \
62 __ASSERT_PRINT("ASSERTION FAIL [%s]\n", \
63 Z_STRINGIFY(test))
64#endif
65
66#if defined(CONFIG_ASSERT_NO_COND_INFO) && defined(CONFIG_ASSERT_NO_FILE_INFO)
67#define __ASSERT_LOC(test) \
68 __ASSERT_PRINT("ASSERTION FAIL\n")
69#endif
70
71#ifdef __ASSERT_ON
72#if (__ASSERT_ON < 0) || (__ASSERT_ON > 2)
73#error "Invalid __ASSERT() level: must be between 0 and 2"
74#endif
75
76#if __ASSERT_ON
77
78#ifdef __cplusplus
79extern "C" {
80#endif
81
82#ifdef CONFIG_ASSERT_NO_FILE_INFO
83void assert_post_action(void);
84#define __ASSERT_POST_ACTION() assert_post_action()
85#else /* CONFIG_ASSERT_NO_FILE_INFO */
86void assert_post_action(const char *file, unsigned int line);
87#define __ASSERT_POST_ACTION() assert_post_action(__FILE__, __LINE__)
88#endif /* CONFIG_ASSERT_NO_FILE_INFO */
89
90#ifdef __cplusplus
91}
92#endif
93
94#define __ASSERT_NO_MSG(test) \
95 do { \
96 if (!(test)) { \
97 __ASSERT_LOC(test); \
98 __ASSERT_POST_ACTION(); \
99 } \
100 } while (false)
101
102#define __ASSERT(test, fmt, ...) \
103 do { \
104 if (!(test)) { \
105 __ASSERT_LOC(test); \
106 __ASSERT_MSG_INFO(fmt, ##__VA_ARGS__); \
107 __ASSERT_POST_ACTION(); \
108 } \
109 } while (false)
110
111#define __ASSERT_EVAL(expr1, expr2, test, fmt, ...) \
112 do { \
113 expr2; \
114 __ASSERT(test, fmt, ##__VA_ARGS__); \
115 } while (false)
116
117#if (__ASSERT_ON == 1)
118#warning "__ASSERT() statements are ENABLED"
119#endif
120#else
121#define __ASSERT(test, fmt, ...) { }
122#define __ASSERT_EVAL(expr1, expr2, test, fmt, ...) expr1
123#define __ASSERT_NO_MSG(test) { }
124#endif
125#else
126#define __ASSERT(test, fmt, ...) { }
127#define __ASSERT_EVAL(expr1, expr2, test, fmt, ...) expr1
128#define __ASSERT_NO_MSG(test) { }
129#endif
130
131#endif /* ZEPHYR_INCLUDE_SYS___ASSERT_H_ */
void assert_print(const char *fmt,...)
void assert_post_action(const char *file, unsigned int line)
Definition: spinlock_error_case.c:33
Macros to abstract toolchain specific capabilities.