aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite
diff options
context:
space:
mode:
authorXavier Roirand <roirand@adacore.com>2017-12-14 22:38:17 -0500
committerJoel Brobecker <brobecker@adacore.com>2017-12-14 23:35:38 -0500
commit828d584679845b6a1d01151f7df3592d15fe8405 (patch)
tree0837aff12b1190e89a98e7dafeea9349f0ec6f44 /gdb/testsuite
parent1e5dd7c95a56fd61e6f2deb145ad99153d5336e5 (diff)
downloadgdb-828d584679845b6a1d01151f7df3592d15fe8405.zip
gdb-828d584679845b6a1d01151f7df3592d15fe8405.tar.gz
gdb-828d584679845b6a1d01151f7df3592d15fe8405.tar.bz2
(Ada) Handle same component names when searching in tagged types
Consider the following code: type Top_T is tagged record N : Integer := 1; U : Integer := 974; A : Integer := 48; end record; type Middle_T is new Top.Top_T with record N : Character := 'a'; C : Integer := 3; end record; type Bottom_T is new Middle.Middle_T with record N : Float := 4.0; C : Character := '5'; X : Integer := 6; A : Character := 'J'; end record; Tagged records in Ada provide object-oriented features, and what is interesting in the code above is that a child tagged record introduce additional components (fields) which sometimes have the same name as one of the components in the parent. For instance, Bottom_T introduces a component named "C", while at the same time inheriting from Middle_T which also has a component named "C"; so, in essence, type Bottom_T has two components with the same name! And before people start wondering why the language can possibly be allowing that, this can only happen if the parent type has a private definition. In our case, this was brought to our attention when the parent was a generic paramenter. With that in mind... Let's say we now have a variable declared and initialized as follow: TC : Top_A := new Bottom_T; And then we use this variable to call this function procedure Assign (Obj: in out Top_T; TV : Integer); as follow: Assign (Top_T (B), 12); Now, we're in the debugger, and we're inside that procedure (Top.Assign in our gdb testcase), and we want to print the value of obj.c: Usually, the tagged record or one of the parent type owns the component to print and there's no issue but in this particular case, what does it mean to ask for Obj.C ? Since the actual type for object is type Bottom_T, it could mean two things: type component C from the Middle_T view, but also component C from Bottom_T. So in that "undefined" case, when the component is not found in the non-resolved type (which includes all the components of the parent type), then resolve it and see if we get better luck once expanded. In the case of homonyms in the derived tagged type, we don't guaranty anything, and pick the one that's easiest for us to program. This patch fixes the behavior like described above. gdb/ChangeLog: * ada-lang.c (ada_value_primitive_field): Handle field search in case of homonyms. (find_struct_field): Ditto. (ada_search_struct_field): Ditto. (ada_value_struct_elt): Ditto. (ada_lookup_struct_elt_type): Ditto. gdb/testsuite/ChangeLog: * gdb.ada/same_component_name: New testcase. Tested on x86_64-linux.
Diffstat (limited to 'gdb/testsuite')
-rw-r--r--gdb/testsuite/ChangeLog4
-rw-r--r--gdb/testsuite/gdb.ada/same_component_name.exp60
-rw-r--r--gdb/testsuite/gdb.ada/same_component_name/foo.adb31
-rw-r--r--gdb/testsuite/gdb.ada/same_component_name/pck.adb42
-rw-r--r--gdb/testsuite/gdb.ada/same_component_name/pck.ads51
5 files changed, 188 insertions, 0 deletions
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 50f371b..84ad6d6 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2017-12-15 Xavier Roirand <roirand@adacore.com>
+
+ * gdb.ada/same_component_name: New testcase.
+
2017-12-14 Joel Brobecker <brobecker@adacore.com>
* gdb.ada/str_binop_equal: New testcase.
diff --git a/gdb/testsuite/gdb.ada/same_component_name.exp b/gdb/testsuite/gdb.ada/same_component_name.exp
new file mode 100644
index 0000000..c3c7645
--- /dev/null
+++ b/gdb/testsuite/gdb.ada/same_component_name.exp
@@ -0,0 +1,60 @@
+# Copyright 2017 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/>.
+
+load_lib "ada.exp"
+
+standard_ada_testfile foo
+
+if {[gdb_compile_ada "${srcfile}" "${binfile}" executable [list debug ]] != "" } {
+ return -1
+}
+
+clean_restart ${testfile}
+
+set bp_top_location [gdb_get_line_number "BREAK_TOP" ${testdir}/pck.adb]
+set bp_middle_location [gdb_get_line_number "BREAK_MIDDLE" ${testdir}/pck.adb]
+set bp_bottom_location [gdb_get_line_number "BREAK_BOTTOM" ${testdir}/pck.adb]
+
+gdb_breakpoint "pck.adb:$bp_top_location"
+gdb_breakpoint "pck.adb:$bp_middle_location"
+gdb_breakpoint "pck.adb:$bp_bottom_location"
+
+gdb_run_cmd
+
+gdb_test "" \
+ ".*Breakpoint $decimal, pck.top.assign \\(.*\\).*" \
+ "run to top assign breakpoint"
+
+gdb_test "print obj.n" " = 1" "Print top component field"
+
+gdb_test "continue" \
+ ".*Breakpoint $decimal, pck.assign \\(.*\\).*" \
+ "continue to bottom assign breakpoint"
+
+gdb_test "print obj.n" " = 4\\.0" "Print bottom component field"
+
+gdb_test "continue" \
+ ".*Breakpoint $decimal, pck.middle.assign \\(.*\\).*" \
+ "continue to middle assign breakpoint"
+
+gdb_test "print obj.a" " = 48" \
+ "Print top component field in middle assign function"
+
+gdb_test "continue" \
+ ".*Breakpoint $decimal, pck.assign \\(.*\\).*" \
+ "continue to bottom assign breakpoint (2nd time)"
+
+gdb_test "print obj.x" " = 6" \
+ "Print field existing only in bottom component"
diff --git a/gdb/testsuite/gdb.ada/same_component_name/foo.adb b/gdb/testsuite/gdb.ada/same_component_name/foo.adb
new file mode 100644
index 0000000..2a3c763
--- /dev/null
+++ b/gdb/testsuite/gdb.ada/same_component_name/foo.adb
@@ -0,0 +1,31 @@
+-- Copyright 2017 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/>.
+
+with Pck;
+use Pck;
+use Pck.Middle;
+use Pck.Top;
+
+procedure Foo is
+ B : Bottom_T;
+ M : Middle_T;
+
+begin
+ Assign (Top_T (B), 12);
+ Assign (B, 10.0);
+
+ Assign (M, 'V');
+ Assign (B, 5.0);
+end Foo;
diff --git a/gdb/testsuite/gdb.ada/same_component_name/pck.adb b/gdb/testsuite/gdb.ada/same_component_name/pck.adb
new file mode 100644
index 0000000..c0f7ba1
--- /dev/null
+++ b/gdb/testsuite/gdb.ada/same_component_name/pck.adb
@@ -0,0 +1,42 @@
+-- Copyright 2010-2017 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/>.
+
+with System;
+
+package body Pck is
+ package body Top is
+ procedure Assign (Obj: in out Top_T; TV : Integer) is
+ begin
+ Do_Nothing (Obj'Address); -- BREAK_TOP
+ end Assign;
+ end Top;
+
+ package body Middle is
+ procedure Assign (Obj: in out Middle_T; MV : Character) is
+ begin
+ Do_Nothing (Obj'Address); -- BREAK_MIDDLE
+ end Assign;
+ end Middle;
+
+ procedure Assign (Obj: in out Bottom_T; BV : Float) is
+ begin
+ Do_Nothing (Obj'Address); -- BREAK_BOTTOM
+ end Assign;
+
+ procedure Do_Nothing (A : System.Address) is
+ begin
+ null;
+ end Do_Nothing;
+end Pck;
diff --git a/gdb/testsuite/gdb.ada/same_component_name/pck.ads b/gdb/testsuite/gdb.ada/same_component_name/pck.ads
new file mode 100644
index 0000000..813fc9d
--- /dev/null
+++ b/gdb/testsuite/gdb.ada/same_component_name/pck.ads
@@ -0,0 +1,51 @@
+-- Copyright 2017 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/>.
+
+with System;
+
+package Pck is
+
+ package Top is
+ type Top_T is tagged private;
+ type Top_A is access Top_T'Class;
+ procedure Assign (Obj: in out Top_T; TV : Integer);
+ private
+ type Top_T is tagged record
+ N : Integer := 1;
+ A : Integer := 48;
+ end record;
+ end Top;
+
+ package Middle is
+ type Middle_T is new Top.Top_T with private;
+ type Middle_A is access Middle_T'Class;
+ procedure Assign (Obj: in out Middle_T; MV : Character);
+ private
+ type Middle_T is new Top.Top_T with record
+ N : Character := 'a';
+ end record;
+ end Middle;
+
+ type Bottom_T is new Middle.Middle_T with record
+ N : Float := 4.0;
+ X : Integer := 6;
+ A : Character := 'J';
+ end record;
+ type Bottom_A is access Bottom_T'Class;
+ procedure Assign (Obj: in out Bottom_T; BV : Float);
+
+ procedure Do_Nothing (A : System.Address);
+
+end Pck;