Zephyr API Documentation  3.0.0
A Scalable Open Source RTOS
3.0.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
__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 <sys/printk.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#if defined(CONFIG_ASSERT_VERBOSE)
25#define __ASSERT_PRINT(fmt, ...) printk(fmt, ##__VA_ARGS__)
26#else /* CONFIG_ASSERT_VERBOSE */
27#define __ASSERT_PRINT(fmt, ...)
28#endif /* CONFIG_ASSERT_VERBOSE */
29
30#ifdef CONFIG_ASSERT_NO_MSG_INFO
31#define __ASSERT_MSG_INFO(fmt, ...)
32#else /* CONFIG_ASSERT_NO_MSG_INFO */
33#define __ASSERT_MSG_INFO(fmt, ...) __ASSERT_PRINT("\t" fmt "\n", ##__VA_ARGS__)
34#endif /* CONFIG_ASSERT_NO_MSG_INFO */
35
36#if !defined(CONFIG_ASSERT_NO_COND_INFO) && !defined(CONFIG_ASSERT_NO_FILE_INFO)
37#define __ASSERT_LOC(test) \
38 __ASSERT_PRINT("ASSERTION FAIL [%s] @ %s:%d\n", \
39 Z_STRINGIFY(test), \
40 __FILE__, __LINE__)
41#endif
42
43#if defined(CONFIG_ASSERT_NO_COND_INFO) && !defined(CONFIG_ASSERT_NO_FILE_INFO)
44#define __ASSERT_LOC(test) \
45 __ASSERT_PRINT("ASSERTION FAIL @ %s:%d\n", \
46 __FILE__, __LINE__)
47#endif
48
49#if !defined(CONFIG_ASSERT_NO_COND_INFO) && defined(CONFIG_ASSERT_NO_FILE_INFO)
50#define __ASSERT_LOC(test) \
51 __ASSERT_PRINT("ASSERTION FAIL [%s]\n", \
52 Z_STRINGIFY(test))
53#endif
54
55#if defined(CONFIG_ASSERT_NO_COND_INFO) && defined(CONFIG_ASSERT_NO_FILE_INFO)
56#define __ASSERT_LOC(test) \
57 __ASSERT_PRINT("ASSERTION FAIL\n")
58#endif
59
60#ifdef __ASSERT_ON
61#if (__ASSERT_ON < 0) || (__ASSERT_ON > 2)
62#error "Invalid __ASSERT() level: must be between 0 and 2"
63#endif
64
65#if __ASSERT_ON
66
67#include <sys/printk.h>
68
69#ifdef __cplusplus
70extern "C" {
71#endif
72
73#ifdef CONFIG_ASSERT_NO_FILE_INFO
74void assert_post_action(void);
75#define __ASSERT_POST_ACTION() assert_post_action()
76#else /* CONFIG_ASSERT_NO_FILE_INFO */
77void assert_post_action(const char *file, unsigned int line);
78#define __ASSERT_POST_ACTION() assert_post_action(__FILE__, __LINE__)
79#endif /* CONFIG_ASSERT_NO_FILE_INFO */
80
81#ifdef __cplusplus
82}
83#endif
84
85#define __ASSERT_NO_MSG(test) \
86 do { \
87 if (!(test)) { \
88 __ASSERT_LOC(test); \
89 __ASSERT_POST_ACTION(); \
90 } \
91 } while (false)
92
93#define __ASSERT(test, fmt, ...) \
94 do { \
95 if (!(test)) { \
96 __ASSERT_LOC(test); \
97 __ASSERT_MSG_INFO(fmt, ##__VA_ARGS__); \
98 __ASSERT_POST_ACTION(); \
99 } \
100 } while (false)
101
102#define __ASSERT_EVAL(expr1, expr2, test, fmt, ...) \
103 do { \
104 expr2; \
105 __ASSERT(test, fmt, ##__VA_ARGS__); \
106 } while (false)
107
108#if (__ASSERT_ON == 1)
109#warning "__ASSERT() statements are ENABLED"
110#endif
111#else
112#define __ASSERT(test, fmt, ...) { }
113#define __ASSERT_EVAL(expr1, expr2, test, fmt, ...) expr1
114#define __ASSERT_NO_MSG(test) { }
115#endif
116#else
117#define __ASSERT(test, fmt, ...) { }
118#define __ASSERT_EVAL(expr1, expr2, test, fmt, ...) expr1
119#define __ASSERT_NO_MSG(test) { }
120#endif
121
122#endif /* ZEPHYR_INCLUDE_SYS___ASSERT_H_ */
void assert_post_action(const char *file, unsigned int line)
Definition: spinlock_error_case.c:33