* [PATCH] gdb/fortran: print rank only for arrays with unresolved dynamic bounds
@ 2026-10-09 15:41 Adhemerval Zanella
2026-10-16 11:57 ` Andrew Burgess
0 siblings, 1 reply; 2+ messages in thread
From: Adhemerval Zanella @ 2026-10-09 15:41 UTC (permalink / raw)
To: gdb-patches; +Cc: Thiago Bauermann
Printing the type of a Fortran subroutine whose array parameter has
bounds given by other variables, e.g. 'integer :: a(n1:n2)', failed
with "Lower bound may not be '*' in F77", since the bounds of the
function's parameter types are never resolved. Print only the array
rank in this case, as is already done for other unresolved dynamic
array types.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34698
---
gdb/f-typeprint.c | 27 ++++++++
.../gdb.dwarf2/fortran-var-string.exp | 25 +++-----
.../gdb.fortran/array-dynamic-bounds.exp | 61 +++++++++++++++++++
.../gdb.fortran/array-dynamic-bounds.f90 | 32 ++++++++++
4 files changed, 127 insertions(+), 18 deletions(-)
create mode 100644 gdb/testsuite/gdb.fortran/array-dynamic-bounds.exp
create mode 100644 gdb/testsuite/gdb.fortran/array-dynamic-bounds.f90
diff --git a/gdb/f-typeprint.c b/gdb/f-typeprint.c
index 123d9995c39..887e243c9b2 100644
--- a/gdb/f-typeprint.c
+++ b/gdb/f-typeprint.c
@@ -85,6 +85,26 @@ f_language::print_type (struct type *type, const char *varstring,
}
}
+/* Return true if any dimension of the Fortran array TYPE has a bound that
+ can only be computed from an object (e.g. a bound given by another
+ variable). */
+
+static bool
+f_array_has_dynamic_bounds (struct type *type)
+{
+ for (; type->code () == TYPE_CODE_ARRAY; type = type->target_type ())
+ {
+ const range_bounds *bounds = type->bounds ();
+
+ if (!bounds->low.is_constant ()
+ || !(bounds->high.is_constant ()
+ || bounds->high.kind () == PROP_UNDEFINED))
+ return true;
+ }
+
+ return false;
+}
+
/* See f-lang.h. */
void
@@ -184,6 +204,13 @@ f_language::f_type_print_varspec_suffix (struct type *type,
properties but cannot be resolved as there is no object. */
print_rank_only = true;
}
+ else if (f_array_has_dynamic_bounds (type))
+ {
+ /* Likewise for bounds that cannot be resolved. PRINT_RANK_ONLY is
+ only passed down to the inner dimensions, so check all of them
+ here so that either all or none of them are printed. */
+ print_rank_only = true;
+ }
if (type->target_type ()->code () == TYPE_CODE_ARRAY)
f_type_print_varspec_suffix (type->target_type (), stream, 0,
diff --git a/gdb/testsuite/gdb.dwarf2/fortran-var-string.exp b/gdb/testsuite/gdb.dwarf2/fortran-var-string.exp
index 925872bc2c8..28703b1cfcb 100644
--- a/gdb/testsuite/gdb.dwarf2/fortran-var-string.exp
+++ b/gdb/testsuite/gdb.dwarf2/fortran-var-string.exp
@@ -150,24 +150,13 @@ gdb_test_no_output "set language fortran"
gdb_test "info functions test_1_func" \
"2:\\s+integer test_1_func\\(character\\*\\(\\*\\)\\);"
-# We print `1` here as the bound because GDB treats this as an assumed
-# size array, and just reports the lower bound value for the upper
-# bound.
-#
-# We might, in the future, decide that there's a better way we could
-# tell the user about the type of this array argument, when that
-# happens it's OK to change the expected results here.
+# The array bounds are dynamic and there is no object to resolve them
+# against, so only the rank of the array is printed.
gdb_test "info functions test_2_func" \
- "3:\\s+integer test_2_func\\(integer \\(1\\)\\);"
+ "3:\\s+integer test_2_func\\(integer \\(:\\)\\);"
-# It's not completely clear that this error is correct here. Why
-# can't the lower bound be a dynamic expression?
-#
-# This test was initially added to guard against the case where GDB
-# was crashing if/when it saw this situation.
-#
-# If later on, GDB's handling of array types with a dynamic loewr
-# bound changes, then it is possible that the expected result here
-# should change.
+# Likewise for a dynamic lower bound. This test was initially added to
+# guard against the case where GDB was crashing if/when it saw this
+# situation.
gdb_test "info functions test_3_func" \
- "4:\\s+Lower bound may not be '\\*' in F77"
+ "4:\\s+integer test_3_func\\(integer \\(:\\)\\);"
diff --git a/gdb/testsuite/gdb.fortran/array-dynamic-bounds.exp b/gdb/testsuite/gdb.fortran/array-dynamic-bounds.exp
new file mode 100644
index 00000000000..c42df7bfeb8
--- /dev/null
+++ b/gdb/testsuite/gdb.fortran/array-dynamic-bounds.exp
@@ -0,0 +1,61 @@
+# Copyright 2026 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# Test printing the type of a subroutine whose array parameters have
+# bounds given by other parameters (PR fortran/34698).
+
+require allow_fortran_tests
+
+standard_testfile .f90
+load_lib fortran.exp
+
+if {[prepare_for_testing "failed to prepare" $testfile $srcfile \
+ {debug f90}]} {
+ return
+}
+
+set int4 [fortran_int4]
+
+# The type of SUB has no associated object, so the bounds of its array
+# parameters cannot be resolved, even when SUB is executing. Only the
+# rank of these arrays is printed.
+set sub_args "$int4 \\(:\\), $int4, $int4, $int4 \\(:,:\\)"
+set sub_type "void \\($sub_args\\)"
+
+gdb_test "print sub" " = \\{$sub_type\\} $hex <sub_?>" \
+ "print sub before running"
+gdb_test "ptype sub" "type = $sub_type" "ptype sub before running"
+gdb_test "whatis sub" "type = $sub_type" "whatis sub before running"
+gdb_test "info functions ^sub\$" "$decimal:\\s+void sub\\($sub_args\\);" \
+ "info functions sub before running"
+
+if {![fortran_runto_main]} {
+ return
+}
+
+gdb_breakpoint [gdb_get_line_number "Break here"]
+gdb_continue_to_breakpoint "Break here"
+
+gdb_test_no_output "set print frame-arguments all"
+
+gdb_test "frame" \
+ [multi_line \
+ "#0 sub \\(a=\\(2, 3, 4, 5, 6\\), n1=2, n2=6, b=\\(\\(1, 2, 3, 4, 5\\) \\(6, 7, 8, 9, 10\\) \\(11, 12, 13, 14, 15\\)\\)\\) at \[^\r\n\]+" \
+ "$decimal\\s+print \\*, a\\s+! Break here\\."]
+gdb_test "print a" " = \\(2, 3, 4, 5, 6\\)"
+gdb_test "ptype a" "type = $int4 \\(2:6\\)"
+gdb_test "ptype b" "type = $int4 \\(2:6,3\\)"
+gdb_test "print sub" " = \\{$sub_type\\} $hex <sub_?>" \
+ "print sub with frame"
diff --git a/gdb/testsuite/gdb.fortran/array-dynamic-bounds.f90 b/gdb/testsuite/gdb.fortran/array-dynamic-bounds.f90
new file mode 100644
index 00000000000..560ace3f2a8
--- /dev/null
+++ b/gdb/testsuite/gdb.fortran/array-dynamic-bounds.f90
@@ -0,0 +1,32 @@
+! Copyright 2026 Free Software Foundation, Inc.
+
+! This program is free software; you can redistribute it and/or modify
+! it under the terms of the GNU General Public License as published by
+! the Free Software Foundation; either version 3 of the License, or
+! (at your option) any later version.
+!
+! This program is distributed in the hope that it will be useful,
+! but WITHOUT ANY WARRANTY; without even the implied warranty of
+! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+! GNU General Public License for more details.
+!
+! You should have received a copy of the GNU General Public License
+! along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+subroutine sub (a, n1, n2, b)
+ integer :: n1, n2
+ integer :: a(n1:n2)
+ integer :: b(n1:n2, 3)
+ print *, a ! Break here.
+ print *, b
+end subroutine sub
+
+program test
+ integer :: array(2:6)
+ integer :: array2(2:6, 3)
+ integer :: i
+
+ array = [2, 3, 4, 5, 6]
+ array2 = reshape ([(i, i = 1, 15)], [5, 3])
+ call sub (array, 2, 6, array2)
+end program test
--
2.47.3
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-10-16 11:57 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-09 15:41 [PATCH] gdb/fortran: print rank only for arrays with unresolved dynamic bounds Adhemerval Zanella
2026-10-16 11:57 ` Andrew Burgess
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox