public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 04/12] Remove ALL_OBJFILES_SAFE
  2018-11-25 16:54 [PATCH 00/12] Remove some ALL_* iteration macros Tom Tromey
                   ` (4 preceding siblings ...)
  2018-11-25 16:54 ` [PATCH 11/12] Remove ALL_OBJSECTIONS Tom Tromey
@ 2018-11-25 16:54 ` Tom Tromey
  2018-11-25 16:54 ` [PATCH 12/12] Remove ALL_OBJFILE_PSYMTABS Tom Tromey
                   ` (8 subsequent siblings)
  14 siblings, 0 replies; 36+ messages in thread
From: Tom Tromey @ 2018-11-25 16:54 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This removes the ALL_OBJFILES_SAFE macro, replacing the uses with
ranged for loops.

gdb/ChangeLog
2018-11-25  Tom Tromey  <tom@tromey.com>

	* common/next-iterator.h (next_adapter): Add Iterator template
	parameter.
	* objfiles.h (ALL_OBJFILES_SAFE): Remove.
	(class all_objfiles_safe): New.
	* jit.c (jit_inferior_exit_hook): Use all_objfiles_safe.
	* objfiles.c (put_objfile_before): Update comment.
	(add_separate_debug_objfile): Likewise.
	(free_all_objfiles): Use all_objfiles_safe.
	(objfile_purge_solibs): Likewise.
---
 gdb/ChangeLog              | 12 ++++++++++++
 gdb/common/next-iterator.h |  4 ++--
 gdb/jit.c                  |  5 +----
 gdb/objfiles.c             | 26 ++++++++++----------------
 gdb/objfiles.h             | 32 ++++++++++++++++++++++++--------
 5 files changed, 49 insertions(+), 30 deletions(-)

diff --git a/gdb/common/next-iterator.h b/gdb/common/next-iterator.h
index 79241a82a4..27c6092f2c 100644
--- a/gdb/common/next-iterator.h
+++ b/gdb/common/next-iterator.h
@@ -72,7 +72,7 @@ private:
 
 /* A range adapter that allows iterating over a linked list.  */
 
-template<typename T>
+template<typename T, typename Iterator = next_iterator<T>>
 class next_adapter
 {
 public:
@@ -82,7 +82,7 @@ public:
   {
   }
 
-  using iterator = next_iterator<T>;
+  using iterator = Iterator;
 
   iterator begin () const
   {
diff --git a/gdb/jit.c b/gdb/jit.c
index 2f5f6f8ade..a217c3ca75 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -1393,10 +1393,7 @@ jit_breakpoint_re_set (void)
 static void
 jit_inferior_exit_hook (struct inferior *inf)
 {
-  struct objfile *objf;
-  struct objfile *temp;
-
-  ALL_OBJFILES_SAFE (objf, temp)
+  for (struct objfile *objf : all_objfiles_safe (current_program_space))
     {
       struct jit_objfile_data *objf_data
 	= (struct jit_objfile_data *) objfile_data (objf, jit_objfile_data);
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index 90d2502dde..19047694af 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -514,7 +514,7 @@ objfile_separate_debug_iterate (const struct objfile *parent,
 
 /* Put one object file before a specified on in the global list.
    This can be used to make sure an object file is destroyed before
-   another when using ALL_OBJFILES_SAFE to free all objfiles.  */
+   another when using all_objfiles_safe to free all objfiles.  */
 void
 put_objfile_before (struct objfile *objfile, struct objfile *before_this)
 {
@@ -587,7 +587,7 @@ add_separate_debug_objfile (struct objfile *objfile, struct objfile *parent)
   parent->separate_debug_objfile = objfile;
 
   /* Put the separate debug object before the normal one, this is so that
-     usage of the ALL_OBJFILES_SAFE macro will stay safe.  */
+     usage of all_objfiles_safe will stay safe.  */
   put_objfile_before (objfile, parent);
 }
 
@@ -730,17 +730,14 @@ objfile::~objfile ()
 void
 free_all_objfiles (void)
 {
-  struct objfile *objfile, *temp;
   struct so_list *so;
 
   /* Any objfile referencewould become stale.  */
   for (so = master_so_list (); so; so = so->next)
     gdb_assert (so->objfile == NULL);
 
-  ALL_OBJFILES_SAFE (objfile, temp)
-  {
+  for (struct objfile *objfile : all_objfiles_safe (current_program_space))
     delete objfile;
-  }
   clear_symtab_users (0);
 }
 \f
@@ -1047,17 +1044,14 @@ have_full_symbols (void)
 void
 objfile_purge_solibs (void)
 {
-  struct objfile *objf;
-  struct objfile *temp;
-
-  ALL_OBJFILES_SAFE (objf, temp)
-  {
-    /* We assume that the solib package has been purged already, or will
-       be soon.  */
+  for (struct objfile *objf : all_objfiles_safe (current_program_space))
+    {
+      /* We assume that the solib package has been purged already, or will
+	 be soon.  */
 
-    if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SHARED))
-      delete objf;
-  }
+      if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SHARED))
+	delete objf;
+    }
 }
 
 
diff --git a/gdb/objfiles.h b/gdb/objfiles.h
index dee64c816a..1d310e5584 100644
--- a/gdb/objfiles.h
+++ b/gdb/objfiles.h
@@ -29,6 +29,7 @@
 #include "gdb_bfd.h"
 #include <vector>
 #include "common/next-iterator.h"
+#include "common/safe-iterator.h"
 
 struct bcache;
 struct htab;
@@ -581,21 +582,36 @@ public:
   }
 };
 
+/* An iterarable object that can be used to iterate over all
+   objfiles.  The basic use is in a foreach, like:
 
-/* Traverse all object files in the current program space.
-   ALL_OBJFILES_SAFE works even if you delete the objfile during the
-   traversal.  */
+   for (struct objfile *objf : all_objfiles_safe (pspace)) { ... }
+
+   This variant uses a basic_safe_iterator so that objfiles can be
+   deleted during iteration.  */
+
+class all_objfiles_safe
+  : public next_adapter<struct objfile,
+			basic_safe_iterator<next_iterator<objfile>>>
+{
+public:
+
+  explicit all_objfiles_safe (struct program_space *pspace)
+    : next_adapter<struct objfile,
+		   basic_safe_iterator<next_iterator<objfile>>>
+        (pspace->objfiles)
+  {
+  }
+};
+
+
+/* Traverse all object files in the current program space.  */
 
 #define ALL_OBJFILES(obj)			    \
   for ((obj) = current_program_space->objfiles; \
        (obj) != NULL;				    \
        (obj) = (obj)->next)
 
-#define ALL_OBJFILES_SAFE(obj,nxt)			\
-  for ((obj) = current_program_space->objfiles;	\
-       (obj) != NULL? ((nxt)=(obj)->next,1) :0;	\
-       (obj) = (nxt))
-
 /* Traverse all symtabs in one objfile.  */
 
 #define ALL_OBJFILE_FILETABS(objfile, cu, s) \
-- 
2.17.2

^ permalink raw reply	[flat|nested] 36+ messages in thread

* [PATCH 01/12] Introduce all_objfiles and next_iterator
  2018-11-25 16:54 [PATCH 00/12] Remove some ALL_* iteration macros Tom Tromey
                   ` (6 preceding siblings ...)
  2018-11-25 16:54 ` [PATCH 12/12] Remove ALL_OBJFILE_PSYMTABS Tom Tromey
@ 2018-11-25 16:54 ` Tom Tromey
  2018-11-25 16:54 ` [PATCH 08/12] Remove ALL_COMPUNIT_FILETABS Tom Tromey
                   ` (6 subsequent siblings)
  14 siblings, 0 replies; 36+ messages in thread
From: Tom Tromey @ 2018-11-25 16:54 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This introduces an iterable object which can be used to iterate over
objfiles.  It also introduces a generic "next_iterator", which can be
used to iterate over types that have a "next" field.

gdb/ChangeLog
2018-11-25  Tom Tromey  <tom@tromey.com>

	* common/next-iterator.h: New file.
	* objfiles.h (class all_objfiles): New.
	(struct objfile_iterator): New.
---
 gdb/ChangeLog              |   6 +++
 gdb/common/next-iterator.h | 102 +++++++++++++++++++++++++++++++++++++
 gdb/objfiles.h             |  17 +++++++
 3 files changed, 125 insertions(+)
 create mode 100644 gdb/common/next-iterator.h

diff --git a/gdb/common/next-iterator.h b/gdb/common/next-iterator.h
new file mode 100644
index 0000000000..79241a82a4
--- /dev/null
+++ b/gdb/common/next-iterator.h
@@ -0,0 +1,102 @@
+/* A "next" iterator for GDB, the GNU debugger.
+   Copyright (C) 2018 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef NEXT_ITERATOR_H
+#define NEXT_ITERATOR_H
+
+/* An iterator that uses the 'next' field of a type to iterate.  This
+   can be used with various GDB types that are stored as linked
+   lists.  */
+
+template<typename T>
+struct next_iterator
+{
+  typedef next_iterator self_type;
+  typedef T *value_type;
+  typedef T *&reference;
+  typedef T **pointer;
+  typedef std::forward_iterator_tag iterator_category;
+  typedef int difference_type;
+
+  explicit next_iterator (T *item)
+    : m_item (item)
+  {
+  }
+
+  /* Create a one-past-the-end iterator.  */
+  next_iterator ()
+    : m_item (nullptr)
+  {
+  }
+
+  value_type operator* () const
+  {
+    return m_item;
+  }
+
+  bool operator== (const self_type &other) const
+  {
+    return m_item == other.m_item;
+  }
+
+  bool operator!= (const self_type &other) const
+  {
+    return m_item != other.m_item;
+  }
+
+  self_type &operator++ ()
+  {
+    m_item = m_item->next;
+    return *this;
+  }
+
+private:
+
+  T *m_item;
+};
+
+/* A range adapter that allows iterating over a linked list.  */
+
+template<typename T>
+class next_adapter
+{
+public:
+
+  explicit next_adapter (T *item)
+    : m_item (item)
+  {
+  }
+
+  using iterator = next_iterator<T>;
+
+  iterator begin () const
+  {
+    return iterator (m_item);
+  }
+
+  iterator end () const
+  {
+    return iterator ();
+  }
+
+private:
+
+  T *m_item;
+};
+
+#endif /* NEXT_ITERATOR_H */
diff --git a/gdb/objfiles.h b/gdb/objfiles.h
index 7a9087b015..9005be2795 100644
--- a/gdb/objfiles.h
+++ b/gdb/objfiles.h
@@ -28,6 +28,7 @@
 #include "registry.h"
 #include "gdb_bfd.h"
 #include <vector>
+#include "common/next-iterator.h"
 
 struct bcache;
 struct htab;
@@ -565,6 +566,22 @@ extern void default_iterate_over_objfiles_in_search_order
    void *cb_data, struct objfile *current_objfile);
 \f
 
+/* An iterarable object that can be used to iterate over all
+   objfiles.  The basic use is in a foreach, like:
+
+   for (struct objfile *objf : all_objfiles (pspace)) { ... }  */
+
+class all_objfiles : public next_adapter<struct objfile>
+{
+public:
+
+  explicit all_objfiles (struct program_space *pspace)
+    : next_adapter<struct objfile> (pspace->objfiles)
+  {
+  }
+};
+
+
 /* Traverse all object files in the current program space.
    ALL_OBJFILES_SAFE works even if you delete the objfile during the
    traversal.  */
-- 
2.17.2

^ permalink raw reply	[flat|nested] 36+ messages in thread

* [PATCH 09/12] Remove ALL_OBJFILE_FILETABS
  2018-11-25 16:54 [PATCH 00/12] Remove some ALL_* iteration macros Tom Tromey
@ 2018-11-25 16:54 ` Tom Tromey
  2018-11-25 16:54 ` [PATCH 03/12] Remove most uses of ALL_OBJFILES Tom Tromey
                   ` (13 subsequent siblings)
  14 siblings, 0 replies; 36+ messages in thread
From: Tom Tromey @ 2018-11-25 16:54 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This removes ALL_OBJFILE_FILETABS, replacing its uses with ranged for
loops.

gdb/ChangeLog
2018-11-25  Tom Tromey  <tom@tromey.com>

	* symmisc.c (print_objfile_statistics, dump_objfile)
	(maintenance_print_symbols): Use compunit_filetabs.
	* source.c (forget_cached_source_info_for_objfile): Use
	compunit_filetabs.
	* objfiles.h (ALL_OBJFILE_FILETABS): Remove.
	(ALL_FILETABS): Use compunit_filetabs.
	* objfiles.c (objfile_relocate1): Use compunit_filetabs.
	* coffread.c (coff_symtab_read): Use compunit_filetabs.
---
 gdb/ChangeLog  | 11 +++++++++
 gdb/coffread.c |  5 ++--
 gdb/objfiles.c | 25 ++++++++++---------
 gdb/objfiles.h | 13 +++-------
 gdb/source.c   | 27 ++++++++++----------
 gdb/symmisc.c  | 67 ++++++++++++++++++++++++++------------------------
 6 files changed, 80 insertions(+), 68 deletions(-)

diff --git a/gdb/coffread.c b/gdb/coffread.c
index 82e6767ae1..741fad69fa 100644
--- a/gdb/coffread.c
+++ b/gdb/coffread.c
@@ -1201,8 +1201,9 @@ coff_symtab_read (minimal_symbol_reader &reader,
   /* Patch up any opaque types (references to types that are not defined
      in the file where they are referenced, e.g. "struct foo *bar").  */
   {
-    ALL_OBJFILE_FILETABS (objfile, cu, s)
-      patch_opaque_types (s);
+    for (struct compunit_symtab *cu : objfile_compunits (objfile))
+      for (struct symtab *s : compunit_filetabs (cu))
+	patch_opaque_types (s);
   }
 
   coffread_objfile = NULL;
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index 3429fdc9d8..9f54b8edf5 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -788,20 +788,21 @@ objfile_relocate1 (struct objfile *objfile,
 
   /* OK, get all the symtabs.  */
   {
-    ALL_OBJFILE_FILETABS (objfile, cust, s)
-    {
-      struct linetable *l;
-
-      /* First the line table.  */
-      l = SYMTAB_LINETABLE (s);
-      if (l)
+    for (struct compunit_symtab *cust : objfile_compunits (objfile))
+      for (struct symtab *s : compunit_filetabs (cust))
 	{
-	  for (int i = 0; i < l->nitems; ++i)
-	    l->item[i].pc += ANOFFSET (delta,
-				       COMPUNIT_BLOCK_LINE_SECTION
-					 (cust));
+	  struct linetable *l;
+
+	  /* First the line table.  */
+	  l = SYMTAB_LINETABLE (s);
+	  if (l)
+	    {
+	      for (int i = 0; i < l->nitems; ++i)
+		l->item[i].pc += ANOFFSET (delta,
+					   COMPUNIT_BLOCK_LINE_SECTION
+					   (cust));
+	    }
 	}
-    }
 
     for (struct compunit_symtab *cust : objfile_compunits (objfile))
       {
diff --git a/gdb/objfiles.h b/gdb/objfiles.h
index 4bbe7ea613..94394e79ae 100644
--- a/gdb/objfiles.h
+++ b/gdb/objfiles.h
@@ -612,12 +612,6 @@ public:
        (obj) != NULL;				    \
        (obj) = (obj)->next)
 
-/* Traverse all symtabs in one objfile.  */
-
-#define ALL_OBJFILE_FILETABS(objfile, cu, s) \
-  for (struct compunit_symtab *cu : objfile_compunits (objfile)) \
-    for (struct symtab *s : compunit_filetabs (cu))
-
 /* A range adapter that makes it possible to iterate over all
    compunits in one objfile.  */
 
@@ -714,9 +708,10 @@ private:
 /* Traverse all symtabs in all objfiles in the current symbol
    space.  */
 
-#define ALL_FILETABS(objfile, ps, s)		\
-  ALL_OBJFILES (objfile)			\
-    ALL_OBJFILE_FILETABS (objfile, ps, s)
+#define ALL_FILETABS(objfile, ps, s)					\
+  ALL_OBJFILES (objfile)						\
+    for (struct compunit_symtab *ps : objfile_compunits (objfile))	\
+      for (struct symtab *s : compunit_filetabs (cu))
 
 #define ALL_OBJFILE_OSECTIONS(objfile, osect)	\
   for (osect = objfile->sections; osect < objfile->sections_end; osect++) \
diff --git a/gdb/source.c b/gdb/source.c
index 65b9bf554d..6111453eb0 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -355,19 +355,20 @@ show_directories_command (struct ui_file *file, int from_tty,
 void
 forget_cached_source_info_for_objfile (struct objfile *objfile)
 {
-  ALL_OBJFILE_FILETABS (objfile, cu, s)
-    {
-      if (s->line_charpos != NULL)
-	{
-	  xfree (s->line_charpos);
-	  s->line_charpos = NULL;
-	}
-      if (s->fullname != NULL)
-	{
-	  xfree (s->fullname);
-	  s->fullname = NULL;
-	}
-    }
+  for (struct compunit_symtab *cu : objfile_compunits (objfile))
+    for (struct symtab *s : compunit_filetabs (cu))
+      {
+	if (s->line_charpos != NULL)
+	  {
+	    xfree (s->line_charpos);
+	    s->line_charpos = NULL;
+	  }
+	if (s->fullname != NULL)
+	  {
+	    xfree (s->fullname);
+	    s->fullname = NULL;
+	  }
+      }
 
   if (objfile->sf)
     objfile->sf->qf->forget_cached_source_info (objfile);
diff --git a/gdb/symmisc.c b/gdb/symmisc.c
index e9ab1c95b8..148e839599 100644
--- a/gdb/symmisc.c
+++ b/gdb/symmisc.c
@@ -107,12 +107,13 @@ print_objfile_statistics (void)
       if (objfile->sf)
 	objfile->sf->qf->print_stats (objfile);
       i = linetables = 0;
-      ALL_OBJFILE_FILETABS (objfile, cu, s)
-	{
-	  i++;
-	  if (SYMTAB_LINETABLE (s) != NULL)
-	    linetables++;
-	}
+      for (struct compunit_symtab *cu : objfile_compunits (objfile))
+	for (struct symtab *s : compunit_filetabs (cu))
+	  {
+	    i++;
+	    if (SYMTAB_LINETABLE (s) != NULL)
+	      linetables++;
+	  }
       blockvectors = std::distance (objfile_compunits (objfile).begin (),
 				    objfile_compunits (objfile).end ());
       printf_filtered (_("  Number of symbol tables: %d\n"), i);
@@ -157,17 +158,18 @@ dump_objfile (struct objfile *objfile)
   if (objfile->compunit_symtabs != NULL)
     {
       printf_filtered ("Symtabs:\n");
-      ALL_OBJFILE_FILETABS (objfile, cust, symtab)
-	{
-	  printf_filtered ("%s at ", symtab_to_filename_for_display (symtab));
-	  gdb_print_host_address (symtab, gdb_stdout);
-	  printf_filtered (", ");
-	  if (SYMTAB_OBJFILE (symtab) != objfile)
-	    {
-	      printf_filtered ("NOT ON CHAIN!  ");
-	    }
-	  wrap_here ("  ");
-	}
+      for (struct compunit_symtab *cu : objfile_compunits (objfile))
+	for (struct symtab *symtab : compunit_filetabs (cu))
+	  {
+	    printf_filtered ("%s at ", symtab_to_filename_for_display (symtab));
+	    gdb_print_host_address (symtab, gdb_stdout);
+	    printf_filtered (", ");
+	    if (SYMTAB_OBJFILE (symtab) != objfile)
+	      {
+		printf_filtered ("NOT ON CHAIN!  ");
+	      }
+	    wrap_here ("  ");
+	  }
       printf_filtered ("\n\n");
     }
 }
@@ -477,22 +479,23 @@ maintenance_print_symbols (const char *args, int from_tty)
 	  if (!print_for_objfile)
 	    continue;
 
-	  ALL_OBJFILE_FILETABS (objfile, cu, s)
-	    {
-	      int print_for_source = 0;
+	  for (struct compunit_symtab *cu : objfile_compunits (objfile))
+	    for (struct symtab *s : compunit_filetabs (cu))
+	      {
+		int print_for_source = 0;
 
-	      QUIT;
-	      if (source_arg != NULL)
-		{
-		  print_for_source
-		    = compare_filenames_for_search
-		        (symtab_to_filename_for_display (s), source_arg);
-		  found = 1;
-		}
-	      if (source_arg == NULL
-		  || print_for_source)
-		dump_symtab (s, outfile);
-	    }
+		QUIT;
+		if (source_arg != NULL)
+		  {
+		    print_for_source
+		      = compare_filenames_for_search
+		      (symtab_to_filename_for_display (s), source_arg);
+		    found = 1;
+		  }
+		if (source_arg == NULL
+		    || print_for_source)
+		  dump_symtab (s, outfile);
+	      }
 	}
 
       if (source_arg != NULL && !found)
-- 
2.17.2

^ permalink raw reply	[flat|nested] 36+ messages in thread

* [PATCH 08/12] Remove ALL_COMPUNIT_FILETABS
  2018-11-25 16:54 [PATCH 00/12] Remove some ALL_* iteration macros Tom Tromey
                   ` (7 preceding siblings ...)
  2018-11-25 16:54 ` [PATCH 01/12] Introduce all_objfiles and next_iterator Tom Tromey
@ 2018-11-25 16:54 ` Tom Tromey
  2018-11-25 16:54 ` [PATCH 05/12] Remove ALL_MSYMBOLS and ALL_OBJFILE_MSYMBOLS Tom Tromey
                   ` (5 subsequent siblings)
  14 siblings, 0 replies; 36+ messages in thread
From: Tom Tromey @ 2018-11-25 16:54 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This removes ALL_COMPUNIT_FILETABS, replacing its uses with ranged for
loops.

Because this is still used in the ALL_OBJFILE_FILETABS macro, in some
places a declaration had to be removed or renamed to avoid shadowing.

gdb/ChangeLog
2018-11-25  Tom Tromey  <tom@tromey.com>

	* symtab.h (ALL_COMPUNIT_FILETABS): Remove.
	(compunit_filetabs): New.
	* symtab.c (iterate_over_some_symtabs, find_pc_sect_line): Use
	compunit_filetabs.
	(info_sources_command, make_source_files_completion_list): Remove
	declaration.
	* symmisc.c (print_objfile_statistics, dump_objfile)
	(maintenance_print_symbols): Remove declaration.
	(maintenance_info_symtabs): Use compunit_filetabs.
	(maintenance_info_line_tables): Likewise.
	* source.c (select_source_symtab): Change local variable name.
	(forget_cached_source_info_for_objfile): Remove declaration.
	* objfiles.h (ALL_OBJFILE_FILETABS): Use compunit_filetabs.
	* objfiles.c (objfile_relocate1): Remove declaration.
	* mi/mi-cmd-file.c (mi_cmd_file_list_exec_source_files): Remove
	declaration.
	* maint.c (count_symtabs_and_blocks): Use compunit_filetabs.
	* coffread.c (coff_symtab_read): Remove declaration.
	* buildsym.c (buildsym_compunit::end_symtab_with_blockvector): Use
	compunit_filetabs.
---
 gdb/ChangeLog        | 23 +++++++++++++++++++++++
 gdb/buildsym.c       |  7 +++----
 gdb/coffread.c       |  2 --
 gdb/maint.c          |  5 ++---
 gdb/mi/mi-cmd-file.c |  1 -
 gdb/objfiles.c       |  2 --
 gdb/objfiles.h       |  2 +-
 gdb/source.c         |  8 +++-----
 gdb/symmisc.c        | 12 ++----------
 gdb/symtab.c         | 10 ++--------
 gdb/symtab.h         | 13 ++++++++++---
 11 files changed, 46 insertions(+), 39 deletions(-)

diff --git a/gdb/buildsym.c b/gdb/buildsym.c
index a9d2698584..5de2883f4e 100644
--- a/gdb/buildsym.c
+++ b/gdb/buildsym.c
@@ -927,7 +927,6 @@ buildsym_compunit::end_symtab_with_blockvector (struct block *static_block,
 						int section, int expandable)
 {
   struct compunit_symtab *cu = m_compunit_symtab;
-  struct symtab *symtab;
   struct blockvector *blockvector;
   struct subfile *subfile;
   CORE_ADDR end_addr;
@@ -979,7 +978,7 @@ buildsym_compunit::end_symtab_with_blockvector (struct block *static_block,
       /* Allocate a symbol table if necessary.  */
       if (subfile->symtab == NULL)
 	subfile->symtab = allocate_symtab (cu, subfile->name);
-      symtab = subfile->symtab;
+      struct symtab *symtab = subfile->symtab;
 
       /* Fill in its components.  */
 
@@ -1011,7 +1010,7 @@ buildsym_compunit::end_symtab_with_blockvector (struct block *static_block,
 
     main_symtab = m_main_subfile->symtab;
     prev_symtab = NULL;
-    ALL_COMPUNIT_FILETABS (cu, symtab)
+    for (struct symtab *symtab : compunit_filetabs (cu))
       {
 	if (symtab == main_symtab)
 	  {
@@ -1061,7 +1060,7 @@ buildsym_compunit::end_symtab_with_blockvector (struct block *static_block,
     int block_i;
 
     /* The main source file's symtab.  */
-    symtab = COMPUNIT_FILETABS (cu);
+    struct symtab *symtab = COMPUNIT_FILETABS (cu);
 
     for (block_i = 0; block_i < BLOCKVECTOR_NBLOCKS (blockvector); block_i++)
       {
diff --git a/gdb/coffread.c b/gdb/coffread.c
index 81d00b5248..82e6767ae1 100644
--- a/gdb/coffread.c
+++ b/gdb/coffread.c
@@ -1201,8 +1201,6 @@ coff_symtab_read (minimal_symbol_reader &reader,
   /* Patch up any opaque types (references to types that are not defined
      in the file where they are referenced, e.g. "struct foo *bar").  */
   {
-    struct symtab *s;
-
     ALL_OBJFILE_FILETABS (objfile, cu, s)
       patch_opaque_types (s);
   }
diff --git a/gdb/maint.c b/gdb/maint.c
index 5cd5b01936..df4bf444ff 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -762,7 +762,6 @@ static void
 count_symtabs_and_blocks (int *nr_symtabs_ptr, int *nr_compunit_symtabs_ptr,
 			  int *nr_blocks_ptr)
 {
-  struct symtab *s;
   int nr_symtabs = 0;
   int nr_compunit_symtabs = 0;
   int nr_blocks = 0;
@@ -777,8 +776,8 @@ count_symtabs_and_blocks (int *nr_symtabs_ptr, int *nr_compunit_symtabs_ptr,
 	  {
 	    ++nr_compunit_symtabs;
 	    nr_blocks += BLOCKVECTOR_NBLOCKS (COMPUNIT_BLOCKVECTOR (cu));
-	    ALL_COMPUNIT_FILETABS (cu, s)
-	      ++nr_symtabs;
+	    nr_symtabs += std::distance (compunit_filetabs (cu).begin (),
+					 compunit_filetabs (cu).end ());
 	  }
     }
 
diff --git a/gdb/mi/mi-cmd-file.c b/gdb/mi/mi-cmd-file.c
index bbf3ed30ac..a74846e21b 100644
--- a/gdb/mi/mi-cmd-file.c
+++ b/gdb/mi/mi-cmd-file.c
@@ -84,7 +84,6 @@ void
 mi_cmd_file_list_exec_source_files (const char *command, char **argv, int argc)
 {
   struct ui_out *uiout = current_uiout;
-  struct symtab *s;
   struct objfile *objfile;
 
   if (!mi_valid_noargs ("-file-list-exec-source-files", argc, argv))
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index abfe425358..3429fdc9d8 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -788,8 +788,6 @@ objfile_relocate1 (struct objfile *objfile,
 
   /* OK, get all the symtabs.  */
   {
-    struct symtab *s;
-
     ALL_OBJFILE_FILETABS (objfile, cust, s)
     {
       struct linetable *l;
diff --git a/gdb/objfiles.h b/gdb/objfiles.h
index 165a6e3cf1..4bbe7ea613 100644
--- a/gdb/objfiles.h
+++ b/gdb/objfiles.h
@@ -616,7 +616,7 @@ public:
 
 #define ALL_OBJFILE_FILETABS(objfile, cu, s) \
   for (struct compunit_symtab *cu : objfile_compunits (objfile)) \
-    ALL_COMPUNIT_FILETABS (cu, s)
+    for (struct symtab *s : compunit_filetabs (cu))
 
 /* A range adapter that makes it possible to iterate over all
    compunits in one objfile.  */
diff --git a/gdb/source.c b/gdb/source.c
index 81be5f5f60..65b9bf554d 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -276,16 +276,16 @@ select_source_symtab (struct symtab *s)
 
   current_source_line = 1;
 
-  ALL_FILETABS (ofp, cu, s)
+  ALL_FILETABS (ofp, cu, symtab)
     {
-      const char *name = s->filename;
+      const char *name = symtab->filename;
       int len = strlen (name);
 
       if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
 			|| strcmp (name, "<<C++-namespaces>>") == 0)))
 	{
 	  current_source_pspace = current_program_space;
-	  current_source_symtab = s;
+	  current_source_symtab = symtab;
 	}
     }
 
@@ -355,8 +355,6 @@ show_directories_command (struct ui_file *file, int from_tty,
 void
 forget_cached_source_info_for_objfile (struct objfile *objfile)
 {
-  struct symtab *s;
-
   ALL_OBJFILE_FILETABS (objfile, cu, s)
     {
       if (s->line_charpos != NULL)
diff --git a/gdb/symmisc.c b/gdb/symmisc.c
index 60aef3c8bb..e9ab1c95b8 100644
--- a/gdb/symmisc.c
+++ b/gdb/symmisc.c
@@ -82,7 +82,6 @@ void
 print_objfile_statistics (void)
 {
   struct program_space *pspace;
-  struct symtab *s;
   int i, linetables, blockvectors;
 
   ALL_PSPACES (pspace)
@@ -144,8 +143,6 @@ print_objfile_statistics (void)
 static void
 dump_objfile (struct objfile *objfile)
 {
-  struct symtab *symtab;
-
   printf_filtered ("\nObject file %s:  ", objfile_name (objfile));
   printf_filtered ("Objfile at ");
   gdb_print_host_address (objfile, gdb_stdout);
@@ -467,7 +464,6 @@ maintenance_print_symbols (const char *args, int from_tty)
     }
   else
     {
-      struct symtab *s;
       int found = 0;
 
       for (struct objfile *objfile : all_objfiles (current_program_space))
@@ -772,8 +768,6 @@ maintenance_info_symtabs (const char *regexp, int from_tty)
   ALL_PSPACES (pspace)
     for (struct objfile *objfile : all_objfiles (pspace))
       {
-	struct symtab *symtab;
-
 	/* We don't want to print anything for this objfile until we
 	   actually find a symtab whose name matches.  */
 	int printed_objfile_start = 0;
@@ -782,7 +776,7 @@ maintenance_info_symtabs (const char *regexp, int from_tty)
 	  {
 	    int printed_compunit_symtab_start = 0;
 
-	    ALL_COMPUNIT_FILETABS (cust, symtab)
+	    for (struct symtab *symtab : compunit_filetabs (cust))
 	      {
 		QUIT;
 
@@ -1026,11 +1020,9 @@ maintenance_info_line_tables (const char *regexp, int from_tty)
   ALL_PSPACES (pspace)
     for (struct objfile *objfile : all_objfiles (pspace))
       {
-	struct symtab *symtab;
-
 	for (struct compunit_symtab *cust : objfile_compunits (objfile))
 	  {
-	    ALL_COMPUNIT_FILETABS (cust, symtab)
+	    for (struct symtab *symtab : compunit_filetabs (cust))
 	      {
 		QUIT;
 
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 86fa99b057..c16fc307ad 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -405,12 +405,11 @@ iterate_over_some_symtabs (const char *name,
 			   gdb::function_view<bool (symtab *)> callback)
 {
   struct compunit_symtab *cust;
-  struct symtab *s;
   const char* base_name = lbasename (name);
 
   for (cust = first; cust != NULL && cust != after_last; cust = cust->next)
     {
-      ALL_COMPUNIT_FILETABS (cust, s)
+      for (struct symtab *s : compunit_filetabs (cust))
 	{
 	  if (compare_filenames_for_search (s->filename, name))
 	    {
@@ -3044,7 +3043,6 @@ struct symtab_and_line
 find_pc_sect_line (CORE_ADDR pc, struct obj_section *section, int notcurrent)
 {
   struct compunit_symtab *cust;
-  struct symtab *iter_s;
   struct linetable *l;
   int len;
   struct linetable_entry *item;
@@ -3181,7 +3179,7 @@ find_pc_sect_line (CORE_ADDR pc, struct obj_section *section, int notcurrent)
      They all have the same apriori range, that we found was right;
      but they have different line tables.  */
 
-  ALL_COMPUNIT_FILETABS (cust, iter_s)
+  for (struct symtab *iter_s : compunit_filetabs (cust))
     {
       /* Find the best line in this symtab.  */
       l = SYMTAB_LINETABLE (iter_s);
@@ -3343,8 +3341,6 @@ find_line_symtab (struct symtab *symtab, int line,
          BEST_INDEX and BEST_LINETABLE identify the item for it.  */
       int best;
 
-      struct symtab *s;
-
       if (best_index >= 0)
 	best = best_linetable->item[best_index].line;
       else
@@ -4178,7 +4174,6 @@ output_partial_symbol_filename (const char *filename, const char *fullname,
 static void
 info_sources_command (const char *ignore, int from_tty)
 {
-  struct symtab *s;
   struct objfile *objfile;
   struct output_source_filename_data data;
 
@@ -5568,7 +5563,6 @@ maybe_add_partial_symtab_filename (const char *filename, const char *fullname,
 completion_list
 make_source_files_completion_list (const char *text, const char *word)
 {
-  struct symtab *s;
   struct objfile *objfile;
   size_t text_len = strlen (text);
   completion_list list;
diff --git a/gdb/symtab.h b/gdb/symtab.h
index e0b870135b..8383addeec 100644
--- a/gdb/symtab.h
+++ b/gdb/symtab.h
@@ -29,6 +29,7 @@
 #include "common/enum-flags.h"
 #include "common/function-view.h"
 #include "common/gdb_optional.h"
+#include "common/next-iterator.h"
 #include "completer.h"
 
 /* Opaque declarations.  */
@@ -1485,10 +1486,16 @@ struct compunit_symtab
 #define COMPUNIT_CALL_SITE_HTAB(cust) ((cust)->call_site_htab)
 #define COMPUNIT_MACRO_TABLE(cust) ((cust)->macro_table)
 
-/* Iterate over all file tables (struct symtab) within a compunit.  */
+/* A range adapter to allowing iterating over all the file tables
+   within a compunit.  */
 
-#define ALL_COMPUNIT_FILETABS(cu, s) \
-  for ((s) = (cu) -> filetabs; (s) != NULL; (s) = (s) -> next)
+struct compunit_filetabs : public next_adapter<struct symtab>
+{
+  compunit_filetabs (struct compunit_symtab *cu)
+    : next_adapter<struct symtab> (cu->filetabs)
+  {
+  }
+};
 
 /* Return the primary symtab of CUST.  */
 
-- 
2.17.2

^ permalink raw reply	[flat|nested] 36+ messages in thread

* [PATCH 07/12] Remove ALL_COMPUNITS
  2018-11-25 16:54 [PATCH 00/12] Remove some ALL_* iteration macros Tom Tromey
  2018-11-25 16:54 ` [PATCH 09/12] Remove ALL_OBJFILE_FILETABS Tom Tromey
  2018-11-25 16:54 ` [PATCH 03/12] Remove most uses of ALL_OBJFILES Tom Tromey
@ 2018-11-25 16:54 ` Tom Tromey
  2018-11-25 16:54 ` [PATCH 10/12] Remove ALL_OBJFILES and ALL_FILETABS Tom Tromey
                   ` (11 subsequent siblings)
  14 siblings, 0 replies; 36+ messages in thread
From: Tom Tromey @ 2018-11-25 16:54 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This removes the ALL_COMPUNITS, replacing its uses with two nested
ranged for loops.

gdb/ChangeLog
2018-11-25  Tom Tromey  <tom@tromey.com>

	* symtab.c (lookup_objfile_from_block)
	(find_pc_sect_compunit_symtab, search_symbols)
	(default_collect_symbol_completion_matches_break_on): Use
	objfile_compunits.
	* objfiles.h (ALL_COMPUNITS): Remove.
	* maint.c (count_symtabs_and_blocks): Use objfile_compunits.
	* cp-support.c (add_symbol_overload_list_qualified): Use
	objfile_compunits.
	* ada-lang.c (ada_collect_symbol_completion_matches)
	(ada_add_global_exceptions): Use objfile_compunits.
---
 gdb/ChangeLog    |  13 +++++
 gdb/ada-lang.c   | 106 ++++++++++++++++++++--------------------
 gdb/cp-support.c |  33 +++++++------
 gdb/maint.c      |  16 +++---
 gdb/objfiles.h   |   6 ---
 gdb/symtab.c     | 125 +++++++++++++++++++++++------------------------
 6 files changed, 152 insertions(+), 147 deletions(-)

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 1a1db17421..64f3b53303 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -6461,41 +6461,42 @@ ada_collect_symbol_completion_matches (completion_tracker &tracker,
   /* Go through the symtabs and check the externs and statics for
      symbols which match.  */
 
-  struct objfile *objfile;
-  ALL_COMPUNITS (objfile, s)
-  {
-    QUIT;
-    b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (s), GLOBAL_BLOCK);
-    ALL_BLOCK_SYMBOLS (b, iter, sym)
-    {
-      if (completion_skip_symbol (mode, sym))
-	continue;
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    for (struct compunit_symtab *s : objfile_compunits (objfile))
+      {
+	QUIT;
+	b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (s), GLOBAL_BLOCK);
+	ALL_BLOCK_SYMBOLS (b, iter, sym)
+	  {
+	    if (completion_skip_symbol (mode, sym))
+	      continue;
 
-      completion_list_add_name (tracker,
-				SYMBOL_LANGUAGE (sym),
-				SYMBOL_LINKAGE_NAME (sym),
-				lookup_name, text, word);
-    }
-  }
+	    completion_list_add_name (tracker,
+				      SYMBOL_LANGUAGE (sym),
+				      SYMBOL_LINKAGE_NAME (sym),
+				      lookup_name, text, word);
+	  }
+      }
 
-  ALL_COMPUNITS (objfile, s)
-  {
-    QUIT;
-    b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (s), STATIC_BLOCK);
-    /* Don't do this block twice.  */
-    if (b == surrounding_static_block)
-      continue;
-    ALL_BLOCK_SYMBOLS (b, iter, sym)
-    {
-      if (completion_skip_symbol (mode, sym))
-	continue;
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    for (struct compunit_symtab *s : objfile_compunits (objfile))
+      {
+	QUIT;
+	b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (s), STATIC_BLOCK);
+	/* Don't do this block twice.  */
+	if (b == surrounding_static_block)
+	  continue;
+	ALL_BLOCK_SYMBOLS (b, iter, sym)
+	  {
+	    if (completion_skip_symbol (mode, sym))
+	      continue;
 
-      completion_list_add_name (tracker,
-				SYMBOL_LANGUAGE (sym),
-				SYMBOL_LINKAGE_NAME (sym),
-				lookup_name, text, word);
-    }
-  }
+	    completion_list_add_name (tracker,
+				      SYMBOL_LANGUAGE (sym),
+				      SYMBOL_LINKAGE_NAME (sym),
+				      lookup_name, text, word);
+	  }
+      }
 }
 
                                 /* Field Access */
@@ -13544,8 +13545,6 @@ static void
 ada_add_global_exceptions (compiled_regex *preg,
 			   std::vector<ada_exc_info> *exceptions)
 {
-  struct objfile *objfile;
-
   /* In Ada, the symbol "search name" is a linkage name, whereas the
      regular expression used to do the matching refers to the natural
      name.  So match against the decoded name.  */
@@ -13559,28 +13558,29 @@ ada_add_global_exceptions (compiled_regex *preg,
 			   NULL,
 			   VARIABLES_DOMAIN);
 
-  ALL_COMPUNITS (objfile, s)
-    {
-      const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (s);
-      int i;
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    for (struct compunit_symtab *s : objfile_compunits (objfile))
+      {
+	const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (s);
+	int i;
 
-      for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
-	{
-	  struct block *b = BLOCKVECTOR_BLOCK (bv, i);
-	  struct block_iterator iter;
-	  struct symbol *sym;
+	for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
+	  {
+	    struct block *b = BLOCKVECTOR_BLOCK (bv, i);
+	    struct block_iterator iter;
+	    struct symbol *sym;
 
-	  ALL_BLOCK_SYMBOLS (b, iter, sym)
-	    if (ada_is_non_standard_exception_sym (sym)
-		&& name_matches_regex (SYMBOL_NATURAL_NAME (sym), preg))
-	      {
-		struct ada_exc_info info
-		  = {SYMBOL_PRINT_NAME (sym), SYMBOL_VALUE_ADDRESS (sym)};
+	    ALL_BLOCK_SYMBOLS (b, iter, sym)
+	      if (ada_is_non_standard_exception_sym (sym)
+		  && name_matches_regex (SYMBOL_NATURAL_NAME (sym), preg))
+		{
+		  struct ada_exc_info info
+		    = {SYMBOL_PRINT_NAME (sym), SYMBOL_VALUE_ADDRESS (sym)};
 
-		exceptions->push_back (info);
-	      }
-	}
-    }
+		  exceptions->push_back (info);
+		}
+	  }
+      }
 }
 
 /* Implements ada_exceptions_list with the regular expression passed
diff --git a/gdb/cp-support.c b/gdb/cp-support.c
index e976354f1f..5b4119b67e 100644
--- a/gdb/cp-support.c
+++ b/gdb/cp-support.c
@@ -1374,7 +1374,6 @@ static void
 add_symbol_overload_list_qualified (const char *func_name,
 				    std::vector<symbol *> *overload_list)
 {
-  struct objfile *objfile;
   const struct block *b, *surrounding_static_block = 0;
 
   /* Look through the partial symtabs for all symbols which begin by
@@ -1397,22 +1396,24 @@ add_symbol_overload_list_qualified (const char *func_name,
   /* Go through the symtabs and check the externs and statics for
      symbols which match.  */
 
-  ALL_COMPUNITS (objfile, cust)
-  {
-    QUIT;
-    b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), GLOBAL_BLOCK);
-    add_symbol_overload_list_block (func_name, b, overload_list);
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    for (struct compunit_symtab *cust : objfile_compunits (objfile))
+      {
+	QUIT;
+	b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), GLOBAL_BLOCK);
+	add_symbol_overload_list_block (func_name, b, overload_list);
+      }
 
-  ALL_COMPUNITS (objfile, cust)
-  {
-    QUIT;
-    b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), STATIC_BLOCK);
-    /* Don't do this block twice.  */
-    if (b == surrounding_static_block)
-      continue;
-    add_symbol_overload_list_block (func_name, b, overload_list);
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    for (struct compunit_symtab *cust : objfile_compunits (objfile))
+      {
+	QUIT;
+	b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), STATIC_BLOCK);
+	/* Don't do this block twice.  */
+	if (b == surrounding_static_block)
+	  continue;
+	add_symbol_overload_list_block (func_name, b, overload_list);
+      }
 }
 
 /* Lookup the rtti type for a class name.  */
diff --git a/gdb/maint.c b/gdb/maint.c
index 56fbf3cfba..5cd5b01936 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -762,7 +762,6 @@ static void
 count_symtabs_and_blocks (int *nr_symtabs_ptr, int *nr_compunit_symtabs_ptr,
 			  int *nr_blocks_ptr)
 {
-  struct objfile *o;
   struct symtab *s;
   int nr_symtabs = 0;
   int nr_compunit_symtabs = 0;
@@ -773,13 +772,14 @@ count_symtabs_and_blocks (int *nr_symtabs_ptr, int *nr_compunit_symtabs_ptr,
      current_program_space may be NULL.  */
   if (current_program_space != NULL)
     {
-      ALL_COMPUNITS (o, cu)
-	{
-	  ++nr_compunit_symtabs;
-	  nr_blocks += BLOCKVECTOR_NBLOCKS (COMPUNIT_BLOCKVECTOR (cu));
-	  ALL_COMPUNIT_FILETABS (cu, s)
-	    ++nr_symtabs;
-	}
+      for (struct objfile *o : all_objfiles (current_program_space))
+	for (struct compunit_symtab *cu : objfile_compunits (o))
+	  {
+	    ++nr_compunit_symtabs;
+	    nr_blocks += BLOCKVECTOR_NBLOCKS (COMPUNIT_BLOCKVECTOR (cu));
+	    ALL_COMPUNIT_FILETABS (cu, s)
+	      ++nr_symtabs;
+	  }
     }
 
   *nr_symtabs_ptr = nr_symtabs;
diff --git a/gdb/objfiles.h b/gdb/objfiles.h
index 916d80f571..165a6e3cf1 100644
--- a/gdb/objfiles.h
+++ b/gdb/objfiles.h
@@ -718,12 +718,6 @@ private:
   ALL_OBJFILES (objfile)			\
     ALL_OBJFILE_FILETABS (objfile, ps, s)
 
-/* Traverse all compunits in all objfiles in the current program space.  */
-
-#define ALL_COMPUNITS(objfile, cu)	\
-  ALL_OBJFILES (objfile)		\
-    for (struct compunit_symtab *cu : objfile_compunits (objfile))
-
 #define ALL_OBJFILE_OSECTIONS(objfile, osect)	\
   for (osect = objfile->sections; osect < objfile->sections_end; osect++) \
     if (osect->the_bfd_section == NULL)					\
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 49dd30235c..86fa99b057 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -2166,22 +2166,21 @@ lookup_local_symbol (const char *name,
 struct objfile *
 lookup_objfile_from_block (const struct block *block)
 {
-  struct objfile *obj;
-
   if (block == NULL)
     return NULL;
 
   block = block_global_block (block);
   /* Look through all blockvectors.  */
-  ALL_COMPUNITS (obj, cust)
-    if (block == BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust),
-				    GLOBAL_BLOCK))
-      {
-	if (obj->separate_debug_objfile_backlink)
-	  obj = obj->separate_debug_objfile_backlink;
+  for (struct objfile *obj : all_objfiles (current_program_space))
+    for (struct compunit_symtab *cust : objfile_compunits (obj))
+      if (block == BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust),
+				      GLOBAL_BLOCK))
+	{
+	  if (obj->separate_debug_objfile_backlink)
+	    obj = obj->separate_debug_objfile_backlink;
 
-	return obj;
-      }
+	  return obj;
+	}
 
   return NULL;
 }
@@ -2871,7 +2870,6 @@ struct compunit_symtab *
 find_pc_sect_compunit_symtab (CORE_ADDR pc, struct obj_section *section)
 {
   struct compunit_symtab *best_cust = NULL;
-  struct objfile *objfile;
   CORE_ADDR distance = 0;
   struct bound_minimal_symbol msymbol;
 
@@ -2904,57 +2902,58 @@ find_pc_sect_compunit_symtab (CORE_ADDR pc, struct obj_section *section)
      It also happens for objfiles that have their functions reordered.
      For these, the symtab we are looking for is not necessarily read in.  */
 
-  ALL_COMPUNITS (objfile, cust)
-  {
-    struct block *b;
-    const struct blockvector *bv;
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    for (struct compunit_symtab *cust : objfile_compunits (objfile))
+      {
+	struct block *b;
+	const struct blockvector *bv;
 
-    bv = COMPUNIT_BLOCKVECTOR (cust);
-    b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
+	bv = COMPUNIT_BLOCKVECTOR (cust);
+	b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
 
-    if (BLOCK_START (b) <= pc
-	&& BLOCK_END (b) > pc
-	&& (distance == 0
-	    || BLOCK_END (b) - BLOCK_START (b) < distance))
-      {
-	/* For an objfile that has its functions reordered,
-	   find_pc_psymtab will find the proper partial symbol table
-	   and we simply return its corresponding symtab.  */
-	/* In order to better support objfiles that contain both
-	   stabs and coff debugging info, we continue on if a psymtab
-	   can't be found.  */
-	if ((objfile->flags & OBJF_REORDERED) && objfile->sf)
+	if (BLOCK_START (b) <= pc
+	    && BLOCK_END (b) > pc
+	    && (distance == 0
+		|| BLOCK_END (b) - BLOCK_START (b) < distance))
 	  {
-	    struct compunit_symtab *result;
-
-	    result
-	      = objfile->sf->qf->find_pc_sect_compunit_symtab (objfile,
-							       msymbol,
-							       pc, section,
-							       0);
-	    if (result != NULL)
-	      return result;
-	  }
-	if (section != 0)
-	  {
-	    struct block_iterator iter;
-	    struct symbol *sym = NULL;
-
-	    ALL_BLOCK_SYMBOLS (b, iter, sym)
+	    /* For an objfile that has its functions reordered,
+	       find_pc_psymtab will find the proper partial symbol table
+	       and we simply return its corresponding symtab.  */
+	    /* In order to better support objfiles that contain both
+	       stabs and coff debugging info, we continue on if a psymtab
+	       can't be found.  */
+	    if ((objfile->flags & OBJF_REORDERED) && objfile->sf)
 	      {
-		fixup_symbol_section (sym, objfile);
-		if (matching_obj_sections (SYMBOL_OBJ_SECTION (objfile, sym),
-					   section))
-		  break;
+		struct compunit_symtab *result;
+
+		result
+		  = objfile->sf->qf->find_pc_sect_compunit_symtab (objfile,
+								   msymbol,
+								   pc, section,
+								   0);
+		if (result != NULL)
+		  return result;
+	      }
+	    if (section != 0)
+	      {
+		struct block_iterator iter;
+		struct symbol *sym = NULL;
+
+		ALL_BLOCK_SYMBOLS (b, iter, sym)
+		  {
+		    fixup_symbol_section (sym, objfile);
+		    if (matching_obj_sections (SYMBOL_OBJ_SECTION (objfile, sym),
+					       section))
+		      break;
+		  }
+		if (sym == NULL)
+		  continue;		/* No symbol in this symtab matches
+					   section.  */
 	      }
-	    if (sym == NULL)
-	      continue;		/* No symbol in this symtab matches
-				   section.  */
+	    distance = BLOCK_END (b) - BLOCK_START (b);
+	    best_cust = cust;
 	  }
-	distance = BLOCK_END (b) - BLOCK_START (b);
-	best_cust = cust;
       }
-  }
 
   if (best_cust != NULL)
     return best_cust;
@@ -4463,7 +4462,7 @@ search_symbols (const char *regexp, enum search_domain kind,
 		  {
 		    /* Note: An important side-effect of these lookup functions
 		       is to expand the symbol table if msymbol is found, for the
-		       benefit of the next loop on ALL_COMPUNITS.  */
+		       benefit of the next loop compunits.  */
 		    if (kind == FUNCTIONS_DOMAIN
 			? (find_pc_compunit_symtab
 			   (MSYMBOL_VALUE_ADDRESS (objfile, msymbol)) == NULL)
@@ -4477,9 +4476,8 @@ search_symbols (const char *regexp, enum search_domain kind,
 	  }
     }
 
-  {
-    struct objfile *objfile;
-    ALL_COMPUNITS (objfile, cust)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    for (struct compunit_symtab *cust : objfile_compunits (objfile))
       {
 	bv = COMPUNIT_BLOCKVECTOR (cust);
 	for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
@@ -4530,7 +4528,6 @@ search_symbols (const char *regexp, enum search_domain kind,
 	      }
 	  }
       }
-  }
 
   if (!result.empty ())
     sort_search_symbols_remove_dups (&result);
@@ -5274,10 +5271,10 @@ default_collect_symbol_completion_matches_break_on
     }
 
   /* Add completions for all currently loaded symbol tables.  */
-  struct objfile *objfile;
-  ALL_COMPUNITS (objfile, cust)
-    add_symtab_completions (cust, tracker, mode, lookup_name,
-			    sym_text, word, code);
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    for (struct compunit_symtab *cust : objfile_compunits (objfile))
+      add_symtab_completions (cust, tracker, mode, lookup_name,
+			      sym_text, word, code);
 
   /* Look through the partial symtabs for all symbols which begin by
      matching SYM_TEXT.  Expand all CUs that you find to the list.  */
-- 
2.17.2

^ permalink raw reply	[flat|nested] 36+ messages in thread

* [PATCH 05/12] Remove ALL_MSYMBOLS and ALL_OBJFILE_MSYMBOLS
  2018-11-25 16:54 [PATCH 00/12] Remove some ALL_* iteration macros Tom Tromey
                   ` (8 preceding siblings ...)
  2018-11-25 16:54 ` [PATCH 08/12] Remove ALL_COMPUNIT_FILETABS Tom Tromey
@ 2018-11-25 16:54 ` Tom Tromey
  2018-11-25 16:55 ` [PATCH 02/12] Remove ALL_PSPACE_OBJFILES Tom Tromey
                   ` (4 subsequent siblings)
  14 siblings, 0 replies; 36+ messages in thread
From: Tom Tromey @ 2018-11-25 16:54 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This removes the ALL_MSYMBOLS and ALL_OBJFILE_MSYMBOLS macros,
replacing their uses with ranged for loops.

In a couple of spots, a new declaration was needed in order to work
around shadowing; these are just temporary and are removed in a
subsequent patch.

gdb/ChangeLog
2018-11-25  Tom Tromey  <tom@tromey.com>

	* symtab.c (search_symbols)
	(default_collect_symbol_completion_matches_break_on): Use
	objfile_msymbols.
	* ada-lang.c (ada_lookup_simple_minsym)
	(ada_collect_symbol_completion_matches): Use objfile_msymbols.
	* minsyms.c (find_solib_trampoline_target): Use objfile_msymbols.
	* hppa-tdep.c (hppa_lookup_stub_minimal_symbol): Use
	objfile_msymbols.
	* coffread.c (coff_symfile_read): Use objfile_msymbols.
	* symmisc.c (dump_msymbols): Use objfile_msymbols.
	* objc-lang.c (find_methods): Use objfile_msymbols.
	(info_selectors_command, info_classes_command): Likewise.
	* stabsread.c (scan_file_globals): Use objfile_msymbols.
	* objfiles.h (class objfile_msymbols): New.
	(ALL_OBJFILE_MSYMBOLS): Remove.
	(ALL_MSYMBOLS): Remove.
---
 gdb/ChangeLog   |  19 +++++
 gdb/ada-lang.c  |  77 +++++++++--------
 gdb/coffread.c  |   4 +-
 gdb/hppa-tdep.c |  31 ++++---
 gdb/minsyms.c   |  35 ++++----
 gdb/objc-lang.c | 153 +++++++++++++++++-----------------
 gdb/objfiles.h  |  90 +++++++++++++++++---
 gdb/stabsread.c |   3 +-
 gdb/symmisc.c   |   3 +-
 gdb/symtab.c    | 213 +++++++++++++++++++++++++-----------------------
 10 files changed, 357 insertions(+), 271 deletions(-)

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 160184ddb3..0520ec144a 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -4913,8 +4913,6 @@ struct bound_minimal_symbol
 ada_lookup_simple_minsym (const char *name)
 {
   struct bound_minimal_symbol result;
-  struct objfile *objfile;
-  struct minimal_symbol *msymbol;
 
   memset (&result, 0, sizeof (result));
 
@@ -4924,16 +4922,17 @@ ada_lookup_simple_minsym (const char *name)
   symbol_name_matcher_ftype *match_name
     = ada_get_symbol_name_matcher (lookup_name);
 
-  ALL_MSYMBOLS (objfile, msymbol)
-  {
-    if (match_name (MSYMBOL_LINKAGE_NAME (msymbol), lookup_name, NULL)
-        && MSYMBOL_TYPE (msymbol) != mst_solib_trampoline)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
       {
-	result.minsym = msymbol;
-	result.objfile = objfile;
-	break;
+	if (match_name (MSYMBOL_LINKAGE_NAME (msymbol), lookup_name, NULL)
+	    && MSYMBOL_TYPE (msymbol) != mst_solib_trampoline)
+	  {
+	    result.minsym = msymbol;
+	    result.objfile = objfile;
+	    break;
+	  }
       }
-  }
 
   return result;
 }
@@ -6391,8 +6390,6 @@ ada_collect_symbol_completion_matches (completion_tracker &tracker,
 {
   struct symbol *sym;
   struct compunit_symtab *s;
-  struct minimal_symbol *msymbol;
-  struct objfile *objfile;
   const struct block *b, *surrounding_static_block = 0;
   struct block_iterator iter;
 
@@ -6412,35 +6409,36 @@ ada_collect_symbol_completion_matches (completion_tracker &tracker,
      anything that isn't a text symbol (everything else will be
      handled by the psymtab code above).  */
 
-  ALL_MSYMBOLS (objfile, msymbol)
-  {
-    QUIT;
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
+      {
+	QUIT;
 
-    if (completion_skip_symbol (mode, msymbol))
-      continue;
+	if (completion_skip_symbol (mode, msymbol))
+	  continue;
 
-    language symbol_language = MSYMBOL_LANGUAGE (msymbol);
-
-    /* Ada minimal symbols won't have their language set to Ada.  If
-       we let completion_list_add_name compare using the
-       default/C-like matcher, then when completing e.g., symbols in a
-       package named "pck", we'd match internal Ada symbols like
-       "pckS", which are invalid in an Ada expression, unless you wrap
-       them in '<' '>' to request a verbatim match.
-
-       Unfortunately, some Ada encoded names successfully demangle as
-       C++ symbols (using an old mangling scheme), such as "name__2Xn"
-       -> "Xn::name(void)" and thus some Ada minimal symbols end up
-       with the wrong language set.  Paper over that issue here.  */
-    if (symbol_language == language_auto
-	|| symbol_language == language_cplus)
-      symbol_language = language_ada;
-
-    completion_list_add_name (tracker,
-			      symbol_language,
-			      MSYMBOL_LINKAGE_NAME (msymbol),
-			      lookup_name, text, word);
-  }
+	language symbol_language = MSYMBOL_LANGUAGE (msymbol);
+
+	/* Ada minimal symbols won't have their language set to Ada.  If
+	   we let completion_list_add_name compare using the
+	   default/C-like matcher, then when completing e.g., symbols in a
+	   package named "pck", we'd match internal Ada symbols like
+	   "pckS", which are invalid in an Ada expression, unless you wrap
+	   them in '<' '>' to request a verbatim match.
+
+	   Unfortunately, some Ada encoded names successfully demangle as
+	   C++ symbols (using an old mangling scheme), such as "name__2Xn"
+	   -> "Xn::name(void)" and thus some Ada minimal symbols end up
+	   with the wrong language set.  Paper over that issue here.  */
+	if (symbol_language == language_auto
+	    || symbol_language == language_cplus)
+	  symbol_language = language_ada;
+
+	completion_list_add_name (tracker,
+				  symbol_language,
+				  MSYMBOL_LINKAGE_NAME (msymbol),
+				  lookup_name, text, word);
+      }
 
   /* Search upwards from currently selected frame (so that we can
      complete on local vars.  */
@@ -6465,6 +6463,7 @@ ada_collect_symbol_completion_matches (completion_tracker &tracker,
   /* Go through the symtabs and check the externs and statics for
      symbols which match.  */
 
+  struct objfile *objfile;
   ALL_COMPUNITS (objfile, s)
   {
     QUIT;
diff --git a/gdb/coffread.c b/gdb/coffread.c
index a473b78245..401aacbbe2 100644
--- a/gdb/coffread.c
+++ b/gdb/coffread.c
@@ -661,9 +661,7 @@ coff_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
 
   if (pe_file)
     {
-      struct minimal_symbol *msym;
-
-      ALL_OBJFILE_MSYMBOLS (objfile, msym)
+      for (struct minimal_symbol *msym : objfile_msymbols (objfile))
 	{
 	  const char *name = MSYMBOL_LINKAGE_NAME (msym);
 
diff --git a/gdb/hppa-tdep.c b/gdb/hppa-tdep.c
index 9f70ef9b73..6e8c2d64fe 100644
--- a/gdb/hppa-tdep.c
+++ b/gdb/hppa-tdep.c
@@ -2540,25 +2540,24 @@ struct bound_minimal_symbol
 hppa_lookup_stub_minimal_symbol (const char *name,
                                  enum unwind_stub_types stub_type)
 {
-  struct objfile *objfile;
-  struct minimal_symbol *msym;
   struct bound_minimal_symbol result = { NULL, NULL };
 
-  ALL_MSYMBOLS (objfile, msym)
-    {
-      if (strcmp (MSYMBOL_LINKAGE_NAME (msym), name) == 0)
-        {
-          struct unwind_table_entry *u;
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    for (struct minimal_symbol *msym : objfile_msymbols (objfile))
+      {
+	if (strcmp (MSYMBOL_LINKAGE_NAME (msym), name) == 0)
+	  {
+	    struct unwind_table_entry *u;
 
-          u = find_unwind_entry (MSYMBOL_VALUE (msym));
-          if (u != NULL && u->stub_unwind.stub_type == stub_type)
-	    {
-	      result.objfile = objfile;
-	      result.minsym = msym;
-	      return result;
-	    }
-        }
-    }
+	    u = find_unwind_entry (MSYMBOL_VALUE (msym));
+	    if (u != NULL && u->stub_unwind.stub_type == stub_type)
+	      {
+		result.objfile = objfile;
+		result.minsym = msym;
+		return result;
+	      }
+	  }
+      }
 
   return result;
 }
diff --git a/gdb/minsyms.c b/gdb/minsyms.c
index 0f854422e0..f2d474efe4 100644
--- a/gdb/minsyms.c
+++ b/gdb/minsyms.c
@@ -1457,30 +1457,29 @@ lookup_solib_trampoline_symbol_by_pc (CORE_ADDR pc)
 CORE_ADDR
 find_solib_trampoline_target (struct frame_info *frame, CORE_ADDR pc)
 {
-  struct objfile *objfile;
-  struct minimal_symbol *msymbol;
   struct minimal_symbol *tsymbol = lookup_solib_trampoline_symbol_by_pc (pc);
 
   if (tsymbol != NULL)
     {
-      ALL_MSYMBOLS (objfile, msymbol)
-      {
-	/* Also handle minimal symbols pointing to function descriptors.  */
-	if ((MSYMBOL_TYPE (msymbol) == mst_text
-	     || MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc
-	     || MSYMBOL_TYPE (msymbol) == mst_data
-	     || MSYMBOL_TYPE (msymbol) == mst_data_gnu_ifunc)
-	    && strcmp (MSYMBOL_LINKAGE_NAME (msymbol),
-		       MSYMBOL_LINKAGE_NAME (tsymbol)) == 0)
+      for (struct objfile *objfile : all_objfiles (current_program_space))
+	for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
 	  {
-	    CORE_ADDR func;
-
-	    /* Ignore data symbols that are not function
-	       descriptors.  */
-	    if (msymbol_is_function (objfile, msymbol, &func))
-	      return func;
+	    /* Also handle minimal symbols pointing to function descriptors.  */
+	    if ((MSYMBOL_TYPE (msymbol) == mst_text
+		 || MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc
+		 || MSYMBOL_TYPE (msymbol) == mst_data
+		 || MSYMBOL_TYPE (msymbol) == mst_data_gnu_ifunc)
+		&& strcmp (MSYMBOL_LINKAGE_NAME (msymbol),
+			   MSYMBOL_LINKAGE_NAME (tsymbol)) == 0)
+	      {
+		CORE_ADDR func;
+
+		/* Ignore data symbols that are not function
+		   descriptors.  */
+		if (msymbol_is_function (objfile, msymbol, &func))
+		  return func;
+	      }
 	  }
-      }
     }
   return 0;
 }
diff --git a/gdb/objc-lang.c b/gdb/objc-lang.c
index 4933e0c343..c9fab64702 100644
--- a/gdb/objc-lang.c
+++ b/gdb/objc-lang.c
@@ -562,8 +562,6 @@ compare_selectors (const void *a, const void *b)
 static void
 info_selectors_command (const char *regexp, int from_tty)
 {
-  struct objfile	*objfile;
-  struct minimal_symbol *msymbol;
   const char            *name;
   char                  *val;
   int                    matches = 0;
@@ -607,36 +605,37 @@ info_selectors_command (const char *regexp, int from_tty)
     }
 
   /* First time thru is JUST to get max length and count.  */
-  ALL_MSYMBOLS (objfile, msymbol)
-    {
-      QUIT;
-      name = MSYMBOL_NATURAL_NAME (msymbol);
-      if (name
-          && (name[0] == '-' || name[0] == '+')
-	  && name[1] == '[')		/* Got a method name.  */
-	{
-	  /* Filter for class/instance methods.  */
-	  if (plusminus && name[0] != plusminus)
-	    continue;
-	  /* Find selector part.  */
-	  name = (char *) strchr (name+2, ' ');
-	  if (name == NULL)
-	    {
-	      complaint (_("Bad method name '%s'"),
-			 MSYMBOL_NATURAL_NAME (msymbol));
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
+      {
+	QUIT;
+	name = MSYMBOL_NATURAL_NAME (msymbol);
+	if (name
+	    && (name[0] == '-' || name[0] == '+')
+	    && name[1] == '[')		/* Got a method name.  */
+	  {
+	    /* Filter for class/instance methods.  */
+	    if (plusminus && name[0] != plusminus)
 	      continue;
-	    }
-	  if (regexp == NULL || re_exec(++name) != 0)
-	    { 
-	      const char *mystart = name;
-	      const char *myend   = strchr (mystart, ']');
+	    /* Find selector part.  */
+	    name = (char *) strchr (name+2, ' ');
+	    if (name == NULL)
+	      {
+		complaint (_("Bad method name '%s'"),
+			   MSYMBOL_NATURAL_NAME (msymbol));
+		continue;
+	      }
+	    if (regexp == NULL || re_exec(++name) != 0)
+	      { 
+		const char *mystart = name;
+		const char *myend   = strchr (mystart, ']');
 	      
-	      if (myend && (myend - mystart > maxlen))
-		maxlen = myend - mystart;	/* Get longest selector.  */
-	      matches++;
-	    }
-	}
-    }
+		if (myend && (myend - mystart > maxlen))
+		  maxlen = myend - mystart;	/* Get longest selector.  */
+		matches++;
+	      }
+	  }
+      }
   if (matches)
     {
       printf_filtered (_("Selectors matching \"%s\":\n\n"), 
@@ -644,23 +643,24 @@ info_selectors_command (const char *regexp, int from_tty)
 
       sym_arr = XALLOCAVEC (struct symbol *, matches);
       matches = 0;
-      ALL_MSYMBOLS (objfile, msymbol)
-	{
-	  QUIT;
-	  name = MSYMBOL_NATURAL_NAME (msymbol);
-	  if (name &&
-	     (name[0] == '-' || name[0] == '+') &&
-	      name[1] == '[')		/* Got a method name.  */
-	    {
-	      /* Filter for class/instance methods.  */
-	      if (plusminus && name[0] != plusminus)
-		continue;
-	      /* Find selector part.  */
-	      name = (char *) strchr(name+2, ' ');
-	      if (regexp == NULL || re_exec(++name) != 0)
-		sym_arr[matches++] = (struct symbol *) msymbol;
-	    }
-	}
+      for (struct objfile *objfile : all_objfiles (current_program_space))
+	for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
+	  {
+	    QUIT;
+	    name = MSYMBOL_NATURAL_NAME (msymbol);
+	    if (name &&
+		(name[0] == '-' || name[0] == '+') &&
+		name[1] == '[')		/* Got a method name.  */
+	      {
+		/* Filter for class/instance methods.  */
+		if (plusminus && name[0] != plusminus)
+		  continue;
+		/* Find selector part.  */
+		name = (char *) strchr(name+2, ' ');
+		if (regexp == NULL || re_exec(++name) != 0)
+		  sym_arr[matches++] = (struct symbol *) msymbol;
+	      }
+	  }
 
       qsort (sym_arr, matches, sizeof (struct minimal_symbol *), 
 	     compare_selectors);
@@ -723,8 +723,6 @@ compare_classes (const void *a, const void *b)
 static void
 info_classes_command (const char *regexp, int from_tty)
 {
-  struct objfile	*objfile;
-  struct minimal_symbol *msymbol;
   const char            *name;
   char                  *val;
   int                    matches = 0;
@@ -757,40 +755,42 @@ info_classes_command (const char *regexp, int from_tty)
     }
 
   /* First time thru is JUST to get max length and count.  */
-  ALL_MSYMBOLS (objfile, msymbol)
-    {
-      QUIT;
-      name = MSYMBOL_NATURAL_NAME (msymbol);
-      if (name &&
-	 (name[0] == '-' || name[0] == '+') &&
-	  name[1] == '[')			/* Got a method name.  */
-	if (regexp == NULL || re_exec(name+2) != 0)
-	  { 
-	    /* Compute length of classname part.  */
-	    const char *mystart = name + 2;
-	    const char *myend   = strchr (mystart, ' ');
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
+      {
+	QUIT;
+	name = MSYMBOL_NATURAL_NAME (msymbol);
+	if (name &&
+	    (name[0] == '-' || name[0] == '+') &&
+	    name[1] == '[')			/* Got a method name.  */
+	  if (regexp == NULL || re_exec(name+2) != 0)
+	    { 
+	      /* Compute length of classname part.  */
+	      const char *mystart = name + 2;
+	      const char *myend   = strchr (mystart, ' ');
 	    
-	    if (myend && (myend - mystart > maxlen))
-	      maxlen = myend - mystart;
-	    matches++;
-	  }
-    }
+	      if (myend && (myend - mystart > maxlen))
+		maxlen = myend - mystart;
+	      matches++;
+	    }
+      }
   if (matches)
     {
       printf_filtered (_("Classes matching \"%s\":\n\n"), 
 		       regexp ? regexp : "*");
       sym_arr = XALLOCAVEC (struct symbol *, matches);
       matches = 0;
-      ALL_MSYMBOLS (objfile, msymbol)
-	{
-	  QUIT;
-	  name = MSYMBOL_NATURAL_NAME (msymbol);
-	  if (name &&
-	     (name[0] == '-' || name[0] == '+') &&
-	      name[1] == '[')			/* Got a method name.  */
-	    if (regexp == NULL || re_exec(name+2) != 0)
+      for (struct objfile *objfile : all_objfiles (current_program_space))
+	for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
+	  {
+	    QUIT;
+	    name = MSYMBOL_NATURAL_NAME (msymbol);
+	    if (name &&
+		(name[0] == '-' || name[0] == '+') &&
+		name[1] == '[')			/* Got a method name.  */
+	      if (regexp == NULL || re_exec(name+2) != 0)
 		sym_arr[matches++] = (struct symbol *) msymbol;
-	}
+	  }
 
       qsort (sym_arr, matches, sizeof (struct minimal_symbol *), 
 	     compare_classes);
@@ -987,7 +987,6 @@ find_methods (char type, const char *theclass, const char *category,
   for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       unsigned int *objc_csym;
-      struct minimal_symbol *msymbol = NULL;
 
       /* The objfile_csym variable counts the number of ObjC methods
 	 that this objfile defines.  We save that count as a private
@@ -1001,7 +1000,7 @@ find_methods (char type, const char *theclass, const char *category,
 	/* There are no ObjC symbols in this objfile.  Skip it entirely.  */
 	continue;
 
-      ALL_OBJFILE_MSYMBOLS (objfile, msymbol)
+      for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
 	{
 	  QUIT;
 
diff --git a/gdb/objfiles.h b/gdb/objfiles.h
index 1d310e5584..b24145f0ca 100644
--- a/gdb/objfiles.h
+++ b/gdb/objfiles.h
@@ -623,12 +623,85 @@ public:
 #define ALL_OBJFILE_COMPUNITS(objfile, cu) \
   for ((cu) = (objfile) -> compunit_symtabs; (cu) != NULL; (cu) = (cu) -> next)
 
-/* Traverse all minimal symbols in one objfile.  */
+/* A range adapter that makes it possible to iterate over all
+   minimal symbols of an objfile.  */
 
-#define	ALL_OBJFILE_MSYMBOLS(objfile, m)	\
-    for ((m) = (objfile)->per_bfd->msymbols;	\
-	 MSYMBOL_LINKAGE_NAME (m) != NULL;	\
-	 (m)++)
+class objfile_msymbols
+{
+public:
+
+  explicit objfile_msymbols (struct objfile *objfile)
+    : m_objfile (objfile)
+  {
+  }
+
+  struct iterator
+  {
+    typedef iterator self_type;
+    typedef struct minimal_symbol *value_type;
+    typedef struct minimal_symbol *&reference;
+    typedef struct minimal_symbol **pointer;
+    typedef std::forward_iterator_tag iterator_category;
+    typedef int difference_type;
+
+    explicit iterator (struct objfile *objfile)
+      : m_msym (objfile->per_bfd->msymbols)
+    {
+      /* Make sure to properly handle the case where there are no
+	 minsyms.  */
+      if (MSYMBOL_LINKAGE_NAME (m_msym) == nullptr)
+	m_msym = nullptr;
+    }
+
+    iterator ()
+      : m_msym (nullptr)
+    {
+    }
+    
+    value_type operator* () const
+    {
+      return m_msym;
+    }
+
+    bool operator== (const self_type &other) const
+    {
+      return m_msym == other.m_msym;
+    }
+
+    bool operator!= (const self_type &other) const
+    {
+      return m_msym != other.m_msym;
+    }
+
+    self_type &operator++ ()
+    {
+      if (m_msym != nullptr)
+	{
+	  ++m_msym;
+	  if (MSYMBOL_LINKAGE_NAME (m_msym) == nullptr)
+	    m_msym = nullptr;
+	}
+      return *this;
+    }
+
+  private:
+    struct minimal_symbol *m_msym;
+  };
+
+  iterator begin () const
+  {
+    return iterator (m_objfile);
+  }
+
+  iterator end () const
+  {
+    return iterator ();
+  }
+
+private:
+
+  struct objfile *m_objfile;
+};
 
 /* Traverse all symtabs in all objfiles in the current symbol
    space.  */
@@ -643,13 +716,6 @@ public:
   ALL_OBJFILES (objfile)		\
     ALL_OBJFILE_COMPUNITS (objfile, cu)
 
-/* Traverse all minimal symbols in all objfiles in the current symbol
-   space.  */
-
-#define	ALL_MSYMBOLS(objfile, m) \
-  ALL_OBJFILES (objfile)	 \
-    ALL_OBJFILE_MSYMBOLS (objfile, m)
-
 #define ALL_OBJFILE_OSECTIONS(objfile, osect)	\
   for (osect = objfile->sections; osect < objfile->sections_end; osect++) \
     if (osect->the_bfd_section == NULL)					\
diff --git a/gdb/stabsread.c b/gdb/stabsread.c
index d355e54148..c1953ee54a 100644
--- a/gdb/stabsread.c
+++ b/gdb/stabsread.c
@@ -4593,7 +4593,6 @@ void
 scan_file_globals (struct objfile *objfile)
 {
   int hash;
-  struct minimal_symbol *msymbol;
   struct symbol *sym, *prev;
   struct objfile *resolve_objfile;
 
@@ -4619,7 +4618,7 @@ scan_file_globals (struct objfile *objfile)
       if (hash >= HASHSIZE)
 	return;
 
-      ALL_OBJFILE_MSYMBOLS (resolve_objfile, msymbol)
+      for (struct minimal_symbol *msymbol : objfile_msymbols (resolve_objfile))
 	{
 	  QUIT;
 
diff --git a/gdb/symmisc.c b/gdb/symmisc.c
index ac4552d478..ebad334d6c 100644
--- a/gdb/symmisc.c
+++ b/gdb/symmisc.c
@@ -183,7 +183,6 @@ static void
 dump_msymbols (struct objfile *objfile, struct ui_file *outfile)
 {
   struct gdbarch *gdbarch = get_objfile_arch (objfile);
-  struct minimal_symbol *msymbol;
   int index;
   char ms_type;
 
@@ -194,7 +193,7 @@ dump_msymbols (struct objfile *objfile, struct ui_file *outfile)
       return;
     }
   index = 0;
-  ALL_OBJFILE_MSYMBOLS (objfile, msymbol)
+  for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
     {
       struct obj_section *section = MSYMBOL_OBJ_SECTION (objfile, msymbol);
 
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 041b7f2ead..73ab0cb0b7 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -4344,8 +4344,6 @@ search_symbols (const char *regexp, enum search_domain kind,
   int i = 0;
   struct block_iterator iter;
   struct symbol *sym;
-  struct objfile *objfile;
-  struct minimal_symbol *msymbol;
   int found_misc = 0;
   static const enum minimal_symbol_type types[]
     = {mst_data, mst_text, mst_abs};
@@ -4454,81 +4452,89 @@ search_symbols (const char *regexp, enum search_domain kind,
 
   if (nfiles == 0 && (kind == VARIABLES_DOMAIN || kind == FUNCTIONS_DOMAIN))
     {
-      ALL_MSYMBOLS (objfile, msymbol)
-      {
-        QUIT;
+      for (struct objfile *objfile : all_objfiles (current_program_space))
+	for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
+	  {
+	    QUIT;
 
-	if (msymbol->created_by_gdb)
-	  continue;
+	    if (msymbol->created_by_gdb)
+	      continue;
 
-	if (MSYMBOL_TYPE (msymbol) == ourtype
-	    || MSYMBOL_TYPE (msymbol) == ourtype2
-	    || MSYMBOL_TYPE (msymbol) == ourtype3
-	    || MSYMBOL_TYPE (msymbol) == ourtype4)
-	  {
-	    if (!preg.has_value ()
-		|| preg->exec (MSYMBOL_NATURAL_NAME (msymbol), 0,
-			       NULL, 0) == 0)
+	    if (MSYMBOL_TYPE (msymbol) == ourtype
+		|| MSYMBOL_TYPE (msymbol) == ourtype2
+		|| MSYMBOL_TYPE (msymbol) == ourtype3
+		|| MSYMBOL_TYPE (msymbol) == ourtype4)
 	      {
-		/* Note: An important side-effect of these lookup functions
-		   is to expand the symbol table if msymbol is found, for the
-		   benefit of the next loop on ALL_COMPUNITS.  */
-		if (kind == FUNCTIONS_DOMAIN
-		    ? (find_pc_compunit_symtab
-		       (MSYMBOL_VALUE_ADDRESS (objfile, msymbol)) == NULL)
-		    : (lookup_symbol_in_objfile_from_linkage_name
-		       (objfile, MSYMBOL_LINKAGE_NAME (msymbol), VAR_DOMAIN)
-		       .symbol == NULL))
-		  found_misc = 1;
+		if (!preg.has_value ()
+		    || preg->exec (MSYMBOL_NATURAL_NAME (msymbol), 0,
+				   NULL, 0) == 0)
+		  {
+		    /* Note: An important side-effect of these lookup functions
+		       is to expand the symbol table if msymbol is found, for the
+		       benefit of the next loop on ALL_COMPUNITS.  */
+		    if (kind == FUNCTIONS_DOMAIN
+			? (find_pc_compunit_symtab
+			   (MSYMBOL_VALUE_ADDRESS (objfile, msymbol)) == NULL)
+			: (lookup_symbol_in_objfile_from_linkage_name
+			   (objfile, MSYMBOL_LINKAGE_NAME (msymbol),
+			    VAR_DOMAIN)
+			   .symbol == NULL))
+		      found_misc = 1;
+		  }
 	      }
 	  }
-      }
     }
 
-  ALL_COMPUNITS (objfile, cust)
   {
-    bv = COMPUNIT_BLOCKVECTOR (cust);
-    for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
+    struct objfile *objfile;
+    ALL_COMPUNITS (objfile, cust)
       {
-	b = BLOCKVECTOR_BLOCK (bv, i);
-	ALL_BLOCK_SYMBOLS (b, iter, sym)
+	bv = COMPUNIT_BLOCKVECTOR (cust);
+	for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
 	  {
-	    struct symtab *real_symtab = symbol_symtab (sym);
-
-	    QUIT;
-
-	    /* Check first sole REAL_SYMTAB->FILENAME.  It does not need to be
-	       a substring of symtab_to_fullname as it may contain "./" etc.  */
-	    if ((file_matches (real_symtab->filename, files, nfiles, 0)
-		 || ((basenames_may_differ
-		      || file_matches (lbasename (real_symtab->filename),
-				       files, nfiles, 1))
-		     && file_matches (symtab_to_fullname (real_symtab),
-				      files, nfiles, 0)))
-		&& ((!preg.has_value ()
-		     || preg->exec (SYMBOL_NATURAL_NAME (sym), 0,
-				    NULL, 0) == 0)
-		    && ((kind == VARIABLES_DOMAIN
-			 && SYMBOL_CLASS (sym) != LOC_TYPEDEF
-			 && SYMBOL_CLASS (sym) != LOC_UNRESOLVED
-			 && SYMBOL_CLASS (sym) != LOC_BLOCK
-			 /* LOC_CONST can be used for more than just enums,
-			    e.g., c++ static const members.
-			    We only want to skip enums here.  */
-			 && !(SYMBOL_CLASS (sym) == LOC_CONST
-			      && (TYPE_CODE (SYMBOL_TYPE (sym))
-				  == TYPE_CODE_ENUM))
-			 && (!treg.has_value ()
-			     || treg_matches_sym_type_name (*treg, sym)))
-			|| (kind == FUNCTIONS_DOMAIN
-			    && SYMBOL_CLASS (sym) == LOC_BLOCK
-			    && (!treg.has_value ()
-				|| treg_matches_sym_type_name (*treg, sym)))
-			|| (kind == TYPES_DOMAIN
-			    && SYMBOL_CLASS (sym) == LOC_TYPEDEF))))
+	    b = BLOCKVECTOR_BLOCK (bv, i);
+	    ALL_BLOCK_SYMBOLS (b, iter, sym)
 	      {
-		/* match */
-		result.emplace_back (i, sym);
+		struct symtab *real_symtab = symbol_symtab (sym);
+
+		QUIT;
+
+		/* Check first sole REAL_SYMTAB->FILENAME.  It does
+		   not need to be a substring of symtab_to_fullname as
+		   it may contain "./" etc.  */
+		if ((file_matches (real_symtab->filename, files, nfiles, 0)
+		     || ((basenames_may_differ
+			  || file_matches (lbasename (real_symtab->filename),
+					   files, nfiles, 1))
+			 && file_matches (symtab_to_fullname (real_symtab),
+					  files, nfiles, 0)))
+		    && ((!preg.has_value ()
+			 || preg->exec (SYMBOL_NATURAL_NAME (sym), 0,
+					NULL, 0) == 0)
+			&& ((kind == VARIABLES_DOMAIN
+			     && SYMBOL_CLASS (sym) != LOC_TYPEDEF
+			     && SYMBOL_CLASS (sym) != LOC_UNRESOLVED
+			     && SYMBOL_CLASS (sym) != LOC_BLOCK
+			     /* LOC_CONST can be used for more than
+				just enums, e.g., c++ static const
+				members.  We only want to skip enums
+				here.  */
+			     && !(SYMBOL_CLASS (sym) == LOC_CONST
+				  && (TYPE_CODE (SYMBOL_TYPE (sym))
+				      == TYPE_CODE_ENUM))
+			     && (!treg.has_value ()
+				 || treg_matches_sym_type_name (*treg, sym)))
+			    || (kind == FUNCTIONS_DOMAIN
+				&& SYMBOL_CLASS (sym) == LOC_BLOCK
+				&& (!treg.has_value ()
+				    || treg_matches_sym_type_name (*treg,
+								   sym)))
+			    || (kind == TYPES_DOMAIN
+				&& SYMBOL_CLASS (sym) == LOC_TYPEDEF))))
+		  {
+		    /* match */
+		    result.emplace_back (i, sym);
+		  }
 	      }
 	  }
       }
@@ -4545,39 +4551,42 @@ search_symbols (const char *regexp, enum search_domain kind,
   if ((found_misc || (nfiles == 0 && kind != FUNCTIONS_DOMAIN))
       && !treg.has_value ())
     {
-      ALL_MSYMBOLS (objfile, msymbol)
-      {
-        QUIT;
+      for (struct objfile *objfile : all_objfiles (current_program_space))
+	for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
+	  {
+	    QUIT;
 
-	if (msymbol->created_by_gdb)
-	  continue;
+	    if (msymbol->created_by_gdb)
+	      continue;
 
-	if (MSYMBOL_TYPE (msymbol) == ourtype
-	    || MSYMBOL_TYPE (msymbol) == ourtype2
-	    || MSYMBOL_TYPE (msymbol) == ourtype3
-	    || MSYMBOL_TYPE (msymbol) == ourtype4)
-	  {
-	    if (!preg.has_value ()
-		|| preg->exec (MSYMBOL_NATURAL_NAME (msymbol), 0,
-			       NULL, 0) == 0)
+	    if (MSYMBOL_TYPE (msymbol) == ourtype
+		|| MSYMBOL_TYPE (msymbol) == ourtype2
+		|| MSYMBOL_TYPE (msymbol) == ourtype3
+		|| MSYMBOL_TYPE (msymbol) == ourtype4)
 	      {
-		/* For functions we can do a quick check of whether the
-		   symbol might be found via find_pc_symtab.  */
-		if (kind != FUNCTIONS_DOMAIN
-		    || (find_pc_compunit_symtab
-			(MSYMBOL_VALUE_ADDRESS (objfile, msymbol)) == NULL))
+		if (!preg.has_value ()
+		    || preg->exec (MSYMBOL_NATURAL_NAME (msymbol), 0,
+				   NULL, 0) == 0)
 		  {
-		    if (lookup_symbol_in_objfile_from_linkage_name
-			(objfile, MSYMBOL_LINKAGE_NAME (msymbol), VAR_DOMAIN)
-			.symbol == NULL)
+		    /* For functions we can do a quick check of whether the
+		       symbol might be found via find_pc_symtab.  */
+		    if (kind != FUNCTIONS_DOMAIN
+			|| (find_pc_compunit_symtab
+			    (MSYMBOL_VALUE_ADDRESS (objfile, msymbol))
+			    == NULL))
 		      {
-			/* match */
-			result.emplace_back (i, msymbol, objfile);
+			if (lookup_symbol_in_objfile_from_linkage_name
+			    (objfile, MSYMBOL_LINKAGE_NAME (msymbol),
+			     VAR_DOMAIN)
+			    .symbol == NULL)
+			  {
+			    /* match */
+			    result.emplace_back (i, msymbol, objfile);
+			  }
 		      }
 		  }
 	      }
 	  }
-      }
     }
 
   return result;
@@ -5188,8 +5197,6 @@ default_collect_symbol_completion_matches_break_on
 
   struct symbol *sym;
   struct compunit_symtab *cust;
-  struct minimal_symbol *msymbol;
-  struct objfile *objfile;
   const struct block *b;
   const struct block *surrounding_static_block, *surrounding_global_block;
   struct block_iterator iter;
@@ -5259,22 +5266,24 @@ default_collect_symbol_completion_matches_break_on
 
   if (code == TYPE_CODE_UNDEF)
     {
-      ALL_MSYMBOLS (objfile, msymbol)
-	{
-	  QUIT;
+      for (struct objfile *objfile : all_objfiles (current_program_space))
+	for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
+	  {
+	    QUIT;
 
-	  if (completion_skip_symbol (mode, msymbol))
-	    continue;
+	    if (completion_skip_symbol (mode, msymbol))
+	      continue;
 
-	  completion_list_add_msymbol (tracker, msymbol, lookup_name,
-				       sym_text, word);
+	    completion_list_add_msymbol (tracker, msymbol, lookup_name,
+					 sym_text, word);
 
-	  completion_list_objc_symbol (tracker, msymbol, lookup_name,
-				       sym_text, word);
-	}
+	    completion_list_objc_symbol (tracker, msymbol, lookup_name,
+					 sym_text, word);
+	  }
     }
 
   /* Add completions for all currently loaded symbol tables.  */
+  struct objfile *objfile;
   ALL_COMPUNITS (objfile, cust)
     add_symtab_completions (cust, tracker, mode, lookup_name,
 			    sym_text, word, code);
-- 
2.17.2

^ permalink raw reply	[flat|nested] 36+ messages in thread

* [PATCH 11/12] Remove ALL_OBJSECTIONS
  2018-11-25 16:54 [PATCH 00/12] Remove some ALL_* iteration macros Tom Tromey
                   ` (3 preceding siblings ...)
  2018-11-25 16:54 ` [PATCH 10/12] Remove ALL_OBJFILES and ALL_FILETABS Tom Tromey
@ 2018-11-25 16:54 ` Tom Tromey
  2018-11-25 16:54 ` [PATCH 04/12] Remove ALL_OBJFILES_SAFE Tom Tromey
                   ` (9 subsequent siblings)
  14 siblings, 0 replies; 36+ messages in thread
From: Tom Tromey @ 2018-11-25 16:54 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This removes the ALL_OBJSECTIONS macro, replacing its uses with ranged
for loops.

The special code in this macro for noticing a "break" from the inner
loop was only needed in a single place; so rather than try to
replicate this, I've simply replaced that use with a "goto".

gdb/ChangeLog
2018-11-25  Tom Tromey  <tom@tromey.com>

	* symfile.c (overlay_invalidate_all, find_pc_overlay)
	(find_pc_mapped_section, list_overlays_command)
	(map_overlay_command, unmap_overlay_command)
	(simple_overlay_update): Use all_objfiles.
	* spu-tdep.c (spu_overlay_update): Use all_objfiles.
	* printcmd.c (info_symbol_command): Use all_objfiles.
	* objfiles.h (ALL_OBJSECTIONS): Remove.
	* maint.c (maintenance_translate_address): Use all_objfiles.
	* gcore.c (gcore_create_callback): Use all_objfiles.
	(objfile_find_memory_regions): Likewise.
---
 gdb/ChangeLog  |  13 ++++
 gdb/gcore.c    | 104 +++++++++++++--------------
 gdb/maint.c    |  16 ++---
 gdb/objfiles.h |  36 ----------
 gdb/printcmd.c | 138 +++++++++++++++++------------------
 gdb/spu-tdep.c |   9 ++-
 gdb/symfile.c  | 190 ++++++++++++++++++++++++-------------------------
 7 files changed, 242 insertions(+), 264 deletions(-)

diff --git a/gdb/gcore.c b/gdb/gcore.c
index 24810a7dee..401f2aaa82 100644
--- a/gdb/gcore.c
+++ b/gdb/gcore.c
@@ -424,34 +424,34 @@ gcore_create_callback (CORE_ADDR vaddr, unsigned long size, int read,
     {
       /* See if this region of memory lies inside a known file on disk.
 	 If so, we can avoid copying its contents by clearing SEC_LOAD.  */
-      struct objfile *objfile;
       struct obj_section *objsec;
 
-      ALL_OBJSECTIONS (objfile, objsec)
-	{
-	  bfd *abfd = objfile->obfd;
-	  asection *asec = objsec->the_bfd_section;
-	  bfd_vma align = (bfd_vma) 1 << bfd_get_section_alignment (abfd,
-								    asec);
-	  bfd_vma start = obj_section_addr (objsec) & -align;
-	  bfd_vma end = (obj_section_endaddr (objsec) + align - 1) & -align;
-
-	  /* Match if either the entire memory region lies inside the
-	     section (i.e. a mapping covering some pages of a large
-	     segment) or the entire section lies inside the memory region
-	     (i.e. a mapping covering multiple small sections).
-
-	     This BFD was synthesized from reading target memory,
-	     we don't want to omit that.  */
-	  if (objfile->separate_debug_objfile_backlink == NULL
-	      && ((vaddr >= start && vaddr + size <= end)
-	          || (start >= vaddr && end <= vaddr + size))
-	      && !(bfd_get_file_flags (abfd) & BFD_IN_MEMORY))
-	    {
-	      flags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
-	      goto keep;	/* Break out of two nested for loops.  */
-	    }
-	}
+      for (struct objfile *objfile : all_objfiles (current_program_space))
+	ALL_OBJFILE_OSECTIONS (objfile, objsec)
+	  {
+	    bfd *abfd = objfile->obfd;
+	    asection *asec = objsec->the_bfd_section;
+	    bfd_vma align = (bfd_vma) 1 << bfd_get_section_alignment (abfd,
+								      asec);
+	    bfd_vma start = obj_section_addr (objsec) & -align;
+	    bfd_vma end = (obj_section_endaddr (objsec) + align - 1) & -align;
+
+	    /* Match if either the entire memory region lies inside the
+	       section (i.e. a mapping covering some pages of a large
+	       segment) or the entire section lies inside the memory region
+	       (i.e. a mapping covering multiple small sections).
+
+	       This BFD was synthesized from reading target memory,
+	       we don't want to omit that.  */
+	    if (objfile->separate_debug_objfile_backlink == NULL
+		&& ((vaddr >= start && vaddr + size <= end)
+		    || (start >= vaddr && end <= vaddr + size))
+		&& !(bfd_get_file_flags (abfd) & BFD_IN_MEMORY))
+	      {
+		flags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
+		goto keep;	/* Break out of two nested for loops.  */
+	      }
+	  }
 
     keep:;
     }
@@ -489,36 +489,36 @@ objfile_find_memory_regions (struct target_ops *self,
 			     find_memory_region_ftype func, void *obfd)
 {
   /* Use objfile data to create memory sections.  */
-  struct objfile *objfile;
   struct obj_section *objsec;
   bfd_vma temp_bottom, temp_top;
 
   /* Call callback function for each objfile section.  */
-  ALL_OBJSECTIONS (objfile, objsec)
-    {
-      bfd *ibfd = objfile->obfd;
-      asection *isec = objsec->the_bfd_section;
-      flagword flags = bfd_get_section_flags (ibfd, isec);
-
-      /* Separate debug info files are irrelevant for gcore.  */
-      if (objfile->separate_debug_objfile_backlink != NULL)
-	continue;
-
-      if ((flags & SEC_ALLOC) || (flags & SEC_LOAD))
-	{
-	  int size = bfd_section_size (ibfd, isec);
-	  int ret;
-
-	  ret = (*func) (obj_section_addr (objsec), size, 
-			 1, /* All sections will be readable.  */
-			 (flags & SEC_READONLY) == 0, /* Writable.  */
-			 (flags & SEC_CODE) != 0, /* Executable.  */
-			 1, /* MODIFIED is unknown, pass it as true.  */
-			 obfd);
-	  if (ret != 0)
-	    return ret;
-	}
-    }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    ALL_OBJFILE_OSECTIONS (objfile, objsec)
+      {
+	bfd *ibfd = objfile->obfd;
+	asection *isec = objsec->the_bfd_section;
+	flagword flags = bfd_get_section_flags (ibfd, isec);
+
+	/* Separate debug info files are irrelevant for gcore.  */
+	if (objfile->separate_debug_objfile_backlink != NULL)
+	  continue;
+
+	if ((flags & SEC_ALLOC) || (flags & SEC_LOAD))
+	  {
+	    int size = bfd_section_size (ibfd, isec);
+	    int ret;
+
+	    ret = (*func) (obj_section_addr (objsec), size, 
+			   1, /* All sections will be readable.  */
+			   (flags & SEC_READONLY) == 0, /* Writable.  */
+			   (flags & SEC_CODE) != 0, /* Executable.  */
+			   1, /* MODIFIED is unknown, pass it as true.  */
+			   obfd);
+	    if (ret != 0)
+	      return ret;
+	  }
+      }
 
   /* Make a stack segment.  */
   if (derive_stack_segment (&temp_bottom, &temp_top))
diff --git a/gdb/maint.c b/gdb/maint.c
index df4bf444ff..0867f62e89 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -430,7 +430,6 @@ maintenance_translate_address (const char *arg, int from_tty)
   struct obj_section *sect;
   const char *p;
   struct bound_minimal_symbol sym;
-  struct objfile *objfile;
 
   if (arg == NULL || *arg == 0)
     error (_("requires argument (address or section + address)"));
@@ -448,14 +447,15 @@ maintenance_translate_address (const char *arg, int from_tty)
       int arg_len = p - arg;
       p = skip_spaces (p + 1);
 
-      ALL_OBJSECTIONS (objfile, sect)
-      {
-	if (strncmp (sect->the_bfd_section->name, arg, arg_len) == 0)
-	  break;
-      }
+      for (struct objfile *objfile : all_objfiles (current_program_space))
+	ALL_OBJFILE_OSECTIONS (objfile, sect)
+	  {
+	    if (strncmp (sect->the_bfd_section->name, arg, arg_len) == 0)
+	      goto found;
+	  }
 
-      if (!objfile)
-	error (_("Unknown section %s."), arg);
+      error (_("Unknown section %s."), arg);
+    found: ;
     }
 
   address = parse_and_eval_address (p);
diff --git a/gdb/objfiles.h b/gdb/objfiles.h
index 9d00a55ce5..5e704177eb 100644
--- a/gdb/objfiles.h
+++ b/gdb/objfiles.h
@@ -705,42 +705,6 @@ private:
       }									\
     else
 
-/* Traverse all obj_sections in all objfiles in the current program
-   space.
-
-   Note that this detects a "break" in the inner loop, and exits
-   immediately from the outer loop as well, thus, client code doesn't
-   need to know that this is implemented with a double for.  The extra
-   hair is to make sure that a "break;" stops the outer loop iterating
-   as well, and both OBJFILE and OSECT are left unmodified:
-
-    - The outer loop learns about the inner loop's end condition, and
-      stops iterating if it detects the inner loop didn't reach its
-      end.  In other words, the outer loop keeps going only if the
-      inner loop reached its end cleanly [(osect) ==
-      (objfile)->sections_end].
-
-    - OSECT is initialized in the outer loop initialization
-      expressions, such as if the inner loop has reached its end, so
-      the check mentioned above succeeds the first time.
-
-    - The trick to not clearing OBJFILE on a "break;" is, in the outer
-      loop's loop expression, advance OBJFILE, but iff the inner loop
-      reached its end.  If not, there was a "break;", so leave OBJFILE
-      as is; the outer loop's conditional will break immediately as
-      well (as OSECT will be different from OBJFILE->sections_end).  */
-
-#define ALL_OBJSECTIONS(objfile, osect)					\
-  for ((objfile) = current_program_space->objfiles,			\
-	 (objfile) != NULL ? ((osect) = (objfile)->sections_end) : 0;	\
-       (objfile) != NULL						\
-	 && (osect) == (objfile)->sections_end;				\
-       ((osect) == (objfile)->sections_end				\
-	? ((objfile) = (objfile)->next,					\
-	   (objfile) != NULL ? (osect) = (objfile)->sections_end : 0)	\
-	: 0))								\
-    ALL_OBJFILE_OSECTIONS (objfile, osect)
-
 #define SECT_OFF_DATA(objfile) \
      ((objfile->sect_index_data == -1) \
       ? (internal_error (__FILE__, __LINE__, \
diff --git a/gdb/printcmd.c b/gdb/printcmd.c
index 8c999188d7..130c64c3e8 100644
--- a/gdb/printcmd.c
+++ b/gdb/printcmd.c
@@ -1271,7 +1271,6 @@ static void
 info_symbol_command (const char *arg, int from_tty)
 {
   struct minimal_symbol *msymbol;
-  struct objfile *objfile;
   struct obj_section *osect;
   CORE_ADDR addr, sect_addr;
   int matches = 0;
@@ -1281,78 +1280,81 @@ info_symbol_command (const char *arg, int from_tty)
     error_no_arg (_("address"));
 
   addr = parse_and_eval_address (arg);
-  ALL_OBJSECTIONS (objfile, osect)
-  {
-    /* Only process each object file once, even if there's a separate
-       debug file.  */
-    if (objfile->separate_debug_objfile_backlink)
-      continue;
-
-    sect_addr = overlay_mapped_address (addr, osect);
-
-    if (obj_section_addr (osect) <= sect_addr
-	&& sect_addr < obj_section_endaddr (osect)
-	&& (msymbol
-	    = lookup_minimal_symbol_by_pc_section (sect_addr, osect).minsym))
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    ALL_OBJFILE_OSECTIONS (objfile, osect)
       {
-	const char *obj_name, *mapped, *sec_name, *msym_name;
-	const char *loc_string;
-
-	matches = 1;
-	offset = sect_addr - MSYMBOL_VALUE_ADDRESS (objfile, msymbol);
-	mapped = section_is_mapped (osect) ? _("mapped") : _("unmapped");
-	sec_name = osect->the_bfd_section->name;
-	msym_name = MSYMBOL_PRINT_NAME (msymbol);
-
-	/* Don't print the offset if it is zero.
-	   We assume there's no need to handle i18n of "sym + offset".  */
-	std::string string_holder;
-	if (offset)
+	/* Only process each object file once, even if there's a separate
+	   debug file.  */
+	if (objfile->separate_debug_objfile_backlink)
+	  continue;
+
+	sect_addr = overlay_mapped_address (addr, osect);
+
+	if (obj_section_addr (osect) <= sect_addr
+	    && sect_addr < obj_section_endaddr (osect)
+	    && (msymbol
+		= lookup_minimal_symbol_by_pc_section (sect_addr,
+						       osect).minsym))
 	  {
-	    string_holder = string_printf ("%s + %u", msym_name, offset);
-	    loc_string = string_holder.c_str ();
-	  }
-	else
-	  loc_string = msym_name;
-
-	gdb_assert (osect->objfile && objfile_name (osect->objfile));
-	obj_name = objfile_name (osect->objfile);
-
-	if (MULTI_OBJFILE_P ())
-	  if (pc_in_unmapped_range (addr, osect))
-	    if (section_is_overlay (osect))
-	      printf_filtered (_("%s in load address range of "
-				 "%s overlay section %s of %s\n"),
-			       loc_string, mapped, sec_name, obj_name);
-	    else
-	      printf_filtered (_("%s in load address range of "
-				 "section %s of %s\n"),
-			       loc_string, sec_name, obj_name);
-	  else
-	    if (section_is_overlay (osect))
-	      printf_filtered (_("%s in %s overlay section %s of %s\n"),
-			       loc_string, mapped, sec_name, obj_name);
-	    else
-	      printf_filtered (_("%s in section %s of %s\n"),
-			       loc_string, sec_name, obj_name);
-	else
-	  if (pc_in_unmapped_range (addr, osect))
-	    if (section_is_overlay (osect))
-	      printf_filtered (_("%s in load address range of %s overlay "
-				 "section %s\n"),
-			       loc_string, mapped, sec_name);
+	    const char *obj_name, *mapped, *sec_name, *msym_name;
+	    const char *loc_string;
+
+	    matches = 1;
+	    offset = sect_addr - MSYMBOL_VALUE_ADDRESS (objfile, msymbol);
+	    mapped = section_is_mapped (osect) ? _("mapped") : _("unmapped");
+	    sec_name = osect->the_bfd_section->name;
+	    msym_name = MSYMBOL_PRINT_NAME (msymbol);
+
+	    /* Don't print the offset if it is zero.
+	       We assume there's no need to handle i18n of "sym + offset".  */
+	    std::string string_holder;
+	    if (offset)
+	      {
+		string_holder = string_printf ("%s + %u", msym_name, offset);
+		loc_string = string_holder.c_str ();
+	      }
 	    else
-	      printf_filtered (_("%s in load address range of section %s\n"),
-			       loc_string, sec_name);
-	  else
-	    if (section_is_overlay (osect))
-	      printf_filtered (_("%s in %s overlay section %s\n"),
-			       loc_string, mapped, sec_name);
+	      loc_string = msym_name;
+
+	    gdb_assert (osect->objfile && objfile_name (osect->objfile));
+	    obj_name = objfile_name (osect->objfile);
+
+	    if (MULTI_OBJFILE_P ())
+	      if (pc_in_unmapped_range (addr, osect))
+		if (section_is_overlay (osect))
+		  printf_filtered (_("%s in load address range of "
+				     "%s overlay section %s of %s\n"),
+				   loc_string, mapped, sec_name, obj_name);
+		else
+		  printf_filtered (_("%s in load address range of "
+				     "section %s of %s\n"),
+				   loc_string, sec_name, obj_name);
+	      else
+		if (section_is_overlay (osect))
+		  printf_filtered (_("%s in %s overlay section %s of %s\n"),
+				   loc_string, mapped, sec_name, obj_name);
+		else
+		  printf_filtered (_("%s in section %s of %s\n"),
+				   loc_string, sec_name, obj_name);
 	    else
-	      printf_filtered (_("%s in section %s\n"),
-			       loc_string, sec_name);
+	      if (pc_in_unmapped_range (addr, osect))
+		if (section_is_overlay (osect))
+		  printf_filtered (_("%s in load address range of %s overlay "
+				     "section %s\n"),
+				   loc_string, mapped, sec_name);
+		else
+		  printf_filtered
+		    (_("%s in load address range of section %s\n"),
+		     loc_string, sec_name);
+	      else
+		if (section_is_overlay (osect))
+		  printf_filtered (_("%s in %s overlay section %s\n"),
+				   loc_string, mapped, sec_name);
+		else
+		  printf_filtered (_("%s in section %s\n"),
+				   loc_string, sec_name);
+	  }
       }
-  }
   if (matches == 0)
     printf_filtered (_("No symbol matches %s.\n"), arg);
 }
diff --git a/gdb/spu-tdep.c b/gdb/spu-tdep.c
index 57dd4f1ed0..830d4d4404 100644
--- a/gdb/spu-tdep.c
+++ b/gdb/spu-tdep.c
@@ -1883,11 +1883,10 @@ spu_overlay_update (struct obj_section *osect)
   /* All sections.  */
   else
     {
-      struct objfile *objfile;
-
-      ALL_OBJSECTIONS (objfile, osect)
-	if (section_is_overlay (osect))
-	  spu_overlay_update_osect (osect);
+      for (struct objfile *objfile : all_objfiles (current_program_space))
+	ALL_OBJFILE_OSECTIONS (objfile, osect)
+	  if (section_is_overlay (osect))
+	    spu_overlay_update_osect (osect);
     }
 }
 
diff --git a/gdb/symfile.c b/gdb/symfile.c
index 55756c9da2..b89e1575ad 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -2950,12 +2950,12 @@ section_is_overlay (struct obj_section *section)
 static void
 overlay_invalidate_all (void)
 {
-  struct objfile *objfile;
   struct obj_section *sect;
 
-  ALL_OBJSECTIONS (objfile, sect)
-    if (section_is_overlay (sect))
-      sect->ovly_mapped = -1;
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    ALL_OBJFILE_OSECTIONS (objfile, sect)
+      if (section_is_overlay (sect))
+	sect->ovly_mapped = -1;
 }
 
 /* Function: section_is_mapped (SECTION)
@@ -3126,24 +3126,24 @@ symbol_overlayed_address (CORE_ADDR address, struct obj_section *section)
 struct obj_section *
 find_pc_overlay (CORE_ADDR pc)
 {
-  struct objfile *objfile;
   struct obj_section *osect, *best_match = NULL;
 
   if (overlay_debugging)
     {
-      ALL_OBJSECTIONS (objfile, osect)
-	if (section_is_overlay (osect))
-	  {
-	    if (pc_in_mapped_range (pc, osect))
-	      {
-		if (section_is_mapped (osect))
-		  return osect;
-		else
-		  best_match = osect;
-	      }
-	    else if (pc_in_unmapped_range (pc, osect))
-	      best_match = osect;
-	  }
+      for (struct objfile *objfile : all_objfiles (current_program_space))
+	ALL_OBJFILE_OSECTIONS (objfile, osect)
+	  if (section_is_overlay (osect))
+	    {
+	      if (pc_in_mapped_range (pc, osect))
+		{
+		  if (section_is_mapped (osect))
+		    return osect;
+		  else
+		    best_match = osect;
+		}
+	      else if (pc_in_unmapped_range (pc, osect))
+		best_match = osect;
+	    }
     }
   return best_match;
 }
@@ -3155,14 +3155,14 @@ find_pc_overlay (CORE_ADDR pc)
 struct obj_section *
 find_pc_mapped_section (CORE_ADDR pc)
 {
-  struct objfile *objfile;
   struct obj_section *osect;
 
   if (overlay_debugging)
     {
-      ALL_OBJSECTIONS (objfile, osect)
-	if (pc_in_mapped_range (pc, osect) && section_is_mapped (osect))
-	  return osect;
+      for (struct objfile *objfile : all_objfiles (current_program_space))
+	ALL_OBJFILE_OSECTIONS (objfile, osect)
+	  if (pc_in_mapped_range (pc, osect) && section_is_mapped (osect))
+	    return osect;
     }
 
   return NULL;
@@ -3175,36 +3175,36 @@ static void
 list_overlays_command (const char *args, int from_tty)
 {
   int nmapped = 0;
-  struct objfile *objfile;
   struct obj_section *osect;
 
   if (overlay_debugging)
     {
-      ALL_OBJSECTIONS (objfile, osect)
-      if (section_is_mapped (osect))
-	{
-	  struct gdbarch *gdbarch = get_objfile_arch (objfile);
-	  const char *name;
-	  bfd_vma lma, vma;
-	  int size;
-
-	  vma = bfd_section_vma (objfile->obfd, osect->the_bfd_section);
-	  lma = bfd_section_lma (objfile->obfd, osect->the_bfd_section);
-	  size = bfd_get_section_size (osect->the_bfd_section);
-	  name = bfd_section_name (objfile->obfd, osect->the_bfd_section);
-
-	  printf_filtered ("Section %s, loaded at ", name);
-	  fputs_filtered (paddress (gdbarch, lma), gdb_stdout);
-	  puts_filtered (" - ");
-	  fputs_filtered (paddress (gdbarch, lma + size), gdb_stdout);
-	  printf_filtered (", mapped at ");
-	  fputs_filtered (paddress (gdbarch, vma), gdb_stdout);
-	  puts_filtered (" - ");
-	  fputs_filtered (paddress (gdbarch, vma + size), gdb_stdout);
-	  puts_filtered ("\n");
-
-	  nmapped++;
-	}
+      for (struct objfile *objfile : all_objfiles (current_program_space))
+	ALL_OBJFILE_OSECTIONS (objfile, osect)
+	  if (section_is_mapped (osect))
+	    {
+	      struct gdbarch *gdbarch = get_objfile_arch (objfile);
+	      const char *name;
+	      bfd_vma lma, vma;
+	      int size;
+
+	      vma = bfd_section_vma (objfile->obfd, osect->the_bfd_section);
+	      lma = bfd_section_lma (objfile->obfd, osect->the_bfd_section);
+	      size = bfd_get_section_size (osect->the_bfd_section);
+	      name = bfd_section_name (objfile->obfd, osect->the_bfd_section);
+
+	      printf_filtered ("Section %s, loaded at ", name);
+	      fputs_filtered (paddress (gdbarch, lma), gdb_stdout);
+	      puts_filtered (" - ");
+	      fputs_filtered (paddress (gdbarch, lma + size), gdb_stdout);
+	      printf_filtered (", mapped at ");
+	      fputs_filtered (paddress (gdbarch, vma), gdb_stdout);
+	      puts_filtered (" - ");
+	      fputs_filtered (paddress (gdbarch, vma + size), gdb_stdout);
+	      puts_filtered ("\n");
+
+	      nmapped++;
+	    }
     }
   if (nmapped == 0)
     printf_filtered (_("No sections are mapped.\n"));
@@ -3216,7 +3216,6 @@ list_overlays_command (const char *args, int from_tty)
 static void
 map_overlay_command (const char *args, int from_tty)
 {
-  struct objfile *objfile, *objfile2;
   struct obj_section *sec, *sec2;
 
   if (!overlay_debugging)
@@ -3228,29 +3227,31 @@ map_overlay_command (const char *args, int from_tty)
     error (_("Argument required: name of an overlay section"));
 
   /* First, find a section matching the user supplied argument.  */
-  ALL_OBJSECTIONS (objfile, sec)
-    if (!strcmp (bfd_section_name (objfile->obfd, sec->the_bfd_section), args))
-    {
-      /* Now, check to see if the section is an overlay.  */
-      if (!section_is_overlay (sec))
-	continue;		/* not an overlay section */
-
-      /* Mark the overlay as "mapped".  */
-      sec->ovly_mapped = 1;
-
-      /* Next, make a pass and unmap any sections that are
-         overlapped by this new section: */
-      ALL_OBJSECTIONS (objfile2, sec2)
-	if (sec2->ovly_mapped && sec != sec2 && sections_overlap (sec, sec2))
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    ALL_OBJFILE_OSECTIONS (objfile, sec)
+      if (!strcmp (bfd_section_name (objfile->obfd, sec->the_bfd_section), args))
 	{
-	  if (info_verbose)
-	    printf_unfiltered (_("Note: section %s unmapped by overlap\n"),
-			     bfd_section_name (objfile->obfd,
-					       sec2->the_bfd_section));
-	  sec2->ovly_mapped = 0;	/* sec2 overlaps sec: unmap sec2.  */
+	  /* Now, check to see if the section is an overlay.  */
+	  if (!section_is_overlay (sec))
+	    continue;		/* not an overlay section */
+
+	  /* Mark the overlay as "mapped".  */
+	  sec->ovly_mapped = 1;
+
+	  /* Next, make a pass and unmap any sections that are
+	     overlapped by this new section: */
+	  for (struct objfile *objfile2 : all_objfiles (current_program_space))
+	    ALL_OBJFILE_OSECTIONS (objfile2, sec2)
+	      if (sec2->ovly_mapped && sec != sec2 && sections_overlap (sec, sec2))
+		{
+		  if (info_verbose)
+		    printf_unfiltered (_("Note: section %s unmapped by overlap\n"),
+				       bfd_section_name (objfile->obfd,
+							 sec2->the_bfd_section));
+		  sec2->ovly_mapped = 0;	/* sec2 overlaps sec: unmap sec2.  */
+		}
+	  return;
 	}
-      return;
-    }
   error (_("No overlay section called %s"), args);
 }
 
@@ -3261,7 +3262,6 @@ map_overlay_command (const char *args, int from_tty)
 static void
 unmap_overlay_command (const char *args, int from_tty)
 {
-  struct objfile *objfile;
   struct obj_section *sec = NULL;
 
   if (!overlay_debugging)
@@ -3273,14 +3273,15 @@ unmap_overlay_command (const char *args, int from_tty)
     error (_("Argument required: name of an overlay section"));
 
   /* First, find a section matching the user supplied argument.  */
-  ALL_OBJSECTIONS (objfile, sec)
-    if (!strcmp (bfd_section_name (objfile->obfd, sec->the_bfd_section), args))
-    {
-      if (!sec->ovly_mapped)
-	error (_("Section %s is not mapped"), args);
-      sec->ovly_mapped = 0;
-      return;
-    }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    ALL_OBJFILE_OSECTIONS (objfile, sec)
+      if (!strcmp (bfd_section_name (objfile->obfd, sec->the_bfd_section), args))
+	{
+	  if (!sec->ovly_mapped)
+	    error (_("Section %s is not mapped"), args);
+	  sec->ovly_mapped = 0;
+	  return;
+	}
   error (_("No overlay section called %s"), args);
 }
 
@@ -3512,8 +3513,6 @@ simple_overlay_update_1 (struct obj_section *osect)
 void
 simple_overlay_update (struct obj_section *osect)
 {
-  struct objfile *objfile;
-
   /* Were we given an osect to look up?  NULL means do all of them.  */
   if (osect)
     /* Have we got a cached copy of the target's overlay table?  */
@@ -3545,20 +3544,21 @@ simple_overlay_update (struct obj_section *osect)
     return;
 
   /* Now may as well update all sections, even if only one was requested.  */
-  ALL_OBJSECTIONS (objfile, osect)
-    if (section_is_overlay (osect))
-    {
-      int i;
-      asection *bsect = osect->the_bfd_section;
-
-      for (i = 0; i < cache_novlys; i++)
-	if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect)
-	    && cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect))
-	  { /* obj_section matches i'th entry in ovly_table.  */
-	    osect->ovly_mapped = cache_ovly_table[i][MAPPED];
-	    break;		/* finished with inner for loop: break out.  */
-	  }
-    }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    ALL_OBJFILE_OSECTIONS (objfile, osect)
+      if (section_is_overlay (osect))
+	{
+	  int i;
+	  asection *bsect = osect->the_bfd_section;
+
+	  for (i = 0; i < cache_novlys; i++)
+	    if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect)
+		&& cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect))
+	      { /* obj_section matches i'th entry in ovly_table.  */
+		osect->ovly_mapped = cache_ovly_table[i][MAPPED];
+		break;		/* finished with inner for loop: break out.  */
+	      }
+	}
 }
 
 /* Set the output sections and output offsets for section SECTP in
-- 
2.17.2

^ permalink raw reply	[flat|nested] 36+ messages in thread

* [PATCH 03/12] Remove most uses of ALL_OBJFILES
  2018-11-25 16:54 [PATCH 00/12] Remove some ALL_* iteration macros Tom Tromey
  2018-11-25 16:54 ` [PATCH 09/12] Remove ALL_OBJFILE_FILETABS Tom Tromey
@ 2018-11-25 16:54 ` Tom Tromey
  2018-11-25 16:54 ` [PATCH 07/12] Remove ALL_COMPUNITS Tom Tromey
                   ` (12 subsequent siblings)
  14 siblings, 0 replies; 36+ messages in thread
From: Tom Tromey @ 2018-11-25 16:54 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This removes most uses of ALL_OBJFILES, replacing them with ranged for
loops.  The remaining uses are all in macros, and will be removed in
subsequent patches.

gdb/ChangeLog
2018-11-25  Tom Tromey  <tom@tromey.com>

	* symtab.c (iterate_over_symtabs, matching_obj_sections)
	(expand_symtab_containing_pc, lookup_static_symbol)
	(basic_lookup_transparent_type, find_pc_sect_compunit_symtab)
	(find_symbol_at_address, find_line_symtab, find_main_name): Use
	all_objfiles.
	* probe.c (find_probe_by_pc, collect_probes): Use all_objfiles.
	* breakpoint.c (create_overlay_event_breakpoint)
	(create_longjmp_master_breakpoint)
	(create_std_terminate_master_breakpoint)
	(create_exception_master_breakpoint): Use all_objfiles.
	* linux-thread-db.c (try_thread_db_load_from_pdir)
	(has_libpthread): Use all_objfiles.
	* ada-lang.c (add_nonlocal_symbols): Use all_objfiles.
	* linespec.c (iterate_over_all_matching_symtabs)
	(search_minsyms_for_name): Use all_objfiles.
	* maint.c (maintenance_info_sections): Use all_objfiles.
	* main.c (captured_main_1): Use all_objfiles.
	* spu-tdep.c (spu_objfile_from_frame): Use all_objfiles.
	* guile/scm-objfile.c (gdbscm_objfiles): Use all_objfiles.
	* guile/scm-pretty-print.c
	(ppscm_find_pretty_printer_from_objfiles): Use all_objfiles.
	* solib-spu.c (append_ocl_sos): Use all_objfiles.
	* symmisc.c (maintenance_print_symbols): Use all_objfiles.
	(maintenance_print_msymbols): Use all_objfiles.
	* source.c (select_source_symtab): Use all_objfiles.
	* jit.c (jit_find_objf_with_entry_addr): Use all_objfiles.
	* symfile.c (remove_symbol_file_command)
	(expand_symtabs_matching, map_symbol_filenames): Use
	all_objfiles.
	* ppc-linux-tdep.c (ppc_linux_spe_context_inferior_created): Use
	all_objfiles.
	* dwarf2-frame.c (dwarf2_frame_find_fde): Use all_objfiles.
	* objc-lang.c (find_methods): Use all_objfiles.
	* objfiles.c (have_partial_symbols, have_full_symbols)
	(have_minimal_symbols, qsort_cmp)
	(default_iterate_over_objfiles_in_search_order): Use
	all_objfiles.
	* hppa-tdep.c (find_unwind_entry): Use all_objfiles.
	* psymtab.c (maintenance_print_psymbols): Use all_objfiles.
	(maintenance_check_psymtabs): Use all_objfiles.
	(ALL_PSYMTABS): Remove.
	* compile/compile-object-run.c (do_module_cleanup): Use
	all_objfiles.
	* blockframe.c (find_pc_partial_function): Use all_objfiles.
	* cp-support.c (add_symbol_overload_list_qualified): Use
	all_objfiles.
	* windows-tdep.c (windows_iterate_over_objfiles_in_search_order):
	Use all_objfiles.
	* dwarf-index-write.c (save_gdb_index_command): Use all_objfiles.
	* python/py-xmethods.c (gdbpy_get_matching_xmethod_workers): Use
	all_objfiles.
	* python/py-objfile.c (objfpy_lookup_objfile_by_name)
	(objfpy_lookup_objfile_by_build_id): Use all_objfiles.
	* python/py-prettyprint.c (find_pretty_printer_from_objfiles):
	Uses all_objfiles.
	* solib.c (solib_read_symbols): Use all_objfiles
---
 gdb/ChangeLog                    |  59 +++++++++
 gdb/ada-lang.c                   |   5 +-
 gdb/blockframe.c                 |  26 ++--
 gdb/breakpoint.c                 | 211 +++++++++++++++----------------
 gdb/compile/compile-object-run.c |   3 +-
 gdb/cp-support.c                 |  10 +-
 gdb/dwarf-index-write.c          |  51 ++++----
 gdb/dwarf2-frame.c               |   4 +-
 gdb/guile/scm-objfile.c          |  11 +-
 gdb/guile/scm-pretty-print.c     |  25 ++--
 gdb/hppa-tdep.c                  |  99 +++++++--------
 gdb/jit.c                        |   4 +-
 gdb/linespec.c                   |  85 ++++++-------
 gdb/linux-thread-db.c            |   8 +-
 gdb/main.c                       |   3 +-
 gdb/maint.c                      |   3 +-
 gdb/objc-lang.c                  |   4 +-
 gdb/objfiles.c                   |  49 +++----
 gdb/ppc-linux-tdep.c             |   4 +-
 gdb/probe.c                      |  38 +++---
 gdb/psymtab.c                    | 159 +++++++++++------------
 gdb/python/py-objfile.c          |   8 +-
 gdb/python/py-prettyprint.c      |  38 +++---
 gdb/python/py-xmethods.c         |   4 +-
 gdb/solib-spu.c                  |   3 +-
 gdb/solib.c                      |  12 +-
 gdb/source.c                     |  14 +-
 gdb/spu-tdep.c                   |   3 +-
 gdb/symfile.c                    |  61 ++++-----
 gdb/symmisc.c                    |  18 ++-
 gdb/symtab.c                     | 192 ++++++++++++++--------------
 gdb/windows-tdep.c               |   3 +-
 32 files changed, 611 insertions(+), 606 deletions(-)

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 95bf6703ac..160184ddb3 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -5604,7 +5604,6 @@ add_nonlocal_symbols (struct obstack *obstackp,
 		      const lookup_name_info &lookup_name,
 		      domain_enum domain, int global)
 {
-  struct objfile *objfile;
   struct compunit_symtab *cu;
   struct match_data data;
 
@@ -5613,7 +5612,7 @@ add_nonlocal_symbols (struct obstack *obstackp,
 
   bool is_wild_match = lookup_name.ada ().wild_match_p ();
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       data.objfile = objfile;
 
@@ -5646,7 +5645,7 @@ add_nonlocal_symbols (struct obstack *obstackp,
       const char *name = ada_lookup_name (lookup_name);
       std::string name1 = std::string ("<_ada_") + name + '>';
 
-      ALL_OBJFILES (objfile)
+      for (struct objfile *objfile : all_objfiles (current_program_space))
         {
 	  data.objfile = objfile;
 	  objfile->sf->qf->map_matching_symbols (objfile, name1.c_str (),
diff --git a/gdb/blockframe.c b/gdb/blockframe.c
index f6dd861c36..e79490c616 100644
--- a/gdb/blockframe.c
+++ b/gdb/blockframe.c
@@ -217,7 +217,6 @@ find_pc_partial_function (CORE_ADDR pc, const char **name, CORE_ADDR *address,
   struct symbol *f;
   struct bound_minimal_symbol msymbol;
   struct compunit_symtab *compunit_symtab = NULL;
-  struct objfile *objfile;
   CORE_ADDR mapped_pc;
 
   /* To ensure that the symbol returned belongs to the correct setion
@@ -237,18 +236,19 @@ find_pc_partial_function (CORE_ADDR pc, const char **name, CORE_ADDR *address,
     goto return_cached_value;
 
   msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section);
-  ALL_OBJFILES (objfile)
-  {
-    if (objfile->sf)
-      {
-	compunit_symtab
-	  = objfile->sf->qf->find_pc_sect_compunit_symtab (objfile, msymbol,
-							   mapped_pc, section,
-							   0);
-      }
-    if (compunit_symtab != NULL)
-      break;
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      if (objfile->sf)
+	{
+	  compunit_symtab
+	    = objfile->sf->qf->find_pc_sect_compunit_symtab (objfile, msymbol,
+							     mapped_pc,
+							     section,
+							     0);
+	}
+      if (compunit_symtab != NULL)
+	break;
+    }
 
   if (compunit_symtab != NULL)
     {
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 8af3d54a77..b992fbd11a 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -3202,10 +3202,9 @@ free_breakpoint_objfile_data (struct objfile *obj, void *data)
 static void
 create_overlay_event_breakpoint (void)
 {
-  struct objfile *objfile;
   const char *const func_name = "_ovly_debug_event";
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       struct breakpoint *b;
       struct breakpoint_objfile_data *bp_objfile_data;
@@ -3261,95 +3260,93 @@ create_longjmp_master_breakpoint (void)
 
   ALL_PSPACES (pspace)
   {
-    struct objfile *objfile;
-
     set_current_program_space (pspace);
 
-    ALL_OBJFILES (objfile)
-    {
-      int i;
-      struct gdbarch *gdbarch;
-      struct breakpoint_objfile_data *bp_objfile_data;
+    for (struct objfile *objfile : all_objfiles (current_program_space))
+      {
+	int i;
+	struct gdbarch *gdbarch;
+	struct breakpoint_objfile_data *bp_objfile_data;
 
-      gdbarch = get_objfile_arch (objfile);
+	gdbarch = get_objfile_arch (objfile);
 
-      bp_objfile_data = get_breakpoint_objfile_data (objfile);
+	bp_objfile_data = get_breakpoint_objfile_data (objfile);
 
-      if (!bp_objfile_data->longjmp_searched)
-	{
-	  std::vector<probe *> ret
-	    = find_probes_in_objfile (objfile, "libc", "longjmp");
+	if (!bp_objfile_data->longjmp_searched)
+	  {
+	    std::vector<probe *> ret
+	      = find_probes_in_objfile (objfile, "libc", "longjmp");
 
-	  if (!ret.empty ())
-	    {
-	      /* We are only interested in checking one element.  */
-	      probe *p = ret[0];
+	    if (!ret.empty ())
+	      {
+		/* We are only interested in checking one element.  */
+		probe *p = ret[0];
 
-	      if (!p->can_evaluate_arguments ())
-		{
-		  /* We cannot use the probe interface here, because it does
-		     not know how to evaluate arguments.  */
-		  ret.clear ();
-		}
-	    }
-	  bp_objfile_data->longjmp_probes = ret;
-	  bp_objfile_data->longjmp_searched = 1;
-	}
+		if (!p->can_evaluate_arguments ())
+		  {
+		    /* We cannot use the probe interface here, because it does
+		       not know how to evaluate arguments.  */
+		    ret.clear ();
+		  }
+	      }
+	    bp_objfile_data->longjmp_probes = ret;
+	    bp_objfile_data->longjmp_searched = 1;
+	  }
 
-      if (!bp_objfile_data->longjmp_probes.empty ())
-	{
-	  for (probe *p : bp_objfile_data->longjmp_probes)
-	    {
-	      struct breakpoint *b;
+	if (!bp_objfile_data->longjmp_probes.empty ())
+	  {
+	    for (probe *p : bp_objfile_data->longjmp_probes)
+	      {
+		struct breakpoint *b;
+
+		b = create_internal_breakpoint (gdbarch,
+						p->get_relocated_address (objfile),
+						bp_longjmp_master,
+						&internal_breakpoint_ops);
+		b->location = new_probe_location ("-probe-stap libc:longjmp");
+		b->enable_state = bp_disabled;
+	      }
 
-	      b = create_internal_breakpoint (gdbarch,
-					      p->get_relocated_address (objfile),
-					      bp_longjmp_master,
-					      &internal_breakpoint_ops);
-	      b->location = new_probe_location ("-probe-stap libc:longjmp");
-	      b->enable_state = bp_disabled;
-	    }
+	    continue;
+	  }
 
+	if (!gdbarch_get_longjmp_target_p (gdbarch))
 	  continue;
-	}
-
-      if (!gdbarch_get_longjmp_target_p (gdbarch))
-	continue;
 
-      for (i = 0; i < NUM_LONGJMP_NAMES; i++)
-	{
-	  struct breakpoint *b;
-	  const char *func_name;
-	  CORE_ADDR addr;
-	  struct explicit_location explicit_loc;
+	for (i = 0; i < NUM_LONGJMP_NAMES; i++)
+	  {
+	    struct breakpoint *b;
+	    const char *func_name;
+	    CORE_ADDR addr;
+	    struct explicit_location explicit_loc;
 
-	  if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
-	    continue;
+	    if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
+	      continue;
 
-	  func_name = longjmp_names[i];
-	  if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
-	    {
-	      struct bound_minimal_symbol m;
+	    func_name = longjmp_names[i];
+	    if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
+	      {
+		struct bound_minimal_symbol m;
 
-	      m = lookup_minimal_symbol_text (func_name, objfile);
-	      if (m.minsym == NULL)
-		{
-		  /* Prevent future lookups in this objfile.  */
-		  bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
-		  continue;
-		}
-	      bp_objfile_data->longjmp_msym[i] = m;
-	    }
+		m = lookup_minimal_symbol_text (func_name, objfile);
+		if (m.minsym == NULL)
+		  {
+		    /* Prevent future lookups in this objfile.  */
+		    bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
+		    continue;
+		  }
+		bp_objfile_data->longjmp_msym[i] = m;
+	      }
 
-	  addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
-	  b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
-					  &internal_breakpoint_ops);
-	  initialize_explicit_location (&explicit_loc);
-	  explicit_loc.function_name = ASTRDUP (func_name);
-	  b->location = new_explicit_location (&explicit_loc);
-	  b->enable_state = bp_disabled;
-	}
-    }
+	    addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
+	    b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
+					    &internal_breakpoint_ops);
+	    initialize_explicit_location (&explicit_loc);
+	    explicit_loc.function_name = ASTRDUP (func_name);
+	    b->location = new_explicit_location (&explicit_loc);
+	    b->enable_state = bp_disabled;
+	  }
+      }
   }
 }
 
@@ -3364,46 +3361,45 @@ create_std_terminate_master_breakpoint (void)
 
   ALL_PSPACES (pspace)
   {
-    struct objfile *objfile;
     CORE_ADDR addr;
 
     set_current_program_space (pspace);
 
-    ALL_OBJFILES (objfile)
-    {
-      struct breakpoint *b;
-      struct breakpoint_objfile_data *bp_objfile_data;
-      struct explicit_location explicit_loc;
+    for (struct objfile *objfile : all_objfiles (current_program_space))
+      {
+	struct breakpoint *b;
+	struct breakpoint_objfile_data *bp_objfile_data;
+	struct explicit_location explicit_loc;
 
-      bp_objfile_data = get_breakpoint_objfile_data (objfile);
+	bp_objfile_data = get_breakpoint_objfile_data (objfile);
 
-      if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
-	continue;
+	if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
+	  continue;
 
-      if (bp_objfile_data->terminate_msym.minsym == NULL)
-	{
-	  struct bound_minimal_symbol m;
+	if (bp_objfile_data->terminate_msym.minsym == NULL)
+	  {
+	    struct bound_minimal_symbol m;
 
-	  m = lookup_minimal_symbol (func_name, NULL, objfile);
-	  if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
-				   && MSYMBOL_TYPE (m.minsym) != mst_file_text))
-	    {
-	      /* Prevent future lookups in this objfile.  */
-	      bp_objfile_data->terminate_msym.minsym = &msym_not_found;
-	      continue;
-	    }
-	  bp_objfile_data->terminate_msym = m;
-	}
+	    m = lookup_minimal_symbol (func_name, NULL, objfile);
+	    if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
+				     && MSYMBOL_TYPE (m.minsym) != mst_file_text))
+	      {
+		/* Prevent future lookups in this objfile.  */
+		bp_objfile_data->terminate_msym.minsym = &msym_not_found;
+		continue;
+	      }
+	    bp_objfile_data->terminate_msym = m;
+	  }
 
-      addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
-      b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
-                                      bp_std_terminate_master,
-				      &internal_breakpoint_ops);
-      initialize_explicit_location (&explicit_loc);
-      explicit_loc.function_name = ASTRDUP (func_name);
-      b->location = new_explicit_location (&explicit_loc);
-      b->enable_state = bp_disabled;
-    }
+	addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
+	b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
+					bp_std_terminate_master,
+					&internal_breakpoint_ops);
+	initialize_explicit_location (&explicit_loc);
+	explicit_loc.function_name = ASTRDUP (func_name);
+	b->location = new_explicit_location (&explicit_loc);
+	b->enable_state = bp_disabled;
+      }
   }
 }
 
@@ -3412,10 +3408,9 @@ create_std_terminate_master_breakpoint (void)
 static void
 create_exception_master_breakpoint (void)
 {
-  struct objfile *objfile;
   const char *const func_name = "_Unwind_DebugHook";
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       struct breakpoint *b;
       struct gdbarch *gdbarch;
diff --git a/gdb/compile/compile-object-run.c b/gdb/compile/compile-object-run.c
index e891e77fba..7bbfb64851 100644
--- a/gdb/compile/compile-object-run.c
+++ b/gdb/compile/compile-object-run.c
@@ -62,7 +62,6 @@ static void
 do_module_cleanup (void *arg, int registers_valid)
 {
   struct do_module_cleanup *data = (struct do_module_cleanup *) arg;
-  struct objfile *objfile;
 
   if (data->executedp != NULL)
     {
@@ -83,7 +82,7 @@ do_module_cleanup (void *arg, int registers_valid)
 	}
     }
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     if ((objfile->flags & OBJF_USERLOADED) == 0
         && (strcmp (objfile_name (objfile), data->objfile_name_string) == 0))
       {
diff --git a/gdb/cp-support.c b/gdb/cp-support.c
index 1f95253cb0..184d637da6 100644
--- a/gdb/cp-support.c
+++ b/gdb/cp-support.c
@@ -1381,11 +1381,11 @@ add_symbol_overload_list_qualified (const char *func_name,
   /* Look through the partial symtabs for all symbols which begin by
      matching FUNC_NAME.  Make sure we read that symbol table in.  */
 
-  ALL_OBJFILES (objfile)
-  {
-    if (objfile->sf)
-      objfile->sf->qf->expand_symtabs_for_function (objfile, func_name);
-  }
+  for (struct objfile *objf : all_objfiles (current_program_space))
+    {
+      if (objf->sf)
+	objf->sf->qf->expand_symtabs_for_function (objf, func_name);
+    }
 
   /* Search upwards from currently selected frame (so that we can
      complete on local vars.  */
diff --git a/gdb/dwarf-index-write.c b/gdb/dwarf-index-write.c
index 8a4c1c7ea4..e2066c6cf1 100644
--- a/gdb/dwarf-index-write.c
+++ b/gdb/dwarf-index-write.c
@@ -1640,7 +1640,6 @@ write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
 static void
 save_gdb_index_command (const char *arg, int from_tty)
 {
-  struct objfile *objfile;
   const char dwarf5space[] = "-dwarf-5 ";
   dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
 
@@ -1658,35 +1657,35 @@ save_gdb_index_command (const char *arg, int from_tty)
   if (!*arg)
     error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
 
-  ALL_OBJFILES (objfile)
-  {
-    struct stat st;
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      struct stat st;
 
-    /* If the objfile does not correspond to an actual file, skip it.  */
-    if (stat (objfile_name (objfile), &st) < 0)
-      continue;
+      /* If the objfile does not correspond to an actual file, skip it.  */
+      if (stat (objfile_name (objfile), &st) < 0)
+	continue;
 
-    struct dwarf2_per_objfile *dwarf2_per_objfile
-      = get_dwarf2_per_objfile (objfile);
+      struct dwarf2_per_objfile *dwarf2_per_objfile
+	= get_dwarf2_per_objfile (objfile);
 
-    if (dwarf2_per_objfile != NULL)
-      {
-	TRY
-	  {
-	    const char *basename = lbasename (objfile_name (objfile));
-	    write_psymtabs_to_index (dwarf2_per_objfile, arg, basename,
-				     index_kind);
-	  }
-	CATCH (except, RETURN_MASK_ERROR)
-	  {
-	    exception_fprintf (gdb_stderr, except,
-			       _("Error while writing index for `%s': "),
-			       objfile_name (objfile));
-	  }
-	END_CATCH
-      }
+      if (dwarf2_per_objfile != NULL)
+	{
+	  TRY
+	    {
+	      const char *basename = lbasename (objfile_name (objfile));
+	      write_psymtabs_to_index (dwarf2_per_objfile, arg, basename,
+				       index_kind);
+	    }
+	  CATCH (except, RETURN_MASK_ERROR)
+	    {
+	      exception_fprintf (gdb_stderr, except,
+				 _("Error while writing index for `%s': "),
+				 objfile_name (objfile));
+	    }
+	  END_CATCH
+	    }
 
-  }
+    }
 }
 
 void
diff --git a/gdb/dwarf2-frame.c b/gdb/dwarf2-frame.c
index 2201c63659..d0b52af202 100644
--- a/gdb/dwarf2-frame.c
+++ b/gdb/dwarf2-frame.c
@@ -1702,9 +1702,7 @@ bsearch_fde_cmp (const void *key, const void *element)
 static struct dwarf2_fde *
 dwarf2_frame_find_fde (CORE_ADDR *pc, CORE_ADDR *out_offset)
 {
-  struct objfile *objfile;
-
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       struct dwarf2_fde_table *fde_table;
       struct dwarf2_fde **p_fde;
diff --git a/gdb/guile/scm-objfile.c b/gdb/guile/scm-objfile.c
index ccf7c66d33..3bdf9555e6 100644
--- a/gdb/guile/scm-objfile.c
+++ b/gdb/guile/scm-objfile.c
@@ -366,17 +366,16 @@ gdbscm_get_current_objfile (void)
 static SCM
 gdbscm_objfiles (void)
 {
-  struct objfile *objf;
   SCM result;
 
   result = SCM_EOL;
 
-  ALL_OBJFILES (objf)
-  {
-    SCM item = ofscm_scm_from_objfile (objf);
+  for (struct objfile *objf : all_objfiles (current_program_space))
+    {
+      SCM item = ofscm_scm_from_objfile (objf);
 
-    result = scm_cons (item, result);
-  }
+      result = scm_cons (item, result);
+    }
 
   return scm_reverse_x (result, SCM_EOL);
 }
diff --git a/gdb/guile/scm-pretty-print.c b/gdb/guile/scm-pretty-print.c
index f292960dbb..20342dc4d8 100644
--- a/gdb/guile/scm-pretty-print.c
+++ b/gdb/guile/scm-pretty-print.c
@@ -427,19 +427,18 @@ ppscm_search_pp_list (SCM list, SCM value)
 static SCM
 ppscm_find_pretty_printer_from_objfiles (SCM value)
 {
-  struct objfile *objfile;
-
-  ALL_OBJFILES (objfile)
-  {
-    objfile_smob *o_smob = ofscm_objfile_smob_from_objfile (objfile);
-    SCM pp = ppscm_search_pp_list (ofscm_objfile_smob_pretty_printers (o_smob),
-				   value);
-
-    /* Note: This will return if pp is a <gdb:exception> object,
-       which is what we want.  */
-    if (gdbscm_is_true (pp))
-      return pp;
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      objfile_smob *o_smob = ofscm_objfile_smob_from_objfile (objfile);
+      SCM pp
+	= ppscm_search_pp_list (ofscm_objfile_smob_pretty_printers (o_smob),
+				value);
+
+      /* Note: This will return if pp is a <gdb:exception> object,
+	 which is what we want.  */
+      if (gdbscm_is_true (pp))
+	return pp;
+    }
 
   return SCM_BOOL_F;
 }
diff --git a/gdb/hppa-tdep.c b/gdb/hppa-tdep.c
index d22b2cf5d2..9f70ef9b73 100644
--- a/gdb/hppa-tdep.c
+++ b/gdb/hppa-tdep.c
@@ -483,7 +483,6 @@ struct unwind_table_entry *
 find_unwind_entry (CORE_ADDR pc)
 {
   int first, middle, last;
-  struct objfile *objfile;
   struct hppa_objfile_private *priv;
 
   if (hppa_debug)
@@ -498,61 +497,61 @@ find_unwind_entry (CORE_ADDR pc)
       return NULL;
     }
 
-  ALL_OBJFILES (objfile)
-  {
-    struct hppa_unwind_info *ui;
-    ui = NULL;
-    priv = ((struct hppa_objfile_private *)
-	    objfile_data (objfile, hppa_objfile_priv_data));
-    if (priv)
-      ui = ((struct hppa_objfile_private *) priv)->unwind_info;
-
-    if (!ui)
-      {
-	read_unwind_info (objfile);
-        priv = ((struct hppa_objfile_private *)
-		objfile_data (objfile, hppa_objfile_priv_data));
-	if (priv == NULL)
-	  error (_("Internal error reading unwind information."));
-        ui = ((struct hppa_objfile_private *) priv)->unwind_info;
-      }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      struct hppa_unwind_info *ui;
+      ui = NULL;
+      priv = ((struct hppa_objfile_private *)
+	      objfile_data (objfile, hppa_objfile_priv_data));
+      if (priv)
+	ui = ((struct hppa_objfile_private *) priv)->unwind_info;
+
+      if (!ui)
+	{
+	  read_unwind_info (objfile);
+	  priv = ((struct hppa_objfile_private *)
+		  objfile_data (objfile, hppa_objfile_priv_data));
+	  if (priv == NULL)
+	    error (_("Internal error reading unwind information."));
+	  ui = ((struct hppa_objfile_private *) priv)->unwind_info;
+	}
 
-    /* First, check the cache.  */
+      /* First, check the cache.  */
 
-    if (ui->cache
-	&& pc >= ui->cache->region_start
-	&& pc <= ui->cache->region_end)
-      {
-	if (hppa_debug)
-	  fprintf_unfiltered (gdb_stdlog, "%s (cached) }\n",
-            hex_string ((uintptr_t) ui->cache));
-        return ui->cache;
-      }
+      if (ui->cache
+	  && pc >= ui->cache->region_start
+	  && pc <= ui->cache->region_end)
+	{
+	  if (hppa_debug)
+	    fprintf_unfiltered (gdb_stdlog, "%s (cached) }\n",
+				hex_string ((uintptr_t) ui->cache));
+	  return ui->cache;
+	}
 
-    /* Not in the cache, do a binary search.  */
+      /* Not in the cache, do a binary search.  */
 
-    first = 0;
-    last = ui->last;
+      first = 0;
+      last = ui->last;
 
-    while (first <= last)
-      {
-	middle = (first + last) / 2;
-	if (pc >= ui->table[middle].region_start
-	    && pc <= ui->table[middle].region_end)
-	  {
-	    ui->cache = &ui->table[middle];
-	    if (hppa_debug)
-	      fprintf_unfiltered (gdb_stdlog, "%s }\n",
-                hex_string ((uintptr_t) ui->cache));
-	    return &ui->table[middle];
-	  }
+      while (first <= last)
+	{
+	  middle = (first + last) / 2;
+	  if (pc >= ui->table[middle].region_start
+	      && pc <= ui->table[middle].region_end)
+	    {
+	      ui->cache = &ui->table[middle];
+	      if (hppa_debug)
+		fprintf_unfiltered (gdb_stdlog, "%s }\n",
+				    hex_string ((uintptr_t) ui->cache));
+	      return &ui->table[middle];
+	    }
 
-	if (pc < ui->table[middle].region_start)
-	  last = middle - 1;
-	else
-	  first = middle + 1;
-      }
-  }				/* ALL_OBJFILES() */
+	  if (pc < ui->table[middle].region_start)
+	    last = middle - 1;
+	  else
+	    first = middle + 1;
+	}
+    }
 
   if (hppa_debug)
     fprintf_unfiltered (gdb_stdlog, "NULL (not found) }\n");
diff --git a/gdb/jit.c b/gdb/jit.c
index e6b3cc25ca..2f5f6f8ade 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -986,9 +986,7 @@ jit_unregister_code (struct objfile *objfile)
 static struct objfile *
 jit_find_objf_with_entry_addr (CORE_ADDR entry_addr)
 {
-  struct objfile *objf;
-
-  ALL_OBJFILES (objf)
+  for (struct objfile *objf : all_objfiles (current_program_space))
     {
       struct jit_objfile_data *objf_data;
 
diff --git a/gdb/linespec.c b/gdb/linespec.c
index 00f59f9c28..8e0fb866e8 100644
--- a/gdb/linespec.c
+++ b/gdb/linespec.c
@@ -1125,7 +1125,6 @@ iterate_over_all_matching_symtabs
    struct program_space *search_pspace, bool include_inline,
    gdb::function_view<symbol_found_callback_ftype> callback)
 {
-  struct objfile *objfile;
   struct program_space *pspace;
 
   ALL_PSPACES (pspace)
@@ -1137,46 +1136,48 @@ iterate_over_all_matching_symtabs
 
     set_current_program_space (pspace);
 
-    ALL_OBJFILES (objfile)
-    {
-      struct compunit_symtab *cu;
+    for (struct objfile *objfile : all_objfiles (current_program_space))
+      {
+	struct compunit_symtab *cu;
 
-      if (objfile->sf)
-	objfile->sf->qf->expand_symtabs_matching (objfile,
-						  NULL,
-						  lookup_name,
-						  NULL, NULL,
-						  search_domain);
+	if (objfile->sf)
+	  objfile->sf->qf->expand_symtabs_matching (objfile,
+						    NULL,
+						    lookup_name,
+						    NULL, NULL,
+						    search_domain);
 
-      ALL_OBJFILE_COMPUNITS (objfile, cu)
-	{
-	  struct symtab *symtab = COMPUNIT_FILETABS (cu);
+	ALL_OBJFILE_COMPUNITS (objfile, cu)
+	  {
+	    struct symtab *symtab = COMPUNIT_FILETABS (cu);
 
-	  iterate_over_file_blocks (symtab, lookup_name, name_domain, callback);
+	    iterate_over_file_blocks (symtab, lookup_name, name_domain,
+				      callback);
 
-	  if (include_inline)
-	    {
-	      struct block *block;
-	      int i;
+	    if (include_inline)
+	      {
+		struct block *block;
+		int i;
 
-	      for (i = FIRST_LOCAL_BLOCK;
-		   i < BLOCKVECTOR_NBLOCKS (SYMTAB_BLOCKVECTOR (symtab));
-		   i++)
-		{
-		  block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), i);
-		  state->language->la_iterate_over_symbols
-		    (block, lookup_name, name_domain, [&] (block_symbol *bsym)
-		     {
-		       /* Restrict calls to CALLBACK to symbols
-			  representing inline symbols only.  */
-		       if (SYMBOL_INLINED (bsym->symbol))
-			 return callback (bsym);
-		       return true;
-		     });
-		}
-	    }
-	}
-    }
+		for (i = FIRST_LOCAL_BLOCK;
+		     i < BLOCKVECTOR_NBLOCKS (SYMTAB_BLOCKVECTOR (symtab));
+		     i++)
+		  {
+		    block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), i);
+		    state->language->la_iterate_over_symbols
+		      (block, lookup_name, name_domain,
+		       [&] (block_symbol *bsym)
+		       {
+			 /* Restrict calls to CALLBACK to symbols
+			    representing inline symbols only.  */
+			 if (SYMBOL_INLINED (bsym->symbol))
+			   return callback (bsym);
+			 return true;
+		       });
+		  }
+	      }
+	  }
+      }
   }
 }
 
@@ -4400,8 +4401,6 @@ search_minsyms_for_name (struct collect_info *info,
 
       ALL_PSPACES (pspace)
       {
-	struct objfile *objfile;
-
 	if (search_pspace != NULL && search_pspace != pspace)
 	  continue;
 	if (pspace->executing_startup)
@@ -4409,17 +4408,17 @@ search_minsyms_for_name (struct collect_info *info,
 
 	set_current_program_space (pspace);
 
-	ALL_OBJFILES (objfile)
-	{
-	  iterate_over_minimal_symbols (objfile, name,
-					[&] (struct minimal_symbol *msym)
+	for (struct objfile *objfile : all_objfiles (current_program_space))
+	  {
+	    iterate_over_minimal_symbols (objfile, name,
+					  [&] (struct minimal_symbol *msym)
 					  {
 					    add_minsym (msym, objfile, nullptr,
 							info->state->list_mode,
 							&minsyms);
 					    return false;
 					  });
-	}
+	  }
       }
     }
   else
diff --git a/gdb/linux-thread-db.c b/gdb/linux-thread-db.c
index 74acec2629..d4213c1f50 100644
--- a/gdb/linux-thread-db.c
+++ b/gdb/linux-thread-db.c
@@ -1044,12 +1044,10 @@ try_thread_db_load_from_pdir_1 (struct objfile *obj, const char *subdir)
 static int
 try_thread_db_load_from_pdir (const char *subdir)
 {
-  struct objfile *obj;
-
   if (!auto_load_thread_db)
     return 0;
 
-  ALL_OBJFILES (obj)
+  for (struct objfile *obj : all_objfiles (current_program_space))
     if (libpthread_name_p (objfile_name (obj)))
       {
 	if (try_thread_db_load_from_pdir_1 (obj, subdir))
@@ -1158,9 +1156,7 @@ thread_db_load_search (void)
 static int
 has_libpthread (void)
 {
-  struct objfile *obj;
-
-  ALL_OBJFILES (obj)
+  for (struct objfile *obj : all_objfiles (current_program_space))
     if (libpthread_name_p (objfile_name (obj)))
       return 1;
 
diff --git a/gdb/main.c b/gdb/main.c
index c71d5b557f..639230652b 100644
--- a/gdb/main.c
+++ b/gdb/main.c
@@ -485,7 +485,6 @@ captured_main_1 (struct captured_main_args *context)
 
   int i;
   int save_auto_load;
-  struct objfile *objfile;
   int ret = 1;
 
 #ifdef HAVE_USEFUL_SBRK
@@ -1124,7 +1123,7 @@ captured_main_1 (struct captured_main_args *context)
      We wait until now because it is common to add to the source search
      path in local_gdbinit.  */
   global_auto_load = save_auto_load;
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     load_auto_scripts_for_objfile (objfile);
 
   /* Process '-x' and '-ex' options.  */
diff --git a/gdb/maint.c b/gdb/maint.c
index 01a80f5d73..5a4aa83eaf 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -348,7 +348,6 @@ maintenance_info_sections (const char *arg, int from_tty)
       printf_filtered (_("file type %s.\n"), bfd_get_target (exec_bfd));
       if (arg && *arg && match_substring (arg, "ALLOBJ"))
 	{
-	  struct objfile *ofile;
 	  struct obj_section *osect;
 
 	  /* Only this function cares about the 'ALLOBJ' argument; 
@@ -358,7 +357,7 @@ maintenance_info_sections (const char *arg, int from_tty)
 	  if (strcmp (arg, "ALLOBJ") == 0)
 	    arg = NULL;
 
-	  ALL_OBJFILES (ofile)
+	  for (struct objfile *ofile : all_objfiles (current_program_space))
 	    {
 	      printf_filtered (_("  Object file: %s\n"), 
 			       bfd_get_filename (ofile->obfd));
diff --git a/gdb/objc-lang.c b/gdb/objc-lang.c
index d51362a4d3..4933e0c343 100644
--- a/gdb/objc-lang.c
+++ b/gdb/objc-lang.c
@@ -972,8 +972,6 @@ find_methods (char type, const char *theclass, const char *category,
 	      const char *selector,
 	      std::vector<const char *> *symbol_names)
 {
-  struct objfile *objfile = NULL;
-
   const char *symname = NULL;
 
   char ntype = '\0';
@@ -986,7 +984,7 @@ find_methods (char type, const char *theclass, const char *category,
 
   gdb_assert (symbol_names != NULL);
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       unsigned int *objc_csym;
       struct minimal_symbol *msymbol = NULL;
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index dec92df816..90d2502dde 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -1016,13 +1016,11 @@ objfile_has_symbols (struct objfile *objfile)
 int
 have_partial_symbols (void)
 {
-  struct objfile *ofp;
-
-  ALL_OBJFILES (ofp)
-  {
-    if (objfile_has_partial_symbols (ofp))
-      return 1;
-  }
+  for (struct objfile *ofp : all_objfiles (current_program_space))
+    {
+      if (objfile_has_partial_symbols (ofp))
+	return 1;
+    }
   return 0;
 }
 
@@ -1033,13 +1031,11 @@ have_partial_symbols (void)
 int
 have_full_symbols (void)
 {
-  struct objfile *ofp;
-
-  ALL_OBJFILES (ofp)
-  {
-    if (objfile_has_full_symbols (ofp))
-      return 1;
-  }
+  for (struct objfile *ofp : all_objfiles (current_program_space))
+    {
+      if (objfile_has_full_symbols (ofp))
+	return 1;
+    }
   return 0;
 }
 
@@ -1072,15 +1068,13 @@ objfile_purge_solibs (void)
 int
 have_minimal_symbols (void)
 {
-  struct objfile *ofp;
-
-  ALL_OBJFILES (ofp)
-  {
-    if (ofp->per_bfd->minimal_symbol_count > 0)
-      {
-	return 1;
-      }
-  }
+  for (struct objfile *ofp : all_objfiles (current_program_space))
+    {
+      if (ofp->per_bfd->minimal_symbol_count > 0)
+	{
+	  return 1;
+	}
+    }
   return 0;
 }
 
@@ -1145,9 +1139,7 @@ qsort_cmp (const void *a, const void *b)
 	{
 	  /* Sort on sequence number of the objfile in the chain.  */
 
-	  const struct objfile *objfile;
-
-	  ALL_OBJFILES (objfile)
+	  for (struct objfile *objfile : all_objfiles (current_program_space))
 	    if (objfile == objfile1)
 	      return -1;
 	    else if (objfile == objfile2)
@@ -1502,7 +1494,7 @@ shared_objfile_contains_address_p (struct program_space *pspace,
 }
 
 /* The default implementation for the "iterate_over_objfiles_in_search_order"
-   gdbarch method.  It is equivalent to use the ALL_OBJFILES macro,
+   gdbarch method.  It is equivalent to use the all_objfiles iterable,
    searching the objfiles in the order they are stored internally,
    ignoring CURRENT_OBJFILE.
 
@@ -1516,9 +1508,8 @@ default_iterate_over_objfiles_in_search_order
    void *cb_data, struct objfile *current_objfile)
 {
   int stop = 0;
-  struct objfile *objfile;
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
        stop = cb (objfile, cb_data);
        if (stop)
diff --git a/gdb/ppc-linux-tdep.c b/gdb/ppc-linux-tdep.c
index ed44cbe555..c6d8546de1 100644
--- a/gdb/ppc-linux-tdep.c
+++ b/gdb/ppc-linux-tdep.c
@@ -1788,10 +1788,8 @@ ppc_linux_spe_context_lookup (struct objfile *objfile)
 static void
 ppc_linux_spe_context_inferior_created (struct target_ops *t, int from_tty)
 {
-  struct objfile *objfile;
-
   ppc_linux_spe_context_lookup (NULL);
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     ppc_linux_spe_context_lookup (objfile);
 }
 
diff --git a/gdb/probe.c b/gdb/probe.c
index ace8efb541..db10691093 100644
--- a/gdb/probe.c
+++ b/gdb/probe.c
@@ -244,29 +244,28 @@ find_probes_in_objfile (struct objfile *objfile, const char *provider,
 struct bound_probe
 find_probe_by_pc (CORE_ADDR pc)
 {
-  struct objfile *objfile;
   struct bound_probe result;
 
   result.objfile = NULL;
   result.prob = NULL;
 
-  ALL_OBJFILES (objfile)
-  {
-    if (!objfile->sf || !objfile->sf->sym_probe_fns
-	|| objfile->sect_index_text == -1)
-      continue;
-
-    /* If this proves too inefficient, we can replace with a hash.  */
-    const std::vector<probe *> &probes
-      = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
-    for (probe *p : probes)
-      if (p->get_relocated_address (objfile) == pc)
-	{
-	  result.objfile = objfile;
-	  result.prob = p;
-	  return result;
-	}
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      if (!objfile->sf || !objfile->sf->sym_probe_fns
+	  || objfile->sect_index_text == -1)
+	continue;
+
+      /* If this proves too inefficient, we can replace with a hash.  */
+      const std::vector<probe *> &probes
+	= objfile->sf->sym_probe_fns->sym_get_probes (objfile);
+      for (probe *p : probes)
+	if (p->get_relocated_address (objfile) == pc)
+	  {
+	    result.objfile = objfile;
+	    result.prob = p;
+	    return result;
+	  }
+    }
 
   return result;
 }
@@ -282,7 +281,6 @@ static std::vector<bound_probe>
 collect_probes (const std::string &objname, const std::string &provider,
 		const std::string &probe_name, const static_probe_ops *spops)
 {
-  struct objfile *objfile;
   std::vector<bound_probe> result;
   gdb::optional<compiled_regex> obj_pat, prov_pat, probe_pat;
 
@@ -296,7 +294,7 @@ collect_probes (const std::string &objname, const std::string &provider,
     obj_pat.emplace (objname.c_str (), REG_NOSUB,
 		     _("Invalid object file regexp"));
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       if (! objfile->sf || ! objfile->sf->sym_probe_fns)
 	continue;
diff --git a/gdb/psymtab.c b/gdb/psymtab.c
index 0ee5c9d516..61fb0ebb70 100644
--- a/gdb/psymtab.c
+++ b/gdb/psymtab.c
@@ -108,12 +108,6 @@ require_partial_symbols (struct objfile *objfile, int verbose)
 
 #undef ALL_OBJFILE_PSYMTABS
 
-/* Traverse all psymtabs in all objfiles.  */
-
-#define ALL_PSYMTABS(objfile, p) \
-  ALL_OBJFILES (objfile)	 \
-    ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
-
 /* Helper function for psym_map_symtabs_matching_filename that
    expands the symtabs and calls the iterator.  */
 
@@ -1874,7 +1868,6 @@ maintenance_print_psymbols (const char *args, int from_tty)
 {
   struct ui_file *outfile = gdb_stdout;
   char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
-  struct objfile *objfile;
   struct partial_symtab *ps;
   int i, outfile_idx, found;
   CORE_ADDR pc = 0;
@@ -1944,7 +1937,7 @@ maintenance_print_psymbols (const char *args, int from_tty)
     }
 
   found = 0;
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       int printed_objfile_header = 0;
       int print_for_objfile = 1;
@@ -2148,93 +2141,93 @@ maintenance_check_psymtabs (const char *ignore, int from_tty)
   struct compunit_symtab *cust = NULL;
   struct partial_symtab *ps;
   const struct blockvector *bv;
-  struct objfile *objfile;
   struct block *b;
   int length;
 
-  ALL_PSYMTABS (objfile, ps)
-  {
-    struct gdbarch *gdbarch = get_objfile_arch (objfile);
-
-    /* We don't call psymtab_to_symtab here because that may cause symtab
-       expansion.  When debugging a problem it helps if checkers leave
-       things unchanged.  */
-    cust = ps->compunit_symtab;
-
-    /* First do some checks that don't require the associated symtab.  */
-    if (ps->text_high (objfile) < ps->text_low (objfile))
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
       {
-	printf_filtered ("Psymtab ");
-	puts_filtered (ps->filename);
-	printf_filtered (" covers bad range ");
-	fputs_filtered (paddress (gdbarch, ps->text_low (objfile)),
-			gdb_stdout);
-	printf_filtered (" - ");
-	fputs_filtered (paddress (gdbarch, ps->text_high (objfile)),
-			gdb_stdout);
-	printf_filtered ("\n");
-	continue;
-      }
+	struct gdbarch *gdbarch = get_objfile_arch (objfile);
 
-    /* Now do checks requiring the associated symtab.  */
-    if (cust == NULL)
-      continue;
-    bv = COMPUNIT_BLOCKVECTOR (cust);
-    b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
-    partial_symbol **psym = &objfile->static_psymbols[ps->statics_offset];
-    length = ps->n_static_syms;
-    while (length--)
-      {
-	sym = block_lookup_symbol (b, symbol_search_name (*psym),
-				   symbol_name_match_type::SEARCH_NAME,
-				   (*psym)->domain);
-	if (!sym)
+	/* We don't call psymtab_to_symtab here because that may cause symtab
+	   expansion.  When debugging a problem it helps if checkers leave
+	   things unchanged.  */
+	cust = ps->compunit_symtab;
+
+	/* First do some checks that don't require the associated symtab.  */
+	if (ps->text_high (objfile) < ps->text_low (objfile))
 	  {
-	    printf_filtered ("Static symbol `");
-	    puts_filtered ((*psym)->name);
-	    printf_filtered ("' only found in ");
+	    printf_filtered ("Psymtab ");
 	    puts_filtered (ps->filename);
-	    printf_filtered (" psymtab\n");
+	    printf_filtered (" covers bad range ");
+	    fputs_filtered (paddress (gdbarch, ps->text_low (objfile)),
+			    gdb_stdout);
+	    printf_filtered (" - ");
+	    fputs_filtered (paddress (gdbarch, ps->text_high (objfile)),
+			    gdb_stdout);
+	    printf_filtered ("\n");
+	    continue;
 	  }
-	psym++;
-      }
-    b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
-    psym = &objfile->global_psymbols[ps->globals_offset];
-    length = ps->n_global_syms;
-    while (length--)
-      {
-	sym = block_lookup_symbol (b, symbol_search_name (*psym),
-				   symbol_name_match_type::SEARCH_NAME,
-				   (*psym)->domain);
-	if (!sym)
+
+	/* Now do checks requiring the associated symtab.  */
+	if (cust == NULL)
+	  continue;
+	bv = COMPUNIT_BLOCKVECTOR (cust);
+	b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
+	partial_symbol **psym = &objfile->static_psymbols[ps->statics_offset];
+	length = ps->n_static_syms;
+	while (length--)
+	  {
+	    sym = block_lookup_symbol (b, symbol_search_name (*psym),
+				       symbol_name_match_type::SEARCH_NAME,
+				       (*psym)->domain);
+	    if (!sym)
+	      {
+		printf_filtered ("Static symbol `");
+		puts_filtered ((*psym)->name);
+		printf_filtered ("' only found in ");
+		puts_filtered (ps->filename);
+		printf_filtered (" psymtab\n");
+	      }
+	    psym++;
+	  }
+	b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
+	psym = &objfile->global_psymbols[ps->globals_offset];
+	length = ps->n_global_syms;
+	while (length--)
 	  {
-	    printf_filtered ("Global symbol `");
-	    puts_filtered ((*psym)->name);
-	    printf_filtered ("' only found in ");
+	    sym = block_lookup_symbol (b, symbol_search_name (*psym),
+				       symbol_name_match_type::SEARCH_NAME,
+				       (*psym)->domain);
+	    if (!sym)
+	      {
+		printf_filtered ("Global symbol `");
+		puts_filtered ((*psym)->name);
+		printf_filtered ("' only found in ");
+		puts_filtered (ps->filename);
+		printf_filtered (" psymtab\n");
+	      }
+	    psym++;
+	  }
+	if (ps->raw_text_high () != 0
+	    && (ps->text_low (objfile) < BLOCK_START (b)
+		|| ps->text_high (objfile) > BLOCK_END (b)))
+	  {
+	    printf_filtered ("Psymtab ");
 	    puts_filtered (ps->filename);
-	    printf_filtered (" psymtab\n");
+	    printf_filtered (" covers ");
+	    fputs_filtered (paddress (gdbarch, ps->text_low (objfile)),
+			    gdb_stdout);
+	    printf_filtered (" - ");
+	    fputs_filtered (paddress (gdbarch, ps->text_high (objfile)),
+			    gdb_stdout);
+	    printf_filtered (" but symtab covers only ");
+	    fputs_filtered (paddress (gdbarch, BLOCK_START (b)), gdb_stdout);
+	    printf_filtered (" - ");
+	    fputs_filtered (paddress (gdbarch, BLOCK_END (b)), gdb_stdout);
+	    printf_filtered ("\n");
 	  }
-	psym++;
       }
-    if (ps->raw_text_high () != 0
-	&& (ps->text_low (objfile) < BLOCK_START (b)
-	    || ps->text_high (objfile) > BLOCK_END (b)))
-      {
-	printf_filtered ("Psymtab ");
-	puts_filtered (ps->filename);
-	printf_filtered (" covers ");
-	fputs_filtered (paddress (gdbarch, ps->text_low (objfile)),
-			gdb_stdout);
-	printf_filtered (" - ");
-	fputs_filtered (paddress (gdbarch, ps->text_high (objfile)),
-			gdb_stdout);
-	printf_filtered (" but symtab covers only ");
-	fputs_filtered (paddress (gdbarch, BLOCK_START (b)), gdb_stdout);
-	printf_filtered (" - ");
-	fputs_filtered (paddress (gdbarch, BLOCK_END (b)), gdb_stdout);
-	printf_filtered ("\n");
-      }
-  }
 }
 
 void
diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c
index dc7f342aef..9ec4fa4778 100644
--- a/gdb/python/py-objfile.c
+++ b/gdb/python/py-objfile.c
@@ -511,9 +511,7 @@ objfpy_build_id_matches (const struct bfd_build_id *build_id,
 static struct objfile *
 objfpy_lookup_objfile_by_name (const char *name)
 {
-  struct objfile *objfile;
-
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       const char *filename;
 
@@ -539,9 +537,7 @@ objfpy_lookup_objfile_by_name (const char *name)
 static struct objfile *
 objfpy_lookup_objfile_by_build_id (const char *build_id)
 {
-  struct objfile *objfile;
-
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       const struct bfd_build_id *obfd_build_id;
 
diff --git a/gdb/python/py-prettyprint.c b/gdb/python/py-prettyprint.c
index fa4107c30d..679247ae73 100644
--- a/gdb/python/py-prettyprint.c
+++ b/gdb/python/py-prettyprint.c
@@ -93,28 +93,26 @@ search_pp_list (PyObject *list, PyObject *value)
 static PyObject *
 find_pretty_printer_from_objfiles (PyObject *value)
 {
-  struct objfile *obj;
+  for (struct objfile *obj : all_objfiles (current_program_space))
+    {
+      gdbpy_ref<> objf = objfile_to_objfile_object (obj);
+      if (objf == NULL)
+	{
+	  /* Ignore the error and continue.  */
+	  PyErr_Clear ();
+	  continue;
+	}
 
-  ALL_OBJFILES (obj)
-  {
-    gdbpy_ref<> objf = objfile_to_objfile_object (obj);
-    if (objf == NULL)
-      {
-	/* Ignore the error and continue.  */
-	PyErr_Clear ();
-	continue;
-      }
-
-    gdbpy_ref<> pp_list (objfpy_get_printers (objf.get (), NULL));
-    gdbpy_ref<> function (search_pp_list (pp_list.get (), value));
-
-    /* If there is an error in any objfile list, abort the search and exit.  */
-    if (function == NULL)
-      return NULL;
+      gdbpy_ref<> pp_list (objfpy_get_printers (objf.get (), NULL));
+      gdbpy_ref<> function (search_pp_list (pp_list.get (), value));
 
-    if (function != Py_None)
-      return function.release ();
-  }
+      /* If there is an error in any objfile list, abort the search and exit.  */
+      if (function == NULL)
+	return NULL;
+
+      if (function != Py_None)
+	return function.release ();
+    }
 
   Py_RETURN_NONE;
 }
diff --git a/gdb/python/py-xmethods.c b/gdb/python/py-xmethods.c
index 1c96b585f7..25e4e28a09 100644
--- a/gdb/python/py-xmethods.c
+++ b/gdb/python/py-xmethods.c
@@ -121,8 +121,6 @@ gdbpy_get_matching_xmethod_workers
    struct type *obj_type, const char *method_name,
    std::vector<xmethod_worker_up> *dm_vec)
 {
-  struct objfile *objfile;
-
   gdb_assert (obj_type != NULL && method_name != NULL);
 
   gdbpy_enter enter_py (get_current_arch (), current_language);
@@ -145,7 +143,7 @@ gdbpy_get_matching_xmethod_workers
   /* Gather debug method matchers registered with the object files.
      This could be done differently by iterating over each objfile's matcher
      list individually, but there's no data yet to show it's needed.  */
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       gdbpy_ref<> py_objfile = objfile_to_objfile_object (objfile);
 
diff --git a/gdb/solib-spu.c b/gdb/solib-spu.c
index 8419c2c139..61f9f94f8b 100644
--- a/gdb/solib-spu.c
+++ b/gdb/solib-spu.c
@@ -101,9 +101,8 @@ static void
 append_ocl_sos (struct so_list **link_ptr)
 {
   CORE_ADDR *ocl_program_addr_base;
-  struct objfile *objfile;
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       ocl_program_addr_base
 	= (CORE_ADDR *) objfile_data (objfile, ocl_program_data_key);
diff --git a/gdb/solib.c b/gdb/solib.c
index e1d237e5a4..36c744b7b1 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -677,11 +677,15 @@ solib_read_symbols (struct so_list *so, symfile_add_flags flags)
       TRY
 	{
 	  /* Have we already loaded this shared object?  */
-	  ALL_OBJFILES (so->objfile)
+	  so->objfile = nullptr;
+	  for (struct objfile *objfile : all_objfiles (current_program_space))
 	    {
-	      if (filename_cmp (objfile_name (so->objfile), so->so_name) == 0
-		  && so->objfile->addr_low == so->addr_low)
-		break;
+	      if (filename_cmp (objfile_name (objfile), so->so_name) == 0
+		  && objfile->addr_low == so->addr_low)
+		{
+		  so->objfile = objfile;
+		  break;
+		}
 	    }
 	  if (so->objfile == NULL)
 	    {
diff --git a/gdb/source.c b/gdb/source.c
index f60c7b5b5f..bef4ffe0ad 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -293,13 +293,13 @@ select_source_symtab (struct symtab *s)
   if (current_source_symtab)
     return;
 
-  ALL_OBJFILES (ofp)
-  {
-    if (ofp->sf)
-      s = ofp->sf->qf->find_last_source_symtab (ofp);
-    if (s)
-      current_source_symtab = s;
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      if (objfile->sf)
+	s = objfile->sf->qf->find_last_source_symtab (objfile);
+      if (s)
+	current_source_symtab = s;
+    }
   if (current_source_symtab)
     return;
 
diff --git a/gdb/spu-tdep.c b/gdb/spu-tdep.c
index 78a93e34b0..57dd4f1ed0 100644
--- a/gdb/spu-tdep.c
+++ b/gdb/spu-tdep.c
@@ -2000,12 +2000,11 @@ spu_objfile_from_frame (struct frame_info *frame)
 {
   struct gdbarch *gdbarch = get_frame_arch (frame);
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
-  struct objfile *obj;
 
   if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
     return NULL;
 
-  ALL_OBJFILES (obj)
+  for (struct objfile *obj : all_objfiles (current_program_space))
     {
       if (obj->sections != obj->sections_end
 	  && SPUADDR_SPU (obj_section_addr (obj->sections)) == tdep->id)
diff --git a/gdb/symfile.c b/gdb/symfile.c
index 8ab6a25de7..55756c9da2 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -2313,12 +2313,16 @@ remove_symbol_file_command (const char *args, int from_tty)
 
       addr = parse_and_eval_address (argv[1]);
 
-      ALL_OBJFILES (objf)
+      for (struct objfile *objfile : all_objfiles (current_program_space))
 	{
-	  if ((objf->flags & OBJF_USERLOADED) != 0
-	      && (objf->flags & OBJF_SHARED) != 0
-	      && objf->pspace == pspace && is_addr_in_objfile (addr, objf))
-	    break;
+	  if ((objfile->flags & OBJF_USERLOADED) != 0
+	      && (objfile->flags & OBJF_SHARED) != 0
+	      && objfile->pspace == pspace
+	      && is_addr_in_objfile (addr, objfile))
+	    {
+	      objf = objfile;
+	      break;
+	    }
 	}
     }
   else if (argv[0] != NULL)
@@ -2330,13 +2334,16 @@ remove_symbol_file_command (const char *args, int from_tty)
 
       gdb::unique_xmalloc_ptr<char> filename (tilde_expand (argv[0]));
 
-      ALL_OBJFILES (objf)
+      for (struct objfile *objfile : all_objfiles (current_program_space))
 	{
-	  if ((objf->flags & OBJF_USERLOADED) != 0
-	      && (objf->flags & OBJF_SHARED) != 0
-	      && objf->pspace == pspace
-	      && filename_cmp (filename.get (), objfile_name (objf)) == 0)
-	    break;
+	  if ((objfile->flags & OBJF_USERLOADED) != 0
+	      && (objfile->flags & OBJF_SHARED) != 0
+	      && objfile->pspace == pspace
+	      && filename_cmp (filename.get (), objfile_name (objfile)) == 0)
+	    {
+	      objf = objfile;
+	      break;
+	    }
 	}
     }
 
@@ -3753,16 +3760,14 @@ expand_symtabs_matching
    gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
    enum search_domain kind)
 {
-  struct objfile *objfile;
-
-  ALL_OBJFILES (objfile)
-  {
-    if (objfile->sf)
-      objfile->sf->qf->expand_symtabs_matching (objfile, file_matcher,
-						lookup_name,
-						symbol_matcher,
-						expansion_notify, kind);
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      if (objfile->sf)
+	objfile->sf->qf->expand_symtabs_matching (objfile, file_matcher,
+						  lookup_name,
+						  symbol_matcher,
+						  expansion_notify, kind);
+    }
 }
 
 /* Wrapper around the quick_symbol_functions map_symbol_filenames "method".
@@ -3773,14 +3778,12 @@ void
 map_symbol_filenames (symbol_filename_ftype *fun, void *data,
 		      int need_fullname)
 {
-  struct objfile *objfile;
-
-  ALL_OBJFILES (objfile)
-  {
-    if (objfile->sf)
-      objfile->sf->qf->map_symbol_filenames (objfile, fun, data,
-					     need_fullname);
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      if (objfile->sf)
+	objfile->sf->qf->map_symbol_filenames (objfile, fun, data,
+					       need_fullname);
+    }
 }
 
 #if GDB_SELF_TEST
diff --git a/gdb/symmisc.c b/gdb/symmisc.c
index fd1d298ec1..ac4552d478 100644
--- a/gdb/symmisc.c
+++ b/gdb/symmisc.c
@@ -470,12 +470,11 @@ maintenance_print_symbols (const char *args, int from_tty)
     }
   else
     {
-      struct objfile *objfile;
       struct compunit_symtab *cu;
       struct symtab *s;
       int found = 0;
 
-      ALL_OBJFILES (objfile)
+      for (struct objfile *objfile : all_objfiles (current_program_space))
 	{
 	  int print_for_objfile = 1;
 
@@ -690,7 +689,6 @@ maintenance_print_msymbols (const char *args, int from_tty)
 {
   struct ui_file *outfile = gdb_stdout;
   char *objfile_arg = NULL;
-  struct objfile *objfile;
   int i, outfile_idx;
 
   dont_repeat ();
@@ -734,13 +732,13 @@ maintenance_print_msymbols (const char *args, int from_tty)
       outfile = &arg_outfile;
     }
 
-  ALL_OBJFILES (objfile)
-  {
-    QUIT;
-    if (objfile_arg == NULL
-	|| compare_filenames_for_search (objfile_name (objfile), objfile_arg))
-      dump_msymbols (objfile, outfile);
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      QUIT;
+      if (objfile_arg == NULL
+	  || compare_filenames_for_search (objfile_name (objfile), objfile_arg))
+	dump_msymbols (objfile, outfile);
+    }
 }
 
 static void
diff --git a/gdb/symtab.c b/gdb/symtab.c
index f8c755fdab..041b7f2ead 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -464,7 +464,6 @@ void
 iterate_over_symtabs (const char *name,
 		      gdb::function_view<bool (symtab *)> callback)
 {
-  struct objfile *objfile;
   gdb::unique_xmalloc_ptr<char> real_path;
 
   /* Here we are interested in canonicalizing an absolute path, not
@@ -475,7 +474,7 @@ iterate_over_symtabs (const char *name,
       gdb_assert (IS_ABSOLUTE_PATH (real_path.get ()));
     }
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       if (iterate_over_some_symtabs (name, real_path.get (),
 				     objfile->compunit_symtabs, NULL,
@@ -486,7 +485,7 @@ iterate_over_symtabs (const char *name,
   /* Same search rules as above apply here, but now we look thru the
      psymtabs.  */
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       if (objfile->sf
 	  && objfile->sf->qf->map_symtabs_matching_filename (objfile,
@@ -1013,9 +1012,12 @@ matching_obj_sections (struct obj_section *obj_first,
 
   /* Otherwise check that they are in corresponding objfiles.  */
 
-  ALL_OBJFILES (obj)
-    if (obj->obfd == first->owner)
-      break;
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    if (objfile->obfd == first->owner)
+      {
+	obj = objfile;
+	break;
+      }
   gdb_assert (obj != NULL);
 
   if (obj->separate_debug_objfile != NULL
@@ -1033,7 +1035,6 @@ matching_obj_sections (struct obj_section *obj_first,
 void
 expand_symtab_containing_pc (CORE_ADDR pc, struct obj_section *section)
 {
-  struct objfile *objfile;
   struct bound_minimal_symbol msymbol;
 
   /* If we know that this is not a text address, return failure.  This is
@@ -1048,16 +1049,16 @@ expand_symtab_containing_pc (CORE_ADDR pc, struct obj_section *section)
 	  || MSYMBOL_TYPE (msymbol.minsym) == mst_file_bss))
     return;
 
-  ALL_OBJFILES (objfile)
-  {
-    struct compunit_symtab *cust = NULL;
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      struct compunit_symtab *cust = NULL;
 
-    if (objfile->sf)
-      cust = objfile->sf->qf->find_pc_sect_compunit_symtab (objfile, msymbol,
-							    pc, section, 0);
-    if (cust)
-      return;
-  }
+      if (objfile->sf)
+	cust = objfile->sf->qf->find_pc_sect_compunit_symtab (objfile, msymbol,
+							      pc, section, 0);
+      if (cust)
+	return;
+    }
 }
 \f
 /* Hash function for the symbol cache.  */
@@ -2576,7 +2577,6 @@ struct block_symbol
 lookup_static_symbol (const char *name, const domain_enum domain)
 {
   struct symbol_cache *cache = get_symbol_cache (current_program_space);
-  struct objfile *objfile;
   struct block_symbol result;
   struct block_symbol_cache *bsc;
   struct symbol_cache_slot *slot;
@@ -2592,7 +2592,7 @@ lookup_static_symbol (const char *name, const domain_enum domain)
       return result;
     }
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       result = lookup_symbol_in_objfile (objfile, STATIC_BLOCK, name, domain);
       if (result.symbol != NULL)
@@ -2793,7 +2793,6 @@ basic_lookup_transparent_type_1 (struct objfile *objfile, int block_index,
 struct type *
 basic_lookup_transparent_type (const char *name)
 {
-  struct objfile *objfile;
   struct type *t;
 
   /* Now search all the global symbols.  Do the symtab's first, then
@@ -2801,19 +2800,19 @@ basic_lookup_transparent_type (const char *name)
      of the desired name as a global, then do psymtab-to-symtab
      conversion on the fly and return the found symbol.  */
 
-  ALL_OBJFILES (objfile)
-  {
-    t = basic_lookup_transparent_type_1 (objfile, GLOBAL_BLOCK, name);
-    if (t)
-      return t;
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      t = basic_lookup_transparent_type_1 (objfile, GLOBAL_BLOCK, name);
+      if (t)
+	return t;
+    }
 
-  ALL_OBJFILES (objfile)
-  {
-    t = basic_lookup_transparent_type_quick (objfile, GLOBAL_BLOCK, name);
-    if (t)
-      return t;
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      t = basic_lookup_transparent_type_quick (objfile, GLOBAL_BLOCK, name);
+      if (t)
+	return t;
+    }
 
   /* Now search the static file-level symbols.
      Not strictly correct, but more useful than an error.
@@ -2822,19 +2821,19 @@ basic_lookup_transparent_type (const char *name)
      of the desired name as a file-level static, then do psymtab-to-symtab
      conversion on the fly and return the found symbol.  */
 
-  ALL_OBJFILES (objfile)
-  {
-    t = basic_lookup_transparent_type_1 (objfile, STATIC_BLOCK, name);
-    if (t)
-      return t;
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      t = basic_lookup_transparent_type_1 (objfile, STATIC_BLOCK, name);
+      if (t)
+	return t;
+    }
 
-  ALL_OBJFILES (objfile)
-  {
-    t = basic_lookup_transparent_type_quick (objfile, STATIC_BLOCK, name);
-    if (t)
-      return t;
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      t = basic_lookup_transparent_type_quick (objfile, STATIC_BLOCK, name);
+      if (t)
+	return t;
+    }
 
   return (struct type *) 0;
 }
@@ -2967,19 +2966,19 @@ find_pc_sect_compunit_symtab (CORE_ADDR pc, struct obj_section *section)
 
   /* Not found in symtabs, search the "quick" symtabs (e.g. psymtabs).  */
 
-  ALL_OBJFILES (objfile)
-  {
-    struct compunit_symtab *result;
-
-    if (!objfile->sf)
-      continue;
-    result = objfile->sf->qf->find_pc_sect_compunit_symtab (objfile,
-							    msymbol,
-							    pc, section,
-							    1);
-    if (result != NULL)
-      return result;
-  }
+  for (struct objfile *objf : all_objfiles (current_program_space))
+    {
+      struct compunit_symtab *result;
+
+      if (!objf->sf)
+	continue;
+      result = objf->sf->qf->find_pc_sect_compunit_symtab (objf,
+							   msymbol,
+							   pc, section,
+							   1);
+      if (result != NULL)
+	return result;
+    }
 
   return NULL;
 }
@@ -2999,35 +2998,33 @@ find_pc_compunit_symtab (CORE_ADDR pc)
 struct symbol *
 find_symbol_at_address (CORE_ADDR address)
 {
-  struct objfile *objfile;
-
-  ALL_OBJFILES (objfile)
-  {
-    if (objfile->sf == NULL
-	|| objfile->sf->qf->find_compunit_symtab_by_address == NULL)
-      continue;
-
-    struct compunit_symtab *symtab
-      = objfile->sf->qf->find_compunit_symtab_by_address (objfile, address);
-    if (symtab != NULL)
-      {
-	const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (symtab);
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      if (objfile->sf == NULL
+	  || objfile->sf->qf->find_compunit_symtab_by_address == NULL)
+	continue;
 
-	for (int i = GLOBAL_BLOCK; i <= STATIC_BLOCK; ++i)
-	  {
-	    struct block *b = BLOCKVECTOR_BLOCK (bv, i);
-	    struct block_iterator iter;
-	    struct symbol *sym;
+      struct compunit_symtab *symtab
+	= objfile->sf->qf->find_compunit_symtab_by_address (objfile, address);
+      if (symtab != NULL)
+	{
+	  const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (symtab);
 
-	    ALL_BLOCK_SYMBOLS (b, iter, sym)
+	  for (int i = GLOBAL_BLOCK; i <= STATIC_BLOCK; ++i)
 	    {
-	      if (SYMBOL_CLASS (sym) == LOC_STATIC
-		  && SYMBOL_VALUE_ADDRESS (sym) == address)
-		return sym;
+	      struct block *b = BLOCKVECTOR_BLOCK (bv, i);
+	      struct block_iterator iter;
+	      struct symbol *sym;
+
+	      ALL_BLOCK_SYMBOLS (b, iter, sym)
+		{
+		  if (SYMBOL_CLASS (sym) == LOC_STATIC
+		      && SYMBOL_VALUE_ADDRESS (sym) == address)
+		    return sym;
+		}
 	    }
-	  }
-      }
-  }
+	}
+    }
 
   return NULL;
 }
@@ -3352,7 +3349,6 @@ find_line_symtab (struct symtab *symtab, int line,
          BEST_INDEX and BEST_LINETABLE identify the item for it.  */
       int best;
 
-      struct objfile *objfile;
       struct compunit_symtab *cu;
       struct symtab *s;
 
@@ -3361,13 +3357,14 @@ find_line_symtab (struct symtab *symtab, int line,
       else
 	best = 0;
 
-      ALL_OBJFILES (objfile)
-      {
-	if (objfile->sf)
-	  objfile->sf->qf->expand_symtabs_with_fullname (objfile,
-						   symtab_to_fullname (symtab));
-      }
+      for (struct objfile *objfile : all_objfiles (current_program_space))
+	{
+	  if (objfile->sf)
+	    objfile->sf->qf->expand_symtabs_with_fullname
+	      (objfile, symtab_to_fullname (symtab));
+	}
 
+      struct objfile *objfile;
       ALL_FILETABS (objfile, cu, s)
       {
 	struct linetable *l;
@@ -5691,7 +5688,6 @@ static void
 find_main_name (void)
 {
   const char *new_main_name;
-  struct objfile *objfile;
 
   /* First check the objfiles to see whether a debuginfo reader has
      picked up the appropriate main name.  Historically the main name
@@ -5699,15 +5695,15 @@ find_main_name (void)
      relies on the order of objfile creation -- which still isn't
      guaranteed to get the correct answer, but is just probably more
      accurate.  */
-  ALL_OBJFILES (objfile)
-  {
-    if (objfile->per_bfd->name_of_main != NULL)
-      {
-	set_main_name (objfile->per_bfd->name_of_main,
-		       objfile->per_bfd->language_of_main);
-	return;
-      }
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      if (objfile->per_bfd->name_of_main != NULL)
+	{
+	  set_main_name (objfile->per_bfd->name_of_main,
+			 objfile->per_bfd->language_of_main);
+	  return;
+	}
+    }
 
   /* Try to see if the main procedure is in Ada.  */
   /* FIXME: brobecker/2005-03-07: Another way of doing this would
diff --git a/gdb/windows-tdep.c b/gdb/windows-tdep.c
index 41b9948f6d..dc38080547 100644
--- a/gdb/windows-tdep.c
+++ b/gdb/windows-tdep.c
@@ -415,7 +415,6 @@ windows_iterate_over_objfiles_in_search_order
    void *cb_data, struct objfile *current_objfile)
 {
   int stop;
-  struct objfile *objfile;
 
   if (current_objfile)
     {
@@ -424,7 +423,7 @@ windows_iterate_over_objfiles_in_search_order
 	return;
     }
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       if (objfile != current_objfile)
 	{
-- 
2.17.2

^ permalink raw reply	[flat|nested] 36+ messages in thread

* [PATCH 10/12] Remove ALL_OBJFILES and ALL_FILETABS
  2018-11-25 16:54 [PATCH 00/12] Remove some ALL_* iteration macros Tom Tromey
                   ` (2 preceding siblings ...)
  2018-11-25 16:54 ` [PATCH 07/12] Remove ALL_COMPUNITS Tom Tromey
@ 2018-11-25 16:54 ` Tom Tromey
  2018-11-25 16:54 ` [PATCH 11/12] Remove ALL_OBJSECTIONS Tom Tromey
                   ` (10 subsequent siblings)
  14 siblings, 0 replies; 36+ messages in thread
From: Tom Tromey @ 2018-11-25 16:54 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This removes the ALL_OBJFILES and ALL_FILETABS macros, replacing them
with ranged for loops.

gdb/ChangeLog
2018-11-25  Tom Tromey  <tom@tromey.com>

	* symtab.c (find_line_symtab, info_sources_command)
	(make_source_files_completion_list): Use objfile_compunits.
	* source.c (select_source_symtab): Use objfile_compunits.
	* objfiles.h (struct objfile): Update comment.
	(ALL_OBJFILES): Remove.
	(ALL_FILETABS): Remove.
	* mi/mi-cmd-file.c (mi_cmd_file_list_exec_source_files): Use
	objfile_compunits.
---
 gdb/ChangeLog        |  11 ++++
 gdb/mi/mi-cmd-file.c |  17 +++---
 gdb/objfiles.h       |  18 +------
 gdb/source.c         |  24 ++++-----
 gdb/symtab.c         | 121 ++++++++++++++++++++++---------------------
 5 files changed, 95 insertions(+), 96 deletions(-)

diff --git a/gdb/mi/mi-cmd-file.c b/gdb/mi/mi-cmd-file.c
index a74846e21b..9fabdb9d71 100644
--- a/gdb/mi/mi-cmd-file.c
+++ b/gdb/mi/mi-cmd-file.c
@@ -84,7 +84,6 @@ void
 mi_cmd_file_list_exec_source_files (const char *command, char **argv, int argc)
 {
   struct ui_out *uiout = current_uiout;
-  struct objfile *objfile;
 
   if (!mi_valid_noargs ("-file-list-exec-source-files", argc, argv))
     error (_("-file-list-exec-source-files: Usage: No args"));
@@ -93,15 +92,17 @@ mi_cmd_file_list_exec_source_files (const char *command, char **argv, int argc)
   uiout->begin (ui_out_type_list, "files");
 
   /* Look at all of the file symtabs.  */
-  ALL_FILETABS (objfile, cu, s)
-  {
-    uiout->begin (ui_out_type_tuple, NULL);
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    for (struct compunit_symtab *cu : objfile_compunits (objfile))
+      for (struct symtab *s : compunit_filetabs (cu))
+	{
+	  uiout->begin (ui_out_type_tuple, NULL);
 
-    uiout->field_string ("file", symtab_to_filename_for_display (s));
-    uiout->field_string ("fullname", symtab_to_fullname (s));
+	  uiout->field_string ("file", symtab_to_filename_for_display (s));
+	  uiout->field_string ("fullname", symtab_to_fullname (s));
 
-    uiout->end (ui_out_type_tuple);
-  }
+	  uiout->end (ui_out_type_tuple);
+	}
 
   map_symbol_filenames (print_partial_file_name, NULL,
 			1 /*need_fullname*/);
diff --git a/gdb/objfiles.h b/gdb/objfiles.h
index 94394e79ae..9d00a55ce5 100644
--- a/gdb/objfiles.h
+++ b/gdb/objfiles.h
@@ -430,7 +430,7 @@ struct objfile
      Although this is a tree structure, GDB only support one level
      (ie a separate debug for a separate debug is not supported).  Note that
      separate debug object are in the main chain and therefore will be
-     visited by ALL_OBJFILES & co iterators.  Separate debug objfile always
+     visited by all_objfiles & co iterators.  Separate debug objfile always
      has a non-nul separate_debug_objfile_backlink.  */
 
   /* Link to the first separate debug object, if any.  */
@@ -604,14 +604,6 @@ public:
   }
 };
 
-
-/* Traverse all object files in the current program space.  */
-
-#define ALL_OBJFILES(obj)			    \
-  for ((obj) = current_program_space->objfiles; \
-       (obj) != NULL;				    \
-       (obj) = (obj)->next)
-
 /* A range adapter that makes it possible to iterate over all
    compunits in one objfile.  */
 
@@ -705,14 +697,6 @@ private:
   struct objfile *m_objfile;
 };
 
-/* Traverse all symtabs in all objfiles in the current symbol
-   space.  */
-
-#define ALL_FILETABS(objfile, ps, s)					\
-  ALL_OBJFILES (objfile)						\
-    for (struct compunit_symtab *ps : objfile_compunits (objfile))	\
-      for (struct symtab *s : compunit_filetabs (cu))
-
 #define ALL_OBJFILE_OSECTIONS(objfile, osect)	\
   for (osect = objfile->sections; osect < objfile->sections_end; osect++) \
     if (osect->the_bfd_section == NULL)					\
diff --git a/gdb/source.c b/gdb/source.c
index 6111453eb0..c0ea866d92 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -243,8 +243,6 @@ clear_current_source_symtab_and_line (void)
 void
 select_source_symtab (struct symtab *s)
 {
-  struct objfile *ofp;
-
   if (s)
     {
       current_source_symtab = s;
@@ -276,18 +274,20 @@ select_source_symtab (struct symtab *s)
 
   current_source_line = 1;
 
-  ALL_FILETABS (ofp, cu, symtab)
-    {
-      const char *name = symtab->filename;
-      int len = strlen (name);
-
-      if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
-			|| strcmp (name, "<<C++-namespaces>>") == 0)))
+  for (struct objfile *ofp : all_objfiles (current_program_space))
+    for (struct compunit_symtab *cu : objfile_compunits (ofp))
+      for (struct symtab *symtab : compunit_filetabs (cu))
 	{
-	  current_source_pspace = current_program_space;
-	  current_source_symtab = symtab;
+	  const char *name = symtab->filename;
+	  int len = strlen (name);
+
+	  if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
+			    || strcmp (name, "<<C++-namespaces>>") == 0)))
+	    {
+	      current_source_pspace = current_program_space;
+	      current_source_symtab = symtab;
+	    }
 	}
-    }
 
   if (current_source_symtab)
     return;
diff --git a/gdb/symtab.c b/gdb/symtab.c
index c16fc307ad..145971f1b9 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -3353,37 +3353,38 @@ find_line_symtab (struct symtab *symtab, int line,
 	      (objfile, symtab_to_fullname (symtab));
 	}
 
-      struct objfile *objfile;
-      ALL_FILETABS (objfile, cu, s)
-      {
-	struct linetable *l;
-	int ind;
+      for (struct objfile *objfile : all_objfiles (current_program_space))
+	for (struct compunit_symtab *cu : objfile_compunits (objfile))
+	  for (struct symtab *s : compunit_filetabs (cu))
+	    {
+	      struct linetable *l;
+	      int ind;
 
-	if (FILENAME_CMP (symtab->filename, s->filename) != 0)
-	  continue;
-	if (FILENAME_CMP (symtab_to_fullname (symtab),
-			  symtab_to_fullname (s)) != 0)
-	  continue;	
-	l = SYMTAB_LINETABLE (s);
-	ind = find_line_common (l, line, &exact, 0);
-	if (ind >= 0)
-	  {
-	    if (exact)
-	      {
-		best_index = ind;
-		best_linetable = l;
-		best_symtab = s;
-		goto done;
-	      }
-	    if (best == 0 || l->item[ind].line < best)
-	      {
-		best = l->item[ind].line;
-		best_index = ind;
-		best_linetable = l;
-		best_symtab = s;
-	      }
-	  }
-      }
+	      if (FILENAME_CMP (symtab->filename, s->filename) != 0)
+		continue;
+	      if (FILENAME_CMP (symtab_to_fullname (symtab),
+				symtab_to_fullname (s)) != 0)
+		continue;	
+	      l = SYMTAB_LINETABLE (s);
+	      ind = find_line_common (l, line, &exact, 0);
+	      if (ind >= 0)
+		{
+		  if (exact)
+		    {
+		      best_index = ind;
+		      best_linetable = l;
+		      best_symtab = s;
+		      goto done;
+		    }
+		  if (best == 0 || l->item[ind].line < best)
+		    {
+		      best = l->item[ind].line;
+		      best_index = ind;
+		      best_linetable = l;
+		      best_symtab = s;
+		    }
+		}
+	    }
     }
 done:
   if (best_index < 0)
@@ -4174,7 +4175,6 @@ output_partial_symbol_filename (const char *filename, const char *fullname,
 static void
 info_sources_command (const char *ignore, int from_tty)
 {
-  struct objfile *objfile;
   struct output_source_filename_data data;
 
   if (!have_full_symbols () && !have_partial_symbols ())
@@ -4189,12 +4189,14 @@ info_sources_command (const char *ignore, int from_tty)
   printf_filtered ("Source files for which symbols have been read in:\n\n");
 
   data.first = 1;
-  ALL_FILETABS (objfile, cu, s)
-  {
-    const char *fullname = symtab_to_fullname (s);
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    for (struct compunit_symtab *cu : objfile_compunits (objfile))
+      for (struct symtab *s : compunit_filetabs (cu))
+	{
+	  const char *fullname = symtab_to_fullname (s);
 
-    output_source_filename (fullname, &data);
-  }
+	  output_source_filename (fullname, &data);
+	}
   printf_filtered ("\n\n");
 
   printf_filtered ("Source files for which symbols "
@@ -5563,7 +5565,6 @@ maybe_add_partial_symtab_filename (const char *filename, const char *fullname,
 completion_list
 make_source_files_completion_list (const char *text, const char *word)
 {
-  struct objfile *objfile;
   size_t text_len = strlen (text);
   completion_list list;
   const char *base_name;
@@ -5574,30 +5575,32 @@ make_source_files_completion_list (const char *text, const char *word)
 
   filename_seen_cache filenames_seen;
 
-  ALL_FILETABS (objfile, cu, s)
-    {
-      if (not_interesting_fname (s->filename))
-	continue;
-      if (!filenames_seen.seen (s->filename)
-	  && filename_ncmp (s->filename, text, text_len) == 0)
-	{
-	  /* This file matches for a completion; add it to the current
-	     list of matches.  */
-	  add_filename_to_list (s->filename, text, word, &list);
-	}
-      else
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    for (struct compunit_symtab *cu : objfile_compunits (objfile))
+      for (struct symtab *s : compunit_filetabs (cu))
 	{
-	  /* NOTE: We allow the user to type a base name when the
-	     debug info records leading directories, but not the other
-	     way around.  This is what subroutines of breakpoint
-	     command do when they parse file names.  */
-	  base_name = lbasename (s->filename);
-	  if (base_name != s->filename
-	      && !filenames_seen.seen (base_name)
-	      && filename_ncmp (base_name, text, text_len) == 0)
-	    add_filename_to_list (base_name, text, word, &list);
+	  if (not_interesting_fname (s->filename))
+	    continue;
+	  if (!filenames_seen.seen (s->filename)
+	      && filename_ncmp (s->filename, text, text_len) == 0)
+	    {
+	      /* This file matches for a completion; add it to the current
+		 list of matches.  */
+	      add_filename_to_list (s->filename, text, word, &list);
+	    }
+	  else
+	    {
+	      /* NOTE: We allow the user to type a base name when the
+		 debug info records leading directories, but not the other
+		 way around.  This is what subroutines of breakpoint
+		 command do when they parse file names.  */
+	      base_name = lbasename (s->filename);
+	      if (base_name != s->filename
+		  && !filenames_seen.seen (base_name)
+		  && filename_ncmp (base_name, text, text_len) == 0)
+		add_filename_to_list (base_name, text, word, &list);
+	    }
 	}
-    }
 
   datum.filename_seen_cache = &filenames_seen;
   datum.text = text;
-- 
2.17.2

^ permalink raw reply	[flat|nested] 36+ messages in thread

* [PATCH 12/12] Remove ALL_OBJFILE_PSYMTABS
  2018-11-25 16:54 [PATCH 00/12] Remove some ALL_* iteration macros Tom Tromey
                   ` (5 preceding siblings ...)
  2018-11-25 16:54 ` [PATCH 04/12] Remove ALL_OBJFILES_SAFE Tom Tromey
@ 2018-11-25 16:54 ` Tom Tromey
  2018-11-25 16:54 ` [PATCH 01/12] Introduce all_objfiles and next_iterator Tom Tromey
                   ` (7 subsequent siblings)
  14 siblings, 0 replies; 36+ messages in thread
From: Tom Tromey @ 2018-11-25 16:54 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This removes the ALL_OBJFILE_PSYMTABS macro, replacing its uses with
ranged for loops.

gdb/ChangeLog
2018-11-25  Tom Tromey  <tom@tromey.com>

	* dbxread.c (dbx_end_psymtab): Use objfile_psymtabs.
	* mdebugread.c (parse_partial_symbols): Use objfile_psymtabs.
	* psymtab.c (ALL_OBJFILE_PSYMTABS_REQUIRED): Remove.
	(psym_map_symtabs_matching_filename, find_pc_sect_psymtab)
	(psym_lookup_symbol, psym_find_last_source_symtab)
	(psym_forget_cached_source_info, psym_print_stats)
	(psym_expand_symtabs_for_function, psym_expand_all_symtabs)
	(psym_expand_symtabs_with_fullname, psym_map_symbol_filenames)
	(psym_map_matching_symbols, psym_expand_symtabs_matching)
	(psym_find_compunit_symtab_by_address)
	(maintenance_print_psymbols, maintenance_info_psymtabs)
	(maintenance_check_psymtabs): Use ranged for.
	* psymtab.h (class objfile_psymtabs): New.
	(require_partial_symbols): Return objfile_psymtabs.
	* psympriv.h (ALL_OBJFILE_PSYMTABS): Remove.
---
 gdb/ChangeLog    |  18 +++
 gdb/dbxread.c    |   6 +-
 gdb/mdebugread.c |  22 ++--
 gdb/psympriv.h   |   5 -
 gdb/psymtab.c    | 278 ++++++++++++++++++++---------------------------
 gdb/psymtab.h    |  22 +++-
 6 files changed, 168 insertions(+), 183 deletions(-)

diff --git a/gdb/dbxread.c b/gdb/dbxread.c
index e004e8cb93..36b027193a 100644
--- a/gdb/dbxread.c
+++ b/gdb/dbxread.c
@@ -2014,18 +2014,14 @@ dbx_end_psymtab (struct objfile *objfile, struct partial_symtab *pst,
     pst->set_text_low (pst->raw_text_high ());
   else
     {
-      struct partial_symtab *p1;
-
       /* If we know our own starting text address, then walk through all other
          psymtabs for this objfile, and if any didn't know their ending text
          address, set it to our starting address.  Take care to not set our
          own ending address to our starting address.  */
 
-      ALL_OBJFILE_PSYMTABS (objfile, p1)
-      {
+      for (struct partial_symtab *p1 : objfile_psymtabs (objfile))
 	if (!p1->text_high_valid && p1->text_low_valid && p1 != pst)
 	  p1->set_text_high (pst->raw_text_low ());
-      }
     }
 
   /* End of kludge for patching Solaris textlow and texthigh.  */
diff --git a/gdb/mdebugread.c b/gdb/mdebugread.c
index 26687f6c8d..7014861f33 100644
--- a/gdb/mdebugread.c
+++ b/gdb/mdebugread.c
@@ -3681,17 +3681,17 @@ parse_partial_symbols (minimal_symbol_reader &reader,
 	  && save_pst->text_low_valid
 	  && !(objfile->flags & OBJF_REORDERED))
 	{
-	  ALL_OBJFILE_PSYMTABS (objfile, pst)
-	  {
-	    if (save_pst != pst
-		&& save_pst->raw_text_low () >= pst->raw_text_low ()
-		&& save_pst->raw_text_low () < pst->raw_text_high ()
-		&& save_pst->raw_text_high () > pst->raw_text_high ())
-	      {
-		objfile->flags |= OBJF_REORDERED;
-		break;
-	      }
-	  }
+	  for (struct partial_symtab *iter : objfile_psymtabs (objfile))
+	    {
+	      if (save_pst != iter
+		  && save_pst->raw_text_low () >= iter->raw_text_low ()
+		  && save_pst->raw_text_low () < iter->raw_text_high ()
+		  && save_pst->raw_text_high () > iter->raw_text_high ())
+		{
+		  objfile->flags |= OBJF_REORDERED;
+		  break;
+		}
+	    }
 	}
     }
 
diff --git a/gdb/psympriv.h b/gdb/psympriv.h
index f8eade7df3..16bfa8914d 100644
--- a/gdb/psympriv.h
+++ b/gdb/psympriv.h
@@ -331,9 +331,4 @@ class psymtab_discarder
   struct partial_symtab *m_psymtab;
 };
 
-/* Traverse all psymtabs in one objfile.  */
-
-#define	ALL_OBJFILE_PSYMTABS(objfile, p) \
-    for ((p) = (objfile) -> psymtabs; (p) != NULL; (p) = (p) -> next)
-
 #endif /* PSYMPRIV_H */
diff --git a/gdb/psymtab.c b/gdb/psymtab.c
index 61fb0ebb70..566ee20334 100644
--- a/gdb/psymtab.c
+++ b/gdb/psymtab.c
@@ -19,8 +19,8 @@
 
 #include "defs.h"
 #include "symtab.h"
-#include "psympriv.h"
 #include "objfiles.h"
+#include "psympriv.h"
 #include "block.h"
 #include "filenames.h"
 #include "source.h"
@@ -65,10 +65,9 @@ static struct partial_symbol *find_pc_sect_psymbol (struct objfile *,
 static struct compunit_symtab *psymtab_to_symtab (struct objfile *objfile,
 						  struct partial_symtab *pst);
 
-/* Ensure that the partial symbols for OBJFILE have been loaded.  This
-   function always returns its argument, as a convenience.  */
+/* See psymtab.h.  */
 
-struct objfile *
+objfile_psymtabs
 require_partial_symbols (struct objfile *objfile, int verbose)
 {
   if ((objfile->flags & OBJF_PSYMTABS_READ) == 0)
@@ -93,21 +92,9 @@ require_partial_symbols (struct objfile *objfile, int verbose)
 	}
     }
 
-  return objfile;
+  return objfile_psymtabs (objfile);
 }
 
-/* Traverse all psymtabs in one objfile, requiring that the psymtabs
-   be read in.  */
-
-#define ALL_OBJFILE_PSYMTABS_REQUIRED(objfile, p)		\
-    for ((p) = require_partial_symbols (objfile, 1)->psymtabs;	\
-	 (p) != NULL;						\
-	 (p) = (p)->next)
-
-/* We want to make sure this file always requires psymtabs.  */
-
-#undef ALL_OBJFILE_PSYMTABS
-
 /* Helper function for psym_map_symtabs_matching_filename that
    expands the symtabs and calls the iterator.  */
 
@@ -146,57 +133,56 @@ psym_map_symtabs_matching_filename
    const char *real_path,
    gdb::function_view<bool (symtab *)> callback)
 {
-  struct partial_symtab *pst;
   const char *name_basename = lbasename (name);
 
-  ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
-  {
-    /* We can skip shared psymtabs here, because any file name will be
-       attached to the unshared psymtab.  */
-    if (pst->user != NULL)
-      continue;
-
-    /* Anonymous psymtabs don't have a file name.  */
-    if (pst->anonymous)
-      continue;
+  for (struct partial_symtab *pst : require_partial_symbols (objfile, 1))
+    {
+      /* We can skip shared psymtabs here, because any file name will be
+	 attached to the unshared psymtab.  */
+      if (pst->user != NULL)
+	continue;
 
-    if (compare_filenames_for_search (pst->filename, name))
-      {
-	if (partial_map_expand_apply (objfile, name, real_path,
-				      pst, callback))
-	  return true;
+      /* Anonymous psymtabs don't have a file name.  */
+      if (pst->anonymous)
 	continue;
-      }
 
-    /* Before we invoke realpath, which can get expensive when many
-       files are involved, do a quick comparison of the basenames.  */
-    if (! basenames_may_differ
-	&& FILENAME_CMP (name_basename, lbasename (pst->filename)) != 0)
-      continue;
+      if (compare_filenames_for_search (pst->filename, name))
+	{
+	  if (partial_map_expand_apply (objfile, name, real_path,
+					pst, callback))
+	    return true;
+	  continue;
+	}
 
-    if (compare_filenames_for_search (psymtab_to_fullname (pst), name))
-      {
-	if (partial_map_expand_apply (objfile, name, real_path,
-				      pst, callback))
-	  return true;
+      /* Before we invoke realpath, which can get expensive when many
+	 files are involved, do a quick comparison of the basenames.  */
+      if (! basenames_may_differ
+	  && FILENAME_CMP (name_basename, lbasename (pst->filename)) != 0)
 	continue;
-      }
 
-    /* If the user gave us an absolute path, try to find the file in
-       this symtab and use its absolute path.  */
-    if (real_path != NULL)
-      {
-	gdb_assert (IS_ABSOLUTE_PATH (real_path));
-	gdb_assert (IS_ABSOLUTE_PATH (name));
-	if (filename_cmp (psymtab_to_fullname (pst), real_path) == 0)
-	  {
-	    if (partial_map_expand_apply (objfile, name, real_path,
-					  pst, callback))
-	      return true;
-	    continue;
-	  }
-      }
-  }
+      if (compare_filenames_for_search (psymtab_to_fullname (pst), name))
+	{
+	  if (partial_map_expand_apply (objfile, name, real_path,
+					pst, callback))
+	    return true;
+	  continue;
+	}
+
+      /* If the user gave us an absolute path, try to find the file in
+	 this symtab and use its absolute path.  */
+      if (real_path != NULL)
+	{
+	  gdb_assert (IS_ABSOLUTE_PATH (real_path));
+	  gdb_assert (IS_ABSOLUTE_PATH (name));
+	  if (filename_cmp (psymtab_to_fullname (pst), real_path) == 0)
+	    {
+	      if (partial_map_expand_apply (objfile, name, real_path,
+					    pst, callback))
+		return true;
+	      continue;
+	    }
+	}
+    }
 
   return false;
 }
@@ -289,7 +275,6 @@ find_pc_sect_psymtab (struct objfile *objfile, CORE_ADDR pc,
 		      struct obj_section *section,
 		      struct bound_minimal_symbol msymbol)
 {
-  struct partial_symtab *pst;
   CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
 				 SECT_OFF_TEXT (objfile));
 
@@ -298,8 +283,9 @@ find_pc_sect_psymtab (struct objfile *objfile, CORE_ADDR pc,
 
   if (objfile->psymtabs_addrmap != NULL)
     {
-      pst = ((struct partial_symtab *)
-	     addrmap_find (objfile->psymtabs_addrmap, pc - baseaddr));
+      struct partial_symtab *pst
+	= ((struct partial_symtab *)
+	   addrmap_find (objfile->psymtabs_addrmap, pc - baseaddr));
       if (pst != NULL)
 	{
 	  /* FIXME: addrmaps currently do not handle overlayed sections,
@@ -341,7 +327,7 @@ find_pc_sect_psymtab (struct objfile *objfile, CORE_ADDR pc,
      its CUs may be missing in PSYMTABS_ADDRMAP as they may be varying
      debug info type in single OBJFILE.  */
 
-  ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
+  for (struct partial_symtab *pst : require_partial_symbols (objfile, 1))
     if (!pst->psymtabs_addrmap_supported
 	&& pc >= pst->text_low (objfile) && pc < pst->text_high (objfile))
       {
@@ -458,43 +444,42 @@ psym_lookup_symbol (struct objfile *objfile,
 		    int block_index, const char *name,
 		    const domain_enum domain)
 {
-  struct partial_symtab *ps;
   const int psymtab_index = (block_index == GLOBAL_BLOCK ? 1 : 0);
   struct compunit_symtab *stab_best = NULL;
 
   lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
 
-  ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
-  {
-    if (!ps->readin && lookup_partial_symbol (objfile, ps, name,
-					      psymtab_index, domain))
-      {
-	struct symbol *sym, *with_opaque = NULL;
-	struct compunit_symtab *stab = psymtab_to_symtab (objfile, ps);
-	/* Note: While psymtab_to_symtab can return NULL if the partial symtab
-	   is empty, we can assume it won't here because lookup_partial_symbol
-	   succeeded.  */
-	const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
-	struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
-
-	sym = block_find_symbol (block, name, domain,
-				 block_find_non_opaque_type_preferred,
-				 &with_opaque);
-
-	/* Some caution must be observed with overloaded functions
-	   and methods, since the index will not contain any overload
-	   information (but NAME might contain it).  */
-
-	if (sym != NULL
-	    && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
-	  return stab;
-	if (with_opaque != NULL
-	    && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
-	  stab_best = stab;
-
-	/* Keep looking through other psymtabs.  */
-      }
-  }
+  for (struct partial_symtab *ps : require_partial_symbols (objfile, 1))
+    {
+      if (!ps->readin && lookup_partial_symbol (objfile, ps, name,
+						psymtab_index, domain))
+	{
+	  struct symbol *sym, *with_opaque = NULL;
+	  struct compunit_symtab *stab = psymtab_to_symtab (objfile, ps);
+	  /* Note: While psymtab_to_symtab can return NULL if the
+	     partial symtab is empty, we can assume it won't here
+	     because lookup_partial_symbol succeeded.  */
+	  const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
+	  struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
+
+	  sym = block_find_symbol (block, name, domain,
+				   block_find_non_opaque_type_preferred,
+				   &with_opaque);
+
+	  /* Some caution must be observed with overloaded functions
+	     and methods, since the index will not contain any overload
+	     information (but NAME might contain it).  */
+
+	  if (sym != NULL
+	      && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
+	    return stab;
+	  if (with_opaque != NULL
+	      && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
+	    stab_best = stab;
+
+	  /* Keep looking through other psymtabs.  */
+	}
+    }
 
   return stab_best;
 }
@@ -752,10 +737,9 @@ psymtab_to_symtab (struct objfile *objfile, struct partial_symtab *pst)
 static struct symtab *
 psym_find_last_source_symtab (struct objfile *ofp)
 {
-  struct partial_symtab *ps;
   struct partial_symtab *cs_pst = NULL;
 
-  ALL_OBJFILE_PSYMTABS_REQUIRED (ofp, ps)
+  for (struct partial_symtab *ps : require_partial_symbols (ofp, 1))
     {
       const char *name = ps->filename;
       int len = strlen (name);
@@ -791,9 +775,7 @@ psym_find_last_source_symtab (struct objfile *ofp)
 static void
 psym_forget_cached_source_info (struct objfile *objfile)
 {
-  struct partial_symtab *pst;
-
-  ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
+  for (struct partial_symtab *pst : require_partial_symbols (objfile, 1))
     {
       if (pst->fullname != NULL)
 	{
@@ -973,10 +955,9 @@ static void
 psym_print_stats (struct objfile *objfile)
 {
   int i;
-  struct partial_symtab *ps;
 
   i = 0;
-  ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
+  for (struct partial_symtab *ps : require_partial_symbols (objfile, 1))
     {
       if (ps->readin == 0)
 	i++;
@@ -1016,19 +997,17 @@ static void
 psym_expand_symtabs_for_function (struct objfile *objfile,
 				  const char *func_name)
 {
-  struct partial_symtab *ps;
-
-  ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
-  {
-    if (ps->readin)
-      continue;
+  for (struct partial_symtab *ps : require_partial_symbols (objfile, 1))
+    {
+      if (ps->readin)
+	continue;
 
-    if ((lookup_partial_symbol (objfile, ps, func_name, 1, VAR_DOMAIN)
-	 != NULL)
-	|| (lookup_partial_symbol (objfile, ps, func_name, 0, VAR_DOMAIN)
-	    != NULL))
-      psymtab_to_symtab (objfile, ps);
-  }
+      if ((lookup_partial_symbol (objfile, ps, func_name, 1, VAR_DOMAIN)
+	   != NULL)
+	  || (lookup_partial_symbol (objfile, ps, func_name, 0, VAR_DOMAIN)
+	      != NULL))
+	psymtab_to_symtab (objfile, ps);
+    }
 }
 
 /* Psymtab version of expand_all_symtabs.  See its definition in
@@ -1037,12 +1016,8 @@ psym_expand_symtabs_for_function (struct objfile *objfile,
 static void
 psym_expand_all_symtabs (struct objfile *objfile)
 {
-  struct partial_symtab *psymtab;
-
-  ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, psymtab)
-    {
-      psymtab_to_symtab (objfile, psymtab);
-    }
+  for (struct partial_symtab *psymtab : require_partial_symbols (objfile, 1))
+    psymtab_to_symtab (objfile, psymtab);
 }
 
 /* Psymtab version of expand_symtabs_with_fullname.  See its definition in
@@ -1052,9 +1027,7 @@ static void
 psym_expand_symtabs_with_fullname (struct objfile *objfile,
 				   const char *fullname)
 {
-  struct partial_symtab *p;
-
-  ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
+  for (struct partial_symtab *p : require_partial_symbols (objfile, 1))
     {
       /* Anonymous psymtabs don't have a name of a source file.  */
       if (p->anonymous)
@@ -1077,9 +1050,7 @@ psym_map_symbol_filenames (struct objfile *objfile,
 			   symbol_filename_ftype *fun, void *data,
 			   int need_fullname)
 {
-  struct partial_symtab *ps;
-
-  ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
+  for (struct partial_symtab *ps : require_partial_symbols (objfile, 1))
     {
       const char *fullname;
 
@@ -1192,9 +1163,8 @@ psym_map_matching_symbols (struct objfile *objfile,
 			   symbol_compare_ftype *ordered_compare)
 {
   const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
-  struct partial_symtab *ps;
 
-  ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
+  for (struct partial_symtab *ps : require_partial_symbols (objfile, 1))
     {
       QUIT;
       if (ps->readin
@@ -1317,17 +1287,13 @@ psym_expand_symtabs_matching
    gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
    enum search_domain domain)
 {
-  struct partial_symtab *ps;
-
   lookup_name_info lookup_name = lookup_name_in.make_ignore_params ();
 
   /* Clear the search flags.  */
-  ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
-    {
-      ps->searched_flag = PST_NOT_SEARCHED;
-    }
+  for (struct partial_symtab *ps : require_partial_symbols (objfile, 1))
+    ps->searched_flag = PST_NOT_SEARCHED;
 
-  ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
+  for (struct partial_symtab *ps : objfile_psymtabs (objfile))
     {
       QUIT;
 
@@ -1416,23 +1382,21 @@ psym_find_compunit_symtab_by_address (struct objfile *objfile,
 {
   if (objfile->psymbol_map.empty ())
     {
-      struct partial_symtab *pst;
-
       std::set<CORE_ADDR> seen_addrs;
 
-      ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
-      {
-	psym_fill_psymbol_map (objfile, pst,
-			       &seen_addrs,
-			       objfile->global_psymbols,
-			       pst->globals_offset,
-			       pst->n_global_syms);
-	psym_fill_psymbol_map (objfile, pst,
-			       &seen_addrs,
-			       objfile->static_psymbols,
-			       pst->statics_offset,
-			       pst->n_static_syms);
-      }
+      for (struct partial_symtab *pst : require_partial_symbols (objfile, 1))
+	{
+	  psym_fill_psymbol_map (objfile, pst,
+				 &seen_addrs,
+				 objfile->global_psymbols,
+				 pst->globals_offset,
+				 pst->n_global_syms);
+	  psym_fill_psymbol_map (objfile, pst,
+				 &seen_addrs,
+				 objfile->static_psymbols,
+				 pst->statics_offset,
+				 pst->n_static_syms);
+	}
 
       objfile->psymbol_map.shrink_to_fit ();
 
@@ -1868,7 +1832,6 @@ maintenance_print_psymbols (const char *args, int from_tty)
 {
   struct ui_file *outfile = gdb_stdout;
   char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
-  struct partial_symtab *ps;
   int i, outfile_idx, found;
   CORE_ADDR pc = 0;
   struct obj_section *section = NULL;
@@ -1956,7 +1919,8 @@ maintenance_print_psymbols (const char *args, int from_tty)
 
 	  /* We don't assume each pc has a unique objfile (this is for
 	     debugging).  */
-	  ps = find_pc_sect_psymtab (objfile, pc, section, msymbol);
+	  struct partial_symtab *ps = find_pc_sect_psymtab (objfile, pc,
+							    section, msymbol);
 	  if (ps != NULL)
 	    {
 	      if (!printed_objfile_header)
@@ -1972,7 +1936,7 @@ maintenance_print_psymbols (const char *args, int from_tty)
 	}
       else
 	{
-	  ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
+	  for (struct partial_symtab *ps : require_partial_symbols (objfile, 1))
 	    {
 	      int print_for_source = 0;
 
@@ -2032,13 +1996,12 @@ maintenance_info_psymtabs (const char *regexp, int from_tty)
     for (struct objfile *objfile : all_objfiles (pspace))
       {
 	struct gdbarch *gdbarch = get_objfile_arch (objfile);
-	struct partial_symtab *psymtab;
 
 	/* We don't want to print anything for this objfile until we
 	   actually find a symtab whose name matches.  */
 	int printed_objfile_start = 0;
 
-	ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, psymtab)
+	for (struct partial_symtab *psymtab : require_partial_symbols (objfile, 1))
 	  {
 	    QUIT;
 
@@ -2139,13 +2102,12 @@ maintenance_check_psymtabs (const char *ignore, int from_tty)
 {
   struct symbol *sym;
   struct compunit_symtab *cust = NULL;
-  struct partial_symtab *ps;
   const struct blockvector *bv;
   struct block *b;
   int length;
 
   for (struct objfile *objfile : all_objfiles (current_program_space))
-    ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
+    for (struct partial_symtab *ps : require_partial_symbols (objfile, 1))
       {
 	struct gdbarch *gdbarch = get_objfile_arch (objfile);
 
diff --git a/gdb/psymtab.h b/gdb/psymtab.h
index 6d9f257f31..978fccb866 100644
--- a/gdb/psymtab.h
+++ b/gdb/psymtab.h
@@ -21,6 +21,7 @@
 #define PSYMTAB_H
 
 #include "symfile.h"
+#include "common/next-iterator.h"
 
 /* A bcache for partial symbols.  */
 
@@ -35,12 +36,25 @@ extern const struct quick_symbol_functions psym_functions;
 extern const struct quick_symbol_functions dwarf2_gdb_index_functions;
 extern const struct quick_symbol_functions dwarf2_debug_names_functions;
 
+/* A range adapter that makes it possible to iterate over all
+   psymtabs in one objfile.  */
+
+class objfile_psymtabs : public next_adapter<struct partial_symtab>
+{
+public:
+
+  explicit objfile_psymtabs (struct objfile *objfile)
+    : next_adapter<struct partial_symtab> (objfile->psymtabs)
+  {
+  }
+};
+
 /* Ensure that the partial symbols for OBJFILE have been loaded.  If
    VERBOSE is non-zero, then this will print a message when symbols
-   are loaded.  This function always returns its argument, as a
-   convenience.  */
+   are loaded.  This function returns a range adapter suitable for
+   iterating over the psymtabs of OBJFILE.  */
 
-extern struct objfile *require_partial_symbols (struct objfile *objfile,
-						int verbose);
+extern objfile_psymtabs require_partial_symbols (struct objfile *objfile,
+						 int verbose);
 
 #endif /* PSYMTAB_H */
-- 
2.17.2

^ permalink raw reply	[flat|nested] 36+ messages in thread

* [PATCH 00/12] Remove some ALL_* iteration macros
@ 2018-11-25 16:54 Tom Tromey
  2018-11-25 16:54 ` [PATCH 09/12] Remove ALL_OBJFILE_FILETABS Tom Tromey
                   ` (14 more replies)
  0 siblings, 15 replies; 36+ messages in thread
From: Tom Tromey @ 2018-11-25 16:54 UTC (permalink / raw)
  To: gdb-patches

This series removes various ALL_* iteration macros, in favor of C++
iterators, range adapters, and ranged for loops.  I've been wanting
this for a while, because it helps a little bit with various
experiments of mine that involve changing objfile lifetime management;
Pedro's thread iterator patch prompted me to finally do this.

The main downside of removing these macros is that it involves some
reindentation; and expanding some macros to two nested loops means a
couple somewhat ugly reformattings.

On the plus side, though, this tightens the scope of iteration
variables, which is good.  And, it removes some hairy code,
particularly the ALL_OBJSECTIONS patch.

There are still a few more such macros that could be converted.  And,
I think inf_threads_iterator could be converted to use next_iterator.
I can do some of this if there's interest.

Regression tested by the buildbot.

Tom


^ permalink raw reply	[flat|nested] 36+ messages in thread

* [PATCH 02/12] Remove ALL_PSPACE_OBJFILES
  2018-11-25 16:54 [PATCH 00/12] Remove some ALL_* iteration macros Tom Tromey
                   ` (9 preceding siblings ...)
  2018-11-25 16:54 ` [PATCH 05/12] Remove ALL_MSYMBOLS and ALL_OBJFILE_MSYMBOLS Tom Tromey
@ 2018-11-25 16:55 ` Tom Tromey
  2018-11-25 16:55 ` [PATCH 06/12] Remove ALL_OBJFILE_COMPUNITS Tom Tromey
                   ` (3 subsequent siblings)
  14 siblings, 0 replies; 36+ messages in thread
From: Tom Tromey @ 2018-11-25 16:55 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This removes the ALL_PSPACE_OBJFILES macro in favor of ranged for
loops.

gdb/ChangeLog
2018-11-25  Tom Tromey  <tom@tromey.com>

	* probe.c (parse_probes_in_pspace): Use all_objfiles.
	* guile/scm-progspace.c (gdbscm_progspace_objfiles): Use
	all_objfiles.
	* objfiles.h (ALL_PSPACE_OBJFILES): Remove.
	* symmisc.c (print_symbol_bcache_statistics)
	(print_objfile_statistics, maintenance_print_objfiles)
	(maintenance_info_symtabs, maintenance_check_symtabs)
	(maintenance_expand_symtabs, maintenance_info_line_tables): Use
	all_objfiles.
	* source.c (forget_cached_source_info): Use all_objfiles.
	* symfile-debug.c (set_debug_symfile): Use all_objfiles.
	* elfread.c (elf_gnu_ifunc_resolve_by_cache)
	(elf_gnu_ifunc_resolve_by_got): Use all_objfiles.
	* objfiles.c (update_section_map): Use all_objfiles.
	(shared_objfile_contains_address_p): Likewise.
	* psymtab.c (maintenance_info_psymtabs): Use all_objfiles.
	* python/py-progspace.c (pspy_get_objfiles): Use all_objfiles.
---
 gdb/ChangeLog             |  20 ++
 gdb/elfread.c             |   7 +-
 gdb/guile/scm-progspace.c |  17 +-
 gdb/objfiles.c            |   9 +-
 gdb/objfiles.h            |   5 -
 gdb/probe.c               |   4 +-
 gdb/psymtab.c             | 190 ++++++++---------
 gdb/python/py-progspace.c |   4 +-
 gdb/source.c              |   9 +-
 gdb/symfile-debug.c       |  27 ++-
 gdb/symmisc.c             | 425 +++++++++++++++++++-------------------
 11 files changed, 359 insertions(+), 358 deletions(-)

diff --git a/gdb/elfread.c b/gdb/elfread.c
index 71e6fcca6e..0749f0d205 100644
--- a/gdb/elfread.c
+++ b/gdb/elfread.c
@@ -758,9 +758,7 @@ elf_gnu_ifunc_record_cache (const char *name, CORE_ADDR addr)
 static int
 elf_gnu_ifunc_resolve_by_cache (const char *name, CORE_ADDR *addr_p)
 {
-  struct objfile *objfile;
-
-  ALL_PSPACE_OBJFILES (current_program_space, objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       htab_t htab;
       struct elf_gnu_ifunc_cache *entry_p;
@@ -800,13 +798,12 @@ static int
 elf_gnu_ifunc_resolve_by_got (const char *name, CORE_ADDR *addr_p)
 {
   char *name_got_plt;
-  struct objfile *objfile;
   const size_t got_suffix_len = strlen (SYMBOL_GOT_PLT_SUFFIX);
 
   name_got_plt = (char *) alloca (strlen (name) + got_suffix_len + 1);
   sprintf (name_got_plt, "%s" SYMBOL_GOT_PLT_SUFFIX, name);
 
-  ALL_PSPACE_OBJFILES (current_program_space, objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       bfd *obfd = objfile->obfd;
       struct gdbarch *gdbarch = get_objfile_arch (objfile);
diff --git a/gdb/guile/scm-progspace.c b/gdb/guile/scm-progspace.c
index 088f067370..2689fd63ac 100644
--- a/gdb/guile/scm-progspace.c
+++ b/gdb/guile/scm-progspace.c
@@ -285,20 +285,19 @@ gdbscm_progspace_objfiles (SCM self)
 {
   pspace_smob *p_smob
     = psscm_get_valid_pspace_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
-  struct objfile *objfile;
   SCM result;
 
   result = SCM_EOL;
 
-  ALL_PSPACE_OBJFILES (p_smob->pspace, objfile)
-  {
-    if (objfile->separate_debug_objfile_backlink == NULL)
-      {
-	SCM item = ofscm_scm_from_objfile (objfile);
+  for (struct objfile *objfile : all_objfiles (p_smob->pspace))
+    {
+      if (objfile->separate_debug_objfile_backlink == NULL)
+	{
+	  SCM item = ofscm_scm_from_objfile (objfile);
 
-	result = scm_cons (item, result);
-      }
-  }
+	  result = scm_cons (item, result);
+	}
+    }
 
   /* We don't really have to return the list in the same order as recorded
      internally, but for consistency we do.  We still advertise that one
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index a9b8fa7c58..dec92df816 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -1322,7 +1322,6 @@ update_section_map (struct program_space *pspace,
   struct objfile_pspace_info *pspace_info;
   int alloc_size, map_size, i;
   struct obj_section *s, **map;
-  struct objfile *objfile;
 
   pspace_info = get_objfile_pspace_data (pspace);
   gdb_assert (pspace_info->section_map_dirty != 0
@@ -1332,7 +1331,7 @@ update_section_map (struct program_space *pspace,
   xfree (map);
 
   alloc_size = 0;
-  ALL_PSPACE_OBJFILES (pspace, objfile)
+  for (struct objfile *objfile : all_objfiles (pspace))
     ALL_OBJFILE_OSECTIONS (objfile, s)
       if (insert_section_p (objfile->obfd, s->the_bfd_section))
 	alloc_size += 1;
@@ -1348,7 +1347,7 @@ update_section_map (struct program_space *pspace,
   map = XNEWVEC (struct obj_section *, alloc_size);
 
   i = 0;
-  ALL_PSPACE_OBJFILES (pspace, objfile)
+  for (struct objfile *objfile : all_objfiles (pspace))
     ALL_OBJFILE_OSECTIONS (objfile, s)
       if (insert_section_p (objfile->obfd, s->the_bfd_section))
 	map[i++] = s;
@@ -1492,9 +1491,7 @@ int
 shared_objfile_contains_address_p (struct program_space *pspace,
 				   CORE_ADDR address)
 {
-  struct objfile *objfile;
-
-  ALL_PSPACE_OBJFILES (pspace, objfile)
+  for (struct objfile *objfile : all_objfiles (pspace))
     {
       if ((objfile->flags & OBJF_SHARED) != 0
 	  && is_addr_in_objfile (address, objfile))
diff --git a/gdb/objfiles.h b/gdb/objfiles.h
index 9005be2795..dee64c816a 100644
--- a/gdb/objfiles.h
+++ b/gdb/objfiles.h
@@ -586,11 +586,6 @@ public:
    ALL_OBJFILES_SAFE works even if you delete the objfile during the
    traversal.  */
 
-/* Traverse all object files in program space SS.  */
-
-#define ALL_PSPACE_OBJFILES(ss, obj)					\
-  for ((obj) = ss->objfiles; (obj) != NULL; (obj) = (obj)->next)
-
 #define ALL_OBJFILES(obj)			    \
   for ((obj) = current_program_space->objfiles; \
        (obj) != NULL;				    \
diff --git a/gdb/probe.c b/gdb/probe.c
index 1f3da213ef..ace8efb541 100644
--- a/gdb/probe.c
+++ b/gdb/probe.c
@@ -73,9 +73,7 @@ parse_probes_in_pspace (const static_probe_ops *spops,
 			const char *name,
 			std::vector<symtab_and_line> *result)
 {
-  struct objfile *objfile;
-
-  ALL_PSPACE_OBJFILES (search_pspace, objfile)
+  for (struct objfile *objfile : all_objfiles (search_pspace))
     {
       if (!objfile->sf || !objfile->sf->sym_probe_fns)
 	continue;
diff --git a/gdb/psymtab.c b/gdb/psymtab.c
index 6d76e8d489..0ee5c9d516 100644
--- a/gdb/psymtab.c
+++ b/gdb/psymtab.c
@@ -2031,106 +2031,112 @@ static void
 maintenance_info_psymtabs (const char *regexp, int from_tty)
 {
   struct program_space *pspace;
-  struct objfile *objfile;
 
   if (regexp)
     re_comp (regexp);
 
   ALL_PSPACES (pspace)
-    ALL_PSPACE_OBJFILES (pspace, objfile)
-    {
-      struct gdbarch *gdbarch = get_objfile_arch (objfile);
-      struct partial_symtab *psymtab;
-
-      /* We don't want to print anything for this objfile until we
-         actually find a symtab whose name matches.  */
-      int printed_objfile_start = 0;
-
-      ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, psymtab)
-	{
-	  QUIT;
-
-	  if (! regexp
-	      || re_exec (psymtab->filename))
-	    {
-	      if (! printed_objfile_start)
-		{
-		  printf_filtered ("{ objfile %s ", objfile_name (objfile));
-		  wrap_here ("  ");
-		  printf_filtered ("((struct objfile *) %s)\n",
-				   host_address_to_string (objfile));
-		  printed_objfile_start = 1;
-		}
-
-	      printf_filtered ("  { psymtab %s ", psymtab->filename);
-	      wrap_here ("    ");
-	      printf_filtered ("((struct partial_symtab *) %s)\n",
-			       host_address_to_string (psymtab));
-
-	      printf_filtered ("    readin %s\n",
-			       psymtab->readin ? "yes" : "no");
-	      printf_filtered ("    fullname %s\n",
-			       psymtab->fullname
-			       ? psymtab->fullname : "(null)");
-	      printf_filtered ("    text addresses ");
-	      fputs_filtered (paddress (gdbarch, psymtab->text_low (objfile)),
-			      gdb_stdout);
-	      printf_filtered (" -- ");
-	      fputs_filtered (paddress (gdbarch, psymtab->text_high (objfile)),
-			      gdb_stdout);
-	      printf_filtered ("\n");
-	      printf_filtered ("    psymtabs_addrmap_supported %s\n",
-			       (psymtab->psymtabs_addrmap_supported
-				? "yes" : "no"));
-	      printf_filtered ("    globals ");
-	      if (psymtab->n_global_syms)
-		{
-		  auto p = &objfile->global_psymbols[psymtab->globals_offset];
-
-		  printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
-				   host_address_to_string (p),
-				   psymtab->n_global_syms);
-		}
-	      else
-		printf_filtered ("(none)\n");
-	      printf_filtered ("    statics ");
-	      if (psymtab->n_static_syms)
-		{
-		  auto p = &objfile->static_psymbols[psymtab->statics_offset];
-
-		  printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
-				   host_address_to_string (p),
-				   psymtab->n_static_syms);
-		}
-	      else
-		printf_filtered ("(none)\n");
-	      printf_filtered ("    dependencies ");
-	      if (psymtab->number_of_dependencies)
-		{
-		  int i;
+    for (struct objfile *objfile : all_objfiles (pspace))
+      {
+	struct gdbarch *gdbarch = get_objfile_arch (objfile);
+	struct partial_symtab *psymtab;
 
-		  printf_filtered ("{\n");
-		  for (i = 0; i < psymtab->number_of_dependencies; i++)
-		    {
-		      struct partial_symtab *dep = psymtab->dependencies[i];
+	/* We don't want to print anything for this objfile until we
+	   actually find a symtab whose name matches.  */
+	int printed_objfile_start = 0;
 
-		      /* Note the string concatenation there --- no comma.  */
-		      printf_filtered ("      psymtab %s "
-				       "((struct partial_symtab *) %s)\n",
-				       dep->filename,
-				       host_address_to_string (dep));
-		    }
-		  printf_filtered ("    }\n");
-		}
-	      else
-		printf_filtered ("(none)\n");
-	      printf_filtered ("  }\n");
-	    }
-	}
+	ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, psymtab)
+	  {
+	    QUIT;
+
+	    if (! regexp
+		|| re_exec (psymtab->filename))
+	      {
+		if (! printed_objfile_start)
+		  {
+		    printf_filtered ("{ objfile %s ", objfile_name (objfile));
+		    wrap_here ("  ");
+		    printf_filtered ("((struct objfile *) %s)\n",
+				     host_address_to_string (objfile));
+		    printed_objfile_start = 1;
+		  }
+
+		printf_filtered ("  { psymtab %s ", psymtab->filename);
+		wrap_here ("    ");
+		printf_filtered ("((struct partial_symtab *) %s)\n",
+				 host_address_to_string (psymtab));
+
+		printf_filtered ("    readin %s\n",
+				 psymtab->readin ? "yes" : "no");
+		printf_filtered ("    fullname %s\n",
+				 psymtab->fullname
+				 ? psymtab->fullname : "(null)");
+		printf_filtered ("    text addresses ");
+		fputs_filtered (paddress (gdbarch,
+					  psymtab->text_low (objfile)),
+				gdb_stdout);
+		printf_filtered (" -- ");
+		fputs_filtered (paddress (gdbarch,
+					  psymtab->text_high (objfile)),
+				gdb_stdout);
+		printf_filtered ("\n");
+		printf_filtered ("    psymtabs_addrmap_supported %s\n",
+				 (psymtab->psymtabs_addrmap_supported
+				  ? "yes" : "no"));
+		printf_filtered ("    globals ");
+		if (psymtab->n_global_syms)
+		  {
+		    auto p
+		      = &objfile->global_psymbols[psymtab->globals_offset];
+
+		    printf_filtered
+		      ("(* (struct partial_symbol **) %s @ %d)\n",
+		       host_address_to_string (p),
+		       psymtab->n_global_syms);
+		  }
+		else
+		  printf_filtered ("(none)\n");
+		printf_filtered ("    statics ");
+		if (psymtab->n_static_syms)
+		  {
+		    auto p
+		      = &objfile->static_psymbols[psymtab->statics_offset];
+
+		    printf_filtered
+		      ("(* (struct partial_symbol **) %s @ %d)\n",
+		       host_address_to_string (p),
+		       psymtab->n_static_syms);
+		  }
+		else
+		  printf_filtered ("(none)\n");
+		printf_filtered ("    dependencies ");
+		if (psymtab->number_of_dependencies)
+		  {
+		    int i;
+
+		    printf_filtered ("{\n");
+		    for (i = 0; i < psymtab->number_of_dependencies; i++)
+		      {
+			struct partial_symtab *dep = psymtab->dependencies[i];
+
+			/* Note the string concatenation there --- no
+			   comma.  */
+			printf_filtered ("      psymtab %s "
+					 "((struct partial_symtab *) %s)\n",
+					 dep->filename,
+					 host_address_to_string (dep));
+		      }
+		    printf_filtered ("    }\n");
+		  }
+		else
+		  printf_filtered ("(none)\n");
+		printf_filtered ("  }\n");
+	      }
+	  }
 
-      if (printed_objfile_start)
-        printf_filtered ("}\n");
-    }
+	if (printed_objfile_start)
+	  printf_filtered ("}\n");
+      }
 }
 
 /* Check consistency of currently expanded psymtabs vs symtabs.  */
diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c
index bd195a54c1..9ffcfc5c97 100644
--- a/gdb/python/py-progspace.c
+++ b/gdb/python/py-progspace.c
@@ -341,9 +341,7 @@ pspy_get_objfiles (PyObject *self_, PyObject *args)
 
   if (self->pspace != NULL)
     {
-      struct objfile *objf;
-
-      ALL_PSPACE_OBJFILES (self->pspace, objf)
+      for (struct objfile *objf : all_objfiles (self->pspace))
 	{
 	  gdbpy_ref<> item = objfile_to_objfile_object (objf);
 
diff --git a/gdb/source.c b/gdb/source.c
index e295fbf49e..f60c7b5b5f 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -385,13 +385,12 @@ void
 forget_cached_source_info (void)
 {
   struct program_space *pspace;
-  struct objfile *objfile;
 
   ALL_PSPACES (pspace)
-    ALL_PSPACE_OBJFILES (pspace, objfile)
-    {
-      forget_cached_source_info_for_objfile (objfile);
-    }
+    for (struct objfile *objfile : all_objfiles (pspace))
+      {
+	forget_cached_source_info_for_objfile (objfile);
+      }
 
   last_source_visited = NULL;
 }
diff --git a/gdb/symfile-debug.c b/gdb/symfile-debug.c
index 3aab1736b4..d9ae9a8f0c 100644
--- a/gdb/symfile-debug.c
+++ b/gdb/symfile-debug.c
@@ -660,22 +660,21 @@ static void
 set_debug_symfile (const char *args, int from_tty, struct cmd_list_element *c)
 {
   struct program_space *pspace;
-  struct objfile *objfile;
 
   ALL_PSPACES (pspace)
-    ALL_PSPACE_OBJFILES (pspace, objfile)
-    {
-      if (debug_symfile)
-	{
-	  if (!symfile_debug_installed (objfile))
-	    install_symfile_debug_logging (objfile);
-	}
-      else
-	{
-	  if (symfile_debug_installed (objfile))
-	    uninstall_symfile_debug_logging (objfile);
-	}
-    }
+    for (struct objfile *objfile : all_objfiles (pspace))
+      {
+	if (debug_symfile)
+	  {
+	    if (!symfile_debug_installed (objfile))
+	      install_symfile_debug_logging (objfile);
+	  }
+	else
+	  {
+	    if (symfile_debug_installed (objfile))
+	      uninstall_symfile_debug_logging (objfile);
+	  }
+      }
 }
 
 static void
diff --git a/gdb/symmisc.c b/gdb/symmisc.c
index d30a35481e..fd1d298ec1 100644
--- a/gdb/symmisc.c
+++ b/gdb/symmisc.c
@@ -62,86 +62,84 @@ void
 print_symbol_bcache_statistics (void)
 {
   struct program_space *pspace;
-  struct objfile *objfile;
 
   ALL_PSPACES (pspace)
-    ALL_PSPACE_OBJFILES (pspace, objfile)
-  {
-    QUIT;
-    printf_filtered (_("Byte cache statistics for '%s':\n"),
-		     objfile_name (objfile));
-    print_bcache_statistics (psymbol_bcache_get_bcache (objfile->psymbol_cache),
-                             "partial symbol cache");
-    print_bcache_statistics (objfile->per_bfd->macro_cache,
-			     "preprocessor macro cache");
-    print_bcache_statistics (objfile->per_bfd->filename_cache,
-			     "file name cache");
-  }
+    for (struct objfile *objfile : all_objfiles (pspace))
+      {
+	QUIT;
+	printf_filtered (_("Byte cache statistics for '%s':\n"),
+			 objfile_name (objfile));
+	print_bcache_statistics (psymbol_bcache_get_bcache (objfile->psymbol_cache),
+				 "partial symbol cache");
+	print_bcache_statistics (objfile->per_bfd->macro_cache,
+				 "preprocessor macro cache");
+	print_bcache_statistics (objfile->per_bfd->filename_cache,
+				 "file name cache");
+      }
 }
 
 void
 print_objfile_statistics (void)
 {
   struct program_space *pspace;
-  struct objfile *objfile;
   struct compunit_symtab *cu;
   struct symtab *s;
   int i, linetables, blockvectors;
 
   ALL_PSPACES (pspace)
-    ALL_PSPACE_OBJFILES (pspace, objfile)
-  {
-    QUIT;
-    printf_filtered (_("Statistics for '%s':\n"), objfile_name (objfile));
-    if (OBJSTAT (objfile, n_stabs) > 0)
-      printf_filtered (_("  Number of \"stab\" symbols read: %d\n"),
-		       OBJSTAT (objfile, n_stabs));
-    if (objfile->per_bfd->n_minsyms > 0)
-      printf_filtered (_("  Number of \"minimal\" symbols read: %d\n"),
-		       objfile->per_bfd->n_minsyms);
-    if (OBJSTAT (objfile, n_psyms) > 0)
-      printf_filtered (_("  Number of \"partial\" symbols read: %d\n"),
-		       OBJSTAT (objfile, n_psyms));
-    if (OBJSTAT (objfile, n_syms) > 0)
-      printf_filtered (_("  Number of \"full\" symbols read: %d\n"),
-		       OBJSTAT (objfile, n_syms));
-    if (OBJSTAT (objfile, n_types) > 0)
-      printf_filtered (_("  Number of \"types\" defined: %d\n"),
-		       OBJSTAT (objfile, n_types));
-    if (objfile->sf)
-      objfile->sf->qf->print_stats (objfile);
-    i = linetables = blockvectors = 0;
-    ALL_OBJFILE_FILETABS (objfile, cu, s)
-      {
-        i++;
-        if (SYMTAB_LINETABLE (s) != NULL)
-          linetables++;
-      }
-    ALL_OBJFILE_COMPUNITS (objfile, cu)
-      blockvectors++;
-    printf_filtered (_("  Number of symbol tables: %d\n"), i);
-    printf_filtered (_("  Number of symbol tables with line tables: %d\n"),
-                     linetables);
-    printf_filtered (_("  Number of symbol tables with blockvectors: %d\n"),
-                     blockvectors);
-
-    if (OBJSTAT (objfile, sz_strtab) > 0)
-      printf_filtered (_("  Space used by string tables: %d\n"),
-		       OBJSTAT (objfile, sz_strtab));
-    printf_filtered (_("  Total memory used for objfile obstack: %s\n"),
-		     pulongest (obstack_memory_used (&objfile
-						     ->objfile_obstack)));
-    printf_filtered (_("  Total memory used for BFD obstack: %s\n"),
-		     pulongest (obstack_memory_used (&objfile->per_bfd
-						     ->storage_obstack)));
-    printf_filtered (_("  Total memory used for psymbol cache: %d\n"),
-		     bcache_memory_used (psymbol_bcache_get_bcache
-		                          (objfile->psymbol_cache)));
-    printf_filtered (_("  Total memory used for macro cache: %d\n"),
-		     bcache_memory_used (objfile->per_bfd->macro_cache));
-    printf_filtered (_("  Total memory used for file name cache: %d\n"),
-		     bcache_memory_used (objfile->per_bfd->filename_cache));
-  }
+  for (struct objfile *objfile : all_objfiles (pspace))
+    {
+      QUIT;
+      printf_filtered (_("Statistics for '%s':\n"), objfile_name (objfile));
+      if (OBJSTAT (objfile, n_stabs) > 0)
+	printf_filtered (_("  Number of \"stab\" symbols read: %d\n"),
+			 OBJSTAT (objfile, n_stabs));
+      if (objfile->per_bfd->n_minsyms > 0)
+	printf_filtered (_("  Number of \"minimal\" symbols read: %d\n"),
+			 objfile->per_bfd->n_minsyms);
+      if (OBJSTAT (objfile, n_psyms) > 0)
+	printf_filtered (_("  Number of \"partial\" symbols read: %d\n"),
+			 OBJSTAT (objfile, n_psyms));
+      if (OBJSTAT (objfile, n_syms) > 0)
+	printf_filtered (_("  Number of \"full\" symbols read: %d\n"),
+			 OBJSTAT (objfile, n_syms));
+      if (OBJSTAT (objfile, n_types) > 0)
+	printf_filtered (_("  Number of \"types\" defined: %d\n"),
+			 OBJSTAT (objfile, n_types));
+      if (objfile->sf)
+	objfile->sf->qf->print_stats (objfile);
+      i = linetables = blockvectors = 0;
+      ALL_OBJFILE_FILETABS (objfile, cu, s)
+	{
+	  i++;
+	  if (SYMTAB_LINETABLE (s) != NULL)
+	    linetables++;
+	}
+      ALL_OBJFILE_COMPUNITS (objfile, cu)
+	blockvectors++;
+      printf_filtered (_("  Number of symbol tables: %d\n"), i);
+      printf_filtered (_("  Number of symbol tables with line tables: %d\n"),
+		       linetables);
+      printf_filtered (_("  Number of symbol tables with blockvectors: %d\n"),
+		       blockvectors);
+
+      if (OBJSTAT (objfile, sz_strtab) > 0)
+	printf_filtered (_("  Space used by string tables: %d\n"),
+			 OBJSTAT (objfile, sz_strtab));
+      printf_filtered (_("  Total memory used for objfile obstack: %s\n"),
+		       pulongest (obstack_memory_used (&objfile
+						       ->objfile_obstack)));
+      printf_filtered (_("  Total memory used for BFD obstack: %s\n"),
+		       pulongest (obstack_memory_used (&objfile->per_bfd
+						       ->storage_obstack)));
+      printf_filtered (_("  Total memory used for psymbol cache: %d\n"),
+		       bcache_memory_used (psymbol_bcache_get_bcache
+					   (objfile->psymbol_cache)));
+      printf_filtered (_("  Total memory used for macro cache: %d\n"),
+		       bcache_memory_used (objfile->per_bfd->macro_cache));
+      printf_filtered (_("  Total memory used for file name cache: %d\n"),
+		       bcache_memory_used (objfile->per_bfd->filename_cache));
+    }
 }
 
 static void
@@ -749,7 +747,6 @@ static void
 maintenance_print_objfiles (const char *regexp, int from_tty)
 {
   struct program_space *pspace;
-  struct objfile *objfile;
 
   dont_repeat ();
 
@@ -757,7 +754,7 @@ maintenance_print_objfiles (const char *regexp, int from_tty)
     re_comp (regexp);
 
   ALL_PSPACES (pspace)
-    ALL_PSPACE_OBJFILES (pspace, objfile)
+    for (struct objfile *objfile : all_objfiles (pspace))
       {
 	QUIT;
 	if (! regexp
@@ -772,7 +769,6 @@ static void
 maintenance_info_symtabs (const char *regexp, int from_tty)
 {
   struct program_space *pspace;
-  struct objfile *objfile;
 
   dont_repeat ();
 
@@ -780,78 +776,78 @@ maintenance_info_symtabs (const char *regexp, int from_tty)
     re_comp (regexp);
 
   ALL_PSPACES (pspace)
-    ALL_PSPACE_OBJFILES (pspace, objfile)
-    {
-      struct compunit_symtab *cust;
-      struct symtab *symtab;
-
-      /* We don't want to print anything for this objfile until we
-         actually find a symtab whose name matches.  */
-      int printed_objfile_start = 0;
-
-      ALL_OBJFILE_COMPUNITS (objfile, cust)
-	{
-	  int printed_compunit_symtab_start = 0;
+    for (struct objfile *objfile : all_objfiles (pspace))
+      {
+	struct compunit_symtab *cust;
+	struct symtab *symtab;
 
-	  ALL_COMPUNIT_FILETABS (cust, symtab)
-	    {
-	      QUIT;
+	/* We don't want to print anything for this objfile until we
+	   actually find a symtab whose name matches.  */
+	int printed_objfile_start = 0;
 
-	      if (! regexp
-		  || re_exec (symtab_to_filename_for_display (symtab)))
-		{
-		  if (! printed_objfile_start)
-		    {
-		      printf_filtered ("{ objfile %s ", objfile_name (objfile));
-		      wrap_here ("  ");
-		      printf_filtered ("((struct objfile *) %s)\n",
-				       host_address_to_string (objfile));
-		      printed_objfile_start = 1;
-		    }
-		  if (! printed_compunit_symtab_start)
-		    {
-		      printf_filtered ("  { ((struct compunit_symtab *) %s)\n",
-				       host_address_to_string (cust));
-		      printf_filtered ("    debugformat %s\n",
-				       COMPUNIT_DEBUGFORMAT (cust));
-		      printf_filtered ("    producer %s\n",
-				       COMPUNIT_PRODUCER (cust) != NULL
-				       ? COMPUNIT_PRODUCER (cust)
-				       : "(null)");
-		      printf_filtered ("    dirname %s\n",
-				       COMPUNIT_DIRNAME (cust) != NULL
-				       ? COMPUNIT_DIRNAME (cust)
-				       : "(null)");
-		      printf_filtered ("    blockvector"
-				       " ((struct blockvector *) %s)\n",
-				       host_address_to_string
+	ALL_OBJFILE_COMPUNITS (objfile, cust)
+	  {
+	    int printed_compunit_symtab_start = 0;
+
+	    ALL_COMPUNIT_FILETABS (cust, symtab)
+	      {
+		QUIT;
+
+		if (! regexp
+		    || re_exec (symtab_to_filename_for_display (symtab)))
+		  {
+		    if (! printed_objfile_start)
+		      {
+			printf_filtered ("{ objfile %s ", objfile_name (objfile));
+			wrap_here ("  ");
+			printf_filtered ("((struct objfile *) %s)\n",
+					 host_address_to_string (objfile));
+			printed_objfile_start = 1;
+		      }
+		    if (! printed_compunit_symtab_start)
+		      {
+			printf_filtered ("  { ((struct compunit_symtab *) %s)\n",
+					 host_address_to_string (cust));
+			printf_filtered ("    debugformat %s\n",
+					 COMPUNIT_DEBUGFORMAT (cust));
+			printf_filtered ("    producer %s\n",
+					 COMPUNIT_PRODUCER (cust) != NULL
+					 ? COMPUNIT_PRODUCER (cust)
+					 : "(null)");
+			printf_filtered ("    dirname %s\n",
+					 COMPUNIT_DIRNAME (cust) != NULL
+					 ? COMPUNIT_DIRNAME (cust)
+					 : "(null)");
+			printf_filtered ("    blockvector"
+					 " ((struct blockvector *) %s)\n",
+					 host_address_to_string
 				         (COMPUNIT_BLOCKVECTOR (cust)));
-		      printed_compunit_symtab_start = 1;
-		    }
-
-		  printf_filtered ("\t{ symtab %s ",
-				   symtab_to_filename_for_display (symtab));
-		  wrap_here ("    ");
-		  printf_filtered ("((struct symtab *) %s)\n",
-				   host_address_to_string (symtab));
-		  printf_filtered ("\t  fullname %s\n",
-				   symtab->fullname != NULL
-				   ? symtab->fullname
-				   : "(null)");
-		  printf_filtered ("\t  "
-				   "linetable ((struct linetable *) %s)\n",
-				   host_address_to_string (symtab->linetable));
-		  printf_filtered ("\t}\n");
-		}
-	    }
-
-	  if (printed_compunit_symtab_start)
-	    printf_filtered ("  }\n");
-	}
+			printed_compunit_symtab_start = 1;
+		      }
+
+		    printf_filtered ("\t{ symtab %s ",
+				     symtab_to_filename_for_display (symtab));
+		    wrap_here ("    ");
+		    printf_filtered ("((struct symtab *) %s)\n",
+				     host_address_to_string (symtab));
+		    printf_filtered ("\t  fullname %s\n",
+				     symtab->fullname != NULL
+				     ? symtab->fullname
+				     : "(null)");
+		    printf_filtered ("\t  "
+				     "linetable ((struct linetable *) %s)\n",
+				     host_address_to_string (symtab->linetable));
+		    printf_filtered ("\t}\n");
+		  }
+	      }
+
+	    if (printed_compunit_symtab_start)
+	      printf_filtered ("  }\n");
+	  }
 
-      if (printed_objfile_start)
-        printf_filtered ("}\n");
-    }
+	if (printed_objfile_start)
+	  printf_filtered ("}\n");
+      }
 }
 
 /* Check consistency of symtabs.
@@ -866,49 +862,48 @@ static void
 maintenance_check_symtabs (const char *ignore, int from_tty)
 {
   struct program_space *pspace;
-  struct objfile *objfile;
 
   ALL_PSPACES (pspace)
-    ALL_PSPACE_OBJFILES (pspace, objfile)
-    {
-      struct compunit_symtab *cust;
-
-      /* We don't want to print anything for this objfile until we
-         actually find something worth printing.  */
-      int printed_objfile_start = 0;
-
-      ALL_OBJFILE_COMPUNITS (objfile, cust)
-	{
-	  int found_something = 0;
-	  struct symtab *symtab = compunit_primary_filetab (cust);
-
-	  QUIT;
+    for (struct objfile *objfile : all_objfiles (pspace))
+      {
+	struct compunit_symtab *cust;
 
-	  if (COMPUNIT_BLOCKVECTOR (cust) == NULL)
-	    found_something = 1;
-	  /* Add more checks here.  */
+	/* We don't want to print anything for this objfile until we
+	   actually find something worth printing.  */
+	int printed_objfile_start = 0;
 
-	  if (found_something)
-	    {
-	      if (! printed_objfile_start)
-		{
-		  printf_filtered ("{ objfile %s ", objfile_name (objfile));
-		  wrap_here ("  ");
-		  printf_filtered ("((struct objfile *) %s)\n",
-				   host_address_to_string (objfile));
-		  printed_objfile_start = 1;
-		}
-	      printf_filtered ("  { symtab %s\n",
-			       symtab_to_filename_for_display (symtab));
-	      if (COMPUNIT_BLOCKVECTOR (cust) == NULL)
-		printf_filtered ("    NULL blockvector\n");
-	      printf_filtered ("  }\n");
-	    }
-	}
+	ALL_OBJFILE_COMPUNITS (objfile, cust)
+	  {
+	    int found_something = 0;
+	    struct symtab *symtab = compunit_primary_filetab (cust);
+
+	    QUIT;
+
+	    if (COMPUNIT_BLOCKVECTOR (cust) == NULL)
+	      found_something = 1;
+	    /* Add more checks here.  */
+
+	    if (found_something)
+	      {
+		if (! printed_objfile_start)
+		  {
+		    printf_filtered ("{ objfile %s ", objfile_name (objfile));
+		    wrap_here ("  ");
+		    printf_filtered ("((struct objfile *) %s)\n",
+				     host_address_to_string (objfile));
+		    printed_objfile_start = 1;
+		  }
+		printf_filtered ("  { symtab %s\n",
+				 symtab_to_filename_for_display (symtab));
+		if (COMPUNIT_BLOCKVECTOR (cust) == NULL)
+		  printf_filtered ("    NULL blockvector\n");
+		printf_filtered ("  }\n");
+	      }
+	  }
 
-      if (printed_objfile_start)
-        printf_filtered ("}\n");
-    }
+	if (printed_objfile_start)
+	  printf_filtered ("}\n");
+      }
 }
 
 /* Expand all symbol tables whose name matches an optional regexp.  */
@@ -917,7 +912,6 @@ static void
 maintenance_expand_symtabs (const char *args, int from_tty)
 {
   struct program_space *pspace;
-  struct objfile *objfile;
   char *regexp = NULL;
 
   /* We use buildargv here so that we handle spaces in the regexp
@@ -938,28 +932,28 @@ maintenance_expand_symtabs (const char *args, int from_tty)
     re_comp (regexp);
 
   ALL_PSPACES (pspace)
-    ALL_PSPACE_OBJFILES (pspace, objfile)
-    {
-      if (objfile->sf)
-	{
-	  objfile->sf->qf->expand_symtabs_matching
-	    (objfile,
-	     [&] (const char *filename, bool basenames)
-	     {
-	       /* KISS: Only apply the regexp to the complete file name.  */
-	       return (!basenames
-		       && (regexp == NULL || re_exec (filename)));
-	     },
-	     lookup_name_info::match_any (),
-	     [] (const char *symname)
-	     {
-	       /* Since we're not searching on symbols, just return true.  */
-	       return true;
-	     },
-	     NULL,
-	     ALL_DOMAIN);
-	}
-    }
+    for (struct objfile *objfile : all_objfiles (pspace))
+      {
+	if (objfile->sf)
+	  {
+	    objfile->sf->qf->expand_symtabs_matching
+	      (objfile,
+	       [&] (const char *filename, bool basenames)
+	       {
+		 /* KISS: Only apply the regexp to the complete file name.  */
+		 return (!basenames
+			 && (regexp == NULL || re_exec (filename)));
+	       },
+	       lookup_name_info::match_any (),
+	       [] (const char *symname)
+	       {
+		 /* Since we're not searching on symbols, just return true.  */
+		 return true;
+	       },
+	       NULL,
+	       ALL_DOMAIN);
+	  }
+      }
 }
 \f
 
@@ -1032,7 +1026,6 @@ static void
 maintenance_info_line_tables (const char *regexp, int from_tty)
 {
   struct program_space *pspace;
-  struct objfile *objfile;
 
   dont_repeat ();
 
@@ -1040,23 +1033,23 @@ maintenance_info_line_tables (const char *regexp, int from_tty)
     re_comp (regexp);
 
   ALL_PSPACES (pspace)
-    ALL_PSPACE_OBJFILES (pspace, objfile)
-    {
-      struct compunit_symtab *cust;
-      struct symtab *symtab;
-
-      ALL_OBJFILE_COMPUNITS (objfile, cust)
-	{
-	  ALL_COMPUNIT_FILETABS (cust, symtab)
-	    {
-	      QUIT;
+    for (struct objfile *objfile : all_objfiles (pspace))
+      {
+	struct compunit_symtab *cust;
+	struct symtab *symtab;
 
-	      if (regexp == NULL
-		  || re_exec (symtab_to_filename_for_display (symtab)))
-		maintenance_print_one_line_table (symtab, NULL);
-	    }
-	}
-    }
+	ALL_OBJFILE_COMPUNITS (objfile, cust)
+	  {
+	    ALL_COMPUNIT_FILETABS (cust, symtab)
+	      {
+		QUIT;
+
+		if (regexp == NULL
+		    || re_exec (symtab_to_filename_for_display (symtab)))
+		  maintenance_print_one_line_table (symtab, NULL);
+	      }
+	  }
+      }
 }
 
 \f
-- 
2.17.2

^ permalink raw reply	[flat|nested] 36+ messages in thread

* [PATCH 06/12] Remove ALL_OBJFILE_COMPUNITS
  2018-11-25 16:54 [PATCH 00/12] Remove some ALL_* iteration macros Tom Tromey
                   ` (10 preceding siblings ...)
  2018-11-25 16:55 ` [PATCH 02/12] Remove ALL_PSPACE_OBJFILES Tom Tromey
@ 2018-11-25 16:55 ` Tom Tromey
  2018-12-23  7:00 ` [PATCH 00/12] Remove some ALL_* iteration macros Joel Brobecker
                   ` (2 subsequent siblings)
  14 siblings, 0 replies; 36+ messages in thread
From: Tom Tromey @ 2018-11-25 16:55 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This removes ALL_OBJFILE_COMPUNITS, replacing its uses with ranged for
loops.  Because ALL_COMPUNITS is also updated, in some places a
declaration must be deleted to avoid shadowing.

gdb/ChangeLog
2018-11-25  Tom Tromey  <tom@tromey.com>

	* source.c (select_source_symtab)
	(forget_cached_source_info_for_objfile): Remove declaration.
	* mi/mi-cmd-file.c (mi_cmd_file_list_exec_source_files): Remove
	declaration.
	* maint.c (count_symtabs_and_blocks): Remove declaration.
	* cp-support.c (add_symbol_overload_list_qualified): Remove
	declaration.
	* coffread.c (coff_symtab_read): Remove declaration.
	* symtab.c (lookup_symbol_in_objfile_symtabs)
	(basic_lookup_transparent_type_1): Use objfile_compunits.
	(lookup_objfile_from_block, find_pc_sect_compunit_symtab)
	(info_sources_command, search_symbols)
	(default_collect_symbol_completion_matches_break_on)
	(make_source_files_completion_list): Remove declaration.
	* ada-lang.c (add_nonlocal_symbols): Use objfile_compunits.
	(ada_collect_symbol_completion_matches)
	(ada_add_global_exceptions): Remove declaration.
	* linespec.c (iterate_over_all_matching_symtabs): Use
	objfile_compunits.
	* objfiles.h (ALL_OBJFILE_COMPUNITS): Remove.
	(class objfile_compunits): New.
	(ALL_COMPUNITS): Use objfile_compunits.
	* symmisc.c (print_objfile_statistics, maintenance_info_symtabs)
	(maintenance_check_symtabs, maintenance_info_line_tables): Use
	objfile_compunits.
	* objfiles.c (objfile_relocate1): Use objfile_compunits.
---
 gdb/ChangeLog        | 29 +++++++++++++++++++
 gdb/ada-lang.c       |  5 +---
 gdb/coffread.c       |  1 -
 gdb/cp-support.c     |  1 -
 gdb/linespec.c       |  4 +--
 gdb/maint.c          |  1 -
 gdb/mi/mi-cmd-file.c |  1 -
 gdb/objfiles.c       | 68 ++++++++++++++++++++++----------------------
 gdb/objfiles.h       | 18 ++++++++----
 gdb/source.c         |  2 --
 gdb/symmisc.c        | 19 ++++---------
 gdb/symtab.c         | 14 ++-------
 12 files changed, 86 insertions(+), 77 deletions(-)

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 0520ec144a..1a1db17421 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -5603,7 +5603,6 @@ add_nonlocal_symbols (struct obstack *obstackp,
 		      const lookup_name_info &lookup_name,
 		      domain_enum domain, int global)
 {
-  struct compunit_symtab *cu;
   struct match_data data;
 
   memset (&data, 0, sizeof data);
@@ -5628,7 +5627,7 @@ add_nonlocal_symbols (struct obstack *obstackp,
 					       symbol_name_match_type::FULL,
 					       compare_names);
 
-      ALL_OBJFILE_COMPUNITS (objfile, cu)
+      for (struct compunit_symtab *cu : objfile_compunits (objfile))
 	{
 	  const struct block *global_block
 	    = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cu), GLOBAL_BLOCK);
@@ -6389,7 +6388,6 @@ ada_collect_symbol_completion_matches (completion_tracker &tracker,
 				       enum type_code code)
 {
   struct symbol *sym;
-  struct compunit_symtab *s;
   const struct block *b, *surrounding_static_block = 0;
   struct block_iterator iter;
 
@@ -13547,7 +13545,6 @@ ada_add_global_exceptions (compiled_regex *preg,
 			   std::vector<ada_exc_info> *exceptions)
 {
   struct objfile *objfile;
-  struct compunit_symtab *s;
 
   /* In Ada, the symbol "search name" is a linkage name, whereas the
      regular expression used to do the matching refers to the natural
diff --git a/gdb/coffread.c b/gdb/coffread.c
index 401aacbbe2..81d00b5248 100644
--- a/gdb/coffread.c
+++ b/gdb/coffread.c
@@ -1201,7 +1201,6 @@ coff_symtab_read (minimal_symbol_reader &reader,
   /* Patch up any opaque types (references to types that are not defined
      in the file where they are referenced, e.g. "struct foo *bar").  */
   {
-    struct compunit_symtab *cu;
     struct symtab *s;
 
     ALL_OBJFILE_FILETABS (objfile, cu, s)
diff --git a/gdb/cp-support.c b/gdb/cp-support.c
index 184d637da6..e976354f1f 100644
--- a/gdb/cp-support.c
+++ b/gdb/cp-support.c
@@ -1374,7 +1374,6 @@ static void
 add_symbol_overload_list_qualified (const char *func_name,
 				    std::vector<symbol *> *overload_list)
 {
-  struct compunit_symtab *cust;
   struct objfile *objfile;
   const struct block *b, *surrounding_static_block = 0;
 
diff --git a/gdb/linespec.c b/gdb/linespec.c
index 8e0fb866e8..b641c22a03 100644
--- a/gdb/linespec.c
+++ b/gdb/linespec.c
@@ -1138,8 +1138,6 @@ iterate_over_all_matching_symtabs
 
     for (struct objfile *objfile : all_objfiles (current_program_space))
       {
-	struct compunit_symtab *cu;
-
 	if (objfile->sf)
 	  objfile->sf->qf->expand_symtabs_matching (objfile,
 						    NULL,
@@ -1147,7 +1145,7 @@ iterate_over_all_matching_symtabs
 						    NULL, NULL,
 						    search_domain);
 
-	ALL_OBJFILE_COMPUNITS (objfile, cu)
+	for (struct compunit_symtab *cu : objfile_compunits (objfile))
 	  {
 	    struct symtab *symtab = COMPUNIT_FILETABS (cu);
 
diff --git a/gdb/maint.c b/gdb/maint.c
index 5a4aa83eaf..56fbf3cfba 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -763,7 +763,6 @@ count_symtabs_and_blocks (int *nr_symtabs_ptr, int *nr_compunit_symtabs_ptr,
 			  int *nr_blocks_ptr)
 {
   struct objfile *o;
-  struct compunit_symtab *cu;
   struct symtab *s;
   int nr_symtabs = 0;
   int nr_compunit_symtabs = 0;
diff --git a/gdb/mi/mi-cmd-file.c b/gdb/mi/mi-cmd-file.c
index 6e40e5d736..bbf3ed30ac 100644
--- a/gdb/mi/mi-cmd-file.c
+++ b/gdb/mi/mi-cmd-file.c
@@ -84,7 +84,6 @@ void
 mi_cmd_file_list_exec_source_files (const char *command, char **argv, int argc)
 {
   struct ui_out *uiout = current_uiout;
-  struct compunit_symtab *cu;
   struct symtab *s;
   struct objfile *objfile;
 
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index 19047694af..abfe425358 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -788,7 +788,6 @@ objfile_relocate1 (struct objfile *objfile,
 
   /* OK, get all the symtabs.  */
   {
-    struct compunit_symtab *cust;
     struct symtab *s;
 
     ALL_OBJFILE_FILETABS (objfile, cust, s)
@@ -806,41 +805,42 @@ objfile_relocate1 (struct objfile *objfile,
 	}
     }
 
-    ALL_OBJFILE_COMPUNITS (objfile, cust)
-    {
-      const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (cust);
-      int block_line_section = COMPUNIT_BLOCK_LINE_SECTION (cust);
-
-      if (BLOCKVECTOR_MAP (bv))
-	addrmap_relocate (BLOCKVECTOR_MAP (bv),
-			  ANOFFSET (delta, block_line_section));
-
-      for (int i = 0; i < BLOCKVECTOR_NBLOCKS (bv); ++i)
-	{
-	  struct block *b;
-	  struct symbol *sym;
-	  struct dict_iterator iter;
-
-	  b = BLOCKVECTOR_BLOCK (bv, i);
-	  BLOCK_START (b) += ANOFFSET (delta, block_line_section);
-	  BLOCK_END (b) += ANOFFSET (delta, block_line_section);
-
-	  if (BLOCK_RANGES (b) != nullptr)
-	    for (int j = 0; j < BLOCK_NRANGES (b); j++)
+    for (struct compunit_symtab *cust : objfile_compunits (objfile))
+      {
+	const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (cust);
+	int block_line_section = COMPUNIT_BLOCK_LINE_SECTION (cust);
+
+	if (BLOCKVECTOR_MAP (bv))
+	  addrmap_relocate (BLOCKVECTOR_MAP (bv),
+			    ANOFFSET (delta, block_line_section));
+
+	for (int i = 0; i < BLOCKVECTOR_NBLOCKS (bv); ++i)
+	  {
+	    struct block *b;
+	    struct symbol *sym;
+	    struct dict_iterator iter;
+
+	    b = BLOCKVECTOR_BLOCK (bv, i);
+	    BLOCK_START (b) += ANOFFSET (delta, block_line_section);
+	    BLOCK_END (b) += ANOFFSET (delta, block_line_section);
+
+	    if (BLOCK_RANGES (b) != nullptr)
+	      for (int j = 0; j < BLOCK_NRANGES (b); j++)
+		{
+		  BLOCK_RANGE_START (b, j)
+		    += ANOFFSET (delta, block_line_section);
+		  BLOCK_RANGE_END (b, j) += ANOFFSET (delta,
+						      block_line_section);
+		}
+
+	    /* We only want to iterate over the local symbols, not any
+	       symbols in included symtabs.  */
+	    ALL_DICT_SYMBOLS (BLOCK_DICT (b), iter, sym)
 	      {
-		BLOCK_RANGE_START (b, j)
-		  += ANOFFSET (delta, block_line_section);
-		BLOCK_RANGE_END (b, j) += ANOFFSET (delta, block_line_section);
+		relocate_one_symbol (sym, objfile, delta);
 	      }
-
-	  /* We only want to iterate over the local symbols, not any
-	     symbols in included symtabs.  */
-	  ALL_DICT_SYMBOLS (BLOCK_DICT (b), iter, sym)
-	    {
-	      relocate_one_symbol (sym, objfile, delta);
-	    }
-	}
-    }
+	  }
+      }
   }
 
   /* This stores relocated addresses and so must be cleared.  This
diff --git a/gdb/objfiles.h b/gdb/objfiles.h
index b24145f0ca..916d80f571 100644
--- a/gdb/objfiles.h
+++ b/gdb/objfiles.h
@@ -615,13 +615,21 @@ public:
 /* Traverse all symtabs in one objfile.  */
 
 #define ALL_OBJFILE_FILETABS(objfile, cu, s) \
-  ALL_OBJFILE_COMPUNITS (objfile, cu) \
+  for (struct compunit_symtab *cu : objfile_compunits (objfile)) \
     ALL_COMPUNIT_FILETABS (cu, s)
 
-/* Traverse all compunits in one objfile.  */
+/* A range adapter that makes it possible to iterate over all
+   compunits in one objfile.  */
+
+class objfile_compunits : public next_adapter<struct compunit_symtab>
+{
+public:
 
-#define ALL_OBJFILE_COMPUNITS(objfile, cu) \
-  for ((cu) = (objfile) -> compunit_symtabs; (cu) != NULL; (cu) = (cu) -> next)
+  explicit objfile_compunits (struct objfile *objfile)
+    : next_adapter<struct compunit_symtab> (objfile->compunit_symtabs)
+  {
+  }
+};
 
 /* A range adapter that makes it possible to iterate over all
    minimal symbols of an objfile.  */
@@ -714,7 +722,7 @@ private:
 
 #define ALL_COMPUNITS(objfile, cu)	\
   ALL_OBJFILES (objfile)		\
-    ALL_OBJFILE_COMPUNITS (objfile, cu)
+    for (struct compunit_symtab *cu : objfile_compunits (objfile))
 
 #define ALL_OBJFILE_OSECTIONS(objfile, osect)	\
   for (osect = objfile->sections; osect < objfile->sections_end; osect++) \
diff --git a/gdb/source.c b/gdb/source.c
index bef4ffe0ad..81be5f5f60 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -244,7 +244,6 @@ void
 select_source_symtab (struct symtab *s)
 {
   struct objfile *ofp;
-  struct compunit_symtab *cu;
 
   if (s)
     {
@@ -356,7 +355,6 @@ show_directories_command (struct ui_file *file, int from_tty,
 void
 forget_cached_source_info_for_objfile (struct objfile *objfile)
 {
-  struct compunit_symtab *cu;
   struct symtab *s;
 
   ALL_OBJFILE_FILETABS (objfile, cu, s)
diff --git a/gdb/symmisc.c b/gdb/symmisc.c
index ebad334d6c..60aef3c8bb 100644
--- a/gdb/symmisc.c
+++ b/gdb/symmisc.c
@@ -82,7 +82,6 @@ void
 print_objfile_statistics (void)
 {
   struct program_space *pspace;
-  struct compunit_symtab *cu;
   struct symtab *s;
   int i, linetables, blockvectors;
 
@@ -108,15 +107,15 @@ print_objfile_statistics (void)
 			 OBJSTAT (objfile, n_types));
       if (objfile->sf)
 	objfile->sf->qf->print_stats (objfile);
-      i = linetables = blockvectors = 0;
+      i = linetables = 0;
       ALL_OBJFILE_FILETABS (objfile, cu, s)
 	{
 	  i++;
 	  if (SYMTAB_LINETABLE (s) != NULL)
 	    linetables++;
 	}
-      ALL_OBJFILE_COMPUNITS (objfile, cu)
-	blockvectors++;
+      blockvectors = std::distance (objfile_compunits (objfile).begin (),
+				    objfile_compunits (objfile).end ());
       printf_filtered (_("  Number of symbol tables: %d\n"), i);
       printf_filtered (_("  Number of symbol tables with line tables: %d\n"),
 		       linetables);
@@ -145,7 +144,6 @@ print_objfile_statistics (void)
 static void
 dump_objfile (struct objfile *objfile)
 {
-  struct compunit_symtab *cust;
   struct symtab *symtab;
 
   printf_filtered ("\nObject file %s:  ", objfile_name (objfile));
@@ -469,7 +467,6 @@ maintenance_print_symbols (const char *args, int from_tty)
     }
   else
     {
-      struct compunit_symtab *cu;
       struct symtab *s;
       int found = 0;
 
@@ -775,14 +772,13 @@ maintenance_info_symtabs (const char *regexp, int from_tty)
   ALL_PSPACES (pspace)
     for (struct objfile *objfile : all_objfiles (pspace))
       {
-	struct compunit_symtab *cust;
 	struct symtab *symtab;
 
 	/* We don't want to print anything for this objfile until we
 	   actually find a symtab whose name matches.  */
 	int printed_objfile_start = 0;
 
-	ALL_OBJFILE_COMPUNITS (objfile, cust)
+	for (struct compunit_symtab *cust : objfile_compunits (objfile))
 	  {
 	    int printed_compunit_symtab_start = 0;
 
@@ -863,13 +859,11 @@ maintenance_check_symtabs (const char *ignore, int from_tty)
   ALL_PSPACES (pspace)
     for (struct objfile *objfile : all_objfiles (pspace))
       {
-	struct compunit_symtab *cust;
-
 	/* We don't want to print anything for this objfile until we
 	   actually find something worth printing.  */
 	int printed_objfile_start = 0;
 
-	ALL_OBJFILE_COMPUNITS (objfile, cust)
+	for (struct compunit_symtab *cust : objfile_compunits (objfile))
 	  {
 	    int found_something = 0;
 	    struct symtab *symtab = compunit_primary_filetab (cust);
@@ -1032,10 +1026,9 @@ maintenance_info_line_tables (const char *regexp, int from_tty)
   ALL_PSPACES (pspace)
     for (struct objfile *objfile : all_objfiles (pspace))
       {
-	struct compunit_symtab *cust;
 	struct symtab *symtab;
 
-	ALL_OBJFILE_COMPUNITS (objfile, cust)
+	for (struct compunit_symtab *cust : objfile_compunits (objfile))
 	  {
 	    ALL_COMPUNIT_FILETABS (cust, symtab)
 	      {
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 73ab0cb0b7..49dd30235c 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -2167,7 +2167,6 @@ struct objfile *
 lookup_objfile_from_block (const struct block *block)
 {
   struct objfile *obj;
-  struct compunit_symtab *cust;
 
   if (block == NULL)
     return NULL;
@@ -2255,8 +2254,6 @@ static struct block_symbol
 lookup_symbol_in_objfile_symtabs (struct objfile *objfile, int block_index,
 				  const char *name, const domain_enum domain)
 {
-  struct compunit_symtab *cust;
-
   gdb_assert (block_index == GLOBAL_BLOCK || block_index == STATIC_BLOCK);
 
   if (symbol_lookup_debug > 1)
@@ -2269,7 +2266,7 @@ lookup_symbol_in_objfile_symtabs (struct objfile *objfile, int block_index,
 			  name, domain_name (domain));
     }
 
-  ALL_OBJFILE_COMPUNITS (objfile, cust)
+  for (struct compunit_symtab *cust : objfile_compunits (objfile))
     {
       const struct blockvector *bv;
       const struct block *block;
@@ -2763,12 +2760,11 @@ static struct type *
 basic_lookup_transparent_type_1 (struct objfile *objfile, int block_index,
 				 const char *name)
 {
-  const struct compunit_symtab *cust;
   const struct blockvector *bv;
   const struct block *block;
   const struct symbol *sym;
 
-  ALL_OBJFILE_COMPUNITS (objfile, cust)
+  for (struct compunit_symtab *cust : objfile_compunits (objfile))
     {
       bv = COMPUNIT_BLOCKVECTOR (cust);
       block = BLOCKVECTOR_BLOCK (bv, block_index);
@@ -2874,7 +2870,6 @@ iterate_over_symbols (const struct block *block,
 struct compunit_symtab *
 find_pc_sect_compunit_symtab (CORE_ADDR pc, struct obj_section *section)
 {
-  struct compunit_symtab *cust;
   struct compunit_symtab *best_cust = NULL;
   struct objfile *objfile;
   CORE_ADDR distance = 0;
@@ -3349,7 +3344,6 @@ find_line_symtab (struct symtab *symtab, int line,
          BEST_INDEX and BEST_LINETABLE identify the item for it.  */
       int best;
 
-      struct compunit_symtab *cu;
       struct symtab *s;
 
       if (best_index >= 0)
@@ -4185,7 +4179,6 @@ output_partial_symbol_filename (const char *filename, const char *fullname,
 static void
 info_sources_command (const char *ignore, int from_tty)
 {
-  struct compunit_symtab *cu;
   struct symtab *s;
   struct objfile *objfile;
   struct output_source_filename_data data;
@@ -4338,7 +4331,6 @@ search_symbols (const char *regexp, enum search_domain kind,
 		const char *t_regexp,
 		int nfiles, const char *files[])
 {
-  struct compunit_symtab *cust;
   const struct blockvector *bv;
   struct block *b;
   int i = 0;
@@ -5196,7 +5188,6 @@ default_collect_symbol_completion_matches_break_on
      won't be that many.  */
 
   struct symbol *sym;
-  struct compunit_symtab *cust;
   const struct block *b;
   const struct block *surrounding_static_block, *surrounding_global_block;
   struct block_iterator iter;
@@ -5580,7 +5571,6 @@ maybe_add_partial_symtab_filename (const char *filename, const char *fullname,
 completion_list
 make_source_files_completion_list (const char *text, const char *word)
 {
-  struct compunit_symtab *cu;
   struct symtab *s;
   struct objfile *objfile;
   size_t text_len = strlen (text);
-- 
2.17.2

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH 00/12] Remove some ALL_* iteration macros
  2018-11-25 16:54 [PATCH 00/12] Remove some ALL_* iteration macros Tom Tromey
                   ` (11 preceding siblings ...)
  2018-11-25 16:55 ` [PATCH 06/12] Remove ALL_OBJFILE_COMPUNITS Tom Tromey
@ 2018-12-23  7:00 ` Joel Brobecker
  2018-12-24 20:54   ` Tom Tromey
  2018-12-27  1:52 ` Simon Marchi
  2019-01-10 16:44 ` Pedro Alves
  14 siblings, 1 reply; 36+ messages in thread
From: Joel Brobecker @ 2018-12-23  7:00 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

> This series removes various ALL_* iteration macros, in favor of C++
> iterators, range adapters, and ranged for loops.  I've been wanting
> this for a while, because it helps a little bit with various
> experiments of mine that involve changing objfile lifetime management;
> Pedro's thread iterator patch prompted me to finally do this.
> 
> The main downside of removing these macros is that it involves some
> reindentation; and expanding some macros to two nested loops means a
> couple somewhat ugly reformattings.
> 
> On the plus side, though, this tightens the scope of iteration
> variables, which is good.  And, it removes some hairy code,
> particularly the ALL_OBJSECTIONS patch.
> 
> There are still a few more such macros that could be converted.  And,
> I think inf_threads_iterator could be converted to use next_iterator.
> I can do some of this if there's interest.
> 
> Regression tested by the buildbot.

FWIW, this seems like a good change to me; I am particularly
receptive to the fact that the scope of the iteration variables
is now restricted to the loop itself.

As for the downside, I don't consider re-indentation a downside
other than it makes review a bit more painful. The two-nested
loop didn't seem like they were making the code less readable.
Did you have any other concerns with that?

I admit I was scanning the last few patches much faster than the first
few ones, but they seem fairly mechanical to me, so I think the risk
is low.

-- 
Joel

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH 00/12] Remove some ALL_* iteration macros
  2018-12-23  7:00 ` [PATCH 00/12] Remove some ALL_* iteration macros Joel Brobecker
@ 2018-12-24 20:54   ` Tom Tromey
  2018-12-26 17:30     ` Simon Marchi
  0 siblings, 1 reply; 36+ messages in thread
From: Tom Tromey @ 2018-12-24 20:54 UTC (permalink / raw)
  To: Joel Brobecker; +Cc: Tom Tromey, gdb-patches

>>>>> "Joel" == Joel Brobecker <brobecker@adacore.com> writes:

Joel> As for the downside, I don't consider re-indentation a downside
Joel> other than it makes review a bit more painful. The two-nested
Joel> loop didn't seem like they were making the code less readable.
Joel> Did you have any other concerns with that?

Nope.

Joel> I admit I was scanning the last few patches much faster than the first
Joel> few ones, but they seem fairly mechanical to me, so I think the risk
Joel> is low.

I think I will have to redo much of the series to account for that
version of gcc that Simon sometimes uses -- the one that requires ranged
for loops to use the "struct" keyword.  I probably can't really test
that change but I'll make a best effort at it in order to reduce the
amount of work he has to do.

Tom

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH 00/12] Remove some ALL_* iteration macros
  2018-12-24 20:54   ` Tom Tromey
@ 2018-12-26 17:30     ` Simon Marchi
  2018-12-26 22:28       ` Tom Tromey
  0 siblings, 1 reply; 36+ messages in thread
From: Simon Marchi @ 2018-12-26 17:30 UTC (permalink / raw)
  To: Tom Tromey, Joel Brobecker; +Cc: gdb-patches

On 2018-12-24 3:54 p.m., Tom Tromey wrote:
>>>>>> "Joel" == Joel Brobecker <brobecker@adacore.com> writes:
> 
> Joel> As for the downside, I don't consider re-indentation a downside
> Joel> other than it makes review a bit more painful. The two-nested
> Joel> loop didn't seem like they were making the code less readable.
> Joel> Did you have any other concerns with that?
> 
> Nope.
> 
> Joel> I admit I was scanning the last few patches much faster than the first
> Joel> few ones, but they seem fairly mechanical to me, so I think the risk
> Joel> is low.
> 
> I think I will have to redo much of the series to account for that
> version of gcc that Simon sometimes uses -- the one that requires ranged
> for loops to use the "struct" keyword.  I probably can't really test
> that change but I'll make a best effort at it in order to reduce the
> amount of work he has to do.

Hehe.  Looking back at those patches, it's with gcc 6.3.0.  Those are cross-compilers
I built with crosstool-ng 1.22:

$ ~/x-tools/aarch64-rpi3-linux-gnueabi/bin/aarch64-rpi3-linux-gnueabi-gcc --version
aarch64-rpi3-linux-gnueabi-gcc (crosstool-NG crosstool-ng-1.22.0-677-ga3dd55b9) 6.3.0

I see crosstool-ng 1.23 has been released, but it still uses gcc 6.3.0.  And it looks like
the gcc version used in current Debian stable (codename stretch) is also 6.3.0:

  https://packages.debian.org/stretch/gcc

I'll give a try to this patchset (and take a quick look at the same time).

Simon

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH 00/12] Remove some ALL_* iteration macros
  2018-12-26 17:30     ` Simon Marchi
@ 2018-12-26 22:28       ` Tom Tromey
  2018-12-26 22:32         ` Tom Tromey
  0 siblings, 1 reply; 36+ messages in thread
From: Tom Tromey @ 2018-12-26 22:28 UTC (permalink / raw)
  To: Simon Marchi; +Cc: Tom Tromey, Joel Brobecker, gdb-patches

>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:

Simon> I'll give a try to this patchset (and take a quick look at the
Simon> same time).

It's on submit/remove-loop-macros on my github.
I rebased it today.

Tom

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH 00/12] Remove some ALL_* iteration macros
  2018-12-26 22:28       ` Tom Tromey
@ 2018-12-26 22:32         ` Tom Tromey
  2018-12-26 22:35           ` Simon Marchi
  0 siblings, 1 reply; 36+ messages in thread
From: Tom Tromey @ 2018-12-26 22:32 UTC (permalink / raw)
  To: Tom Tromey; +Cc: Simon Marchi, Joel Brobecker, gdb-patches

>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:

>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:
Simon> I'll give a try to this patchset (and take a quick look at the
Simon> same time).

Tom> It's on submit/remove-loop-macros on my github.
Tom> I rebased it today.

Apparently I thought ahead and used the struct tags when I wrote the
patch.  Funny that I don't remember that.

I'll push it in some day soon.

Tom

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH 00/12] Remove some ALL_* iteration macros
  2018-12-26 22:32         ` Tom Tromey
@ 2018-12-26 22:35           ` Simon Marchi
  2018-12-26 22:52             ` Tom Tromey
  0 siblings, 1 reply; 36+ messages in thread
From: Simon Marchi @ 2018-12-26 22:35 UTC (permalink / raw)
  To: Tom Tromey; +Cc: Joel Brobecker, gdb-patches

On 2018-12-26 5:32 p.m., Tom Tromey wrote:
>>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:
> 
>>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:
> Simon> I'll give a try to this patchset (and take a quick look at the
> Simon> same time).
> 
> Tom> It's on submit/remove-loop-macros on my github.
> Tom> I rebased it today.
> 
> Apparently I thought ahead and used the struct tags when I wrote the
> patch.  Funny that I don't remember that.
> 
> I'll push it in some day soon.
> 
> Tom
> 

Usually, the problem I had was the other way around, that having the struct tag
causes a compilation problem:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=commit;h=8634679f82df75cf482b0c0814c2b3865da91d22

The recent Hurd problem, however, happened when not using the struct tag
with thread_info...

Simon

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH 00/12] Remove some ALL_* iteration macros
  2018-12-26 22:35           ` Simon Marchi
@ 2018-12-26 22:52             ` Tom Tromey
  2018-12-26 23:45               ` Tom Tromey
  0 siblings, 1 reply; 36+ messages in thread
From: Tom Tromey @ 2018-12-26 22:52 UTC (permalink / raw)
  To: Simon Marchi; +Cc: Tom Tromey, Joel Brobecker, gdb-patches

>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:

Simon> Usually, the problem I had was the other way around, that having the struct tag
Simon> causes a compilation problem:

Aha, that's what I misremembered.
Ok, I can fix that.

Tom

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH 00/12] Remove some ALL_* iteration macros
  2018-12-26 22:52             ` Tom Tromey
@ 2018-12-26 23:45               ` Tom Tromey
  2018-12-27  0:46                 ` Simon Marchi
  0 siblings, 1 reply; 36+ messages in thread
From: Tom Tromey @ 2018-12-26 23:45 UTC (permalink / raw)
  To: Tom Tromey; +Cc: Simon Marchi, Joel Brobecker, gdb-patches

>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:

>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:
Simon> Usually, the problem I had was the other way around, that having the struct tag
Simon> causes a compilation problem:

Tom> Aha, that's what I misremembered.
Tom> Ok, I can fix that.

I did it.  A few spots needed an additional tweak to avoid a clash
between a variable ("objfile" or "symtab") and the type.
I've force-pushed it to that same branch, in case you feel like giving
it a try.  I'll re-send the series sometime.

Tom

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH 00/12] Remove some ALL_* iteration macros
  2018-12-26 23:45               ` Tom Tromey
@ 2018-12-27  0:46                 ` Simon Marchi
  2018-12-27  6:22                   ` Tom Tromey
  0 siblings, 1 reply; 36+ messages in thread
From: Simon Marchi @ 2018-12-27  0:46 UTC (permalink / raw)
  To: Tom Tromey; +Cc: Joel Brobecker, gdb-patches

On 2018-12-26 18:45, Tom Tromey wrote:
>>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:
> 
>>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:
> Simon> Usually, the problem I had was the other way around, that
> having the struct tag
> Simon> causes a compilation problem:
> 
> Tom> Aha, that's what I misremembered.
> Tom> Ok, I can fix that.
> 
> I did it.  A few spots needed an additional tweak to avoid a clash
> between a variable ("objfile" or "symtab") and the type.
> I've force-pushed it to that same branch, in case you feel like giving
> it a try.  I'll re-send the series sometime.

When I played with it, the compiler did not complain when I used:

   for (objfile *objfile : ...)

That was with gcc 8.2.0.  Do you know if it's really problematic with 
other compilers/versions?

Simon

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH 00/12] Remove some ALL_* iteration macros
  2018-11-25 16:54 [PATCH 00/12] Remove some ALL_* iteration macros Tom Tromey
                   ` (12 preceding siblings ...)
  2018-12-23  7:00 ` [PATCH 00/12] Remove some ALL_* iteration macros Joel Brobecker
@ 2018-12-27  1:52 ` Simon Marchi
  2019-01-03 21:46   ` Tom Tromey
  2019-01-10 16:44 ` Pedro Alves
  14 siblings, 1 reply; 36+ messages in thread
From: Simon Marchi @ 2018-12-27  1:52 UTC (permalink / raw)
  To: Tom Tromey, gdb-patches

On 2018-11-25 11:54 a.m., Tom Tromey wrote:
> This series removes various ALL_* iteration macros, in favor of C++
> iterators, range adapters, and ranged for loops.  I've been wanting
> this for a while, because it helps a little bit with various
> experiments of mine that involve changing objfile lifetime management;
> Pedro's thread iterator patch prompted me to finally do this.
> 
> The main downside of removing these macros is that it involves some
> reindentation; and expanding some macros to two nested loops means a
> couple somewhat ugly reformattings.
> 
> On the plus side, though, this tightens the scope of iteration
> variables, which is good.  And, it removes some hairy code,
> particularly the ALL_OBJSECTIONS patch.
> 
> There are still a few more such macros that could be converted.  And,
> I think inf_threads_iterator could be converted to use next_iterator.
> I can do some of this if there's interest.
> 
> Regression tested by the buildbot.
> 
> Tom

I gave that a quick look.

I was wondering if you had thought about replacing, for example

  ALL_COMPUNITS (objfile, s)

with an equivalent like this

  for (compunit_symtab *s : all_compunits (current_program_space))

in order to avoid nested loops like

  for (objfile *objfile : all_objfiles (current_program_space))
    for (compunit_symtab *s : objfile_compunits (objfile))
      {
        ...
        ...
      }

In most cases, the objfile variable is not needed for anything else than
iterating on the compunit_symtabs.  For cases where the outer iteration
variable is needed, then we can use the nested loops.

I am not sure which one I like best.  The flat version reduces indentation, but
the nested version makes it clear and explicit how the data is represented, so
it might help readers who are less familiar with the code.

Also, in theory, according to the coding style, we should write

for (...)
  {
    for (...)
      {
        ...
        ...
      }
  }

Simon

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH 00/12] Remove some ALL_* iteration macros
  2018-12-27  0:46                 ` Simon Marchi
@ 2018-12-27  6:22                   ` Tom Tromey
  0 siblings, 0 replies; 36+ messages in thread
From: Tom Tromey @ 2018-12-27  6:22 UTC (permalink / raw)
  To: Simon Marchi; +Cc: Tom Tromey, Joel Brobecker, gdb-patches

>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:

Simon> When I played with it, the compiler did not complain when I used:
Simon>   for (objfile *objfile : ...)
Simon> That was with gcc 8.2.0.  Do you know if it's really problematic with
Simon> other compilers/versions?

That case is fine, but the problem is a case like:

for (objfile *objfile : ...)
  for (objfile *objfile2 : ...)

Here the compiler complains about the inner for, because 'objfile' is a
variable.

Another case was one where 'symtab' was the name of a function
parameter, causing problems with "for (symbtab *... : ...)"

Tom

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH 00/12] Remove some ALL_* iteration macros
  2018-12-27  1:52 ` Simon Marchi
@ 2019-01-03 21:46   ` Tom Tromey
  2019-01-03 22:45     ` Simon Marchi
  0 siblings, 1 reply; 36+ messages in thread
From: Tom Tromey @ 2019-01-03 21:46 UTC (permalink / raw)
  To: Simon Marchi; +Cc: Tom Tromey, gdb-patches

>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:

Simon> I was wondering if you had thought about replacing, for example
Simon>   ALL_COMPUNITS (objfile, s)
Simon> with an equivalent like this
Simon>   for (compunit_symtab *s : all_compunits (current_program_space))
Simon> in order to avoid nested loops like
[...]

Yeah, I don't think I really considered it.

Simon> I am not sure which one I like best.  The flat version reduces indentation, but
Simon> the nested version makes it clear and explicit how the data is represented, so
Simon> it might help readers who are less familiar with the code.

Same for me.  Maybe I lean a bit toward the explicit form but that might
only be because I already have the patch in hand.

Simon> Also, in theory, according to the coding style, we should write
Simon> for (...)
Simon>   {
Simon>     for (...)
Simon>       {
Simon>         ...
Simon>         ...
Simon>       }
Simon>   }

I thought it was ok to leave a single statement unbraced, though I
personally never do this for something like:

   for (...)
     if ...
     else...

...since I think that's less readable than the braced version.

If the braces are needed then that probably argues for a smarter
iterator, to avoid excessive indentation.

Tom

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH 00/12] Remove some ALL_* iteration macros
  2019-01-03 21:46   ` Tom Tromey
@ 2019-01-03 22:45     ` Simon Marchi
  2019-01-06 20:10       ` Tom Tromey
  0 siblings, 1 reply; 36+ messages in thread
From: Simon Marchi @ 2019-01-03 22:45 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

On 2019-01-03 16:45, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:
> 
> Simon> I was wondering if you had thought about replacing, for example
> Simon>   ALL_COMPUNITS (objfile, s)
> Simon> with an equivalent like this
> Simon>   for (compunit_symtab *s : all_compunits 
> (current_program_space))
> Simon> in order to avoid nested loops like
> [...]
> 
> Yeah, I don't think I really considered it.
> 
> Simon> I am not sure which one I like best.  The flat version reduces
> indentation, but
> Simon> the nested version makes it clear and explicit how the data is
> represented, so
> Simon> it might help readers who are less familiar with the code.
> 
> Same for me.  Maybe I lean a bit toward the explicit form but that 
> might
> only be because I already have the patch in hand.
> 
> Simon> Also, in theory, according to the coding style, we should write
> Simon> for (...)
> Simon>   {
> Simon>     for (...)
> Simon>       {
> Simon>         ...
> Simon>         ...
> Simon>       }
> Simon>   }
> 
> I thought it was ok to leave a single statement unbraced, though I
> personally never do this for something like:
> 
>    for (...)
>      if ...
>      else...
> 
> ...since I think that's less readable than the braced version.
> 
> If the braces are needed then that probably argues for a smarter
> iterator, to avoid excessive indentation.

They are needed if we want to strictly follow the GNU coding style:

https://www.gnu.org/prep/standards/standards.html#Clean-Use-of-C-Constructs

I think what you did is easy to read, since it's pretty straightforward. 
  We could always make an exception for these constructs, but it would 
probably end up being confusing to understand and explain when you can 
omit the braces and when you can't.

If we end up using "smarter iterators" (for the lack of a better name) 
they could be overloads:

/* Iterate on all compunits of an objfile.  */
... all_compunits (objfile *);
/ Iterate on all compunits of a program space.  */
... all_compunits (program_space *);

And let's say that all_compunits (program_space *) returns tuples of 
<objfile *, compunit_symtab *>, we'll be able to use structured bindings 
when we switch to C++17 :).  Something like:

for (const auto &[objfile, compunit] : all_compunits (pspace))

Simon

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH 00/12] Remove some ALL_* iteration macros
  2019-01-03 22:45     ` Simon Marchi
@ 2019-01-06 20:10       ` Tom Tromey
  2019-01-09 19:49         ` Simon Marchi
  2019-01-10 16:45         ` Pedro Alves
  0 siblings, 2 replies; 36+ messages in thread
From: Tom Tromey @ 2019-01-06 20:10 UTC (permalink / raw)
  To: Simon Marchi; +Cc: Tom Tromey, gdb-patches

>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:

Simon> I think what you did is easy to read, since it's pretty
Simon> straightforward. We could always make an exception for these
Simon> constructs, but it would probably end up being confusing to understand
Simon> and explain when you can omit the braces and when you can't.

[...]

Simon> And let's say that all_compunits (program_space *) returns tuples of
Simon> <objfile *, compunit_symtab *>, we'll be able to use structured
Simon> bindings when we switch to C++17 :).  Something like:

Simon> for (const auto &[objfile, compunit] : all_compunits (pspace))

I gave this a brief try.  I wrote a "nested" iterator to make this
generic and then converted all_compunits.

With the nested iterator, one needs to write:

  for (auto blah : all_compunits ())
    {
      compunit_symtab *s = blah.second;

This looked ugly to me.


Alternatively, we'd have to write a "second-selecting" wrapper iterator.
I didn't implement it but I suppose it would look like:

  for (compunit_symtab *s : second_adapter<all_compunits> ())
     ...

This seemed a bit obscure to me, though.

In the end I think I prefer the explicit route.  It does need more
indentation (I will add the missing braces), but the explicitness seems
good.  To me, there doesn't seem to be a strong reason to prefer the
shorter formulations; and the new iterators and adapters that would be
needed are just a bunch more code to try to track through.

Let me know what you think about this.  If you like the second_adapter
approach, I can implement that.

Tom

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH 00/12] Remove some ALL_* iteration macros
  2019-01-06 20:10       ` Tom Tromey
@ 2019-01-09 19:49         ` Simon Marchi
  2019-01-10  1:29           ` Tom Tromey
  2019-01-10 16:45         ` Pedro Alves
  1 sibling, 1 reply; 36+ messages in thread
From: Simon Marchi @ 2019-01-09 19:49 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

On 2019-01-06 15:10, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:
> 
> Simon> I think what you did is easy to read, since it's pretty
> Simon> straightforward. We could always make an exception for these
> Simon> constructs, but it would probably end up being confusing to 
> understand
> Simon> and explain when you can omit the braces and when you can't.
> 
> [...]
> 
> Simon> And let's say that all_compunits (program_space *) returns 
> tuples of
> Simon> <objfile *, compunit_symtab *>, we'll be able to use structured
> Simon> bindings when we switch to C++17 :).  Something like:
> 
> Simon> for (const auto &[objfile, compunit] : all_compunits (pspace))
> 
> I gave this a brief try.  I wrote a "nested" iterator to make this
> generic and then converted all_compunits.
> 
> With the nested iterator, one needs to write:
> 
>   for (auto blah : all_compunits ())
>     {
>       compunit_symtab *s = blah.second;
> 
> This looked ugly to me.

In that case it would always return a specialized struct

   struct {
     objfile *objfile;
     compunit_symtab *compunit;
   };

> Alternatively, we'd have to write a "second-selecting" wrapper 
> iterator.
> I didn't implement it but I suppose it would look like:
> 
>   for (compunit_symtab *s : second_adapter<all_compunits> ())
>      ...
> 
> This seemed a bit obscure to me, though.
> 
> In the end I think I prefer the explicit route.  It does need more
> indentation (I will add the missing braces), but the explicitness seems
> good.  To me, there doesn't seem to be a strong reason to prefer the
> shorter formulations; and the new iterators and adapters that would be
> needed are just a bunch more code to try to track through.
> 
> Let me know what you think about this.  If you like the second_adapter
> approach, I can implement that.

Yeah, I think the second_adapter approach just makes things more 
obscure.

Really, I am fine with what you did.

Simon

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH 00/12] Remove some ALL_* iteration macros
  2019-01-09 19:49         ` Simon Marchi
@ 2019-01-10  1:29           ` Tom Tromey
  0 siblings, 0 replies; 36+ messages in thread
From: Tom Tromey @ 2019-01-10  1:29 UTC (permalink / raw)
  To: Simon Marchi; +Cc: Tom Tromey, gdb-patches

>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:

Simon> Yeah, I think the second_adapter approach just makes things more
Simon> obscure.

Simon> Really, I am fine with what you did.

Ok.  I added the missing braces and re-tested it on the buildbot
("Fedora-x86_64-m64"); and I am going to push it now.

Tom

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH 00/12] Remove some ALL_* iteration macros
  2018-11-25 16:54 [PATCH 00/12] Remove some ALL_* iteration macros Tom Tromey
                   ` (13 preceding siblings ...)
  2018-12-27  1:52 ` Simon Marchi
@ 2019-01-10 16:44 ` Pedro Alves
  2019-01-10 18:06   ` Tom Tromey
  14 siblings, 1 reply; 36+ messages in thread
From: Pedro Alves @ 2019-01-10 16:44 UTC (permalink / raw)
  To: Tom Tromey, gdb-patches

Hi Tom,

On 11/25/2018 04:54 PM, Tom Tromey wrote:
> This series removes various ALL_* iteration macros, in favor of C++
> iterators, range adapters, and ranged for loops.  I've been wanting
> this for a while, because it helps a little bit with various
> experiments of mine that involve changing objfile lifetime management;
> Pedro's thread iterator patch prompted me to finally do this.

I'm only now catching up with this.  Thanks so much for doing it!

> There are still a few more such macros that could be converted.  And,
> I think inf_threads_iterator could be converted to use next_iterator.
> I can do some of this if there's interest.

If it's a natural fit, then I think it'd be nice.  Is there a downside?

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH 00/12] Remove some ALL_* iteration macros
  2019-01-06 20:10       ` Tom Tromey
  2019-01-09 19:49         ` Simon Marchi
@ 2019-01-10 16:45         ` Pedro Alves
  2019-01-10 18:10           ` Tom Tromey
  1 sibling, 1 reply; 36+ messages in thread
From: Pedro Alves @ 2019-01-10 16:45 UTC (permalink / raw)
  To: Tom Tromey, Simon Marchi; +Cc: gdb-patches

On 01/06/2019 08:10 PM, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:
> 
> Simon> I think what you did is easy to read, since it's pretty
> Simon> straightforward. We could always make an exception for these
> Simon> constructs, but it would probably end up being confusing to understand
> Simon> and explain when you can omit the braces and when you can't.
> 
> [...]
> 
> Simon> And let's say that all_compunits (program_space *) returns tuples of
> Simon> <objfile *, compunit_symtab *>, we'll be able to use structured
> Simon> bindings when we switch to C++17 :).  Something like:
> 
> Simon> for (const auto &[objfile, compunit] : all_compunits (pspace))
> 
> I gave this a brief try.  I wrote a "nested" iterator to make this
> generic and then converted all_compunits.
> 
> With the nested iterator, one needs to write:
> 
>   for (auto blah : all_compunits ())
>     {
>       compunit_symtab *s = blah.second;
> 
> This looked ugly to me.

Yeah.  Though, I think that if we took this route, then 

  all_compunits ()

could still return just a compunit pointer, not two things:

  compunit_symtab *all_compunits ()

because you can always get the objfile pointer from compunit_symtab::objfile.

> 
> 
> Alternatively, we'd have to write a "second-selecting" wrapper iterator.
> I didn't implement it but I suppose it would look like:
> 
>   for (compunit_symtab *s : second_adapter<all_compunits> ())
>      ...
> 
> This seemed a bit obscure to me, though.

Definitely.

> 
> In the end I think I prefer the explicit route.  It does need more
> indentation (I will add the missing braces), but the explicitness seems
> good.  To me, there doesn't seem to be a strong reason to prefer the
> shorter formulations; and the new iterators and adapters that would be
> needed are just a bunch more code to try to track through.
> 
> Let me know what you think about this.  If you like the second_adapter
> approach, I can implement that.

Me, I don't.


BTW, in

> +/* A range adapter that makes it possible to iterate over all
> +   compunits in one objfile.  */
> +
> +class objfile_compunits : public next_adapter<struct compunit_symtab>
> +{
> +public:
>

in the threads/inferiors iterators I named the range types xxx_range
and then added methods to return the range instead of calling
the ctor directly.  In this case, it'd be the equivalent of naming
the class above

  class objfile_compunits_range : public next_adapter<struct compunit_symtab>
  {

and then adding a method to objfile::compunits() method like:

  objfile_compunits_range compunits ()
  { return objfile_compunits_range (this); }

Then the client code would look like:

> -	ALL_OBJFILE_COMPUNITS (objfile, cust)
> +	for (struct compunit_symtab *cust : objfile->compunits ())

Instead of:

 > -	ALL_OBJFILE_COMPUNITS (objfile, cust)
 > +	for (struct compunit_symtab *cust : objfile_compunits (objfile))

At the time, I thought that read better on the client side.  I.e.,
we have:

  inf->threads ()

instead of:

  inf_threads (inf)

OOC, did you consider following that, and decided again?
No need to redo the series or anything, I'm just curious,
since I would have taken a different choice.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH 00/12] Remove some ALL_* iteration macros
  2019-01-10 16:44 ` Pedro Alves
@ 2019-01-10 18:06   ` Tom Tromey
  2019-01-10 18:09     ` Pedro Alves
  0 siblings, 1 reply; 36+ messages in thread
From: Tom Tromey @ 2019-01-10 18:06 UTC (permalink / raw)
  To: Pedro Alves; +Cc: Tom Tromey, gdb-patches

>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

>> There are still a few more such macros that could be converted.  And,
>> I think inf_threads_iterator could be converted to use next_iterator.
>> I can do some of this if there's interest.

Pedro> If it's a natural fit, then I think it'd be nice.  Is there a downside?

I don't think there is one.
What do you think of this?

Tom

commit f4cd9886913b5216ed6073af23d250401f006551
Author: Tom Tromey <tom@tromey.com>
Date:   Thu Jan 10 10:50:43 2019 -0700

    Replace inf_threads_iterator with next_iterator
    
    This changes inf_threads_iterator and some range adapters in
    thread-iter.h to use next_iterator and next_adapter instead.
    
    gdb/ChangeLog
    2019-01-10  Tom Tromey  <tom@tromey.com>
    
            * thread-iter.h (inf_threads_iterator): Use next_iterator.
            (basic_inf_threads_range): Remove.
            (inf_threads_range, inf_non_exited_threads_range)
            (safe_inf_threads_range): Use next_adapter.

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 32fe0bbe814..ac1dd540ef7 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,10 @@
+2019-01-10  Tom Tromey  <tom@tromey.com>
+
+	* thread-iter.h (inf_threads_iterator): Use next_iterator.
+	(basic_inf_threads_range): Remove.
+	(inf_threads_range, inf_non_exited_threads_range)
+	(safe_inf_threads_range): Use next_adapter.
+
 2019-01-10  Tom Tromey  <tom@tromey.com>
 
 	* objfiles.h (objfile::reset_psymtabs): Update.
diff --git a/gdb/thread-iter.h b/gdb/thread-iter.h
index a1145d4938a..be6ab73c686 100644
--- a/gdb/thread-iter.h
+++ b/gdb/thread-iter.h
@@ -20,68 +20,13 @@
 #define THREAD_ITER_H
 
 #include "common/filtered-iterator.h"
+#include "common/next-iterator.h"
 #include "common/safe-iterator.h"
 
 /* A forward iterator that iterates over a given inferior's
    threads.  */
 
-class inf_threads_iterator
-{
-public:
-  typedef inf_threads_iterator self_type;
-  typedef struct thread_info *value_type;
-  typedef struct thread_info *&reference;
-  typedef struct thread_info **pointer;
-  typedef std::forward_iterator_tag iterator_category;
-  typedef int difference_type;
-
-  /* Create an iterator pointing at HEAD.  This takes a thread pointer
-     instead of an inferior pointer to avoid circular dependencies
-     between the thread and inferior header files.  */
-  explicit inf_threads_iterator (struct thread_info *head)
-    : m_thr (head)
-  {}
-
-  /* Create a one-past-end iterator.  */
-  inf_threads_iterator ()
-    : m_thr (nullptr)
-  {}
-
-  inf_threads_iterator& operator++ ()
-  {
-    m_thr = m_thr->next;
-    return *this;
-  }
-
-  thread_info *operator* () const { return m_thr; }
-
-  bool operator!= (const inf_threads_iterator &other) const
-  { return m_thr != other.m_thr; }
-
-private:
-  /* The currently-iterated thread.  NULL if we reached the end of the
-     list.  */
-  thread_info *m_thr;
-};
-
-/* A range adapter that makes it possible to iterate over an
-   inferior's thread list with range-for.  */
-template<typename Iterator>
-struct basic_inf_threads_range
-{
-  friend struct inferior;
-private:
-  explicit basic_inf_threads_range (struct thread_info *head)
-    : m_head (head)
-  {}
-
-public:
-  Iterator begin () const { return Iterator (m_head); }
-  Iterator end () const { return Iterator (); }
-
-private:
-  thread_info *m_head;
-};
+using inf_threads_iterator = next_iterator<thread_info>;
 
 /* A forward iterator that iterates over all threads of all
    inferiors.  */
@@ -223,19 +168,19 @@ using safe_inf_threads_iterator
    of an inferior with range-for.  */
 
 using inf_threads_range
-  = basic_inf_threads_range<inf_threads_iterator>;
+  = next_adapter<thread_info, inf_threads_iterator>;
 
 /* A range adapter that makes it possible to iterate over all
    non-exited threads of an inferior with range-for.  */
 
 using inf_non_exited_threads_range
-  = basic_inf_threads_range<inf_non_exited_threads_iterator>;
+  = next_adapter<thread_info, inf_non_exited_threads_iterator>;
 
 /* A range adapter that makes it possible to iterate over all threads
    of an inferior with range-for, safely.  */
 
 using safe_inf_threads_range
-  = basic_inf_threads_range<safe_inf_threads_iterator>;
+  = next_adapter<thread_info, safe_inf_threads_iterator>;
 
 /* A range adapter that makes it possible to iterate over all threads
    of all inferiors with range-for.  */

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH 00/12] Remove some ALL_* iteration macros
  2019-01-10 18:06   ` Tom Tromey
@ 2019-01-10 18:09     ` Pedro Alves
  2019-01-10 22:53       ` Tom Tromey
  0 siblings, 1 reply; 36+ messages in thread
From: Pedro Alves @ 2019-01-10 18:09 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

On 01/10/2019 06:06 PM, Tom Tromey wrote:
>>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:
> 
>>> There are still a few more such macros that could be converted.  And,
>>> I think inf_threads_iterator could be converted to use next_iterator.
>>> I can do some of this if there's interest.
> 
> Pedro> If it's a natural fit, then I think it'd be nice.  Is there a downside?
> 
> I don't think there is one.
> What do you think of this?

Looks good, thanks!

Pedro Alves

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH 00/12] Remove some ALL_* iteration macros
  2019-01-10 16:45         ` Pedro Alves
@ 2019-01-10 18:10           ` Tom Tromey
  2019-01-10 19:58             ` Pedro Alves
  0 siblings, 1 reply; 36+ messages in thread
From: Tom Tromey @ 2019-01-10 18:10 UTC (permalink / raw)
  To: Pedro Alves; +Cc: Tom Tromey, Simon Marchi, gdb-patches

>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

Pedro> Yeah.  Though, I think that if we took this route, then 
Pedro>   all_compunits ()
Pedro> could still return just a compunit pointer, not two things:
Pedro>   compunit_symtab *all_compunits ()
Pedro> because you can always get the objfile pointer from compunit_symtab::objfile.

In the long run I still hope to get rid of compunit_symtab::objfile.
Though I suppose this could just be added to the future to-do list.

[...]
Pedro> in the threads/inferiors iterators I named the range types xxx_range
Pedro> and then added methods to return the range instead of calling
Pedro> the ctor directly.  In this case, it'd be the equivalent of naming
Pedro> the class above

Pedro>   class objfile_compunits_range : public next_adapter<struct compunit_symtab>
Pedro>   {

Pedro> and then adding a method to objfile::compunits() method like:

Pedro>   objfile_compunits_range compunits ()
Pedro>   { return objfile_compunits_range (this); }

[...]

Pedro> OOC, did you consider following that, and decided again?
Pedro> No need to redo the series or anything, I'm just curious,
Pedro> since I would have taken a different choice.

I didn't even think of it.  I will add this to my to-do list, it does
look better.

Also maybe I next_adapter should be renamed to something better, at
least having the word "range" in it?

Tom

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH 00/12] Remove some ALL_* iteration macros
  2019-01-10 18:10           ` Tom Tromey
@ 2019-01-10 19:58             ` Pedro Alves
  0 siblings, 0 replies; 36+ messages in thread
From: Pedro Alves @ 2019-01-10 19:58 UTC (permalink / raw)
  To: Tom Tromey; +Cc: Simon Marchi, gdb-patches

On 01/10/2019 06:10 PM, Tom Tromey wrote:

> Pedro> OOC, did you consider following that, and decided again?
> Pedro> No need to redo the series or anything, I'm just curious,
> Pedro> since I would have taken a different choice.
> 
> I didn't even think of it.  I will add this to my to-do list, it does
> look better.
> 
> Also maybe I next_adapter should be renamed to something better, at
> least having the word "range" in it?
> 

Yeah.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH 00/12] Remove some ALL_* iteration macros
  2019-01-10 18:09     ` Pedro Alves
@ 2019-01-10 22:53       ` Tom Tromey
  0 siblings, 0 replies; 36+ messages in thread
From: Tom Tromey @ 2019-01-10 22:53 UTC (permalink / raw)
  To: Pedro Alves; +Cc: Tom Tromey, gdb-patches

>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

>> I don't think there is one.
>> What do you think of this?

Pedro> Looks good, thanks!

I'm going to check it in.

Tom

^ permalink raw reply	[flat|nested] 36+ messages in thread

end of thread, other threads:[~2019-01-10 22:53 UTC | newest]

Thread overview: 36+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-25 16:54 [PATCH 00/12] Remove some ALL_* iteration macros Tom Tromey
2018-11-25 16:54 ` [PATCH 09/12] Remove ALL_OBJFILE_FILETABS Tom Tromey
2018-11-25 16:54 ` [PATCH 03/12] Remove most uses of ALL_OBJFILES Tom Tromey
2018-11-25 16:54 ` [PATCH 07/12] Remove ALL_COMPUNITS Tom Tromey
2018-11-25 16:54 ` [PATCH 10/12] Remove ALL_OBJFILES and ALL_FILETABS Tom Tromey
2018-11-25 16:54 ` [PATCH 11/12] Remove ALL_OBJSECTIONS Tom Tromey
2018-11-25 16:54 ` [PATCH 04/12] Remove ALL_OBJFILES_SAFE Tom Tromey
2018-11-25 16:54 ` [PATCH 12/12] Remove ALL_OBJFILE_PSYMTABS Tom Tromey
2018-11-25 16:54 ` [PATCH 01/12] Introduce all_objfiles and next_iterator Tom Tromey
2018-11-25 16:54 ` [PATCH 08/12] Remove ALL_COMPUNIT_FILETABS Tom Tromey
2018-11-25 16:54 ` [PATCH 05/12] Remove ALL_MSYMBOLS and ALL_OBJFILE_MSYMBOLS Tom Tromey
2018-11-25 16:55 ` [PATCH 02/12] Remove ALL_PSPACE_OBJFILES Tom Tromey
2018-11-25 16:55 ` [PATCH 06/12] Remove ALL_OBJFILE_COMPUNITS Tom Tromey
2018-12-23  7:00 ` [PATCH 00/12] Remove some ALL_* iteration macros Joel Brobecker
2018-12-24 20:54   ` Tom Tromey
2018-12-26 17:30     ` Simon Marchi
2018-12-26 22:28       ` Tom Tromey
2018-12-26 22:32         ` Tom Tromey
2018-12-26 22:35           ` Simon Marchi
2018-12-26 22:52             ` Tom Tromey
2018-12-26 23:45               ` Tom Tromey
2018-12-27  0:46                 ` Simon Marchi
2018-12-27  6:22                   ` Tom Tromey
2018-12-27  1:52 ` Simon Marchi
2019-01-03 21:46   ` Tom Tromey
2019-01-03 22:45     ` Simon Marchi
2019-01-06 20:10       ` Tom Tromey
2019-01-09 19:49         ` Simon Marchi
2019-01-10  1:29           ` Tom Tromey
2019-01-10 16:45         ` Pedro Alves
2019-01-10 18:10           ` Tom Tromey
2019-01-10 19:58             ` Pedro Alves
2019-01-10 16:44 ` Pedro Alves
2019-01-10 18:06   ` Tom Tromey
2019-01-10 18:09     ` Pedro Alves
2019-01-10 22:53       ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).