diff --git a/include/assert_macros.h b/include/assert_macros.h index 065f8d1..0b33206 100644 --- a/include/assert_macros.h +++ b/include/assert_macros.h @@ -6,8 +6,21 @@ #undef call_assert_describe #undef call_assert_diagnose -#ifdef DEBUG -# define call_assert(assertion) call assert(assertion, "No description provided (see file " // __FILE__ // ", line " // string(__LINE__) // ")") +! Check user-provided flags to decide whether assertions are enabled +#if defined(DEBUG) && defined(NDEBUG) +# error Please define at most one of DEBUG or NDEBUG +#endif + +! Deal with Fortran's stringification debacle: +! https://gcc.gnu.org/legacy-ml/fortran/2009-06/msg00131.html +#ifdef __GFORTRAN__ +#define STRINGIFY(x) "x" +#else +#define STRINGIFY(x) #x +#endif + +#if defined(DEBUG) +# define call_assert(assertion) call assert(assertion, STRINGIFY(assertion) // " in file " // __FILE__ // ", line " // string(__LINE__) // ")") # define call_assert_describe(assertion, description) call assert(assertion, description // " in file " // __FILE__ // ", line " // string(__LINE__) // ": " ) # define call_assert_diagnose(assertion, description, diagnostic_data) call assert(assertion, "file " // __FILE__ // ", line " // string(__LINE__) // ": " // description, diagnostic_data) #else diff --git a/test/test-assert-macro.F90 b/test/test-assert-macro.F90 index 770df48..dda15a8 100644 --- a/test/test-assert-macro.F90 +++ b/test/test-assert-macro.F90 @@ -53,6 +53,10 @@ program test_assert_macros end block + call_assert(100 == 200) + ! call_assert(100 + 200 + 1 + 1 + 1 + 1 + 1 == 2 + 2 + 2 + 2 + 3 + 1) + call_assert_diagnose(1==2, "up is down", diagnostic_data=1) + #undef DEBUG #include "assert_macros.h" call_assert_describe(.false., "")