Issue 146362
Summary [Flang] Preprocessor
Labels flang
Assignees
Reporter marikurz-amd
    I encountered 3 problems with the Flang preprocessor that give unexpected results that do not match the output of the Intel, GNU or Cray Fortran compilers. The following gives a reproducer and details for each of them. Each code snippet is compiled with:
```bash
flang -cpp -o demonstrator demonstrator.f90 && ./demonstrator
```
using
```
flang version 21.0.0git (https://github.com/llvm/llvm-project 532facc78e075255afde69f2d86f26e4d4dd4c7f)
Target: x86_64-unknown-linux-gnu
```

### Bug 1:

Compiling the following code:
```fortran
#define UNITY(k) 1_ ## k
PROGRAM REPRODUCER
WRITE(*,*) UNITY(4)
END PROGRAM REPRODUCER
```
causes a core dump in the preprocessing stage.
The expected output would be to just print a `1` to standard out (intel, Cray compiler) or throw an compilation error (GNU) since `1_` is not a parsable token. In no case it should crash.

### Bug 2:

The following demonstrator reveals a problem in a composite truth evaluation:

```fortran
PROGRAM Demonstrator
#if 1
WRITE(*,*) '`1` evaluates to TRUE'
#else
WRITE(*,*) '`1` evaluates to FALSE'
#endif
#if 2
WRITE(*,*) '`2` evaluates to TRUE'
#else
WRITE(*,*) '`2` evaluates to FALSE'
#endif
#if (1 && 2)
WRITE(*,*) '`1 && 2` evaluates to TRUE'
#else
WRITE(*,*) '`1 && 2` evaluates to FALSE'
#endif
END PROGRAM Demonstrator
```
The program outputs:
```
 `1` evaluates to TRUE
 `2` evaluates to TRUE
 `1 && 2` evaluates to FALSE
```
even though both values individually evaluate to `TRUE`.
The generated preprocessed file looks like (empty lines removed):
```fortran
#line "./demonstrator.f90" 1
 PROGRAM Demonstrator
      WRITE(*,*) '`1` evaluates to TRUE'
 WRITE(*,*) '`2` evaluates to TRUE'
      WRITE(*,*) '`1 && 2` evaluates to FALSE'
      END PROGRAM Demonstrator
```
The expected output (and the one obtained with Intel, GNU, Cray) would be:
```
 `1` evaluates to TRUE
 `2` evaluates to TRUE
 `1 && 2` evaluates to TRUE
```

### Bug 3:

The token `NUM` is not correctly replaced in the following example:
```fortran
#define NUM 1
PROGRAM Reproducer
IMPLICIT NONE
! This compiles
WRITE(*,*) &
NUM
! This does also compile
WRITE(*,*) (&
1,NUM )
! This does NOT compile
WRITE(*,*) (&
NUM )
END PROGRAM Reproducer
```
The preprocessed output yields (empty lines removed)
```fortran
#line "./demonstrator.f90" 2
      PROGRAM Reproducer
      IMPLICIT NONE
      WRITE(*,*) 1
      WRITE(*,*)( 1,1)
 WRITE(*,*)( NUM)
      END PROGRAM Reproducer
```
The final `NUM` does not get replaced due to the newline and the brackets, causing the compilation to fail, since `NUM` is not defined. Ifort, Gfortran and Cray-Fortran all compile this code and yield something like this in the output:
```
 1
 (1.,1.)
 1
```
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to