Hi there, I’m wondering if Fortran has an analogue for C++'s FUNCTION variable? For background, in C++ the FUNCTION variable will tell you what function the program is currently in. Also for context, I have written an allocation subroutine that takes an allocatable array, a dimension to allocate, the name of the subroutine, and the name of the array. As well as allocating the arrays, a string is written to a log file that I can review later, so I know what subroutine the allocation is being called for, and what array is being allocated.
The routine works absolutely fine in its current manifestation, but I lament having to write the function name and the array name by hand every time. I can get around the array name by storing it as a string in the variable declarations, but a simpler method would be good.
That’s a preprocessor symbol, and no, Fortran does not currently have anything like that. As it happens, adding a cpp-like preprocessor to the Fortran standard is being seriously considered for the next revision (aka Fortran 202Y), and something like FUNCTION is very much on the table.
With gfortran one can use __FILE__ and __LINE__ macros and compile with -cpp compiler option (-fpp with intel fortran) to get the filename and line number.
__FUNCTION__ is not standard C or C++. The standard form is __func__ which is not a preprocessor symbol (unlike __FILE__ and __LINE__).
Fortran could have an intrinsic function that returns the name of the current subprogram. There is no need to wait for preprocessing to be standardized.
We have implemented something like this in fpt. fpt will insert a subroutine call to mark the entry to every function and subroutine, and to mark the exits (there may be several). Please see the description at fpt Reference: MONITOR ENTRY . You are welcome to download and use fpt - please contact us if you encounter problems.
FWIW I would prefer to have a Fortran intrinsic PLACE() which returns a character literal something like “program_name:module_name:sub_module_name:… :procedure_name:block_type and name:…” with optional arguments for selecting specific components.
Hi John, I’ve gone on the SimCon website to look at fpt, and I had no idea this level of analysis was available for Fortran code. Thank you very much for pointing this out!