From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH] Printing of Fortran arrays with dynamic elements
Date: Fri, 19 Jun 2026 15:42:27 -0600 [thread overview]
Message-ID: <20260619214227.1998085-1-tom@tromey.com> (raw)
This bug points out a case where a Fortran array has dynamic elements,
and each element must have its dynamic type resolved separately.
I took the test case directly from the bug, figuring it was ok as the
author is at AMD and has some tags in the repository already.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34276
---
gdb/f-array-walker.h | 9 +++--
.../gdb.fortran/dynamic-elements.exp | 37 +++++++++++++++++++
.../gdb.fortran/dynamic-elements.f90 | 34 +++++++++++++++++
3 files changed, 77 insertions(+), 3 deletions(-)
create mode 100644 gdb/testsuite/gdb.fortran/dynamic-elements.exp
create mode 100644 gdb/testsuite/gdb.fortran/dynamic-elements.f90
diff --git a/gdb/f-array-walker.h b/gdb/f-array-walker.h
index bb8cb0ca599..4392e54013b 100644
--- a/gdb/f-array-walker.h
+++ b/gdb/f-array-walker.h
@@ -267,13 +267,16 @@ class fortran_array_walker
{
LONGEST elt_off = offset + calc.index_offset (i);
- if (is_dynamic_type (elt_type))
+ struct type *this_elt_type = elt_type;
+ if (is_dynamic_type (this_elt_type))
{
CORE_ADDR e_address = m_address + elt_off;
- elt_type = resolve_dynamic_type (elt_type, {}, e_address);
+ this_elt_type = resolve_dynamic_type (this_elt_type, {},
+ e_address);
}
- m_impl.process_element (elt_type, elt_off, i, i == upperbound);
+ m_impl.process_element (this_elt_type, elt_off, i,
+ i == upperbound);
}
}
diff --git a/gdb/testsuite/gdb.fortran/dynamic-elements.exp b/gdb/testsuite/gdb.fortran/dynamic-elements.exp
new file mode 100644
index 00000000000..4a83ad09438
--- /dev/null
+++ b/gdb/testsuite/gdb.fortran/dynamic-elements.exp
@@ -0,0 +1,37 @@
+# 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 where an array has elements of dynamic type, where each element
+# is different.
+
+require allow_fortran_tests
+
+standard_testfile .f90
+load_lib "fortran.exp"
+
+if {[prepare_for_testing "failed to prepare" ${testfile} ${srcfile} \
+ {debug f90}]} {
+ return
+}
+
+if {![fortran_runto_main]} {
+ return
+}
+
+gdb_breakpoint [gdb_get_line_number "stop"]
+gdb_continue_to_breakpoint "stop"
+
+gdb_test "print keys" \
+ [quotemeta "@DECIMAL = (( f = (1, 2) ), ( f = (3, 4, 5) ))"]
diff --git a/gdb/testsuite/gdb.fortran/dynamic-elements.f90 b/gdb/testsuite/gdb.fortran/dynamic-elements.f90
new file mode 100644
index 00000000000..61a2b6edb3c
--- /dev/null
+++ b/gdb/testsuite/gdb.fortran/dynamic-elements.f90
@@ -0,0 +1,34 @@
+! 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/>.
+
+program test
+ implicit none
+
+ type :: arr
+ integer, allocatable :: f (:)
+ end type
+
+ type(arr), dimension(2) :: keys
+ allocate (keys(1)%f(2))
+ allocate (keys(2)%f(3))
+ keys(1)%f(1) = 1
+ keys(1)%f(2) = 2
+ keys(2)%f(1) = 3
+ keys(2)%f(2) = 4
+ keys(2)%f(3) = 5
+ print *, keys(1)%f(1) ! stop
+ print *, keys(2)%f(1)
+
+end program test
--
2.49.0
next reply other threads:[~2026-06-19 21:44 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-19 21:42 Tom Tromey [this message]
2026-06-20 12:38 ` Tom de Vries
2026-06-26 16:32 ` Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260619214227.1998085-1-tom@tromey.com \
--to=tom@tromey.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox