Subroutine Merge Capture

Is there a way to check if anti-aliasing is occurring in the Fortran routine, or at least notify the compiler of a warning issue?

Consider this (rather simplified) example:

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

Here in the subroutine, the result is zero. If the same variable is specified as input and output, the result is (obviously) incorrect. Is there a way to capture this type of alias at runtime or at compile time?

+4
source share
2 answers

, gfortran -Waliasing, in out. , c intent(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)
+4

g95 gfortran . . (in) , , , .

module alias
contains
  subroutine myAdd(a, b, c)
    integer,intent(in)    :: a, b
    integer,intent(inout) :: c
    c = 0
    c = a + b
  end subroutine myadd
end module alias

program test
  use alias, only: myadd
  integer :: a, b
  a = 1 ; b = 2
  call myAdd((a),(b),b) ! parentheses around arguments a and b
  print*, b,"is 3"
  call myAdd(a, b, b)
  print*, b,"is not 3"
end program test
+3

Source: https://habr.com/ru/post/1540866/


All Articles