Skip to content

Commit

Permalink
feat(example): demo invoke macro, better macro msg
Browse files Browse the repository at this point in the history
  • Loading branch information
rouson committed Aug 7, 2024
1 parent 890b220 commit 3eef611
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
25 changes: 25 additions & 0 deletions example/invoke-via-macro.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "../src/assert/assert_macros.h"

program invoke_via_macro
!! Demonstrate how to invoke the 'assert' subroutine using a preprocessor macro that facilitates
!! the complete removal of the call in the absence of the compiler flag -DDEBUG.
use assert_m, only : assert, intrinsic_array_t, string
!! If an "only" clause is employed as above, it must include the "string" function that the
!! call_assert* macros reference when transforming the code below into "assert" subroutine calls.
implicit none

#ifndef DEBUG
print *
print *,'To enable the "assert" call, define -DDEBUG, e.g., fpm run --example invoke-via-macro --flag "-DDEBUG -fcoarray=single"'
print *
#endif

! The C preprocessor will convert each call_assert* macro below into calls to the "assert" subroutine
! (if -DDEBUG is in the compiler command) or into nothing (if -DDEBUG is not in the compiler command).

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_diagnose(1+1>2, "example with array diagnostic data" , intrinsic_array_t([1,1,2])) ! false assertion

end program invoke_via_macro
4 changes: 2 additions & 2 deletions src/assert/assert_macros.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifdef DEBUG
# define call_assert(assertion) call assert(assertion, "file " // __FILE__ // ", line " // string(__LINE__))
# define call_assert_describe(assertion, description) call assert(assertion, "file " // __FILE__ // ", line " // string(__LINE__) // ": " // description)
# define call_assert(assertion) call assert(assertion, "No description provided (see 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
# define call_assert(assertion)
Expand Down

0 comments on commit 3eef611

Please sign in to comment.