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