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

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