1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 */ 5 #ifndef __ASSERT_H 6 #define __ASSERT_H 7 8 #include <compiler.h> 9 #include <trace.h> 10 11 void __noreturn _assert_break(void); 12 void _assert_log(const char *expr, const char *file, const int line, 13 const char *func); 14 15 static inline void __noreturn _assert_trap(const char *expr_str, 16 const char *file, const int line, 17 const char *func) 18 { 19 _assert_log(expr_str, file, line, func); 20 _assert_break(); 21 } 22 23 #ifdef NDEBUG 24 #define assert(expr) ((void)0) 25 #else 26 #define assert(expr) \ 27 ((expr) ? (void)0 : _assert_trap(#expr, __FILE__, __LINE__, __func__)) 28 #endif 29 30 /* This macro is deprecated, please use static_assert instead */ 31 #define COMPILE_TIME_ASSERT(x) \ 32 do { \ 33 switch (0) { case 0: case ((x) ? 1: 0): default : break; } \ 34 } while (0) 35 36 #endif 37 38 #if !defined(__cplusplus) || (__cplusplus < 201103L) 39 #if defined(__HAVE_SINGLE_ARGUMENT_STATIC_ASSERT) 40 #define static_assert _Static_assert 41 #else 42 /* 43 * In gcc prior to 9.1 _Static_assert requires two arguments. To allow 44 * passing a single argument to static_assert() add a workaround with 45 * macros. 46 */ 47 #define ___args_count(_0, _1, x, ...) x 48 #define __args_count(...) ___args_count(__VA_ARGS__, 2, 1, 0) 49 50 #define __static_assert_1(expr) _Static_assert(expr, "") 51 #define __static_assert_2(expr, msg) _Static_assert(expr, msg) 52 #define ___static_assert(count, ...) __static_assert_ ## count(__VA_ARGS__) 53 #define __static_assert(count, ...) ___static_assert(count, __VA_ARGS__) 54 55 #define static_assert(...) \ 56 __static_assert(__args_count(__VA_ARGS__), __VA_ARGS__) 57 #endif 58 #endif /* __ASSERT_H */ 59