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 
12 #ifdef CONFIG_ASSERT
13 #ifndef __ASSERT_ON
14 #define __ASSERT_ON CONFIG_ASSERT_LEVEL
15 #endif
16 #endif
17 
18 #ifdef CONFIG_FORCE_NO_ASSERT
19 #undef __ASSERT_ON
20 #define __ASSERT_ON 0
21 #endif
22 
23 #if defined(CONFIG_ASSERT_VERBOSE)
24 #define __ASSERT_PRINT(fmt, ...) printk(fmt, ##__VA_ARGS__)
25 #else /* CONFIG_ASSERT_VERBOSE */
26 #define __ASSERT_PRINT(fmt, ...)
27 #endif /* CONFIG_ASSERT_VERBOSE */
28 
29 #ifdef CONFIG_ASSERT_NO_MSG_INFO
30 #define __ASSERT_MSG_INFO(fmt, ...)
31 #else  /* CONFIG_ASSERT_NO_MSG_INFO */
32 #define __ASSERT_MSG_INFO(fmt, ...) __ASSERT_PRINT("\t" fmt "\n", ##__VA_ARGS__)
33 #endif /* CONFIG_ASSERT_NO_MSG_INFO */
34 
35 #if !defined(CONFIG_ASSERT_NO_COND_INFO) && !defined(CONFIG_ASSERT_NO_FILE_INFO)
36 #define __ASSERT_LOC(test)                              \
37 	__ASSERT_PRINT("ASSERTION FAIL [%s] @ %s:%d\n", \
38 		Z_STRINGIFY(test),                      \
39 		__FILE__, __LINE__)
40 #endif
41 
42 #if defined(CONFIG_ASSERT_NO_COND_INFO) && !defined(CONFIG_ASSERT_NO_FILE_INFO)
43 #define __ASSERT_LOC(test)                         \
44 	__ASSERT_PRINT("ASSERTION FAIL @ %s:%d\n", \
45 		__FILE__, __LINE__)
46 #endif
47 
48 #if !defined(CONFIG_ASSERT_NO_COND_INFO) && defined(CONFIG_ASSERT_NO_FILE_INFO)
49 #define __ASSERT_LOC(test)                      \
50 	__ASSERT_PRINT("ASSERTION FAIL [%s]\n", \
51 		Z_STRINGIFY(test))
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\n")
57 #endif
58 
59 #ifdef __ASSERT_ON
60 #if (__ASSERT_ON < 0) || (__ASSERT_ON > 2)
61 #error "Invalid __ASSERT() level: must be between 0 and 2"
62 #endif
63 
64 #if __ASSERT_ON
65 
66 #include <sys/printk.h>
67 
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
71 
72 #ifdef CONFIG_ASSERT_NO_FILE_INFO
73 void assert_post_action(void);
74 #define __ASSERT_POST_ACTION() assert_post_action()
75 #else  /* CONFIG_ASSERT_NO_FILE_INFO */
76 void assert_post_action(const char *file, unsigned int line);
77 #define __ASSERT_POST_ACTION() assert_post_action(__FILE__, __LINE__)
78 #endif /* CONFIG_ASSERT_NO_FILE_INFO */
79 
80 #ifdef __cplusplus
81 }
82 #endif
83 
84 #define __ASSERT_NO_MSG(test)                                             \
85 	do {                                                              \
86 		if (!(test)) {                                            \
87 			__ASSERT_LOC(test);                               \
88 			__ASSERT_POST_ACTION();                           \
89 		}                                                         \
90 	} while (false)
91 
92 #define __ASSERT(test, fmt, ...)                                          \
93 	do {                                                              \
94 		if (!(test)) {                                            \
95 			__ASSERT_LOC(test);                               \
96 			__ASSERT_MSG_INFO(fmt, ##__VA_ARGS__);            \
97 			__ASSERT_POST_ACTION();                           \
98 		}                                                         \
99 	} while (false)
100 
101 #define __ASSERT_EVAL(expr1, expr2, test, fmt, ...)                \
102 	do {                                                       \
103 		expr2;                                             \
104 		__ASSERT(test, fmt, ##__VA_ARGS__);                \
105 	} while (false)
106 
107 #if (__ASSERT_ON == 1)
108 #warning "__ASSERT() statements are ENABLED"
109 #endif
110 #else
111 #define __ASSERT(test, fmt, ...) { }
112 #define __ASSERT_EVAL(expr1, expr2, test, fmt, ...) expr1
113 #define __ASSERT_NO_MSG(test) { }
114 #endif
115 #else
116 #define __ASSERT(test, fmt, ...) { }
117 #define __ASSERT_EVAL(expr1, expr2, test, fmt, ...) expr1
118 #define __ASSERT_NO_MSG(test) { }
119 #endif
120 
121 #endif /* ZEPHYR_INCLUDE_SYS___ASSERT_H_ */
122