[5/5] Handle type qualifier for enumeration name

Message ID 20210716191750.2619599-6-tromey@adacore.com
State New
Headers show
Series
  • Fix Ada qualification bug
Related show

Commit Message

Tom Tromey July 16, 2021, 7:17 p.m.
Pierre-Marie noticed that the Ada expression "TYPE'(NAME)" resolved
incorrectly when "TYPE" was an enumeration type.  Here, "NAME" should
be unambiguous.

This patch fixes this problem.  Note that the patch is not perfect --
it does not give an error if TYPE is an enumeration type but NAME is
not an enumerator but does have some other meaning in scope.  Fixing
this proved difficult, and so I've left it out.
---
 gdb/ada-lang.c                            | 33 +++++++++++++++++++++--
 gdb/testsuite/gdb.ada/enum_qual.exp       | 32 ++++++++++++++++++++++
 gdb/testsuite/gdb.ada/enum_qual/gener.ads | 22 +++++++++++++++
 gdb/testsuite/gdb.ada/enum_qual/qual.adb  | 27 +++++++++++++++++++
 4 files changed, 112 insertions(+), 2 deletions(-)
 create mode 100644 gdb/testsuite/gdb.ada/enum_qual.exp
 create mode 100644 gdb/testsuite/gdb.ada/enum_qual/gener.ads
 create mode 100644 gdb/testsuite/gdb.ada/enum_qual/qual.adb

-- 
2.26.3

Patch

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 1038ccbb316..caf8780fe7c 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -3425,6 +3425,29 @@  ada_resolve_funcall (struct symbol *sym, const struct block *block,
   return candidates[i];
 }
 
+/* Resolve a mention of a name where the context type is an
+   enumeration type.  */
+
+static int
+ada_resolve_enum (std::vector<struct block_symbol> &syms,
+		  const char *name, struct type *context_type,
+		  bool parse_completion)
+{
+  gdb_assert (context_type->code () == TYPE_CODE_ENUM);
+  context_type = ada_check_typedef (context_type);
+
+  for (int i = 0; i < syms.size (); ++i)
+    {
+      /* We already know the name matches, so we're just looking for
+	 an element of the correct enum type.  */
+      if (ada_check_typedef (SYMBOL_TYPE (syms[i].symbol)) == context_type)
+	return i;
+    }
+
+  error (_("No name '%s' in enumeration type '%s'"), name,
+	 ada_type_name (context_type));
+}
+
 /* See ada-lang.h.  */
 
 block_symbol
@@ -3474,6 +3497,10 @@  ada_resolve_variable (struct symbol *sym, const struct block *block,
     error (_("No definition found for %s"), sym->print_name ());
   else if (candidates.size () == 1)
     i = 0;
+  else if (context_type != nullptr
+	   && context_type->code () == TYPE_CODE_ENUM)
+    i = ada_resolve_enum (candidates, sym->linkage_name (), context_type,
+			  parse_completion);
   else if (deprocedure_p && !is_nonfunction (candidates))
     {
       i = ada_resolve_function
@@ -4937,8 +4964,10 @@  ada_add_local_symbols (std::vector<struct block_symbol> &result,
     {
       ada_add_block_symbols (result, block, lookup_name, domain, NULL);
 
-      /* If we found a non-function match, assume that's the one.  */
-      if (is_nonfunction (result))
+      /* If we found a non-function match, assume that's the one.  We
+	 only check this when finding a function boundary, so that we
+	 can accumulate all results from intervening blocks first.  */
+      if (BLOCK_FUNCTION (block) != nullptr && is_nonfunction (result))
 	return;
 
       block = BLOCK_SUPERBLOCK (block);
diff --git a/gdb/testsuite/gdb.ada/enum_qual.exp b/gdb/testsuite/gdb.ada/enum_qual.exp
new file mode 100644
index 00000000000..783574a6dcf
--- /dev/null
+++ b/gdb/testsuite/gdb.ada/enum_qual.exp
@@ -0,0 +1,32 @@ 
+# 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/>.
+
+load_lib "ada.exp"
+
+if { [skip_ada_tests] } { return -1 }
+
+standard_ada_testfile qual
+
+if {[gdb_compile_ada "${srcfile}" "${binfile}" executable debug] != ""} {
+    return -1
+}
+
+clean_restart ${testfile}
+
+set bp_location [gdb_get_line_number "STOP" ${testdir}/qual.adb]
+runto "qual.adb:$bp_location"
+
+gdb_test "print kind'(no_element)" " = no_element" \
+    "print qualified no_element"
diff --git a/gdb/testsuite/gdb.ada/enum_qual/gener.ads b/gdb/testsuite/gdb.ada/enum_qual/gener.ads
new file mode 100644
index 00000000000..882185a3fa8
--- /dev/null
+++ b/gdb/testsuite/gdb.ada/enum_qual/gener.ads
@@ -0,0 +1,22 @@ 
+--  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/>.
+
+generic
+  type Component_T is private;
+package Gener is
+
+  No_Element : Component_T;
+
+end Gener;
diff --git a/gdb/testsuite/gdb.ada/enum_qual/qual.adb b/gdb/testsuite/gdb.ada/enum_qual/qual.adb
new file mode 100644
index 00000000000..a6eff245e40
--- /dev/null
+++ b/gdb/testsuite/gdb.ada/enum_qual/qual.adb
@@ -0,0 +1,27 @@ 
+--  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/>.
+
+with Gener;
+
+procedure Qual is
+
+   package P is new Gener (Integer);
+
+   type Kind is (Present, No_Element);
+   K : Kind := No_Element;
+
+begin
+   null;  -- STOP
+end Qual;