[RFA 01/42] Use new and delete for buildsym_compunit

Tom Tromey
2018-05-23T04:59:02Z

This changes buildsym_compunit to use new and delete.

gdb/ChangeLog
2018-05-22 Tom Tromey <tom@tromey.com>

* buildsym.c (struct buildsym_compunit): Add constructor,
destructor, initializers.
(start_buildsym_compunit): Remove.
(free_buildsym_compunit): Use "delete".
(start_symtab, restart_symtab): Use "new".
---
gdb/ChangeLog | 8 ++++++
gdb/buildsym.c | 91 ++++++++++++++++++++++++++--------------------------------
2 files changed, 49 insertions(+), 50 deletions(-)

diff --git a/gdb/buildsym.c b/gdb/buildsym.c
index 5d38cb250f..9863e4ea1e 100644
--- a/gdb/buildsym.c
+++ b/gdb/buildsym.c
@@ -100,6 +100,35 @@

struct buildsym_compunit
{
+ /* Start recording information about a primary source file (IOW, not an
+ included source file).
+ COMP_DIR is the directory in which the compilation unit was compiled
+ (or NULL if not known). */
+
+ buildsym_compunit (struct objfile *objfile_, const char *comp_dir_,
+ enum language language_)
+ : objfile (objfile_),
+ comp_dir (comp_dir_ == nullptr ? nullptr : xstrdup (comp_dir_)),
+ language (language_)
+ {
+ }
+
+ ~buildsym_compunit ()
+ {
+ struct subfile *subfile, *nextsub;
+
+ for (subfile = subfiles;
+ subfile != NULL;
+ subfile = nextsub)
+ {
+ nextsub = subfile->next;
+ xfree (subfile->name);
+ xfree (subfile->line_vector);
+ xfree (subfile);
+ }
+ xfree (comp_dir);
+ }
+
/* The objfile we're reading debug info from. */
struct objfile *objfile;

@@ -107,24 +136,24 @@ struct buildsym_compunit
Files are added to the front of the list.
This is important mostly for the language determination hacks we use,
which iterate over previously added files. */
- struct subfile *subfiles;
+ struct subfile *subfiles = nullptr;

/* The subfile of the main source file. */
- struct subfile *main_subfile;
+ struct subfile *main_subfile = nullptr;

/* E.g., DW_AT_comp_dir if DWARF. Space for this is malloc'd. */
char *comp_dir;

/* Space for this is not malloc'd, and is assumed to have at least
the same lifetime as objfile. */
- const char *producer;
+ const char *producer = nullptr;

/* Space for this is not malloc'd, and is assumed to have at least
the same lifetime as objfile. */
- const char *debugformat;
+ const char *debugformat = nullptr;

/* The compunit we are building. */
- struct compunit_symtab *compunit_symtab;
+ struct compunit_symtab *compunit_symtab = nullptr;

/* Language of this compunit_symtab. */
enum language language;
@@ -763,54 +792,14 @@ start_subfile (const char *name)
}
}

-/* Start recording information about a primary source file (IOW, not an
- included source file).
- COMP_DIR is the directory in which the compilation unit was compiled
- (or NULL if not known). */
-
-static struct buildsym_compunit *
-start_buildsym_compunit (struct objfile *objfile, const char *comp_dir,
- enum language language)
-{
- struct buildsym_compunit *bscu;
-
- bscu = XNEW (struct buildsym_compunit);
- memset (bscu, 0, sizeof (struct buildsym_compunit));
-
- bscu->objfile = objfile;
- bscu->comp_dir = (comp_dir == NULL) ? NULL : xstrdup (comp_dir);
- bscu->language = language;
-
- /* Initialize the debug format string to NULL. We may supply it
- later via a call to record_debugformat. */
- bscu->debugformat = NULL;
-
- /* Similarly for the producer. */
- bscu->producer = NULL;
-
- return bscu;
-}
-
/* Delete the buildsym compunit. */

static void
free_buildsym_compunit (void)
{
- struct subfile *subfile, *nextsub;
-
if (buildsym_compunit == NULL)
return;
- for (subfile = buildsym_compunit->subfiles;
- subfile != NULL;
- subfile = nextsub)
- {
- nextsub = subfile->next;
- xfree (subfile->name);
- xfree (subfile->line_vector);
- xfree (subfile);
- }
- xfree (buildsym_compunit->comp_dir);
- xfree (buildsym_compunit);
+ delete buildsym_compunit;
buildsym_compunit = NULL;
current_subfile = NULL;
}
@@ -1057,7 +1046,8 @@ start_symtab (struct objfile *objfile, const char *name, const char *comp_dir,
{
prepare_for_building (name, start_addr);

- buildsym_compunit = start_buildsym_compunit (objfile, comp_dir, language);
+ buildsym_compunit = new struct buildsym_compunit (objfile, comp_dir,
+ language);

/* Allocate the compunit symtab now. The caller needs it to allocate
non-primary symtabs. It is also needed by get_macro_table. */
@@ -1093,9 +1083,10 @@ restart_symtab (struct compunit_symtab *cust,
{
prepare_for_building (name, start_addr);

- buildsym_compunit = start_buildsym_compunit (COMPUNIT_OBJFILE (cust),
- COMPUNIT_DIRNAME (cust),
- compunit_language (cust));
+ buildsym_compunit
+ = new struct buildsym_compunit (COMPUNIT_OBJFILE (cust),
+ COMPUNIT_DIRNAME (cust),
+ compunit_language (cust));
buildsym_compunit->compunit_symtab = cust;
}

--
2.13.6