[9/9] Simplify quick_symbol_functions::map_matching_symbols

Message ID 20210325170458.2351251-10-tom@tromey.com
State New
Headers show
Series
  • Simplify quick_symbol_functions
Related show

Commit Message

Tom Tromey March 25, 2021, 5:04 p.m.
quick_symbol_functions::map_matching_symbols is only used by the Ada
code.  Currently, it both expands certain psymtabs and then walks over
the full symtabs -- including any already-expanded ones -- calling a
callback.

It appears to work lazily as well, in that if the callback returns
false, iteration stops.  However, only the psymtab implementation does
this; the DWARF index implementations are not lazy.  It turns out,
though, that the only callback that is ever passed here never returns
false.

This patch simplifies this method by removing the callback.  The
method is also renamed.  In the new scheme, the caller is responsible
for walking the full symtabs, which removes some redundancy as well.

gdb/ChangeLog
2021-03-25  Tom Tromey  <tom@tromey.com>

	* psymtab.c (psymbol_functions::expand_matching_symbols): Rename
	from map_matching_symbols.  Change parameters.
	* psympriv.h (struct psymbol_functions) <expand_matching_symbols>:
	Rename from map_matching_symbols.  Change parameters.
	* dwarf2/read.c (struct dwarf2_gdb_index)
	<expand_matching_symbols>: Rename from map_matching_symbols.
	Change parameters.
	(struct dwarf2_debug_names_index) <expand_matching_symbols>:
	Rename from map_matching_symbols.  Change parameters.
	(dwarf2_gdb_index::expand_matching_symbols): Rename from
	dw2_map_matching_symbols.  Change parameters.
	(dwarf2_gdb_index::expand_matching_symbols): Remove old
	implementation.
	(dwarf2_debug_names_index::expand_matching_symbols): Rename from
	map_matching_symbols.  Change parameters.
	* objfiles.h (struct objfile) <expand_matching_symbols>: Rename
	from map_matching_symbols.  Change parameters.
	* symfile-debug.c (objfile::expand_matching_symbols): Rename from
	map_matching_symbols.  Change parameters.
	* ada-lang.c (map_matching_symbols): New function.
	(add_nonlocal_symbols): Update.
---
 gdb/ChangeLog       | 24 +++++++++++++++++++
 gdb/ada-lang.c      | 51 +++++++++++++++++++++++++++++------------
 gdb/dwarf2/read.c   | 56 ++++-----------------------------------------
 gdb/objfiles.h      |  3 +--
 gdb/psympriv.h      |  3 +--
 gdb/psymtab.c       | 23 ++++---------------
 gdb/quick-symbol.h  | 15 +++++-------
 gdb/symfile-debug.c |  9 ++++----
 8 files changed, 82 insertions(+), 102 deletions(-)

-- 
2.26.2

Patch

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index f529d842f90..7e50ab08313 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -5180,6 +5180,38 @@  ada_lookup_name (const lookup_name_info &lookup_name)
   return lookup_name.ada ().lookup_name ().c_str ();
 }
 
+/* A helper for add_nonlocal_symbols.  Call expand_matching_symbols
+   for OBJFILE, then walk the objfile's symtabs and update the
+   results.  */
+
+static void
+map_matching_symbols (struct objfile *objfile,
+		      const lookup_name_info &lookup_name,
+		      bool is_wild_match,
+		      domain_enum domain,
+		      int global,
+		      match_data *data)
+{
+  data->objfile = objfile;
+  objfile->expand_matching_symbols (lookup_name, domain, global,
+				    is_wild_match ? nullptr : compare_names);
+
+  auto callback = [&] (struct block_symbol *bsym)
+    {
+      return aux_add_nonlocal_symbols (bsym, data);
+    };
+
+  const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
+  for (compunit_symtab *symtab : objfile->compunits ())
+    {
+      const struct block *block
+	= BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
+      if (!iterate_over_symbols_terminated (block, lookup_name,
+					    domain, callback))
+	break;
+    }
+}
+
 /* Add to RESULT all non-local symbols whose name and domain match
    LOOKUP_NAME and DOMAIN respectively.  The search is performed on
    GLOBAL_BLOCK symbols if GLOBAL is non-zero, or on STATIC_BLOCK
@@ -5194,17 +5226,10 @@  add_nonlocal_symbols (std::vector<struct block_symbol> &result,
 
   bool is_wild_match = lookup_name.ada ().wild_match_p ();
 
-  auto callback = [&] (struct block_symbol *bsym)
-    {
-      return aux_add_nonlocal_symbols (bsym, &data);
-    };
-
   for (objfile *objfile : current_program_space->objfiles ())
     {
-      data.objfile = objfile;
-
-      objfile->map_matching_symbols (lookup_name, domain, global, callback,
-				     is_wild_match ? NULL : compare_names);
+      map_matching_symbols (objfile, lookup_name, is_wild_match, domain,
+			    global, &data);
 
       for (compunit_symtab *cu : objfile->compunits ())
 	{
@@ -5224,12 +5249,8 @@  add_nonlocal_symbols (std::vector<struct block_symbol> &result,
       lookup_name_info name1 (bracket_name, symbol_name_match_type::FULL);
 
       for (objfile *objfile : current_program_space->objfiles ())
-	{
-	  data.objfile = objfile;
-	  objfile->map_matching_symbols (name1, domain, global, callback,
-					 compare_names);
-	}
-    }      	
+	map_matching_symbols (objfile, name1, false, domain, global, &data);
+    }
 }
 
 /* Find symbols in DOMAIN matching LOOKUP_NAME, in BLOCK and, if
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 498e34b097d..3088dbd0d17 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -2231,12 +2231,11 @@  struct dwarf2_gdb_index : public dwarf2_base_index_functions
 {
   void dump (struct objfile *objfile) override;
 
-  void map_matching_symbols
+  void expand_matching_symbols
     (struct objfile *,
      const lookup_name_info &lookup_name,
      domain_enum domain,
      int global,
-     gdb::function_view<symbol_found_callback_ftype> callback,
      symbol_compare_ftype *ordered_compare) override;
 
   bool expand_symtabs_matching
@@ -2254,12 +2253,11 @@  struct dwarf2_debug_names_index : public dwarf2_base_index_functions
 {
   void dump (struct objfile *objfile) override;
 
-  void map_matching_symbols
+  void expand_matching_symbols
     (struct objfile *,
      const lookup_name_info &lookup_name,
      domain_enum domain,
      int global,
-     gdb::function_view<symbol_found_callback_ftype> callback,
      symbol_compare_ftype *ordered_compare) override;
 
   bool expand_symtabs_matching
@@ -3508,12 +3506,11 @@  dw2_expand_symtabs_matching_one
    gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
    gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
 
-static void
-dw2_map_matching_symbols
+void
+dwarf2_gdb_index::expand_matching_symbols
   (struct objfile *objfile,
    const lookup_name_info &name, domain_enum domain,
    int global,
-   gdb::function_view<symbol_found_callback_ftype> callback,
    symbol_compare_ftype *ordered_compare)
 {
   /* Used for Ada.  */
@@ -3552,30 +3549,6 @@  dw2_map_matching_symbols
       /* We have -readnow: no .gdb_index, but no partial symtabs either.  So,
 	 proceed assuming all symtabs have been read in.  */
     }
-
-  for (compunit_symtab *cust : objfile->compunits ())
-    {
-      const struct block *block;
-
-      if (cust == NULL)
-	continue;
-      block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
-      if (!iterate_over_symbols_terminated (block, name,
-					    domain, callback))
-	return;
-    }
-}
-
-void
-dwarf2_gdb_index::map_matching_symbols
-  (struct objfile *objfile,
-   const lookup_name_info &name, domain_enum domain,
-   int global,
-   gdb::function_view<symbol_found_callback_ftype> callback,
-   symbol_compare_ftype *ordered_compare)
-{
-  dw2_map_matching_symbols (objfile, name, domain, global, callback,
-			    ordered_compare);
 }
 
 /* Starting from a search name, return the string that finds the upper
@@ -5508,11 +5481,10 @@  dwarf2_debug_names_index::dump (struct objfile *objfile)
 }
 
 void
-dwarf2_debug_names_index::map_matching_symbols
+dwarf2_debug_names_index::expand_matching_symbols
   (struct objfile *objfile,
    const lookup_name_info &name, domain_enum domain,
    int global,
-   gdb::function_view<symbol_found_callback_ftype> callback,
    symbol_compare_ftype *ordered_compare)
 {
   dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
@@ -5522,7 +5494,6 @@  dwarf2_debug_names_index::map_matching_symbols
     return;
 
   mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
-  const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
   const block_search_flags block_flags
     = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
 
@@ -5548,23 +5519,6 @@  dwarf2_debug_names_index::map_matching_symbols
 					 nullptr);
       return true;
     }, per_objfile);
-
-  /* It's a shame we couldn't do this inside the
-     dw2_expand_symtabs_matching_symbol callback, but that skips CUs
-     that have already been expanded.  Instead, this loop matches what
-     the psymtab code does.  */
-  for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
-    {
-      compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
-      if (symtab != nullptr)
-	{
-	  const struct block *block
-	    = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
-	  if (!iterate_over_symbols_terminated (block, name,
-						domain, callback))
-	    break;
-	}
-    }
 }
 
 bool
diff --git a/gdb/objfiles.h b/gdb/objfiles.h
index 264e783c76e..cc33fdfbd6d 100644
--- a/gdb/objfiles.h
+++ b/gdb/objfiles.h
@@ -593,10 +593,9 @@  struct objfile
   void expand_symtabs_with_fullname (const char *fullname);
 
   /* See quick_symbol_functions.  */
-  void map_matching_symbols
+  void expand_matching_symbols
     (const lookup_name_info &name, domain_enum domain,
      int global,
-     gdb::function_view<symbol_found_callback_ftype> callback,
      symbol_compare_ftype *ordered_compare);
 
   /* See quick_symbol_functions.  */
diff --git a/gdb/psympriv.h b/gdb/psympriv.h
index 664f14b45d5..424566a1736 100644
--- a/gdb/psympriv.h
+++ b/gdb/psympriv.h
@@ -517,12 +517,11 @@  struct psymbol_functions : public quick_symbol_functions
 
   void expand_all_symtabs (struct objfile *objfile) override;
 
-  void map_matching_symbols
+  void expand_matching_symbols
     (struct objfile *,
      const lookup_name_info &lookup_name,
      domain_enum domain,
      int global,
-     gdb::function_view<symbol_found_callback_ftype> callback,
      symbol_compare_ftype *ordered_compare) override;
 
   bool expand_symtabs_matching
diff --git a/gdb/psymtab.c b/gdb/psymtab.c
index f35a9f1dca2..1477ffc9b3e 100644
--- a/gdb/psymtab.c
+++ b/gdb/psymtab.c
@@ -979,36 +979,23 @@  psymtab_to_fullname (struct partial_symtab *ps)
   return ps->fullname;
 }
 
-/* Psymtab version of map_matching_symbols.  See its definition in
+/* Psymtab version of expand_matching_symbols.  See its definition in
    the definition of quick_symbol_functions in symfile.h.  */
 
 void
-psymbol_functions::map_matching_symbols
+psymbol_functions::expand_matching_symbols
   (struct objfile *objfile,
    const lookup_name_info &name, domain_enum domain,
    int global,
-   gdb::function_view<symbol_found_callback_ftype> callback,
    symbol_compare_ftype *ordered_compare)
 {
-  const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
-
   for (partial_symtab *ps : require_partial_symbols (objfile))
     {
       QUIT;
-      if (ps->readin_p (objfile)
-	  || match_partial_symbol (objfile, ps, global, name, domain,
+      if (!ps->readin_p (objfile)
+	  && match_partial_symbol (objfile, ps, global, name, domain,
 				   ordered_compare))
-	{
-	  struct compunit_symtab *cust = psymtab_to_symtab (objfile, ps);
-	  const struct block *block;
-
-	  if (cust == NULL)
-	    continue;
-	  block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
-	  if (!iterate_over_symbols_terminated (block, name,
-						domain, callback))
-	    return;
-	}
+	psymtab_to_symtab (objfile, ps);
     }
 }
 
diff --git a/gdb/quick-symbol.h b/gdb/quick-symbol.h
index 064922a3b66..d8b6e636103 100644
--- a/gdb/quick-symbol.h
+++ b/gdb/quick-symbol.h
@@ -120,11 +120,10 @@  struct quick_symbol_functions
   virtual void expand_all_symtabs (struct objfile *objfile) = 0;
 
   /* Find global or static symbols in all tables that are in DOMAIN
-     and for which MATCH (symbol name, NAME) == 0, passing each to 
-     CALLBACK, reading in partial symbol tables as needed.  Look
-     through global symbols if GLOBAL and otherwise static symbols.
-     Passes NAME and NAMESPACE to CALLBACK with each symbol
-     found.  After each block is processed, passes NULL to CALLBACK.
+     and for which MATCH (symbol name, NAME) == 0, reading in partial
+     symbol tables as needed.  Look through global symbols if GLOBAL
+     and otherwise static symbols.
+
      MATCH must be weaker than strcmp_iw_ordered in the sense that
      strcmp_iw_ordered(x,y) == 0 --> MATCH(x,y) == 0.  ORDERED_COMPARE,
      if non-null, must be an ordering relation compatible with
@@ -133,15 +132,13 @@  struct quick_symbol_functions
      and 
 	    strcmp_iw_ordered(x,y) <= 0 --> ORDERED_COMPARE(x,y) <= 0
      (allowing strcmp_iw_ordered(x,y) < 0 while ORDERED_COMPARE(x, y) == 0).
-     CALLBACK returns true to indicate that the scan should continue, or
-     false to indicate that the scan should be terminated.  */
+  */
 
-  virtual void map_matching_symbols
+  virtual void expand_matching_symbols
     (struct objfile *,
      const lookup_name_info &lookup_name,
      domain_enum domain,
      int global,
-     gdb::function_view<symbol_found_callback_ftype> callback,
      symbol_compare_ftype *ordered_compare) = 0;
 
   /* Expand all symbol tables in OBJFILE matching some criteria.
diff --git a/gdb/symfile-debug.c b/gdb/symfile-debug.c
index 7addb85453f..447483779e4 100644
--- a/gdb/symfile-debug.c
+++ b/gdb/symfile-debug.c
@@ -353,22 +353,21 @@  objfile::expand_symtabs_with_fullname (const char *fullname)
 }
 
 void
-objfile::map_matching_symbols
+objfile::expand_matching_symbols
   (const lookup_name_info &name, domain_enum domain,
    int global,
-   gdb::function_view<symbol_found_callback_ftype> callback,
    symbol_compare_ftype *ordered_compare)
 {
   if (debug_symfile)
     fprintf_filtered (gdb_stdlog,
-		      "qf->map_matching_symbols (%s, %s, %d, %s)\n",
+		      "qf->expand_matching_symbols (%s, %s, %d, %s)\n",
 		      objfile_debug_name (this),
 		      domain_name (domain), global,
 		      host_address_to_string (ordered_compare));
 
   for (const auto &iter : qf)
-    iter->map_matching_symbols (this, name, domain, global,
-				callback, ordered_compare);
+    iter->expand_matching_symbols (this, name, domain, global,
+				   ordered_compare);
 }
 
 bool