xref: /linux/tools/include/nolibc/assert.h (revision 0eaed89c18aeedf0898baf2dbf5ff027c6795152)
1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2 /*
3  * Assert for NOLIBC
4  * Copyright (C) 2026 Thomas Weißschuh <linux@weissschuh.net>
5  */
6 
7 /* make sure to include all global symbols */
8 #include "nolibc.h"
9 
10 #ifndef _NOLIBC_ASSERT_H
11 #define _NOLIBC_ASSERT_H
12 
13 #include "errno.h"
14 #include "stdio.h"
15 #include "stdlib.h"
16 
17 #endif /* _NOLIBC_ASSERT_H */
18 
19 /* NDEBUG needs to be evaluated on *each* inclusion */
20 #ifdef assert
21 #undef assert
22 #endif
23 
24 #ifndef NDEBUG
25 #define assert(expr)									\
26 ({											\
27 	if (!(expr)) {									\
28 		fprintf(stderr, "%s: %s:%d: %s: Assertion `%s' failed.\n",		\
29 			program_invocation_short_name, __FILE__, __LINE__, __func__,	\
30 			#expr);								\
31 		abort();								\
32 	}										\
33 })
34 #else
35 #define assert(expr) ((void)0)
36 #endif
37