Has anyone done some attempts at automatically identifying unused code in large Fortran projects?
Take the following as an example:
! unused_no_interface.f90 integer function unused_no_interface(i) result(j) implicit none integer, intent(in) :: i j = i + 1 end function ! some_module.f90 module some_module implicit none private public used_sub public unused_sub public unused_val integer :: unused_val = 123 integer :: unused_val2 = 123 contains subroutine used_sub() write(*,*) 'Hello world' end subroutine subroutine unused_sub() write(*,*) 'The answer is 42' end subroutine subroutine unused_sub2() write(*,*) 'I am not used' end subroutine end module ! main.f90 program main ! Imports all procedures (yuck!), but only use one use some_module implicit none call used_sub() end program
-Wall will correctly identify
unused_val2 as unused. As the compiler only operates on one file at a time it has no chance of detecting that
unused_val also are unused though.
Such a tool would be very handy for large Fortran projects, particularly those with much old (legacy) code, but I haven’t been able to find any. It could certainly be done by parsing source files but that would require a more or less complete Fortran grammar, close to what the compilers already have.