]> pd.if.org Git - pdclib/blobdiff - includes/assert.h
The other code did not work, and ((void)0) is actually specified in the standard.
[pdclib] / includes / assert.h
index f99ccd785fbb7431c47b2b71d4c25e6c6369ae40..44b30b87b7bdbe98281ad086afed1162fb623a4c 100644 (file)
@@ -1,35 +1,70 @@
-// ----------------------------------------------------------------------------
-// $Id$
-// ----------------------------------------------------------------------------
-// Public Domain C Library - http://pdclib.sourceforge.net
-// This code is Public Domain. Use, modify, and redistribute at will.
-// ----------------------------------------------------------------------------
-// Provides the debug macro assert().
-// ----------------------------------------------------------------------------
+/* 7.2 Diagnostics <assert.h>
 
-#ifndef __ASSERT_H
-#define __ASSERT_H __ASSERT_H
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
 
-// ----------------------------------------------------------------------------
-// AUXILIARY
+#include <_PDCLIB_aux.h>
+#include <_PDCLIB_config.h>
 
-// Helper function doing the print to stderr and call to abort().
-void __assert( char const * const expression, // the tested expression
-               char const * const file,       // name of source file
-               char const * const function,   // name of function
-               int const line );              // number of source file line
+/*
+   Defines a macro assert() that, depending on the value of the preprocessor
+   symbol NDEBUG, does
+   * evaluate to a void expression if NDEBUG is set OR the parameter expression
+     evaluates to true;
+   * print an error message and terminates the program if NDEBUG is not set AND
+     the parameter expression evaluates to false.
+  The error message contains the parameter expression, name of the source file
+  (__FILE__), line number (__LINE__), and (from C99 onward) name of the function
+  (__func__).
+   The header can be included MULTIPLE times, and redefines the macro depending
+   on the current setting of NDEBUG.
+*/
 
-// ----------------------------------------------------------------------------
-// DEFINES
+_PDCLIB_BEGIN_EXTERN_C
 
-// TODO: Check the macro for if-compatibility.
+#ifndef _PDCLIB_ASSERT_H
+#define _PDCLIB_ASSERT_H _PDCLIB_ASSERT_H
 
+/* Functions _NOT_ tagged noreturn as this hampers debugging */
+void _PDCLIB_assert99( char const * const, char const * const, char const * const );
+void _PDCLIB_assert89( char const * const );
+
+#if _PDCLIB_C_VERSION >= 2011
+#define static_assert _Static_assert
+#else
+#define static_assert( e, m )
+#endif
+
+#endif
+
+/* If NDEBUG is set, assert() is a null operation. */
 #undef assert
-#if defined NDEBUG
-#define assert( x ) ( (void) 0 )
+
+#ifdef NDEBUG
+#define assert( ignore ) ( (void) 0 )
+#elif _PDCLIB_C_MIN(99)
+#define assert(expression) \
+    do { if(!(expression)) { \
+        _PDCLIB_assert99("Assertion failed: " _PDCLIB_symbol2string(expression)\
+                         ", function ", __func__, \
+                         ", file " __FILE__ \
+                         ", line " _PDCLIB_symbol2string( __LINE__ ) \
+                         "." _PDCLIB_endl ); \
+        _PDCLIB_UNREACHABLE; \
+      } \
+    } while(0)
+    
 #else
-#define assert( x ) ( x ) ? ( (void) 0 ) \
-                          :  __assert( #x, __FILE__, __func__, __LINE__ )
+#define assert(expression) \
+    do { if(!(expression)) { \
+        _PDCLIB_assert89("Assertion failed: " _PDCLIB_symbol2string(expression)\
+                         ", file " __FILE__ \
+                         ", line " _PDCLIB_symbol2string( __LINE__ ) \
+                         "." _PDCLIB_endl ); \
+        _PDCLIB_UNREACHABLE; \
+      } \
+    } while(0)
 #endif
 
-#endif // __ASSERT_H
+_PDCLIB_END_EXTERN_C