Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@efficios.com>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>
Subject: [PATCH 08/11] gdb: split iterate_over_symtabs in for_each_symtab and find_symtab
Date: Thu, 16 Apr 2026 16:16:18 -0400	[thread overview]
Message-ID: <20260416202408.422441-9-simon.marchi@efficios.com> (raw)
In-Reply-To: <20260416202408.422441-1-simon.marchi@efficios.com>

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
---
 gdb/linespec.c |  5 ++---
 gdb/symtab.c   | 55 ++++++++++++++++++++++++++++++++++----------------
 gdb/symtab.h   | 19 +++++++++--------
 3 files changed, 51 insertions(+), 28 deletions(-)

diff --git a/gdb/linespec.c b/gdb/linespec.c
index 4170d4ecf2c4..0afe0c619a13 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 5a39081dfc54..6a5c8c7058e4 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -629,12 +629,16 @@ 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.
 
-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 = NULL;
+
   gdb::unique_xmalloc_ptr<char> real_path;
 
   /* Here we are interested in canonicalizing an absolute path, not
@@ -645,11 +649,37 @@ 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.  */
+
+void
+for_each_symtab (program_space *pspace, const char *name,
+		 for_each_symtab_callback_ftype callback)
+{
+  find_symtab (pspace, name, [&] (symtab *symtab)
+	       {
+		 callback (symtab);
+		 return false;
+	       });
 }
 
 /* See symtab.h.  */
@@ -657,15 +687,7 @@ iterate_over_symtabs (program_space *pspace, const char *name,
 symtab *
 lookup_symtab (program_space *pspace, const char *name)
 {
-  struct symtab *result = NULL;
-
-  iterate_over_symtabs (pspace, name, [&] (symtab *symtab)
-    {
-      result = symtab;
-      return iteration_status::stop;
-    });
-
-  return result;
+  return find_symtab (pspace, name, [&] (symtab *symtab) { return true; });
 }
 
 \f
@@ -6140,12 +6162,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 9e45f087adda..3936052706f1 100644
--- a/gdb/symtab.h
+++ b/gdb/symtab.h
@@ -2789,16 +2789,19 @@ 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.
+using for_each_symtab_callback_ftype = std::function<void (symtab *)>;
 
-   Call CALLBACK with each symtab that is found.  If CALLBACK returns
-   true, the search stops.  */
+/* Check in PSPACE for a symtab of a specific name.  *If* there is no '/' in
+   the name, a match after a '/' in the symtab filename will also work.
 
-void iterate_over_symtabs
-  (program_space *pspace, const char *name,
-   gdb::function_view<iteration_status (symtab *)> callback);
+   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);
-- 
2.53.0


  parent reply	other threads:[~2026-04-16 20:26 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-16 20:16 [PATCH 00/11] Readability improvements of some iteration functions Simon Marchi
2026-04-16 20:16 ` [PATCH 01/11] gdb/dwarf: remove unused file_match parameter from dwarf2_base_index_functions::search_one Simon Marchi
2026-04-17 11:11   ` Andrew Burgess
2026-04-16 20:16 ` [PATCH 02/11] gdb: rename search_symtabs_expansion_listener -> compunit_symtab_iteration_callback Simon Marchi
2026-04-17 15:08   ` Simon Marchi
2026-04-17 17:17   ` Tom Tromey
2026-04-17 19:34     ` Simon Marchi
2026-04-16 20:16 ` [PATCH 03/11] gdb: introduce iteration_status enum, use it for search callbacks Simon Marchi
2026-04-17 11:01   ` Andrew Burgess
2026-04-17 14:33     ` Simon Marchi
2026-04-17 14:34   ` Tom Tromey
2026-04-16 20:16 ` [PATCH 04/11] gdb, gdbserver: make iterate_over_lwps_ftype a function_view Simon Marchi
2026-04-17 11:09   ` Andrew Burgess
2026-04-17 14:37     ` Simon Marchi
2026-04-16 20:16 ` [PATCH 05/11] gdb, gdbserver: split iterate_over_lwps in for_each_lwp and find_lwp Simon Marchi
2026-04-17 11:25   ` Andrew Burgess
2026-04-17 14:53     ` Simon Marchi
2026-04-16 20:16 ` [PATCH 06/11] gdb: split iterate_over_threads in for_each_thread and find_thread Simon Marchi
2026-04-17 11:33   ` Andrew Burgess
2026-04-16 20:16 ` [PATCH 07/11] gdb: split iterate_over_minimal_symbols in for_each_minimal_symbol and find_minimal_symbol Simon Marchi
2026-04-17 12:13   ` Andrew Burgess
2026-04-16 20:16 ` Simon Marchi [this message]
2026-04-17 13:31   ` [PATCH 08/11] gdb: split iterate_over_symtabs in for_each_symtab and find_symtab Andrew Burgess
2026-04-17 14:54     ` Simon Marchi
2026-04-16 20:16 ` [PATCH 09/11] gdb: change objfile::map_symtabs_matching_filename to find_symtab_matching_filename Simon Marchi
2026-04-17 13:50   ` Andrew Burgess
2026-04-17 15:03     ` Simon Marchi
2026-04-16 20:16 ` [PATCH 10/11] gdb: make symbol_found_callback_ftype a function_view Simon Marchi
2026-04-17 13:55   ` Andrew Burgess
2026-04-17 15:04     ` Simon Marchi
2026-04-16 20:16 ` [PATCH 11/11] gdb: make iterate_over_symbols return void, rename to for_each_symbol Simon Marchi
2026-04-17 14:05   ` Andrew Burgess
2026-04-17 15:06     ` Simon Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260416202408.422441-9-simon.marchi@efficios.com \
    --to=simon.marchi@efficios.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox