Giter Site home page Giter Site logo

fortran-linter's Introduction

Fortran linter

This linter works on a line-by-line basis to enforce some rules regarding the format of Fortran files.

The linter does not ship with any grammar and is solely based on regular expressions. This allows to easily add new rules, but this implies some limitations.

Installation

There are 2 ways of installing the linter. The recommended one is through pip

pip install fortran-linter

The other way is to clone this repository and install it from the local copy:

git clone https://github.com/cphyc/fortran-syntax.git
cd fortran-syntax
pip install .

Please note that depending on your installation, you may have to add sudo to the pip install line. This is due to the fact that the package is shipped with a script fortran-linter. For some installation, the creation of this file may require root access.

Usage

This tool checks for fortran syntax against a few rules. To print a list of all the warnings for a file, run:

fortran-linter myfile.f90 --syntax-only

To autofix (most) warnings in place, do:

fortran-linter myfile.f90 -i

The original file will be backup'ed into myfile.f90.orig. All the safe fixes will be done and stored in the file myfile.f90.

For more help, you can type

fortran-linter -h

Rules

Here is a non-comprehensive set of rules that are enforced:

  • Punctuation should be followed by a space, this includes ,, ; and ).
  • Binary operators (==, +, ...) should be surrounded by spaces
  • The following special characters are surrounded by at least one space: ::, =.
  • A line should not exceed 120 characters (this is somehow already extreme). The maximum line length can be controlled from the CLI.
  • One should use use mpi instead of include "mpif.h". Note that this is not fixed by default as it may break codes where include "mpif.h" follows and implicit none statement.
  • Spaces are preferred over tabs, trailing whitespaces are cleaned.
  • Warnings are raised if you use real(8) :: foo. One should rather use integer, parameter :: dp = selected_real_kind(15); real(dp) :: foo or use iso_fortran_env; real(real64) :: foo
  • print statements should look like print *, "something"
  • write statements should look like write(*, *) "something"
  • Lines should be indented consistently (by default, using an indentation of 4 spaces)
  • FORD Compatibility: !! and !> are preserved and treated as comments like ! with one space after and at least one space before.

TODO list

  • ship on pip
  • add more rules (this one will never end)

fortran-linter's People

Contributors

cphyc avatar jatkinson1000 avatar leliel12 avatar pre-commit-ci[bot] avatar pums974 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

fortran-linter's Issues

fortran-linter doesn't detect OpenMP flags

         !$OMP PARALLEL DEFAULT(NONE) &
          1
Warning: Exactly one space after comment at (1).

Is there a way to specify specific cases to omit? Couldn't find any in the documentation

its not working any more...please check

Hi,

I have just found this excellent project. Though I have problem in installing.

 sudo  pip3 install fortran-linter
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting fortran-linter
  Using cached https://files.pythonhosted.org/packages/fe/ef/9112b8c5aa7e3a6d1e6b924ddd01892797bf79beb3fb53466e9cdbf49529/fortran_linter-1.0.1.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-4725p54t/fortran-linter/setup.py", line 7, in <module>
        long_description = open(README).read()
    FileNotFoundError: [Errno 2] No such file or directory: '/tmp/pip-install-4725p54t/fortran-linter/Readme.md'
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-4725p54t/fortran-linter/

Then, with git clone; setup I have installed it in my userspace. now:

~/.local/bin/fortran-linter -h
Traceback (most recent call last):
  File "/home/rudra/.local/bin/fortran-linter", line 11, in <module>
    load_entry_point('fortran-linter==1.0.1', 'console_scripts', 'fortran-linter')()
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 489, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2793, in load_entry_point
    return ep.load()
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2411, in load
    return self.resolve()
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2417, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
ImportError: No module named fortran_linter.cli

Fixing warning leads to too long line

Hi,

Suppose there is the following code

Program main
  
  integer :: a1, a2,a3,a4,a5,a6,a7,a8,a9,a10000000000000000, a2222222222222222,a333333333333333333,a2222222222222222222, b23,b33

  write (*,*) "Hello World"
End program

--syntax-only in 1.0.9 will indicate missing space after punctuation. After -i, the new line will exceed the 132 character limit. It may or may not cause a problem. In this case, gfortran is OK. In some other cases, I got issue.

Is that possible to add some auto change line & after certain limit? Thanks

Does not run with Intel Python distribution on Windows

Thanks for creating this. I have the Intel Python 3 distribution installed on Windows, and I installed fortran-linter using pip. Running it, I get this:

c:\fortran\2015>fortran-linter reg_stats.f90 --syntax-only
Traceback (most recent call last):
File "c:\intelpython3\lib\runpy.py", line 193, in run_module_as_main
"main", mod_spec)
File "c:\intelpython3\lib\runpy.py", line 85, in run_code
exec(code, run_globals)
File "c:\intelpython3\Scripts\fortran-linter.exe_main
.py", line 4, in
File "c:\intelpython3\lib\site-packages\fortran_linter_init
.py", line 1, in
from .main import FortranRules, LineChecker # noqa: F401
File "c:\intelpython3\lib\site-packages\fortran_linter\main.py", line 7, in
def to_lowercase(line: str, match: re.Match):
AttributeError: module 're' has no attribute 'Match'

broken variable after a linter pass

Hi,

Thank you for this linter, it will be very usefull for us

However, I think I have spotted a problem with your linter.

before

module my_module
contains
     subroutine my_subroutine()
          implicit none
          integer :: bug
          bug= 1
     end subroutine my_subroutine
end module my_module

after fortran-linter --linelength 100 -i file.f90

module my_module
contains
     subroutine my_subroutine()
          implicit none
          integer :: bug
          bu = g1
     end subroutine my_subroutine
end module my_module

I'm using the latest version available on PyPy and my Python version is 3.9.2

Support for ALE in vim

Dear developer,

Is there a way to enable fortran linting with this tool through ALE in vim?
Something along the lines of:

let g:ale_linters = {'fortran':['fortran-linter']}

Thank you!

Make FORD Compatible

Hi, I really like this tool, thank you!

A lot of Fortran programmers use FORD to generate documentation for projects.

This relies on using !! or !> to denote FORD comments, which gets picked up as an error by the linter due to not having a space after the first !.

It would be great if the linter could be made FORD compatible and suppress warnings in these cases.

Linter changing string content

Hi, I'm sorry I'm faced with an other issue
the following code

module my_module
    implicit none
contains
    subroutine my_subroutine()
        implicit none
        print*, '<test>'
        print*, "<test>"
    end subroutine my_subroutine
end module my_module

is transformed into

module my_module
    implicit none
contains
    subroutine my_subroutine()
        implicit none
        print*, '<test >'
        print*, "<test >"
    end subroutine my_subroutine
end module my_module

I really wish that my string stayed untouched

correcting `use omp_lib` leads to `Error: Invalid character 0x01 at (1)`

For the following code

Program main

  use omp_lib
  
  integer :: a1, a2, a3, a4, a5, a6, a7, a8, a9, a10000000000000000, a2222222222222222, a333333333333333333

  write (*,*) "Hello World"
End program

fortran-linter (1.0.9) with --syntax only leads to

  use omp_lib
  1
Warning: Should prepend with "!$" at (1).

and -i leads to
�! $ use omp_lib and Error: Invalid character 0x01 at (1).

It seems use omp_lib is ok, https://curc.readthedocs.io/en/latest/programming/OpenMP-Fortran.html,
is this a false-positive case? Thanks.

Rules matching correct patterns

The rules for 'print' and 'write' also match the correct version of the expressions, which means that the linter never returns a non-zero value, even when applied on linted code.

Flip order in association?

I think there is a small mistake in the README. The association statements for precision have to be swapped, i.e. dp=>real64 instead of real64=>dp

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.