[3/4] Remove ALL_DICT_SYMBOLS

Message ID 20230416-remove-all-macros-v1-3-f118956faafe@tromey.com
State New
Headers
Series Remove remaining ALL_ iterator macros |

Commit Message

Tom Tromey April 16, 2023, 4:28 p.m. UTC
  This replaces ALL_DICT_SYMBOLS with an iterator so that for-each can
be used.
---
 gdb/block.h      |  4 ++++
 gdb/buildsym.c   | 15 ++++++---------
 gdb/dictionary.h | 55 ++++++++++++++++++++++++++++++++++++++++++++-----------
 gdb/objfiles.c   |  9 ++-------
 gdb/symmisc.c    |  4 +---
 5 files changed, 57 insertions(+), 30 deletions(-)
  

Patch

diff --git a/gdb/block.h b/gdb/block.h
index cdcee0844ec..f132d351bb6 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -143,6 +143,10 @@  struct block : public allocate_on_obstack
   multidictionary *multidict () const
   { return m_multidict; }
 
+  /* Return an iterator range for this block's multidict.  */
+  iterator_range<mdict_iterator_wrapper> multidict_symbols () const
+  { return iterator_range<mdict_iterator_wrapper> (m_multidict); }
+
   /* Set this block's multidict.  */
   void set_multidict (multidictionary *multidict)
   { m_multidict = multidict; }
diff --git a/gdb/buildsym.c b/gdb/buildsym.c
index f000233dafa..d12ad2187ab 100644
--- a/gdb/buildsym.c
+++ b/gdb/buildsym.c
@@ -244,7 +244,6 @@  buildsym_compunit::finish_block_internal
   if (symbol)
     {
       struct type *ftype = symbol->type ();
-      struct mdict_iterator miter;
       symbol->set_value_block (block);
       symbol->set_section_index (SECT_OFF_TEXT (m_objfile));
       block->set_function (symbol);
@@ -255,11 +254,10 @@  buildsym_compunit::finish_block_internal
 	     function's type.  Set that from the type of the
 	     parameter symbols.  */
 	  int nparams = 0, iparams;
-	  struct symbol *sym;
 
 	  /* Here we want to directly access the dictionary, because
 	     we haven't fully initialized the block yet.  */
-	  ALL_DICT_SYMBOLS (block->multidict (), miter, sym)
+	  for (struct symbol *sym : block->multidict_symbols ())
 	    {
 	      if (sym->is_argument ())
 		nparams++;
@@ -274,7 +272,7 @@  buildsym_compunit::finish_block_internal
 	      iparams = 0;
 	      /* Here we want to directly access the dictionary, because
 		 we haven't fully initialized the block yet.  */
-	      ALL_DICT_SYMBOLS (block->multidict (), miter, sym)
+	      for (struct symbol *sym : block->multidict_symbols ())
 		{
 		  if (iparams == nparams)
 		    break;
@@ -975,8 +973,6 @@  buildsym_compunit::end_compunit_symtab_with_blockvector
     for (block_i = 0; block_i < blockvector->num_blocks (); block_i++)
       {
 	struct block *block = blockvector->block (block_i);
-	struct symbol *sym;
-	struct mdict_iterator miter;
 
 	/* Inlined functions may have symbols not in the global or
 	   static symbol lists.  */
@@ -985,9 +981,10 @@  buildsym_compunit::end_compunit_symtab_with_blockvector
 	    block->function ()->set_symtab (symtab);
 
 	/* Note that we only want to fix up symbols from the local
-	   blocks, not blocks coming from included symtabs.  That is why
-	   we use ALL_DICT_SYMBOLS here and not a block iterator.  */
-	ALL_DICT_SYMBOLS (block->multidict (), miter, sym)
+	   blocks, not blocks coming from included symtabs.  That is
+	   why we use an mdict iterator here and not a block
+	   iterator.  */
+	for (struct symbol *sym : block->multidict_symbols ())
 	  if (sym->symtab () == NULL)
 	    sym->set_symtab (symtab);
       }
diff --git a/gdb/dictionary.h b/gdb/dictionary.h
index 9dc02c91e04..d982396cb31 100644
--- a/gdb/dictionary.h
+++ b/gdb/dictionary.h
@@ -164,16 +164,49 @@  extern struct symbol *mdict_iter_match_next (const lookup_name_info &name,
 
 extern int mdict_size (const struct multidictionary *mdict);
 
-/* Macro to loop through all symbols in a dictionary DICT, in no
-   particular order.  ITER is a struct dict_iterator (NOTE: __not__ a
-   struct dict_iterator *), and SYM points to the current symbol.
-
-   It's implemented as a single loop, so you can terminate the loop
-   early by a break if you desire.  */
-
-#define ALL_DICT_SYMBOLS(dict, iter, sym)			\
-	for ((sym) = mdict_iterator_first ((dict), &(iter));	\
-	     (sym);						\
-	     (sym) = mdict_iterator_next (&(iter)))
+/* An iterator that wraps an mdict_iterator.  The naming here is
+   unfortunate, but mdict_iterator was named before gdb switched to
+   C++.  */
+struct mdict_iterator_wrapper
+{
+  typedef mdict_iterator_wrapper self_type;
+  typedef struct symbol *value_type;
+
+  explicit mdict_iterator_wrapper (const struct multidictionary *mdict)
+    : m_sym (mdict_iterator_first (mdict, &m_iter))
+  {
+  }
+
+  mdict_iterator_wrapper ()
+    : m_sym (nullptr)
+  {
+  }
+
+  value_type operator* () const
+  {
+    return m_sym;
+  }
+
+  bool operator== (const self_type &other) const
+  {
+    return m_sym == other.m_sym;
+  }
+
+  bool operator!= (const self_type &other) const
+  {
+    return m_sym != other.m_sym;
+  }
+
+  self_type &operator++ ()
+  {
+    m_sym = mdict_iterator_next (&m_iter);
+    return *this;
+  }
+
+private:
+
+  struct symbol *m_sym;
+  struct mdict_iterator m_iter;
+};
 
 #endif /* DICTIONARY_H */
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index 3fefc4ad846..5ba5f0a616d 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -625,9 +625,6 @@  objfile_relocate1 (struct objfile *objfile,
 
       for (block *b : bv->blocks ())
 	{
-	  struct symbol *sym;
-	  struct mdict_iterator miter;
-
 	  b->set_start (b->start () + delta[block_line_section]);
 	  b->set_end (b->end () + delta[block_line_section]);
 
@@ -639,10 +636,8 @@  objfile_relocate1 (struct objfile *objfile,
 
 	  /* We only want to iterate over the local symbols, not any
 	     symbols in included symtabs.  */
-	  ALL_DICT_SYMBOLS (b->multidict (), miter, sym)
-	    {
-	      relocate_one_symbol (sym, objfile, delta);
-	    }
+	  for (struct symbol *sym : b->multidict_symbols ())
+	    relocate_one_symbol (sym, objfile, delta);
 	}
     }
 
diff --git a/gdb/symmisc.c b/gdb/symmisc.c
index fb8a3ebf602..1d838710a66 100644
--- a/gdb/symmisc.c
+++ b/gdb/symmisc.c
@@ -236,9 +236,7 @@  dump_symtab_1 (struct symtab *symtab, struct ui_file *outfile)
 {
   struct objfile *objfile = symtab->compunit ()->objfile ();
   struct gdbarch *gdbarch = objfile->arch ();
-  struct mdict_iterator miter;
   const struct linetable *l;
-  struct symbol *sym;
   int depth;
 
   gdb_printf (outfile, "\nSymtab for file %s at %s\n",
@@ -307,7 +305,7 @@  dump_symtab_1 (struct symtab *symtab, struct ui_file *outfile)
 	  /* Now print each symbol in this block (in no particular order, if
 	     we're using a hashtable).  Note that we only want this
 	     block, not any blocks from included symtabs.  */
-	  ALL_DICT_SYMBOLS (b->multidict (), miter, sym)
+	  for (struct symbol *sym : b->multidict_symbols ())
 	    {
 	      try
 		{