Issue 63844
Summary [flang][Preprocessor] Unexpected error mesage due to unusual macro usage
Labels new issue
Assignees
Reporter rofirrim
    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
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to