[flang-commits] [flang] [flang][Semantics] Introduce `-Wpass-global-variable` warning (PR #160324)
via flang-commits
flang-commits at lists.llvm.org
Tue Oct 7 13:39:57 PDT 2025
foxtran wrote:
> It is perfectly legit and indeed expected to pass global values to procedure calls.
For some cases, it may lead to undefined behaviour. For example, for the following pieces of code:
main.f90:
```fortran
program main
implicit none
common /mem/ imem
integer :: imem
imem = 0
call test(imem)
end program main
subroutine update_imem
implicit none
common /mem/ imem
integer :: imem
imem = 30
end subroutine update_imem
```
test.f90:
```
subroutine test(imem)
integer :: imem
print *, imem
call update_imem()
print *, imem
end subroutine test
```
No opts:
```console
$ flang test.f90 main.f90 -O0 -o a.exe && ./a.exe
0
30
```
With -O3:
```
$ flang test.f90 main.f90 -O3 -o a.exe && ./a.exe
0
0
```
With this PR, one gets:
```console
$ flang -Wpass-global-variable -fsyntax-only *.f90
./main.f90:6:13: warning: Passing global variable 'imem' from COMMON 'mem' as function argument [-Wpass-global-variable]
call test(imem)
^^^^
```
> I think this would be a very noisy warning.
Now, it is not. I have reduced some possible invalid cases that, unfortunately, used a lot, but in general they should not give a problem. And in some valid contexts it also does not give warnings. I have played with GAMESS(US), MRCC and xtb and it does not give a lot of warnings.
https://github.com/llvm/llvm-project/pull/160324
More information about the flang-commits
mailing list