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 static inline void _runtime_assert_trap(const char *expr_str, const char *file, 24 const int line, const char *func) 25 { 26 volatile bool do_break = true; 27 28 _assert_log(expr_str, file, line, func); 29 if (do_break) 30 _assert_break(); 31 } 32 33 /* 34 * runtime_assert() behaves as assert() except that it doesn't tell the 35 * compiler it will never return. This can be used to avoid the warning: 36 * error: function might be candidate for attribute ‘noreturn’ 37 */ 38 #ifdef NDEBUG 39 #define assert(expr) ((void)0) 40 #define runtime_assert(expr) ((void)0) 41 #else 42 #define assert(expr) \ 43 ((expr) ? (void)0 : _assert_trap(#expr, __FILE__, __LINE__, __func__)) 44 #define runtime_assert(expr) \ 45 ((expr) ? (void)0 : \ 46 _runtime_assert_trap(#expr, __FILE__, __LINE__, __func__)) 47 #endif 48 49 /* This macro is deprecated, please use static_assert instead */ 50 #define COMPILE_TIME_ASSERT(x) \ 51 do { \ 52 switch (0) { case 0: case ((x) ? 1: 0): default : break; } \ 53 } while (0) 54 55 #endif 56 57 #if !defined(__cplusplus) || (__cplusplus < 201103L) 58 #if defined(__HAVE_SINGLE_ARGUMENT_STATIC_ASSERT) 59 #define static_assert _Static_assert 60 #else 61 /* 62 * In gcc prior to 9.1 _Static_assert requires two arguments. To allow 63 * passing a single argument to static_assert() add a workaround with 64 * macros. 65 */ 66 #define ___args_count(_0, _1, x, ...) x 67 #define __args_count(...) ___args_count(__VA_ARGS__, 2, 1, 0) 68 69 #define __static_assert_1(expr) _Static_assert(expr, "") 70 #define __static_assert_2(expr, msg) _Static_assert(expr, msg) 71 #define ___static_assert(count, ...) __static_assert_ ## count(__VA_ARGS__) 72 #define __static_assert(count, ...) ___static_assert(count, __VA_ARGS__) 73 74 #define static_assert(...) \ 75 __static_assert(__args_count(__VA_ARGS__), __VA_ARGS__) 76 #endif 77 #endif /* __ASSERT_H */ 78