Blame SOURCES/0010-Fill-in-missing-array-dimensions-using-the-lower-bou.patch

00b2fc
From 21fd7a71d28847103921036595e0dbeac125aa44 Mon Sep 17 00:00:00 2001
00b2fc
From: Mark Eggleston <markeggleston@gcc.gnu.org>
00b2fc
Date: Mon, 3 Feb 2020 10:56:36 +0000
00b2fc
Subject: [PATCH 10/10] Fill in missing array dimensions using the lower bound
21d5dc
21d5dc
Use -fdec-add-missing-indexes to enable feature. Also enabled by fdec.
21d5dc
---
21d5dc
 gcc/fortran/lang.opt                  |  8 ++++++++
21d5dc
 gcc/fortran/options.c                 |  1 +
21d5dc
 gcc/fortran/resolve.c                 | 24 ++++++++++++++++++++++++
21d5dc
 gcc/testsuite/gfortran.dg/array_6.f90 | 23 +++++++++++++++++++++++
21d5dc
 gcc/testsuite/gfortran.dg/array_7.f90 | 23 +++++++++++++++++++++++
21d5dc
 gcc/testsuite/gfortran.dg/array_8.f90 | 23 +++++++++++++++++++++++
21d5dc
 6 files changed, 102 insertions(+)
21d5dc
 create mode 100644 gcc/testsuite/gfortran.dg/array_6.f90
21d5dc
 create mode 100644 gcc/testsuite/gfortran.dg/array_7.f90
21d5dc
 create mode 100644 gcc/testsuite/gfortran.dg/array_8.f90
21d5dc
21d5dc
diff --git a/gcc/fortran/lang.opt b/gcc/fortran/lang.opt
00b2fc
index ca2c0e17350..eb58f00f1c0 100644
21d5dc
--- a/gcc/fortran/lang.opt
21d5dc
+++ b/gcc/fortran/lang.opt
21d5dc
@@ -281,6 +281,10 @@ Wmissing-include-dirs
21d5dc
 Fortran
21d5dc
 ; Documented in C/C++
21d5dc
 
21d5dc
+Wmissing-index
21d5dc
+Fortran Var(warn_missing_index) Warning LangEnabledBy(Fortran,Wall)
21d5dc
+Warn that the lower bound of a missing index will be used.
21d5dc
+
21d5dc
 Wuse-without-only
21d5dc
 Fortran Var(warn_use_without_only) Warning
21d5dc
 Warn about USE statements that have no ONLY qualifier.
00b2fc
@@ -456,6 +460,10 @@ fdec
21d5dc
 Fortran Var(flag_dec)
21d5dc
 Enable all DEC language extensions.
21d5dc
 
21d5dc
+fdec-add-missing-indexes
21d5dc
+Fortran Var(flag_dec_add_missing_indexes)
21d5dc
+Enable the addition of missing indexes using their lower bounds.
21d5dc
+
21d5dc
 fdec-blank-format-item
21d5dc
 Fortran Var(flag_dec_blank_format_item)
21d5dc
 Enable the use of blank format items in format strings.
21d5dc
diff --git a/gcc/fortran/options.c b/gcc/fortran/options.c
00b2fc
index 9f594c6b4a3..92dd74af21d 100644
21d5dc
--- a/gcc/fortran/options.c
21d5dc
+++ b/gcc/fortran/options.c
00b2fc
@@ -84,6 +84,7 @@ set_dec_flags (int value)
21d5dc
   SET_BITFLAG (flag_dec_non_logical_if, value, value);
21d5dc
   SET_BITFLAG (flag_dec_promotion, value, value);
21d5dc
   SET_BITFLAG (flag_dec_sequence, value, value);
21d5dc
+  SET_BITFLAG (flag_dec_add_missing_indexes, value, value);
21d5dc
 }
21d5dc
 
21d5dc
 /* Finalize DEC flags.  */
21d5dc
diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
00b2fc
index 10547704455..2818d220975 100644
21d5dc
--- a/gcc/fortran/resolve.c
21d5dc
+++ b/gcc/fortran/resolve.c
00b2fc
@@ -4771,6 +4771,30 @@ compare_spec_to_ref (gfc_array_ref *ar)
21d5dc
   if (ar->type == AR_FULL)
21d5dc
     return true;
21d5dc
 
21d5dc
+  if (flag_dec_add_missing_indexes && as->rank > ar->dimen)
21d5dc
+    {
21d5dc
+      /* Add in the missing dimensions, assuming they are the lower bound
21d5dc
+         of that dimension if not specified.  */
21d5dc
+      int j;
21d5dc
+      if (warn_missing_index)
21d5dc
+	{
21d5dc
+          gfc_warning (OPT_Wmissing_index, "Using the lower bound for "
21d5dc
+		       "unspecified dimensions in array reference at %L",
21d5dc
+		       &ar->where);
21d5dc
+	}
21d5dc
+      /* Other parts of the code iterate ar->start and ar->end from 0 to
21d5dc
+	 ar->dimen, so it is safe to assume slots from ar->dimen upwards
21d5dc
+	 are unused (i.e. there are no gaps; the specified indexes are
21d5dc
+	 contiguous and start at zero.  */
21d5dc
+      for(j = ar->dimen; j <= as->rank; j++)
21d5dc
+        {
21d5dc
+	  ar->start[j] = gfc_copy_expr (as->lower[j]);
21d5dc
+	  ar->end[j]   = gfc_copy_expr (as->lower[j]);
21d5dc
+	  ar->dimen_type[j] = DIMEN_ELEMENT;
21d5dc
+        }
21d5dc
+      ar->dimen = as->rank;
21d5dc
+    }
21d5dc
+
21d5dc
   if (as->rank != ar->dimen)
21d5dc
     {
21d5dc
       gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
21d5dc
diff --git a/gcc/testsuite/gfortran.dg/array_6.f90 b/gcc/testsuite/gfortran.dg/array_6.f90
21d5dc
new file mode 100644
21d5dc
index 00000000000..5c26e18ab3e
21d5dc
--- /dev/null
21d5dc
+++ b/gcc/testsuite/gfortran.dg/array_6.f90
21d5dc
@@ -0,0 +1,23 @@
21d5dc
+! { dg-do run }
21d5dc
+! { dg-options "-fdec -Wmissing-index" }!
21d5dc
+! Checks that under-specified arrays (referencing arrays with fewer
21d5dc
+! dimensions than the array spec) generates a warning.
21d5dc
+!
21d5dc
+! Contributed by Jim MacArthur <jim.macarthur@codethink.co.uk>
21d5dc
+! Updated by Mark Eggleston <mark.eggleston@codethink.co.uk>
21d5dc
+!
21d5dc
+
21d5dc
+program under_specified_array
21d5dc
+    integer chessboard(8,8)
21d5dc
+    integer chessboard3d(8,8,3:5)
21d5dc
+    chessboard(3,1) = 5
21d5dc
+    chessboard(3,2) = 55
21d5dc
+    chessboard3d(4,1,3) = 6
21d5dc
+    chessboard3d(4,1,4) = 66
21d5dc
+    chessboard3d(4,4,3) = 7
21d5dc
+    chessboard3d(4,4,4) = 77
21d5dc
+  
21d5dc
+    if (chessboard(3).ne.5) stop 1  ! { dg-warning "Using the lower bound for unspecified dimensions in array reference" }
21d5dc
+    if (chessboard3d(4).ne.6) stop 2  ! { dg-warning "Using the lower bound for unspecified dimensions in array reference" }
21d5dc
+    if (chessboard3d(4,4).ne.7) stop 3  ! { dg-warning "Using the lower bound for unspecified dimensions in array reference" }
21d5dc
+end program
21d5dc
diff --git a/gcc/testsuite/gfortran.dg/array_7.f90 b/gcc/testsuite/gfortran.dg/array_7.f90
21d5dc
new file mode 100644
21d5dc
index 00000000000..5588a5bd02d
21d5dc
--- /dev/null
21d5dc
+++ b/gcc/testsuite/gfortran.dg/array_7.f90
21d5dc
@@ -0,0 +1,23 @@
21d5dc
+! { dg-do run }
21d5dc
+! { dg-options "-fdec-add-missing-indexes -Wmissing-index" }!
21d5dc
+! Checks that under-specified arrays (referencing arrays with fewer
21d5dc
+! dimensions than the array spec) generates a warning.
21d5dc
+!
21d5dc
+! Contributed by Jim MacArthur <jim.macarthur@codethink.co.uk>
21d5dc
+! Updated by Mark Eggleston <mark.eggleston@codethink.co.uk>
21d5dc
+!
21d5dc
+
21d5dc
+program under_specified_array
21d5dc
+    integer chessboard(8,8)
21d5dc
+    integer chessboard3d(8,8,3:5)
21d5dc
+    chessboard(3,1) = 5
21d5dc
+    chessboard(3,2) = 55
21d5dc
+    chessboard3d(4,1,3) = 6
21d5dc
+    chessboard3d(4,1,4) = 66
21d5dc
+    chessboard3d(4,4,3) = 7
21d5dc
+    chessboard3d(4,4,4) = 77
21d5dc
+  
21d5dc
+    if (chessboard(3).ne.5) stop 1  ! { dg-warning "Using the lower bound for unspecified dimensions in array reference" }
21d5dc
+    if (chessboard3d(4).ne.6) stop 2  ! { dg-warning "Using the lower bound for unspecified dimensions in array reference" }
21d5dc
+    if (chessboard3d(4,4).ne.7) stop 3  ! { dg-warning "Using the lower bound for unspecified dimensions in array reference" }
21d5dc
+end program
21d5dc
diff --git a/gcc/testsuite/gfortran.dg/array_8.f90 b/gcc/testsuite/gfortran.dg/array_8.f90
21d5dc
new file mode 100644
21d5dc
index 00000000000..f0d2ef5e37d
21d5dc
--- /dev/null
21d5dc
+++ b/gcc/testsuite/gfortran.dg/array_8.f90
21d5dc
@@ -0,0 +1,23 @@
21d5dc
+! { dg-do compile }
21d5dc
+! { dg-options "-fdec -fno-dec-add-missing-indexes" }!
21d5dc
+! Checks that under-specified arrays (referencing arrays with fewer
21d5dc
+! dimensions than the array spec) generates a warning.
21d5dc
+!
21d5dc
+! Contributed by Jim MacArthur <jim.macarthur@codethink.co.uk>
21d5dc
+! Updated by Mark Eggleston <mark.eggleston@codethink.co.uk>
21d5dc
+!
21d5dc
+
21d5dc
+program under_specified_array
21d5dc
+    integer chessboard(8,8)
21d5dc
+    integer chessboard3d(8,8,3:5)
21d5dc
+    chessboard(3,1) = 5
21d5dc
+    chessboard(3,2) = 55
21d5dc
+    chessboard3d(4,1,3) = 6
21d5dc
+    chessboard3d(4,1,4) = 66
21d5dc
+    chessboard3d(4,4,3) = 7
21d5dc
+    chessboard3d(4,4,4) = 77
21d5dc
+  
21d5dc
+    if (chessboard(3).ne.5) stop 1  ! { dg-error "Rank mismatch" }
21d5dc
+    if (chessboard3d(4).ne.6) stop 2  ! { dg-error "Rank mismatch" }
21d5dc
+    if (chessboard3d(4,4).ne.7) stop 3  ! { dg-error "Rank mismatch" }
21d5dc
+end program
21d5dc
-- 
21d5dc
2.11.0
21d5dc