Nice compiler options

There are lots of compiler options in various compilers, and also newer options are constantly added to more recent versions. So I would appreciate it if you share the information about options that you find useful in your actual coding :slight_smile:

In my case, I found -finit-derived in gfortran useful, which initializes components of derived types with some specified values (e.g., NaN for reals). For example, for this code

program main
    implicit none
    type Foo
        integer :: n
        real :: x
        complex :: z
        character(5) :: s
    endtype
    type(Foo) :: f

    print *, f
end

“gfortran-10 test.f90” (with no option) gives

$ ./a.out
  1412855200   4.59163468E-41  (-3.031044989E-24,4.591634678E-41) ^@^@^@^@^@
$ ./a.out
  1563989408   4.59163468E-41  (-3.031044989E-24,4.591634678E-41) ^@^@^@^@^@
$ ./a.out
  1400161696   4.59163468E-41  (-3.031044989E-24,4.591634678E-41) ^@^@^@^@^@

while “gfortran-10 -finit-derived -finit-integer=-1 -finit-real=snan test.f90” gives

-1   NaN  (NaN,NaN)  ^@^@^@^@^@

which I think is useful to detect type components not assigned during execution.

5 Likes

For GFortran here is what I use for Debug mode:

-Wall -Wextra -Wimplicit-interface -Wno-unused-function -fPIC -g -fcheck=all -fbacktrace -ffpe-trap=invalid,zero,overflow -finit-real=snan -finit-integer=-9999999

The -finit-derived is a good one and should be used also, I didn’t know about it. The -ffpe-trap are important to stop a program when a NaN is generated, and the -fbacktrace is important to give you a nice stacktrace.

6 Likes

Gfortran is my main compiler and is excellent, but g95 is my sentimental favorite and has good compile-time checks. It does not support much of Fortran 2003 and beyond.

The result of compiling the code above with the options I use, g95 -c -Wall -Wextra -Wimplicit-none -Werror=100,113,115,137,146,147,159,163 -ftrace=full -fbounds-check -freal=nan -fmodule-private -Wno=112,167 is

In file xxmain.f90:9

type(Foo) :: f
             1

Error (113): Variable ‘f’ at (1) is used but not set

1 Like

I have started a new subproject in my Flibs project at http://flibs.sourceforge.net to examine the capabilities of compilers (and perhaps more dedicated tools) to identify via static analysis problems in the source code. It is something I have been thinking about for some time and with a discussion at work about static analysis of source code in mind I simply sat down for it. Far from complete, but at least there are a few test programs now :).

Main issue: get some framework up and running (like for chkfeatures) but one that reports a nice summary.

Thanks for doing this. The compilers I see discussed are MicroSoft FORTRAN version 5.1 and Lahey. Presumably more modern compilers will be added. You comment favorably on Lahey, and I can confirm that Lahey/Fujitsu Fortran 95 was very good at flagging dubious code. There is an online Fortran Source Check.

Could you mirror your work at GitHub, which I think is the most popular code repository? I do see an flibs fork.

Up to now I have been too lazy (or awed?) to actually do that, but I am gaining more experience with Github and, frankly, SourceForge is evermore uncomfortable to work with.

By the way, suggestions for checking are welcome :slight_smile: