public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] gdb/fortran: Correct the lval type for array elements of internal vars
@ 2021-01-07 19:53 Andrew Burgess
  2021-01-07 21:02 ` Tom Tromey
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew Burgess @ 2021-01-07 19:53 UTC (permalink / raw)
  To: gdb-patches

Since this commit:

  commit a5c641b57b0b5e245b8a011cccc93a4120c8bd63
  Date:   Thu Oct 8 16:45:59 2020 +0100

      gdb/fortran: Add support for Fortran array slices at the GDB prompt

A bug was introduced into GDB.  Consider this Fortan array:

  integer, dimension (1:10) :: array
  array = 1

Now inside GDB:

  (gdb) set $var = array
  (gdb) set $var(1) = 2
  Left operand of assignment is not an lvalue.

The problem is that the new code for slicing Fortran arrays now does
not set the lval type correctly for arrays that are not in memory.
This is easily fixed by making use of value_from_component.

After this the above example behaves as you'd expect.

gdb/ChangeLog:

	* f-lang.c (fortran_value_subarray): Call value_from_component.

gdb/testsuite/ChangeLog:

	* gdb.fortran/intvar-array.exp: New file.
	* gdb.fortran/intvar-array.f90: New file.
---
 gdb/ChangeLog                              |  4 ++
 gdb/f-lang.c                               |  6 +--
 gdb/testsuite/ChangeLog                    |  5 ++
 gdb/testsuite/gdb.fortran/intvar-array.exp | 59 ++++++++++++++++++++++
 gdb/testsuite/gdb.fortran/intvar-array.f90 | 28 ++++++++++
 5 files changed, 97 insertions(+), 5 deletions(-)
 create mode 100644 gdb/testsuite/gdb.fortran/intvar-array.exp
 create mode 100644 gdb/testsuite/gdb.fortran/intvar-array.f90

diff --git a/gdb/f-lang.c b/gdb/f-lang.c
index da66ba8361f..832910969ef 100644
--- a/gdb/f-lang.c
+++ b/gdb/f-lang.c
@@ -690,11 +690,7 @@ fortran_value_subarray (struct value *array, struct expression *exp,
 						      + total_offset));
 	}
       else if (!value_lazy (array))
-	{
-	  const void *valaddr = value_contents (array) + total_offset;
-	  array = allocate_value (array_slice_type);
-	  memcpy (value_contents_raw (array), valaddr, TYPE_LENGTH (array_slice_type));
-	}
+	array = value_from_component (array, array_slice_type, total_offset);
       else
 	error (_("cannot subscript arrays that are not in memory"));
     }
diff --git a/gdb/testsuite/gdb.fortran/intvar-array.exp b/gdb/testsuite/gdb.fortran/intvar-array.exp
new file mode 100644
index 00000000000..1569f51fa4e
--- /dev/null
+++ b/gdb/testsuite/gdb.fortran/intvar-array.exp
@@ -0,0 +1,59 @@
+# Copyright 2021 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/>.
+
+# Place a non-dynamic array into an internal variable, then show that
+# modifications to either the internal variable, or to the original
+# array are independent.
+
+standard_testfile ".f90"
+load_lib "fortran.exp"
+
+if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile} \
+    {debug f90 quiet}] } {
+    return -1
+}
+
+if ![fortran_runto_main] {
+    untested "could not run to main"
+    return -1
+}
+
+gdb_breakpoint [gdb_get_line_number "Break here"]
+gdb_continue_to_breakpoint "Break here"
+
+# Take a copy of the array into an internal variable.
+gdb_test_no_output "set \$a=arr" "set \$a internal variable"
+
+# Validate the original contents.
+gdb_test "print arr" \
+    " = \\(1, 1, 1, 1, 1, 1, 1, 1, 1, 1\\)" \
+    "print arr contents"
+gdb_test "print \$a" \
+    " = \\(1, 1, 1, 1, 1, 1, 1, 1, 1, 1\\)" \
+    "print \$a contents"
+
+# Modify the original array in memory.
+gdb_test_no_output "set arr(5) = 5"
+
+# Modify the internal variable copy.
+gdb_test_no_output "set \$a(3) = 3"
+
+# Now check that the two values have been updated independently.
+gdb_test "print arr" \
+    " = \\(1, 1, 1, 1, 5, 1, 1, 1, 1, 1\\)" \
+    "print arr contents after change"
+gdb_test "print \$a" \
+    " = \\(1, 1, 3, 1, 1, 1, 1, 1, 1, 1\\)" \
+    "print \$a contents after change"
diff --git a/gdb/testsuite/gdb.fortran/intvar-array.f90 b/gdb/testsuite/gdb.fortran/intvar-array.f90
new file mode 100644
index 00000000000..3368b521378
--- /dev/null
+++ b/gdb/testsuite/gdb.fortran/intvar-array.f90
@@ -0,0 +1,28 @@
+! Copyright 2021 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 main
+  integer, dimension (1:10) :: array
+  array = 1
+
+  call take_array (array)
+contains
+  subroutine take_array (arr)
+    integer :: arr (10)
+
+    print *, ""	! Break here.
+    print *, arr
+  end subroutine take_array
+end program main
-- 
2.25.4


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] gdb/fortran: Correct the lval type for array elements of internal vars
  2021-01-07 19:53 [PATCH] gdb/fortran: Correct the lval type for array elements of internal vars Andrew Burgess
@ 2021-01-07 21:02 ` Tom Tromey
  0 siblings, 0 replies; 2+ messages in thread
From: Tom Tromey @ 2021-01-07 21:02 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: gdb-patches

>>>>> "Andrew" == Andrew Burgess <andrew.burgess@embecosm.com> writes:

Andrew> gdb/ChangeLog:

Andrew> 	* f-lang.c (fortran_value_subarray): Call value_from_component.

Thanks, this looks good.

Tom

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-01-07 21:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-07 19:53 [PATCH] gdb/fortran: Correct the lval type for array elements of internal vars Andrew Burgess
2021-01-07 21:02 ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).