fpt and WinFPT Reference Manual - Command-line Commands
| SimCon Home | Ref Manual Home |
CHECK INTENT
Syntax:
[DO NOT] CHECK INTENT
Function:
fpt checks that the INTENT declarations of sub-program arguments are consistent with the way in which the arguments are used.
The Fortran Standard
The Fortran standard provides three INTENT specifications: INTENT(IN), INTENT(OUT) and INTENT(INOUT). An argument declared to be INTENT(IN) may be, but is not required to be read by the sub-program, and data may not be written to it. An argument declared INTENT(OUT) may not be read in the sub-program before data are written to it. Arguably, it may not be exported from the sub-program before data are written to it, and if this view is taken, data must always be written to it. An argument declared INTENT(INOUT) may, but is not required to be both read and written.
Three classes of error may occur:
Errors in INTENT(IN)
All Fortran 90 compilers which we have tested successfully detect the situation where an INTENT(IN) argument is explicitly assigned across an equals sign in the sub-program in which it is declared.
The situation which causes problems is that shown in the example below:
PROGRAM t_intent_in n0=1 CALL s1(n0) WRITE(6,'("After s1: ",I2)')n0 END PROGRAM t_intent_in SUBROUTINE s1(n1) INTEGER,INTENT(IN) :: n1 CALL s2(n1) END SUBROUTINE s1 SUBROUTINE s2(n2) n2=2 END SUBROUTINE s2
The argument n1 is declared INTENT(IN) and is passed into the subroutine s2 where it is assigned. Under the compilers tested, gfortran, g95, Intel ifort, Compaq Visual Fortran and Salford FTN95, only FTN95 reported an error, and the error was detected only with the FTN95 /checkmate switch, and only at run-time. This occurred even though the sub-programs were all compiled together in the same file.
fpt detects this situation without any explicit check of INTENT. The error is marked by a diagnostic:
SUBROUTINE s1(n1) INTEGER,INTENT(IN)::n1 !-----------------------------^------------------------------------------------ !!! fpt - 2491 INTENT declared IN but argument is written to: !------------------------------------------------------------------------------ CALL s2(n1) END SUBROUTINE s1
If the command CHECK INTENT is made, fpt reports a summary of the INTENT errors:
Check of INTENT Declarations ============================ Arguments declared INTENT(IN) which are assigned ------------------------------------------------ Name Sub-prog/Struct Use/COMMON Address Type Size Value/Bounds ---- --------------- ---------- ------- ---- ---- ------------ n1 s1 Formal Input-Output Argument INTEGER *4 df Arguments declared INTENT(OUT) which are read before they are assigned ---------------------------------------------------------------------- None Arguments declared INTENT(OUT) possibly read before they are assigned --------------------------------------------------------------------- None Total number of arguments 2 Declared INTENT(IN) 1 Declared INTENT(OUT) 0 Declared INTENT(INOUT) 0 No INTENT declaration 1 Sub-program formal arguments 0 INTENT(IN) Violations 1 100.0% INTENT(OUT) Violations 0 0.0% Additional possible INTENT(OUT) Violations 0 0.0%
Errors in INTENT(OUT)
In the example shown below, the argument n1 is declared INTENT(OUT) but is always read in the WRITE statement at the start of the subroutine.
PROGRAM t_intent_out_1 n0=1 CALL s1(n0) WRITE(6,'("After s1: ",I2)')n0 END PROGRAM t_intent_out_1 SUBROUTINE s1(n1) INTEGER,INTENT(OUT) :: n1 WRITE(6,'("Start of s1: ",I2)')n1 n1=2 END SUBROUTINE s1
Again, under the compilers tested, gfortran, g95, Intel ifort, Compaq Visual Fortran and Salford FTN95, only FTN95 reported an error, and the error was detected only with the FTN95 /checkmate switch, and only at run-time.
fpt reports a warning by default:
SUBROUTINE s1(n1) INTEGER,INTENT(OUT)::n1 !------------------------------^----------------------------------------------- !!! fpt - 2493 INTENT declared OUT but argument is read: !------------------------------------------------------------------------------ WRITE (6,'("Start of s1: ",I2)')n1 n1=2 END SUBROUTINE s1
However, the check made by default does not fully analyse the program flow. The check made in response to the CHECK INTENT command analyses the flow and finds arguments which are always read before they are assigned. Note that the flow may be data-dependent and fpt also lists the arguments which could be read before they are assigned.
Check of INTENT Declarations ============================ Arguments declared INTENT(IN) which are assigned ------------------------------------------------ None Arguments declared INTENT(OUT) which are read before they are assigned ---------------------------------------------------------------------- Name Sub-prog/Struct Use/COMMON Address Type Size Value/Bounds ---- --------------- ---------- ------- ---- ---- ------------ n1 s1 Formal Input-Output Argument INTEGER *4 df Arguments declared INTENT(OUT) possibly read before they are assigned --------------------------------------------------------------------- None Total number of arguments 1 Declared INTENT(IN) 0 Declared INTENT(OUT) 1 Declared INTENT(INOUT) 0 No INTENT declaration 0 Sub-program formal arguments 0 INTENT(OUT) Violations 1 100.0% Additional possible INTENT(OUT) Violations 0 0.0%
The case where an argument is declared INTENT(OUT) and may be exported from the sub-program without being assigned is not reported by the current version of fpt. This will be addressed in future releases.
Where to Use this Command
Operating system command line | Yes |
Configuration file, config.fsp | Yes |
Specification (fsp) files, *.fsp | Yes |
Interactively, to FPT> prompt | Yes |
Interactive command files | Yes |
Embedded in the Fortran code | Yes |
Default
Checks of INTENT(IN) violations and of possible INTENT(OUT) violations are made by default, but no summary report is generated (Note that defaults may be changed in the configuration file).
Related Commands
The command ENFORCE INTENT IN AS DECLARED modifies the code to correct INTENT(IN) violations by copying the arguments affected to local variables.
The command REMOVE INTENT removes all non-mandatory INTENT declarations from the code.
Copyright ©1995 to 2024 Software Validation Ltd. All rights reserved.