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.  */
 
