]> pd.if.org Git - pdclib/blob - includes/assert.h
PDCLib includes with quotes, not <>.
[pdclib] / includes / assert.h
1 /* 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 #ifndef _PDCLIB_ASSERT_H
25 #define _PDCLIB_ASSERT_H _PDCLIB_ASSERT_H
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 /* Functions _NOT_ tagged noreturn as this hampers debugging */
32 void _PDCLIB_assert99( char const * const, char const * const, char const * const );
33 void _PDCLIB_assert89( char const * const );
34
35 #ifdef __cplusplus
36 }
37 #endif
38
39 #if _PDCLIB_C_VERSION >= 2011
40 #define static_assert _Static_assert
41 #else
42 #define static_assert( e, m )
43 #endif
44
45 #endif
46
47 /* If NDEBUG is set, assert() is a null operation. */
48 #undef assert
49
50 #ifdef NDEBUG
51 #define assert( ignore ) ( (void) 0 )
52 #elif _PDCLIB_C_MIN(99)
53 #define assert(expression) \
54     do { if(!(expression)) { \
55         _PDCLIB_assert99("Assertion failed: " _PDCLIB_symbol2string(expression)\
56                          ", function ", __func__, \
57                          ", file " __FILE__ \
58                          ", line " _PDCLIB_symbol2string( __LINE__ ) \
59                          "." _PDCLIB_endl ); \
60         _PDCLIB_UNREACHABLE; \
61       } \
62     } while(0)
63
64 #else
65 #define assert(expression) \
66     do { if(!(expression)) { \
67         _PDCLIB_assert89("Assertion failed: " _PDCLIB_symbol2string(expression)\
68                          ", file " __FILE__ \
69                          ", line " _PDCLIB_symbol2string( __LINE__ ) \
70                          "." _PDCLIB_endl ); \
71         _PDCLIB_UNREACHABLE; \
72       } \
73     } while(0)
74 #endif