在子程序中捕获别名

Ale*_*ogt 5 fortran

有没有办法检查Fortran子例程中是否出现别名,或者至少告诉编译器发出警告?

考虑这个(相当简单)的例子:

module alias
contains
  subroutine myAdd(a, b, c)
    integer,intent(in)    :: a, b
    integer,intent(inout) :: c

    c = 0
    c = a + b
  end subroutine
end module

program test
  use alias
  integer :: a, b

  a = 1 ; b = 2
  call myAdd(a, b, b)
  print *, b, 'is not 3'
end program
Run Code Online (Sandbox Code Playgroud)

这里,结果在子程序中设置为零.如果给出相同的变量作为输入和输出,结果(显然)是错误的.有没有办法在运行时或编译时捕获这种别名?

M. *_* B. 5

是的,gfortran将使用编译器选项检测一些别名-Waliasing,但是,参数必须具有意图inout.它不适用于您的示例,因为您已将参数声明cintent(inout).在此示例中,您可以简单地更改意图,out因为c未使用输入值.他们尝试编译选项!gfortran输出:

alias.f90:17.16:

  call myAdd(a, b, b)
                1
Warning: Same actual argument associated with INTENT(IN) argument 'b' and INTENT(OUT) argument 'c' at (1)
Run Code Online (Sandbox Code Playgroud)