Skip to main content
GitHub

MSC00-F. Compile cleanly at high warning levels

Compile code using the highest warning level available for your compiler and eliminate warnings by modifying the code.

According to the [ Fortran Standard (subclause 4.2, Conformance ISO/IEC 1539-1:2018) ], a Fortran compiler is not required to diagnose every violation that can lead to erroneous or undefined program behavior:

...

(2) it contains the capability to detect and report the use within a submitted program unit of a form designated herein as obsolescent, insofar as such use can be detected by reference to the numbered syntax rules and constraints;

(3) it contains the capability to detect and report the use within a submitted program unit of a form or relationship that is not permitted by the numbered syntax rules or constraints, including the deleted features described in Annex B;

...

Compiler warnings provide capabilities to that can identify programming errors in Fortran code, such as implicit or inconsistent procedure interfaces, questionable argument association, possible aliasing, implicit conversions that may change a value, unused entities, suspicious control flow, line truncation, use of nonstandard language extensions, and other constructs that are likely to indicate programming errors.

Tip

Eliminating compiler warnings does not guarantee that a program conforms to the Fortran standard or is free of defects. Different compilers and static analysis tools can provide different diagnostics and should be used where available.

The following table shows recommended compiler options for the GNU gfortran and LLVM flang compilers. The availability and precise behavior of diagnostic options are compiler- and version-dependent.

CompilerRecommended optionsPurpose
gfortran-Wall -Wextra -WpedanticEnable commonly useful warnings, additional diagnostics, warnings for nonstandard extensions.
-Wimplicit-interface -Wimplicit-procedure -Wconversion-extraTurn on Fortran-specific warning (e.g. , missing explicit interfaces, implicitly declared procedures, additional implicit conversions).
-Werror.Turn all warnings into errors, causing compilation to fail when a warning is emitted.
flang-pedanticWarn about language extensions.
-WerrorTurn warnings into errors, causing compilation to fail when a warning is emitted.

Noncompliant Code Example (Non-Integer Loop Variable)

In this noncompliant example, the DO construct uses a variable of type REAL as its loop variable. The use of a REAL variable as the DO variable is a deleted Fortran feature. Compilers can continue to support deleted features as extensions, so the program might still compile unless appropriate diagnostic options are enabled.

Non-compliant code
program main
  implicit none

  real :: i

  do i = 1.0, 3.0
    print *, i
  end do
end program main

Implementation Details (gfortran)

$ gfortran --version
GNU Fortran (Ubuntu 14.2.0-4ubuntu2~24.04.1) 14.2.0

$ gfortran -Wall -Werror msc00-f-ncce.f90
msc00-f-ncce.f90:6:5:

    6 |   do i = 1.0, 3.0
      |     1
Error: Deleted feature: Loop variable at (1) must be integer [-Werror]
f951: all warnings being treated as errors

Implementation Details (flang)

$ flang --version
Ubuntu flang version 20.1.2 (0ubuntu1~24.04.3)

$ flang -pedantic -Werror msc00-f-ncce.f90
error: Semantic errors in msc00-f-ncce.f90
./msc00-f-ncce.f90:6:6: portability: DO controls should be INTEGER
    do i = 1.0, 3.0
       ^
./msc00-f-ncce.f90:6:10: portability: DO controls should be INTEGER
    do i = 1.0, 3.0
           ^^^
./msc00-f-ncce.f90:6:15: portability: DO controls should be INTEGER
    do i = 1.0, 3.0
                ^^^

Compliant Code Example (Non-Integer Loop Variable)

The issue can be eliminated by declaring the DO variable as INTEGER and using integer initial, limit, and step expressions. This code uses the standard integer form of the DO construct.

Compliant code
program main
  implicit none

  integer :: i

  do i = 1, 3
    print *, i
  end do
end program main

Risk Assessment

Compiler diagnostics can expose defects that lead to incorrect computation, undefined or otherwise nonconforming behavior, portability problems, and security vulnerabilities. Compiling cleanly with strong diagnostics reduces the likelihood that such defects remain undetected, but it does not replace code review, static analysis, testing, or compliance with the other guidelines in this coding standard.

GuidelineSeverityLikelihoodDetectableRepairablePriorityLevel
MSC00-FMediumLikelyYesNoP4L3
TaxonomyTaxonomy itemRelationship
CERT CMSC00-C. Compile cleanly at high warning levelsRelated guideline