]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: split iterate_over_symtabs into for_each_symtab and find_symtab
authorSimon Marchi <simon.marchi@efficios.com>
Fri, 17 Apr 2026 14:30:23 +0000 (10:30 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Fri, 17 Apr 2026 19:30:31 +0000 (15:30 -0400)
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>
gdb/linespec.c
gdb/symtab.c
gdb/symtab.h

index 4170d4ecf2c474b9a789e0328bb3a1736da1a000..0afe0c619a135379e559c3f2aca6b12bf0f24e61 100644 (file)
@@ -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
index 5a39081dfc54c6d74014eec07e2056cd7c5b09f9..5aea10d3ff950748ded0befce3eac42f91b4797a 100644 (file)
@@ -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; });
 }
 
 \f
@@ -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;
     });
 }
 
index 0c379db372c2ae842485c9ae620c392d60d1f95e..34c19bde62febdfe0c031a9eece52769c3606e24 100644 (file)
@@ -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);