Commit 15e23330 by Thomas Koenig

lang.opt: Add -Wdo-subscript.

2017-09-25  Thomas Koenig  <tkoenig@gcc.gnu.org>

	* lang.opt:  Add -Wdo-subscript.
	* frontend-passes.c (do_t): New type.
	(doloop_list): Use variable of do_type.
	(if_level): Variable to track if levels.
	(select_level): Variable to track select levels.
	(gfc_run_passes): Initialize i_level and select_level.
	(doloop_code): Record current level of if + select
	level in doloop_list.  Add seen_goto if there could
	be a branch outside the loop. Use different type for
	doloop_list.
	(doloop_function): Call do_intent and do_subscript; move
	functionality of checking INTENT to do_intent.
	(insert_index_t): New type, for callback_insert_index.
	(callback_insert_index): New function.
	(insert_index): New function.
	(do_subscript): New function.
	(do_intent): New function.
	(gfc_code_walker): Keep track of if_level and select_level.
	* invoke.texi: Document -Wdo-subscript.

2017-09-25  Thomas Koenig  <tkoenig@gcc.gnu.org>

	* gfortran.dg/do_subscript_1.f90: New test.
	* gfortran.dg/do_subscript_2.f90: New test.
	* gfortran.dg/gomp/associate1.f90: Add out of bounds warning.
	* gfortran.dg/predcom-1.f: Adjust loop bounds.
	* gfortran.dg/unconstrained_commons.f: Add out of bounds warning.

From-SVN: r253156
parent 58e17cf8
2017-09-25 Thomas Koenig <tkoenig@gcc.gnu.org>
* lang.opt: Add -Wdo-subscript.
* frontend-passes.c (do_t): New type.
(doloop_list): Use variable of do_type.
(if_level): Variable to track if levels.
(select_level): Variable to track select levels.
(gfc_run_passes): Initialize i_level and select_level.
(doloop_code): Record current level of if + select
level in doloop_list. Add seen_goto if there could
be a branch outside the loop. Use different type for
doloop_list.
(doloop_function): Call do_intent and do_subscript; move
functionality of checking INTENT to do_intent.
(insert_index_t): New type, for callback_insert_index.
(callback_insert_index): New function.
(insert_index): New function.
(do_subscript): New function.
(do_intent): New function.
(gfc_code_walker): Keep track of if_level and select_level.
* invoke.texi: Document -Wdo-subscript.
2017-09-25 Janne Blomqvist <jb@gcc.gnu.org>
* trans.c (gfc_unlikely): Remove unnecessary fold_convert.
......
......@@ -145,8 +145,8 @@ by type. Explanations are in the following sections.
@xref{Error and Warning Options,,Options to request or suppress errors
and warnings}.
@gccoptlist{-Waliasing -Wall -Wampersand -Wargument-mismatch -Warray-bounds
-Wc-binding-type -Wcharacter-truncation @gol
-Wconversion -Wfunction-elimination -Wimplicit-interface @gol
-Wc-binding-type -Wcharacter-truncation -Wconversion @gol
-Wdo-subscript -Wfunction-elimination -Wimplicit-interface @gol
-Wimplicit-procedure -Wintrinsic-shadow -Wuse-without-only -Wintrinsics-std @gol
-Wline-truncation -Wno-align-commons -Wno-tabs -Wreal-q-constant @gol
-Wsurprising -Wunderflow -Wunused-parameter -Wrealloc-lhs -Wrealloc-lhs-all @gol
......@@ -907,8 +907,8 @@ option does @emph{not} imply @option{-Wconversion}.
@cindex extra warnings
@cindex warnings, extra
Enables some warning options for usages of language features which
may be problematic. This currently includes @option{-Wcompare-reals}
and @option{-Wunused-parameter}.
may be problematic. This currently includes @option{-Wcompare-reals},
@option{-Wunused-parameter} and @option{-Wdo-subscript}.
@item -Wimplicit-interface
@opindex @code{Wimplicit-interface}
......@@ -1080,6 +1080,21 @@ target. This option is implied by @option{-Wall}.
Warn if a @code{DO} loop is known to execute zero times at compile
time. This option is implied by @option{-Wall}.
@item -Wdo-subscript
@opindex @code{Wdo-subscript}
Warn if an array subscript inside a DO loop could lead to an
out-of-bounds access even if the compiler can not prove that the
statement is actually executed, in cases like
@smallexample
real a(3)
do i=1,4
if (condition(i)) then
a(i) = 1.2
end if
end do
@end smallexample
This option is implied by @option{-Wextra}.
@item -Werror
@opindex @code{Werror}
@cindex warnings, to errors
......
......@@ -237,6 +237,10 @@ Wconversion-extra
Fortran Var(warn_conversion_extra) Warning
Warn about most implicit conversions.
Wdo-subscript
Fortran Var(warn_do_subscript) Warning LangEnabledBy(Fortran,Wextra)
Warn about possibly incorrect subscripts in do loops
Wextra
Fortran Warning
; Documented in common
......
2017-09-25 Thomas Koenig <tkoenig@gcc.gnu.org>
* gfortran.dg/do_subscript_1.f90: New test.
* gfortran.dg/do_subscript_2.f90: New test.
* gfortran.dg/gomp/associate1.f90: Add out of bounds warning.
* gfortran.dg/predcom-1.f: Adjust loop bounds.
* gfortran.dg/unconstrained_commons.f: Add out of bounds warning.
2017-09-25 Will Schmidt <will_schmidt@vnet.ibm.com>
* gcc.target/powerpc/fold-vec-st-char.c: New.
......
! { dg-do compile }
program main
real, dimension(3) :: a
a = 42.
do i=-1,3,2 ! { dg-warning "out of bounds" }
a(i) = 0 ! { dg-warning "out of bounds \\(-1 < 1\\)" }
end do
do i=4,1,-1 ! { dg-warning "out of bounds" }
a(i) = 22 ! { dg-warning "out of bounds \\(4 > 3\\)" }
end do
do i=1,4 ! { dg-warning "out of bounds" }
a(i) = 32 ! { dg-warning "out of bounds \\(4 > 3\\)" }
end do
do i=3,0,-1 ! { dg-warning "out of bounds" }
a(i) = 12 ! { dg-warning "out of bounds \\(0 < 1\\)" }
end do
do i=-1,3
if (i>0) a(i) = a(i) + 1 ! No warning inside if
end do
do i=-1,4
select case(i)
case(1:3)
a(i) = -234 ! No warning inside select case
end select
end do
do i=1,3 ! { dg-warning "out of bounds" }
a(i+1) = a(i) ! { dg-warning "out of bounds \\(4 > 3\\)" }
a(i-1) = a(i) ! { dg-warning "out of bounds \\(0 < 1\\)" }
end do
do i=3,1,-1 ! { dg-warning "out of bounds" }
a(i) = a(i-1) ! { dg-warning "out of bounds \\(0 < 1\\)" }
a(i) = a(i+1) ! { dg-warning "out of bounds \\(4 > 3\\)" }
end do
do i=1,2 ! { dg-warning "out of bounds" }
a(i) = a(i*i) ! { dg-warning "out of bounds \\(4 > 3\\)" }
end do
do i=1,4,2
a(i) = a(i)*2 ! No error
end do
do i=1,4
if (i > 3) exit
a(i) = 33
end do
do i=0,3 ! { dg-warning "out of bounds \\(0 < 1\\)" }
a(i) = 13. ! { dg-warning "out of bounds \\(0 < 1\\)" }
if (i < 1) exit
end do
do i=0,3
if (i < 1) cycle
a(i) = -21.
end do
do i=0,3 ! { dg-warning "out of bounds \\(0 < 1\\)" }
do j=1,2
a(i) = -123 ! { dg-warning "out of bounds \\(0 < 1\\)" }
end do
end do
end program main
! { dg-do compile }
! { dg-additional-options "-Wdo-subscript" }
program main
real, dimension(3) :: a
a = 42.
do i=-1,3 ! { dg-warning "out of bounds \\(-1 < 1\\)" }
select case(i)
case(1:3)
a(i) = -234 ! { dg-warning "out of bounds \\(-1 < 1\\)" }
end select
end do
do i=1,4,2
a(i) = a(i)*2 ! No warning - end value is 3
end do
do i=1,4 ! { dg-warning "out of bounds \\(4 > 3\\)" }
if (i > 3) exit
a(i) = 33 ! { dg-warning "out of bounds \\(4 > 3\\)" }
end do
do i=0,3 ! { dg-warning "out of bounds \\(0 < 1\\)" }
if (i < 1) cycle
a(i) = -21. ! { dg-warning "out of bounds \\(0 < 1\\)" }
end do
end program main
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment