Skip to content

Commit

Permalink
WIP: stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
bonachea committed Sep 16, 2024
1 parent 024438f commit 97da93e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
17 changes: 15 additions & 2 deletions include/assert_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/test-assert-macro.F90
Original file line number Diff line number Diff line change
Expand Up @@ -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., "")
Expand Down

0 comments on commit 97da93e

Please sign in to comment.