Bug 88079 - warn about procedure arguments without INTENT
Summary: warn about procedure arguments without INTENT
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: unknown
: P5 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2018-11-18 22:41 UTC by Vivek Rao
Modified: 2021-09-13 00:08 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2019-01-15 00:00:00


Attachments
Warn about missing intent or value (1.97 KB, patch)
2019-07-01 12:27 UTC, MarkEggleston
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Vivek Rao 2018-11-18 22:41:02 UTC
I think all new Fortran code should have the INTENT specified for the arguments of all procedures. I request that gfortran add a warning for code not doing this. If a warning is added, users who agree with my style recommendation can use a -Werror flag to force them to fix code without INTENTs for all arguments.

This is, of course, an enhancement request, not a bug report.
Comment 1 kargls 2018-12-01 01:00:37 UTC
(In reply to Vivek Rao from comment #0)
> I think all new Fortran code should have the INTENT specified for the
> arguments of all procedures. I request that gfortran add a warning for code
> not doing this. If a warning is added, users who agree with my style
> recommendation can use a -Werror flag to force them to fix code without
> INTENTs for all arguments.
> 
> This is, of course, an enhancement request, not a bug report.

How does gfortran differential between new Fortran code and
old Fortran code.  It is the programmer's responsibility to 
know what he or she is doing.
Comment 2 Thomas Koenig 2019-01-15 13:48:57 UTC
(In reply to kargl from comment #1)

> How does gfortran differential between new Fortran code and
> old Fortran code.  It is the programmer's responsibility to 
> know what he or she is doing.

Well, anybody who wants to use old code should never use
such an option :-)

This could be useful, I suppose.
Comment 3 MarkEggleston 2019-06-27 10:20:10 UTC
I propose that -Wintent be added that would provide warnings in the following circumstances:

1. At declaration of a dummy variable, this will include interface blocks.
2. Assignment of a value to a dummy variable that has no intent specified (unless it has the value attribute (Fortran 2003)).
3. When dummy variable (with unknown or in intent) is used as an argument to a procedure where the intent of the associated dummy variable is unknown, out or inout. This would also apply where an implicit interface is used.

The reason a warning should be issued at declaration is so that a suitable warning is raised when there is no way of determining whether the variable can be changed as the procedure is only known by its interface i.e. it is compiled separately.

Code that does not specify the intent of dummy values run the risk containing errors that will result in the program crashing at runtime, e.g. calling a procedure with a constant as an argument and the associated dummy variable is modified. Catching potential bugs at compile time is preferable.

Using -Wintent in conjunction with -Wimplicit-interface and -Werror will encourage the use of interface blocks and intent thus improving Fortran code.
Comment 4 MarkEggleston 2019-07-01 12:27:08 UTC
Created attachment 46539 [details]
Warn about missing intent or value

Work in progress. Warns at declaration, assignment and possible modification of a procedure argument.
Comment 5 MarkEggleston 2019-07-01 12:48:58 UTC
Given the program below:

program main
  implicit none
  integer :: n
  n = 5
  call bar(n)

end program main

subroutine bar(n)
  integer, intent(in) :: n
  real :: x
  print *,"bar before dusty", n
  call dusty(n)
  print *,"bar after dusty", n
end subroutine bar

subroutine dusty(n)
  integer :: n
  n = n - 1
end

the attached patch produces the following warnings:

pr88079_2.f90:19:2:

   19 |   n = n - 1
      |  1
Warning: Dummy variable 'n' in assignment at (1) has missing INTENT or VALUE [-Wintent]
pr88079_2.f90:13:15:

   13 |   call dusty(n)
      |               1
Warning: Dummy variable 'n' at (1) has INTENT(IN) and may be modified [-Wintent]
pr88079_2.f90:17:18:

   17 | subroutine dusty(n)
      |                  1
Warning: Dummy variable 'n' in assignment at (1) has missing INTENT or VALUE [-Wintent]

Note: if compiles with a standard earlier than Fortran 2003 "or VALUE" will be omitted.

I'm not happy with the location of the last warning which is why it is worded as it is. I would prefer the following:

   18 |   integer :: n
      |           1
Warning: Missing INTENT or VALUE at (1)

I currently don't know enough about identifying the location of a warning or error to determine whether this can be done.

Where dusty(n) is called the interface to dusty is known, however, this is not always the case so the attribute of the associate dummy variable can always be checked. To help identify a potential problem -Wimplicit-inteface can be used.
Comment 6 MarkEggleston 2019-07-01 12:58:31 UTC
Where a module is used:

module foo
  implicit none

  interface
    subroutine dusty(n)
      integer :: n
    end subroutine
  end interface

contains
  subroutine bar(n)
    integer, intent(in) :: n
    real :: x
    print *,"bar before dusty", n
    call dusty(n)
    print *,"bar after dusty", n
  end subroutine bar

end module foo

subroutine dusty(n)
  integer :: n
  n = n - 1
end

program main
  use foo
  implicit none
  integer :: n
  n = 5
  call bar(n)

end program main

The call to dusty in bar would be with an implicit interface were it not for the interface block. I get the same warnings as in comment 5.

I would like to also issue a warning for the interface block but don't I know where I could insert an code to issue a warning anywhere near the interface block. I know where the dummy variable is processed from a lexical point of view but I don't know where the interface block is handled. Some indication of where to look would be greatly appreciated.