]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
backport: re PR fortran/91424 (Extend warnings about DO loops)
authorThomas Koenig <tkoenig@gcc.gnu.org>
Tue, 13 Aug 2019 10:05:44 +0000 (10:05 +0000)
committerThomas Koenig <tkoenig@gcc.gnu.org>
Tue, 13 Aug 2019 10:05:44 +0000 (10:05 +0000)
2019-08-13  Thomas Koenig  <tkoenig@gcc.gnu.org>

Backport from trunk
PR fortran/91424
* frontend-passes.c (do_subscript): Do not warn for an
expression a second time.  Do not warn about a zero-trip loop.
(doloop_warn): Also look at contained namespaces.

2019-08-13  Thomas Koenig  <tkoenig@gcc.gnu.org>

Backport from trunk
PR fortran/91424
* gfortran.dg/do_subscript_3.f90: New test.
* gfortran.dg/do_subscript_4.f90: New test.
* gfortran.dg/pr70754.f90: Use indices that to not overflow.

2019-08-13  Thomas Koenig  <tkoenig@gcc.gnu.org>

Backport from trunk
PR fortran/91422
* testsuite/libgomp.oacc-fortran/routine-7.f90: Correct array
dimension.

From-SVN: r274369

gcc/fortran/ChangeLog
gcc/fortran/frontend-passes.c
gcc/testsuite/ChangeLog
gcc/testsuite/gfortran.dg/do_subscript_3.f90 [new file with mode: 0644]
gcc/testsuite/gfortran.dg/do_subscript_4.f90 [new file with mode: 0644]
gcc/testsuite/gfortran.dg/pr70754.f90
libgomp/ChangeLog
libgomp/testsuite/libgomp.oacc-fortran/routine-7.f90

index ef90ba0ce7bd1abab015db47d3117053e7933bef..bcc3a0b5db20dba37ed5459cf266fd136ef68682 100644 (file)
@@ -1,3 +1,11 @@
+2019-08-13  Thomas Koenig  <tkoenig@gcc.gnu.org>
+
+       Backport from trunk
+       PR fortran/91424
+       * frontend-passes.c (do_subscript): Do not warn for an
+       expression a second time.  Do not warn about a zero-trip loop.
+       (doloop_warn): Also look at contained namespaces.
+
 2019-08-12  Steven G. Kargl  <kargl@gcc.gnu.org>
 
        PR fortran/91359
index d4264dafa6fa4f3ceccc047d9dc145d213128931..75b2d05fc85ee2f4dfc75f19ea4203dd65be5baa 100644 (file)
@@ -2557,6 +2557,12 @@ do_subscript (gfc_expr **e)
   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)
@@ -2609,7 +2615,6 @@ do_subscript (gfc_expr **e)
              else
                have_do_start = false;
 
-
              if (dl->ext.iterator->end->expr_type == EXPR_CONSTANT)
                {
                  have_do_end = true;
@@ -2621,6 +2626,17 @@ do_subscript (gfc_expr **e)
              if (!have_do_start && !have_do_end)
                return 0;
 
+             /* No warning inside a zero-trip loop.  */
+             if (have_do_start && have_do_end)
+               {
+                 int sgn, cmp;
+
+                 sgn = mpz_cmp_ui (do_step, 0);
+                 cmp = mpz_cmp (do_end, do_start);
+                 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
+                   break;
+               }
+
              /* May have to correct the end value if the step does not equal
                 one.  */
              if (have_do_start && have_do_end && mpz_cmp_ui (do_step, 1) != 0)
@@ -2762,6 +2778,12 @@ static void
 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.  */
index 8f4b121346ca2bca8c0237b9f668c31c17f8f718..463baa672daf8a5197608e263d712ddbc2b328fb 100644 (file)
@@ -1,3 +1,11 @@
+2019-08-13  Thomas Koenig  <tkoenig@gcc.gnu.org>
+
+       Backport from trunk
+       PR fortran/91424
+       * gfortran.dg/do_subscript_3.f90: New test.
+       * gfortran.dg/do_subscript_4.f90: New test.
+       * gfortran.dg/pr70754.f90: Use indices that to not overflow.
+
 2019-08-12  Steven G. Kargl  <kargl@gcc.gnu.org>
 
        PR fortran/91359
diff --git a/gcc/testsuite/gfortran.dg/do_subscript_3.f90 b/gcc/testsuite/gfortran.dg/do_subscript_3.f90
new file mode 100644 (file)
index 0000000..2f62f58
--- /dev/null
@@ -0,0 +1,22 @@
+! { dg-do compile }
+! PR fortran/91424
+! 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
diff --git a/gcc/testsuite/gfortran.dg/do_subscript_4.f90 b/gcc/testsuite/gfortran.dg/do_subscript_4.f90
new file mode 100644 (file)
index 0000000..c773fe7
--- /dev/null
@@ -0,0 +1,11 @@
+! { dg-do compile }
+! PR 91424 - this used to warn although the DO loop is zero trip.
+program main
+  implicit none
+  integer :: i
+  real :: a(2)
+  do i=1,3,-1
+     a(i) = 2.
+  end do
+  print *,a
+end program main
index d7e790cc036dd182098e47dbca53d95f16646feb..593acf917ee63d92cdce43fbd159f89a50918f11 100644 (file)
@@ -18,12 +18,13 @@ contains
     integer (ii4), dimension(40,40) :: c
     integer  i, j
 
-    do i=1,20
-      b(i,j) = 123 * a(i,j) + 34 * a(i,j+1) &
-             + 34 * a(i,j-1) + a(i+1,j+1) &
-             + a(i+1,j-1) + a(i-1,j+1) &
-             + a(i-1,j-1)
-      c(i,j) = 123
+    j = 10
+    do i=11,30
+       b(i,j) = 123 * a(i,j) + 34 * a(i,j+1) &
+            + 34 * a(i,j-1) + a(i+1,j+1) &
+            + a(i+1,j-1) + a(i-1,j+1) &
+            + a(i-1,j-1)
+       c(i,j) = 123
     end do
 
     where ((xyz(:,:,2) /= 0) .and. (c /= 0))
index 958572a54c046bcc49732d9585e412a6cc14a32c..15d12873e801941d6cc1cf8685770fcc6ca9f7b7 100644 (file)
@@ -1,3 +1,10 @@
+2019-08-13  Thomas Koenig  <tkoenig@gcc.gnu.org>
+
+       Backport from trunk
+       PR fortran/91422
+       * testsuite/libgomp.oacc-fortran/routine-7.f90: Correct array
+       dimension.
+
 2019-08-12  Release Manager
 
        * GCC 9.2.0 released.
index f58a95fe67017a44a5574183370c5d270bf4d016..1009f4a81e55beab62aaf3b16918d096e40ffd4c 100644 (file)
@@ -109,7 +109,7 @@ end subroutine gang
 
 subroutine seq (a)
   !$acc routine seq
-  integer, intent (inout) :: a(M)
+  integer, intent (inout) :: a(N)
   integer :: i
 
   do i = 1, N