<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/63844>63844</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[flang][Preprocessor] Unexpected error mesage due to unusual macro usage
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
rofirrim
</td>
</tr>
</table>
<pre>
The following input (simplified from a real occurence in an in-house application of ours):
```fortran
! input.F90
subroutine foo(a, d)
implicit none
integer :: a
integer :: d
#ifdef SOME_CASE
#define MY_MACRO )
#else
#define MY_MACRO ,d)
#endif
call foo2(a MY_MACRO
a = 3*a
end subroutine foo
```
fails to parse.
```
$ flang-new -c input.F90
error: Could not parse input.F90
./input.F90:14:3: error: expected end of statement
a = 3*a
^
./input.F90:13:3: in the context: execution part construct
call foo2(a MY_MACRO
^
./input.F90:13:3: in the context: execution part
call foo2(a MY_MACRO
^
./input.F90:2:1: in the context: SUBROUTINE subprogram
subroutine foo(a, d)
^
error: end of file
./input.F90:2:1: in the context: SUBROUTINE subprogram
subroutine foo(a, d)
^
error: expected end of line
```
At some point in the scanner, before preprocessing, we have the following token formed:
```
call foo2(a MY_MACRO a = 3*aend subroutine foo
```
After preprocessing this gets expanded into
```
call foo2(a ,d) a = 3*aend subroutine foo
```
The reason for the token to span to the next line in the file seems to be caused by the scanner allowing implicit continuations as described in https://flang.llvm.org/docs/Preprocessing.html but this seems to interact badly in this case.
If you remove the macro definition, then the scanner diagnoses an error much earlier because the lack of expansion of `MY_MACRO` means the parentheses are not well-balanced, so the parser never gets to see the statement.
gfortran and ifort/ifx accept this input.
### Workaround
We can avoid this issue if we factor out the closing parenthesis (`)`) out of the macro and move it to the macro use.
```fortran
! workaround.F90
subroutine foo(a, d)
implicit none
integer :: a
integer :: d
#ifdef SOME_CASE
#define MY_MACRO
#else
#define MY_MACRO ,d
#endif
call foo2(a MY_MACRO )
a = 3*a
end subroutine foo
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVsmO2zgQ_Rr6UmhDpmx3--CD090GcshkkAXBnIISWZQ5oUiBSy9_PyBly8t0Z5IghwEMGSqRfLU8vioMQbeWaM0Wb9jiboIp7pxfe6e097qbNE4-rz_tCJQzxj1q24K2fYrA-E3QXW-00iRBedcBgic04IRInqwg0BbQgrZXO5cCAfa90QKjdhacApd8YHzF6g2r7lh1eC6r4aecjx7t3spnA-50u6oGU0iNdylqm31zjN8g47cg84nlO0BxT-gI1lkajTZSSx4ybL0B3NsvzPLMJV5rJUnBx_fv7r_ebj7ej3ZJKjvw7q-v7za3H97DiM54TSbQdxbeyrPFVmp1Cgog0JgcGs-xjTsPXxFYfQc14xs83UZWwkVmztM6vCrUJkB00KMPNH2xAAff5qAM2vbK0iNciWMZYI_ovfM5Z7cuGQnWxeHQy3pNGd8eTfVmNmf1ps4bxxPoqScRSUKOwikIESN1ZOMh6BfCzka2uH8Foz5gaAtxRyCcjfQUBzASqXCxRx_zlxB9EidY3y_Ab4H9QbBXkXhGexno4-c3H95__vT2j_tMiN671mN3wPvvyzNCHqszFEVpQ_8DZy6oYvThil_ytzw3EYLrCHqnbTz4FwRaSz6DNaScJ-g99d4JCkHbNtsfCXb4QGX5UQGj-0YWlPMdydfka3h9uaqnLP7BC7sPQ0Xy515C3OkALcWQc4JWksxi9vIp5-7sNeiXvcltwRMGV3JRcjRkJjoIPZb_bLT0FEuBDonPFIJA1BUJaggEpkASmufTugCOHecg5JlR2qbSQgJgAElBeN2UmGEXYx9yPfiW8W0RrakxD93U-ZbxrXQiML798zR7013sDDQpDmkcfcrtwKOI0KA0z4PfOoDAC7F8q-DZJfDUuT1LOhTeQZF7nd3MLIo7OmMcSI2tdYFC7o-F09AlsQNCbzR5aKhkpOwxKL5lhpfqhn3vZMtqlIhlBR2hDWV1j55s3FE521OR40cy5qpBg1aQzP4Ed1gbyIOlB_IDg3LhaIAdtXcKpw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