[binutils-gdb] gdb: split iterate_over_symtabs into for_each_symtab and find_symtab

Simon Marchi simark@sourceware.org
Fri Apr 17 19:33:19 GMT 2026


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=5f5a59e211da04128cca635c255b7d8ffcbcd7ac

commit 5f5a59e211da04128cca635c255b7d8ffcbcd7ac
Author: Simon Marchi <simon.marchi@efficios.com>
Date:   Fri Apr 17 10:30:23 2026 -0400

    gdb: split iterate_over_symtabs into for_each_symtab and find_symtab
    
    Same rationale as the previous patches.
    
    For the moment, find_symtab is only needed internally in symtab.c, so
    keep it static there.  Note that the interaction with
    objfile.map_symtabs_matching_filename gets cleaner in a subsequent
    patch.
    
    for_each_symtab is implemented using find_symtab, because the iteration
    behavior is not completely trivial.
    
    find_symtab_callback_ftype is in the header file, because it is used
    from another source file in the next patch.
    
    Change-Id: I6ab8342151eb735327fc2e7935e7a65cede5e1dd
    Approved-By: Andrew Burgess <aburgess@redhat.com>

Diff:
---
 gdb/linespec.c |  5 ++---
 gdb/symtab.c   | 56 +++++++++++++++++++++++++++++++++++++++-----------------
 gdb/symtab.h   | 21 +++++++++++++--------
 3 files changed, 54 insertions(+), 28 deletions(-)

diff --git a/gdb/linespec.c b/gdb/linespec.c
index 4170d4ecf2c..0afe0c619a1 100644
--- a/gdb/linespec.c
+++ b/gdb/linespec.c
@@ -3613,7 +3613,6 @@ collect_symtabs_from_filename (const char *file,
     {
       if (symtab_table.insert (symtab).second)
 	symtabs.push_back (symtab);
-      return iteration_status::keep_going;
     };
 
   /* Find that file's data.  */
@@ -3624,11 +3623,11 @@ collect_symtabs_from_filename (const char *file,
 	  if (pspace->executing_startup)
 	    continue;
 
-	  iterate_over_symtabs (pspace, file, collector);
+	  for_each_symtab (pspace, file, collector);
 	}
     }
   else
-    iterate_over_symtabs (search_pspace, file, collector);
+    for_each_symtab (search_pspace, file, collector);
 
   /* It is tempting to use the unordered_dense 'extract' method here,
      and remove the separate vector -- but it's unclear if ordering
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 5a39081dfc5..5aea10d3ff9 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -629,12 +629,17 @@ compare_filenames_for_search (const char *filename, const char *search_name)
 	      && STRIP_DRIVE_SPEC (filename) == &filename[len - search_len]));
 }
 
-/* See symtab.h.  */
+/* Return the first symtab in PSPACE matching NAME and for which
+   CALLBACK returns true.
 
-void
-iterate_over_symtabs (program_space *pspace, const char *name,
-		      gdb::function_view<iteration_status (symtab *)> callback)
+   See documentation for for_each_symtab for how exactly NAME is matched.  */
+
+static symtab *
+find_symtab (program_space *pspace, const char *name,
+	     find_symtab_callback_ftype callback)
 {
+  struct symtab *result = nullptr;
+
   gdb::unique_xmalloc_ptr<char> real_path;
 
   /* Here we are interested in canonicalizing an absolute path, not
@@ -645,27 +650,45 @@ iterate_over_symtabs (program_space *pspace, const char *name,
       gdb_assert (IS_ABSOLUTE_PATH (real_path.get ()));
     }
 
+  auto map_callback = [&] (symtab *symtab)
+    {
+      if (callback (symtab))
+	{
+	  result = symtab;
+	  return iteration_status::stop;
+	}
+
+      return iteration_status::keep_going;
+    };
+
   for (objfile &objfile : pspace->objfiles ())
     if (objfile.map_symtabs_matching_filename (name, real_path.get (),
-					       callback)
+					       map_callback)
 	== iteration_status::stop)
-      return;
+      return result;
+
+  return nullptr;
 }
 
 /* See symtab.h.  */
 
-symtab *
-lookup_symtab (program_space *pspace, const char *name)
+void
+for_each_symtab (program_space *pspace, const char *name,
+		 for_each_symtab_callback_ftype callback)
 {
-  struct symtab *result = NULL;
+  find_symtab (pspace, name, [&] (symtab *symtab)
+	       {
+		 callback (symtab);
+		 return false;
+	       });
+}
 
-  iterate_over_symtabs (pspace, name, [&] (symtab *symtab)
-    {
-      result = symtab;
-      return iteration_status::stop;
-    });
+/* See symtab.h.  */
 
-  return result;
+symtab *
+lookup_symtab (program_space *pspace, const char *name)
+{
+  return find_symtab (pspace, name, [&] (symtab *symtab) { return true; });
 }
 
 

@@ -6140,12 +6163,11 @@ collect_file_symbol_completion_matches (completion_tracker &tracker,
 
   /* Go through symtabs for SRCFILE and check the externs and statics
      for symbols which match.  */
-  iterate_over_symtabs (current_program_space, srcfile, [&] (symtab *s)
+  for_each_symtab (current_program_space, srcfile, [&] (symtab *s)
     {
       add_symtab_completions (s->compunit (),
 			      tracker, mode, lookup_name,
 			      sym_text, word, TYPE_CODE_UNDEF);
-      return iteration_status::keep_going;
     });
 }
 
diff --git a/gdb/symtab.h b/gdb/symtab.h
index 0c379db372c..34c19bde62f 100644
--- a/gdb/symtab.h
+++ b/gdb/symtab.h
@@ -2789,16 +2789,21 @@ extern bool basenames_may_differ;
 bool compare_filenames_for_search (const char *filename,
 				   const char *search_name);
 
-/* Check in PSPACE for a symtab of a specific name; first in symtabs, then in
-   psymtabs.  *If* there is no '/' in the name, a match after a '/' in the
-   symtab filename will also work.
+/* Callback type for function for_each_symtab.  */
 
-   Call CALLBACK with each symtab that is found.  If CALLBACK returns
-   iteration_status::stop, the search stops.  */
+using for_each_symtab_callback_ftype = std::function<void (symtab *)>;
 
-void iterate_over_symtabs
-  (program_space *pspace, const char *name,
-   gdb::function_view<iteration_status (symtab *)> callback);
+/* Check in PSPACE for symtabs of a specific name.  *If* there is no '/' in
+   the name, a match after a '/' in the symtab filename will also work.
+
+   Call CALLBACK with each symtab that is found.  */
+
+void for_each_symtab (program_space *pspace, const char *name,
+		      for_each_symtab_callback_ftype callback);
+
+/* Callback type for function find_symtab.  */
+
+using find_symtab_callback_ftype = std::function<bool (symtab *)>;
 
 std::vector<const linetable_entry *> find_linetable_entries_for_symtab_line
     (struct symtab *symtab, int line, const linetable_entry **best_entry);


More information about the Gdb-cvs mailing list