This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[gfortran,patch] Improve error messages for bounds-checking and a few other cases

Hi all,

Attached patch yields detailed error messages with -fbounds-check. A sample of the error messages is:

$ cat a1.f90
integer x(1,1)
x(1,2) = 1
$ gfortran -fbounds-check a1.f90 -w && ./a.out
Fortran runtime error: Array reference out of bounds for array 'x', upper bound of dimension 2 exceeded (in file 'a1.f90', at line 2)

$ cat a1bis.f90
integer x(1,1)
x(1:2,1) = 1
$ gfortran -fbounds-check a1bis.f90 -w && ./a.out
Fortran runtime error: Array bound mismatch, upper bound of dimension 1 of array 'x' exceeded (in file 'a1bis.f90', at line 2)

$ cat a2.f
integer i
assign 800 to i
800 continue
$ gfortran -fbounds-check a2.f -w && ./a.out
Fortran runtime error: Label assigned to variable 'i' is not a format label (in file 'a2.f', at line 4)

$ cat a3.f
integer i
assign 800 to i
goto i
800 format(A)
$ gfortran -fbounds-check a3.f -w && ./a.out
Fortran runtime error: Assigned label is not a target label (in file 'a3.f', at line 3)

$ cat a4.f
integer i
assign 800 to i
goto i, (200)
800 continue
200 continue
$ gfortran -fbounds-check a4.f -w && ./a.out
Fortran runtime error: Assigned label is not in the list (in file 'a4.f', at line 3)

$ cat a5.f90
program alloc_dummy

    implicit none
    integer, allocatable :: a(:)

    print *, whatever(a)


    function whatever(x)
        integer, allocatable :: x(:)
        integer :: whatever(size(x)+1)

whatever = x
end function whatever
end program alloc_dummy
$ gfortran -fbounds-check a5.f90 -w && ./a.out
Fortran runtime error: Array bound mismatch, size mismatch for dimension 1 of array 'whatever' (in file 'a5.f90', at line 15)

I hope you think, like me, that it's better than the current error message: "Fortran runtime error: Array bound mismatch", with no source file, line and variable name :)

Now, to what the patch does:
-- _gfortran_runtime_error takes only one argument, so we stop generating calls with three arguments! :)
-- instead of passing a string tree to gfc_trans_runtime_check, give it a string (it will build the tree itself) and a locus (from which it will extract the source file and source line number); if the locus is NULL, then gfc_trans_runtime_check will use the fallback method of gfc_source_file and input_line
-- modify everywhere to pass locus to gfc_trans_runtime_check, whenever possible
-- for every call to gfc_trans_runtime_check, build more precise error messages, including variable name, the dimension involved for out-of-bounds index message, whether it's the lower or upper bound that was exceeded.

There still are some calls to gfc_trans_runtime_check that I don't understand in which cases they are reached:
* in trans-intrinsic.c, at line 764
* in trans-expr.c, at line 2156
For these cases, we now generate the same error message than we previously did, added with source file and approximate line location.

Bootstrapped and tested on i686-linux. OK for mainline?


:ADDPATCH fortran:

Attachment: bounds_check.ChangeLog
Description: Binary data

Attachment: bounds_check.diff
Description: Binary data

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]