+2017-11-04  Tom Tromey  <tom@tromey.com>
+
+       * dwarf2read.c (process_full_comp_unit, process_full_type_unit):
+       Use scoped_free_pendings.
+       * dbxread.c (dbx_symfile_read, dbx_psymtab_to_symtab_1): Use
+       scoped_free_pendings.
+       * xcoffread.c (xcoff_psymtab_to_symtab_1): Use scoped_free_pendings.
+       (xcoff_initial_scan): Likewise.
+       * buildsym.c (reset_symtab_globals): Update comment.
+       (scoped_free_pendings): Rename from really_free_pendings.
+       (prepare_for_building): Update comment.
+       (buildsym_init): Likewise.
+       * buildsym.h (class scoped_free_pendings): New class.
+       (really_free_pendings): Don't declare.
+
 2017-11-03  Ulrich Weigand  <uweigand@de.ibm.com>
 
        * doublest.c (convert_doublest_to_floatformat): Fix uninitialized
 
    The basic way this module is used is as follows:
 
    buildsym_init ();
-   cleanups = make_cleanup (really_free_pendings, NULL);
+   scoped_free_pendings free_pending;
    cust = start_symtab (...);
    ... read debug info ...
    cust = end_symtab (...);
-   do_cleanups (cleanups);
 
    The compunit symtab pointer ("cust") is returned from both start_symtab
    and end_symtab to simplify the debug info readers.
    Reading DWARF Type Units is another variation:
 
    buildsym_init ();
-   cleanups = make_cleanup (really_free_pendings, NULL);
+   scoped_free_pendings free_pending;
    cust = start_symtab (...);
    ... read debug info ...
    cust = end_expandable_symtab (...);
-   do_cleanups (cleanups);
 
    And then reading subsequent Type Units within the containing "Comp Unit"
    will use a second flow:
 
    buildsym_init ();
-   cleanups = make_cleanup (really_free_pendings, NULL);
+   scoped_free_pendings free_pending;
    cust = restart_symtab (...);
    ... read debug info ...
    cust = augment_type_symtab (...);
-   do_cleanups (cleanups);
 
    dbxread.c and xcoffread.c use another variation:
 
    buildsym_init ();
-   cleanups = make_cleanup (really_free_pendings, NULL);
+   scoped_free_pendings free_pending;
    cust = start_symtab (...);
    ... read debug info ...
    cust = end_symtab (...);
    ... start_symtab + read + end_symtab repeated ...
-   do_cleanups (cleanups);
 */
 
 #include "defs.h"
   return (NULL);
 }
 
-/* At end of reading syms, or in case of quit, ensure everything associated
-   with building symtabs is freed.  This is intended to be registered as a
-   cleanup before doing psymtab->symtab expansion.
+/* At end of reading syms, or in case of quit, ensure everything
+   associated with building symtabs is freed.
 
    N.B. This is *not* intended to be used when building psymtabs.  Some debug
    info readers call this anyway, which is harmless if confusing.  */
 
-void
-really_free_pendings (void *dummy)
+scoped_free_pendings::~scoped_free_pendings ()
 {
   struct pending *next, *next1;
 
   context_stack_depth = 0;
 
   /* These should have been reset either by successful completion of building
-     a symtab, or by the really_free_pendings cleanup.  */
+     a symtab, or by the scoped_free_pendings destructor.  */
   gdb_assert (file_symbols == NULL);
   gdb_assert (global_symbols == NULL);
   gdb_assert (global_using_directives == NULL);
 /* Reset state after a successful building of a symtab.
    This exists because dbxread.c and xcoffread.c can call
    start_symtab+end_symtab multiple times after one call to buildsym_init,
-   and before the really_free_pendings cleanup is called.
+   and before the scoped_free_pendings destructor is called.
    We keep the free_pendings list around for dbx/xcoff sake.  */
 
 static void
       context_stack = XNEWVEC (struct context_stack, context_stack_size);
     }
 
-  /* Ensure the really_free_pendings cleanup was called after
+  /* Ensure the scoped_free_pendings destructor was called after
      the last time.  */
   gdb_assert (free_pendings == NULL);
   gdb_assert (pending_blocks == NULL);
 
 extern void record_block_range (struct block *,
                                 CORE_ADDR start, CORE_ADDR end_inclusive);
 
-extern void really_free_pendings (void *dummy);
+class scoped_free_pendings
+{
+public:
+
+  scoped_free_pendings () = default;
+  ~scoped_free_pendings ();
+
+  DISABLE_COPY_AND_ASSIGN (scoped_free_pendings);
+};
 
 extern void start_subfile (const char *name);
 
 
 {
   bfd *sym_bfd;
   int val;
-  struct cleanup *back_to;
 
   sym_bfd = objfile->obfd;
 
   symbol_table_offset = DBX_SYMTAB_OFFSET (objfile);
 
   free_pending_blocks ();
-  back_to = make_cleanup (really_free_pendings, 0);
+  scoped_free_pendings free_pending;
 
   minimal_symbol_reader reader (objfile);
 
      minimal symbols for this objfile.  */
 
   reader.install ();
-
-  do_cleanups (back_to);
 }
 
 /* Initialize anything that needs initializing when a completely new
 static void
 dbx_psymtab_to_symtab_1 (struct objfile *objfile, struct partial_symtab *pst)
 {
-  struct cleanup *old_chain;
   int i;
 
   if (pst->readin)
       /* Init stuff necessary for reading in symbols */
       stabsread_init ();
       buildsym_init ();
-      old_chain = make_cleanup (really_free_pendings, 0);
+      scoped_free_pendings free_pending;
       file_string_table_offset = FILE_STRING_OFFSET (pst);
       symbol_size = SYMBOL_SIZE (pst);
 
       /* Read in this file's symbols.  */
       bfd_seek (objfile->obfd, SYMBOL_OFFSET (pst), SEEK_SET);
       read_ofile_symtab (objfile, pst);
-
-      do_cleanups (old_chain);
     }
 
   pst->readin = 1;
 
   struct gdbarch *gdbarch = get_objfile_arch (objfile);
   CORE_ADDR lowpc, highpc;
   struct compunit_symtab *cust;
-  struct cleanup *back_to, *delayed_list_cleanup;
+  struct cleanup *delayed_list_cleanup;
   CORE_ADDR baseaddr;
   struct block *static_block;
   CORE_ADDR addr;
   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
 
   buildsym_init ();
-  back_to = make_cleanup (really_free_pendings, NULL);
+  scoped_free_pendings free_pending;
   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
 
   cu->list_in_scope = &file_symbols;
 
   /* Push it for inclusion processing later.  */
   VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
-
-  do_cleanups (back_to);
 }
 
 /* Generate full symbol information for type unit PER_CU, whose DIEs have
   struct dwarf2_cu *cu = per_cu->cu;
   struct objfile *objfile = per_cu->objfile;
   struct compunit_symtab *cust;
-  struct cleanup *back_to, *delayed_list_cleanup;
+  struct cleanup *delayed_list_cleanup;
   struct signatured_type *sig_type;
 
   gdb_assert (per_cu->is_debug_types);
   sig_type = (struct signatured_type *) per_cu;
 
   buildsym_init ();
-  back_to = make_cleanup (really_free_pendings, NULL);
+  scoped_free_pendings free_pending;
   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
 
   cu->list_in_scope = &file_symbols;
       pst->compunit_symtab = cust;
       pst->readin = 1;
     }
-
-  do_cleanups (back_to);
 }
 
 /* Process an imported unit DIE.  */
 
 static void
 xcoff_psymtab_to_symtab_1 (struct objfile *objfile, struct partial_symtab *pst)
 {
-  struct cleanup *old_chain;
   int i;
 
   if (!pst)
       /* Init stuff necessary for reading in symbols.  */
       stabsread_init ();
       buildsym_init ();
-      old_chain = make_cleanup (really_free_pendings, 0);
 
+      scoped_free_pendings free_pending;
       read_xcoff_symtab (objfile, pst);
-
-      do_cleanups (old_chain);
     }
 
   pst->readin = 1;
 {
   bfd *abfd;
   int val;
-  struct cleanup *back_to;
   int num_symbols;             /* # of symbols */
   file_ptr symtab_offset;      /* symbol table and */
   file_ptr stringtab_offset;   /* string table file offsets */
     init_psymbol_list (objfile, num_symbols);
 
   free_pending_blocks ();
-  back_to = make_cleanup (really_free_pendings, 0);
 
+  scoped_free_pendings free_pending;
   minimal_symbol_reader reader (objfile);
 
   /* Now that the symbol table data of the executable file are all in core,
     dwarf2_build_psymtabs (objfile);
 
   dwarf2_build_frame_info (objfile);
-
-  do_cleanups (back_to);
 }
 \f
 static void