https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91424

            Bug ID: 91424
           Summary: Extend warnings about DO loops
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tkoenig at gcc dot gnu.org
  Target Milestone: ---

Currently, there are two problems with the warnings about DO loop
indices accessing arrays with out of bounds errors:

- They are not given for contained procedures

- They are given twice for DO loops inside blocks

$ cat do_subscript_3.f90
! { dg-do compile }
! Check that only one warning is issued inside blocks, and that
! warnings are also issued for contained subroutines.

program main
  real :: a(5)
  block
    integer :: j
    do j=0, 5  ! { dg-warning "out of bounds" }
       a(j) = 2. ! { dg-warning "out of bounds" }
    end do
  end block
  call x
contains
  subroutine x
    integer :: i
    do i=1,6 ! { dg-warning "out of bounds" }
       a(i) = 2.  ! { dg-warning "out of bounds" }
    end do
  end subroutine x
end program main
$ ~/Gcc/9-bin/gcc/f951 -quiet do_subscript_3.f90
do_subscript_3.f90:10:9:

    9 |     do j=0, 5  ! { dg-warning "out of bounds" }
      |                                               2
   10 |        a(j) = 2. ! { dg-warning "out of bounds" }
      |         1
Warning: Array reference at (1) out of bounds (0 < 1) in loop beginning at (2)
do_subscript_3.f90:10:9:

    9 |     do j=0, 5  ! { dg-warning "out of bounds" }
      |                                               2
   10 |        a(j) = 2. ! { dg-warning "out of bounds" }
      |         1
Warning: Array reference at (1) out of bounds (0 < 1) in loop beginning at (2)

Here's a patch:

Index: frontend-passes.c
===================================================================
--- frontend-passes.c   (revision 273855)
+++ frontend-passes.c   (working copy)
@@ -2556,6 +2556,12 @@
   if (in_assoc_list)
     return 0;

+  /* We already warned about this.  */
+  if (v->do_not_warn)
+    return 0;
+
+  v->do_not_warn = 1;
+
   for (ref = v->ref; ref; ref = ref->next)
     {
       if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT)
@@ -2761,6 +2767,12 @@
 doloop_warn (gfc_namespace *ns)
 {
   gfc_code_walker (&ns->code, doloop_code, do_function, NULL);
+
+  for (ns = ns->contained; ns; ns = ns->sibling)
+    {
+      if (ns->code == NULL || ns->code->op != EXEC_BLOCK)
+       doloop_warn (ns);
+    }
 }

 /* This selction deals with inlining calls to MATMUL.  */

Reply via email to