Skip to content

Commit

Permalink
WIP: Try to deploy a variadic macro version of call_assert()
Browse files Browse the repository at this point in the history
Unfortunately, gfortran 14.2 does not appear to support variadic macros :-P
  • Loading branch information
bonachea committed Oct 21, 2024
1 parent 05fc0cb commit 3777455
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
8 changes: 4 additions & 4 deletions example/invoke-via-macro.F90
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ program invoke_via_macro
! these calls will be entirely removed by the preprocessor.

call_assert(1==1) ! true assertion
call_assert_describe(2>0, "example assertion invocation via macro") ! true assertion
call_assert_diagnose(1+1==2, "example with scalar diagnostic data", 1+1) ! true assertion
call_assert(2>0, "example assertion invocation via macro") ! true assertion
call_assert(1+1==2, "example with scalar diagnostic data", 1+1) ! true assertion
#if ASSERTIONS
print *
print *,'Here comes the expected assertion failure:'
print *
#endif
!call_assert(1+1>2)
!call_assert_describe(1+1>2, "Mathematics is broken!")
call_assert_diagnose(1+1>2, "example with array diagnostic data" , intrinsic_array_t([1,1,2])) ! false assertion
!call_assert(1+1>2, "Mathematics is broken!")
call_assert(1+1>2, "example with array diagnostic data" , intrinsic_array_t([1,1,2])) ! false assertion

end program invoke_via_macro
13 changes: 7 additions & 6 deletions include/assert_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
#endif

#if ASSERTIONS
# define call_assert(assertion) call assert(assertion, "call_assert(" // 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, description // " in file " // __FILE__ // ", line " // string(__LINE__), diagnostic_data)
# define call_assert_3(cond, desc, data) call assert(cond, "file " // __FILE__ // ", line " // string(__LINE__) // ": " // desc, data)
# define call_assert_2(cond, desc) call assert(cond, "file " // __FILE__ // ", line " // string(__LINE__) // ": " // desc)
# define call_assert_1(cond) call_assert_2(cond, "Failed assertion: " // STRINGIFY(cond))
# define call_assert_dispatch(_1, _2, _3, NAME, ...) NAME
# define call_assert(...) call_assert_dispatch(__VA_ARGS__, call_assert_3, call_assert_2, call_assert_1, _DUMMY)(__VA_ARGS__)
#else
# define call_assert(assertion)
# define call_assert_describe(assertion, description)
# define call_assert_diagnose(assertion, description, diagnostic_data)
# define call_assert(...)
#endif

0 comments on commit 3777455

Please sign in to comment.