The UK Met Office is developing a Python tool called stylist that warns about specified defects in Fortran code. As an example of its capabilities, for the configuration file config.ini
[file-pipe]
x90=fortran:pfp:fpp
[style.multiple]
rules = FortranCharacterset, MissingOnly, MissingImplicit, MissingPointerInit, KindPattern(integer=r'.*_jazz', real=r'.*dp')
and code bad_style.f90
module m
contains
function sum_mat(x) result(xsum)
real*8, intent(in) :: x(:,:)
xsum = 0.0
do i=1,size(x,1)
do j=1,size(x,2)
xsum = xsum + x(i,j)
end do
end do
end function sum_mat
end module m
!
program main
use m
real*8 :: x(2,3)
integer, pointer :: p
x = 10.0
print*,sum_mat(x)
allocate(p)
p = 5
print*,"p=",p
end program main
running stylist -verbose -configuration config.ini bad_style.f90
gives output
Examining: bad_style.f90
Style: Style
Rule: FortranCharacterset - Failed
Rule: MissingOnly - Failed
Rule: MissingImplicit - Failed
Rule: MissingPointerInit - Failed
Rule: KindPattern - Failed
bad_style.f90: 1: Module 'm' is missing an implicit statement
bad_style.f90: 4: Kind '*8' found for real variable 'x(:, :)' does not fit the pattern /.*dp/.
bad_style.f90: 14: Program 'main' is missing an implicit statement
bad_style.f90: 15: Usage of "m" without "only" clause.
bad_style.f90: 16: Kind '*8' found for real variable 'x(2, 3)' does not fit the pattern /.*dp/.
bad_style.f90: 17: Declaration of pointer "p" without initialisation.
bad_style.f90: 18: Found character '\t' not in Fortran character set
Found 7 issues
and for the improved code good_style.f90
module m
implicit none
integer, parameter :: dp = kind(1.0d0)
contains
function sum_mat(x) result(xsum)
real(kind=dp), intent(in) :: x(:,:)
real(kind=dp) :: xsum
integer :: i,j
xsum = 0.0
do i=1,size(x,1)
do j=1,size(x,2)
xsum = xsum + x(i,j)
end do
end do
end function sum_mat
end module m
!
program main
use m, only: dp, sum_mat
implicit none
real(kind=dp) :: x(2,3)
integer, pointer :: p => null()
x = 10.0
print*,sum_mat(x)
allocate(p)
p = 5
print*,"p=",p
end program main
the output of stylist -verbose -configuration config.ini good_style.f90
is
Examining: good_style.f90
Style: Style
Rule: FortranCharacterset - Passed
Rule: MissingOnly - Passed
Rule: MissingImplicit - Passed
Rule: MissingPointerInit - Passed
Rule: KindPattern - Passed
Found 0 issue
I had some trouble installing stylist on Windows (although it should work) but found it easy to install it on WSL2.