How to make gfortran give a warning for reading variables declared as intent(out) without initializing it first?

Jorge D'Elia jdelia@intec.unl.edu.ar
Tue Jun 16 11:09:00 GMT 2015


Hi Peng,

----- Mensaje original -----
> De: "Peng Yu" <pengyu.ut@gmail.com>
> Para: fortran@gcc.gnu.org
> Enviado: Lunes, 15 de Junio 2015 19:33:27
> Asunto: How to make gfortran give a warning for reading variables declared as intent(out) without initializing it
> first?
> 
> Hi,
> 
> The following code shows that even "b" is declared as "real(dp),
> intent(out)" in bar. It still can get the value 2 defined in the 
> main program.
> I'd think that the gfortran should have warned the user that "b" is
> used without being first set in "bar". Is this feature available in
> gfortran? Thanks.
> 
> $ cat main.f90
> program main
>   integer, parameter :: dp=kind(1.0d0)
>   real(dp) a, b
>   a = 1
>   b = 2
>   call bar(a, b)
>   print *, a, b
> end program
> 
> subroutine bar (a, b)
>   integer, parameter :: dp=kind(1.0d0)
>   real(dp), intent(in) :: a
>   real(dp), intent(out) :: b
>   print *, b
>   b = a
> end subroutine
>
> Regards,
> Peng

You must use either an interface or a module, or a contains. 
However, I neither found some compiler flag to detect that 
fact (at least with gcc 4.5.1, 4.7.2 or 6.0.0). 
Moreover, one combination of compiler flags that reports 
a pair of warnings is:

$ gfortran -Wall -Wextra -Wsurprising -std=f2008 -Wpedantic / 
  -Wconversion-extra -Wimplicit-procedure -o main1.exe main1.f90 

main1.f90:7:6:

   a = 1
      1
Warning: Conversion from INTEGER(4) to REAL(8) at (1) [-Wconversion-extra]
main1.f90:8:6:

   b = 2
      1
Warning: Conversion from INTEGER(4) to REAL(8) at (1) [-Wconversion-extra]
main1.f90:9:16:

   call bar(a, b)
                1
Warning: Procedure 'bar' called at (1) is not explicitly declared [-Wimplicit-procedure]


Regards,
Jorge.
--



More information about the Fortran mailing list