Blame SOURCES/gcc11-fortran-fdec-add-missing-indexes.patch

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