tstellar / rpms / gcc

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