xref: /optee_os/lib/libutils/isoc/include/assert.h (revision 8411e6ad673d20c4742ed30c785e3f5cdea54dfa)
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 /* assert() specs: generates a log but does not panic if NDEBUG is defined */
16 #ifdef NDEBUG
17 #define assert(expr)	do { } while (0)
18 #else
19 #define assert(expr) \
20 	do { \
21 		if (!(expr)) { \
22 			_assert_log(#expr, __FILE__, __LINE__, __func__); \
23 			_assert_break(); \
24 		} \
25 	} while (0)
26 #endif
27 
28 /* This macro is deprecated, please use static_assert instead */
29 #define COMPILE_TIME_ASSERT(x) \
30 	do { \
31 		switch (0) { case 0: case ((x) ? 1: 0): default : break; } \
32 	} while (0)
33 
34 #endif
35 
36 #if !defined(__cplusplus) || (__cplusplus < 201103L)
37 #if defined(__HAVE_SINGLE_ARGUMENT_STATIC_ASSERT)
38 #define static_assert _Static_assert
39 #else
40 /*
41  * In gcc prior to 9.1 _Static_assert requires two arguments. To allow
42  * passing a single argument to static_assert() add a workaround with
43  * macros.
44  */
45 #define ___args_count(_0, _1, x, ...) x
46 #define __args_count(...) ___args_count(__VA_ARGS__, 2, 1, 0)
47 
48 #define __static_assert_1(expr)		_Static_assert(expr, "")
49 #define __static_assert_2(expr, msg)	_Static_assert(expr, msg)
50 #define ___static_assert(count, ...)	__static_assert_ ## count(__VA_ARGS__)
51 #define __static_assert(count, ...)	___static_assert(count, __VA_ARGS__)
52 
53 #define static_assert(...) \
54 	__static_assert(__args_count(__VA_ARGS__), __VA_ARGS__)
55 #endif
56 #endif
57