fparser
fparser copied to clipboard
Add support for "function" macros
Hello,
Most of the code I am working with is using "function" like macro definitions like #define LOG(MSG, LEVEL) print *, LEVEL, MSG
. Version 0.0.16 of fparser2 seems to not support adding macro function definition in its symbol table for later resolution, whereas it does for constants. Below is a minimal example to reproduce the issue. I added GFortran commands to check what is the expected behavior (with Intel suite, replace -cpp with -fpp).
echo -n "FParser2 " && python3 -m pip show fparser | grep -i version 2> /dev/null
echo "## Check GFortran behavior"
echo -n "test_success => " && gfortran -cpp test_success.f90 -o exe && ./exe && rm -f exe
echo -n "test_fail => " && gfortran -cpp test_fail.f90 -o exe && ./exe && rm -f exe
echo -e "\n## Compile test_success & check Gfortran behavior"
fparser2 --std=f2008 test_success.f90 > test_success_fparser.f90
echo -n "test_success (compiled) => " && gfortran -cpp test_success_fparser.f90 -o exe && ./exe && rm -f exe
echo -e "\n## Compile test_fail & check Gfortran behavior"
fparser2 --std=f2008 test_fail.f90 > test_fail_fparser.f90
echo -n "test_fail (compiled) => " && gfortran -cpp test_fail_fparser.f90 -o exe && ./exe && rm -f exe
Outputs:
FParser2 Version: 0.0.16
## Check GFortran behavior
test_success => 1337
test_fail => 1337
## Compile test_success & check Gfortran behavior
File: 'test_success.f90'
test_success (compiled) => 1337
## Compile test_fail & check Gfortran behavior
File: 'test_fail.f90'
Syntax error: at line 8
>>> MY_MACRO(myvar)
test_fail (compiled) => /usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o: In function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status
test_success.f90
#define MY_CONSTANT 1337
program test
integer :: myvar
myvar = MY_CONSTANT
print *, myvar
end program test
test_fail.f90
#define MY_CONSTANT 1337
#define MY_MACRO(a) PRINT *, a
program test
integer :: myvar
myvar = MY_CONSTANT
MY_MACRO(myvar)
end program test
Best.
Hi @antoine-morvan, thanks for this. Unfortunately, cpp macros are not Fortran and therefore fparser does not understand them. fparser has had some limited support for CPP directives added to it recently but we don't envisage extending it. Our working practice is to always provide fparser with code after the pre-processor has turned it into Fortran. Will that work for your case?
Hi, thanks for your answer.
I understand your position, and I agree. Although many people might not (see this position for instance). That's what I was doing and will continue to do for now, but might come with a PR later on ;)
BTW fparser2 would greatly benefit from a mention in the documentation of this limitation, at least as a bold notice in the introduction.
Best.