]> pd.if.org Git - pdclib/blob - includes/assert.h
e2618a0e964a32ec5e947acf4aafc2ccb5d2fcde
[pdclib] / includes / assert.h
1 /* 7.2 Diagnostics <assert.h>
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #include <_PDCLIB_aux.h>
8 #include <_PDCLIB_config.h>
9
10 /*
11    Defines a macro assert() that, depending on the value of the preprocessor
12    symbol NDEBUG, does
13    * evaluate to a void expression if NDEBUG is set OR the parameter expression
14      evaluates to true;
15    * print an error message and terminates the program if NDEBUG is not set AND
16      the parameter expression evaluates to false.
17   The error message contains the parameter expression, name of the source file
18   (__FILE__), line number (__LINE__), and (from C99 onward) name of the function
19   (__func__).
20    The header can be included MULTIPLE times, and redefines the macro depending
21    on the current setting of NDEBUG.
22 */
23
24 _PDCLIB_BEGIN_EXTERN_C
25
26 #ifndef _PDCLIB_ASSERT_H
27 #define _PDCLIB_ASSERT_H _PDCLIB_ASSERT_H
28
29 /* Functions _NOT_ tagged noreturn as this hampers debugging */
30 void _PDCLIB_assert99( char const * const, char const * const, char const * const );
31 void _PDCLIB_assert89( char const * const );
32
33 #if _PDCLIB_C_VERSION >= 2011
34 #define static_assert _Static_assert
35 #else
36 #define static_assert( e, m )
37 #endif
38
39 #endif
40
41 /* If NDEBUG is set, assert() is a null operation. */
42 #undef assert
43
44 #ifdef NDEBUG
45 #define assert( ignore ) do { \
46         if(!(expression)) { _PDCLIB_UNREACHABLE; } \
47     } while(0)
48
49 #elif _PDCLIB_C_MIN(99)
50 #define assert(expression) \
51     do { if(!(expression)) { \
52         _PDCLIB_assert99("Assertion failed: " _PDCLIB_symbol2string(expression)\
53                          ", function ", __func__, \
54                          ", file " __FILE__ \
55                          ", line " _PDCLIB_symbol2string( __LINE__ ) \
56                          "." _PDCLIB_endl ); \
57         _PDCLIB_UNREACHABLE; \
58       } \
59     } while(0)
60     
61 #else
62 #define assert(expression) \
63     do { if(!(expression)) { \
64         _PDCLIB_assert89("Assertion failed: " _PDCLIB_symbol2string(expression)\
65                          ", file " __FILE__ \
66                          ", line " _PDCLIB_symbol2string( __LINE__ ) \
67                          "." _PDCLIB_endl ); \
68         _PDCLIB_UNREACHABLE; \
69       } \
70     } while(0)
71 #endif
72
73 _PDCLIB_END_EXTERN_C