gdb: make gdbarch_printable_names return a vector

I noticed that gdbarch_selftest::operator() leaked the value returned by
gdbarch_printable_names.  Make gdbarch_printable_names return an
std::vector and update callers.  That makes it easier for everyone
involved, less manual memory management.

Change-Id: Ia8fc028bdb91f787410cca34f10bf3c5a6da1498
This commit is contained in:
Simon Marchi 2021-08-09 21:47:02 -04:00
parent 65f82b1972
commit 9b1f59fc95
5 changed files with 54 additions and 73 deletions

View file

@ -669,10 +669,14 @@ static const bfd_target *default_bfd_vec;
static enum bfd_endian default_byte_order = BFD_ENDIAN_UNKNOWN;
/* Printable names of architectures. Used as the enum list of the
"set arch" command. */
static std::vector<const char *> arches;
void
initialize_current_architecture (void)
{
const char **arches = gdbarch_printable_names ();
arches = gdbarch_printable_names ();
/* Find a default architecture. */
if (default_bfd_arch == NULL)
@ -680,15 +684,17 @@ initialize_current_architecture (void)
/* Choose the architecture by taking the first one
alphabetically. */
const char *chosen = arches[0];
const char **arch;
for (arch = arches; *arch != NULL; arch++)
for (const char *arch : arches)
{
if (strcmp (*arch, chosen) < 0)
chosen = *arch;
if (strcmp (arch, chosen) < 0)
chosen = arch;
}
if (chosen == NULL)
internal_error (__FILE__, __LINE__,
_("initialize_current_architecture: No arch"));
default_bfd_arch = bfd_scan_arch (chosen);
if (default_bfd_arch == NULL)
internal_error (__FILE__, __LINE__,
@ -743,14 +749,11 @@ initialize_current_architecture (void)
list of architectures. */
{
/* Append ``auto''. */
int nr;
for (nr = 0; arches[nr] != NULL; nr++);
arches = XRESIZEVEC (const char *, arches, nr + 2);
arches[nr + 0] = "auto";
arches[nr + 1] = NULL;
arches.push_back ("auto");
arches.push_back (nullptr);
set_show_commands architecture_cmds
= add_setshow_enum_cmd ("architecture", class_support,
arches, &set_architecture_string,
arches.data (), &set_architecture_string,
_("Set architecture of target."),
_("Show architecture of target."), NULL,
set_architecture, show_architecture,

View file

@ -5549,40 +5549,30 @@ struct gdbarch_registration
static struct gdbarch_registration *gdbarch_registry = NULL;
static void
append_name (const char ***buf, int *nr, const char *name)
{
*buf = XRESIZEVEC (const char *, *buf, *nr + 1);
(*buf)[*nr] = name;
*nr += 1;
}
const char **
gdbarch_printable_names (void)
std::vector<const char *>
gdbarch_printable_names ()
{
/* Accumulate a list of names based on the registed list of
architectures. */
int nr_arches = 0;
const char **arches = NULL;
struct gdbarch_registration *rego;
std::vector<const char *> arches;
for (rego = gdbarch_registry;
rego != NULL;
for (gdbarch_registration *rego = gdbarch_registry;
rego != nullptr;
rego = rego->next)
{
const struct bfd_arch_info *ap;
ap = bfd_lookup_arch (rego->bfd_architecture, 0);
if (ap == NULL)
const struct bfd_arch_info *ap
= bfd_lookup_arch (rego->bfd_architecture, 0);
if (ap == nullptr)
internal_error (__FILE__, __LINE__,
_("gdbarch_architecture_names: multi-arch unknown"));
do
{
append_name (&arches, &nr_arches, ap->printable_name);
arches.push_back (ap->printable_name);
ap = ap->next;
}
while (ap != NULL);
}
append_name (&arches, &nr_arches, NULL);
return arches;
}

View file

@ -1824,12 +1824,11 @@ extern void gdbarch_register (enum bfd_architecture architecture,
gdbarch_dump_tdep_ftype *);
/* Return a freshly allocated, NULL terminated, array of the valid
architecture names. Since architectures are registered during the
_initialize phase this function only returns useful information
once initialization has been completed. */
/* Return a vector of the valid architecture names. Since architectures are
registered during the _initialize phase this function only returns useful
information once initialization has been completed. */
extern const char **gdbarch_printable_names (void);
extern std::vector<const char *> gdbarch_printable_names ();
/* Helper function. Search the list of ARCHES for a GDBARCH that

View file

@ -1576,12 +1576,11 @@ extern void gdbarch_register (enum bfd_architecture architecture,
gdbarch_dump_tdep_ftype *);
/* Return a freshly allocated, NULL terminated, array of the valid
architecture names. Since architectures are registered during the
_initialize phase this function only returns useful information
once initialization has been completed. */
/* Return a vector of the valid architecture names. Since architectures are
registered during the _initialize phase this function only returns useful
information once initialization has been completed. */
extern const char **gdbarch_printable_names (void);
extern std::vector<const char *> gdbarch_printable_names ();
/* Helper function. Search the list of ARCHES for a GDBARCH that
@ -2330,40 +2329,30 @@ struct gdbarch_registration
static struct gdbarch_registration *gdbarch_registry = NULL;
static void
append_name (const char ***buf, int *nr, const char *name)
{
*buf = XRESIZEVEC (const char *, *buf, *nr + 1);
(*buf)[*nr] = name;
*nr += 1;
}
const char **
gdbarch_printable_names (void)
std::vector<const char *>
gdbarch_printable_names ()
{
/* Accumulate a list of names based on the registed list of
architectures. */
int nr_arches = 0;
const char **arches = NULL;
struct gdbarch_registration *rego;
std::vector<const char *> arches;
for (rego = gdbarch_registry;
rego != NULL;
for (gdbarch_registration *rego = gdbarch_registry;
rego != nullptr;
rego = rego->next)
{
const struct bfd_arch_info *ap;
ap = bfd_lookup_arch (rego->bfd_architecture, 0);
if (ap == NULL)
const struct bfd_arch_info *ap
= bfd_lookup_arch (rego->bfd_architecture, 0);
if (ap == nullptr)
internal_error (__FILE__, __LINE__,
_("gdbarch_architecture_names: multi-arch unknown"));
do
{
append_name (&arches, &nr_arches, ap->printable_name);
arches.push_back (ap->printable_name);
ap = ap->next;
}
while (ap != NULL);
}
append_name (&arches, &nr_arches, NULL);
return arches;
}

View file

@ -36,23 +36,23 @@ struct gdbarch_selftest : public selftest
void operator() () const override
{
const char **arches = gdbarch_printable_names ();
std::vector<const char *> arches = gdbarch_printable_names ();
bool pass = true;
for (int i = 0; arches[i] != NULL; i++)
for (const char *arch : arches)
{
if (strcmp ("fr300", arches[i]) == 0)
if (strcmp ("fr300", arch) == 0)
{
/* PR 20946 */
continue;
}
else if (strcmp ("powerpc:EC603e", arches[i]) == 0
|| strcmp ("powerpc:e500mc", arches[i]) == 0
|| strcmp ("powerpc:e500mc64", arches[i]) == 0
|| strcmp ("powerpc:titan", arches[i]) == 0
|| strcmp ("powerpc:vle", arches[i]) == 0
|| strcmp ("powerpc:e5500", arches[i]) == 0
|| strcmp ("powerpc:e6500", arches[i]) == 0)
else if (strcmp ("powerpc:EC603e", arch) == 0
|| strcmp ("powerpc:e500mc", arch) == 0
|| strcmp ("powerpc:e500mc64", arch) == 0
|| strcmp ("powerpc:titan", arch) == 0
|| strcmp ("powerpc:vle", arch) == 0
|| strcmp ("powerpc:e5500", arch) == 0
|| strcmp ("powerpc:e6500", arch) == 0)
{
/* PR 19797 */
continue;
@ -64,7 +64,7 @@ struct gdbarch_selftest : public selftest
{
struct gdbarch_info info;
info.bfd_arch_info = bfd_scan_arch (arches[i]);
info.bfd_arch_info = bfd_scan_arch (arch);
struct gdbarch *gdbarch = gdbarch_find_by_info (info);
SELF_CHECK (gdbarch != NULL);
@ -75,7 +75,7 @@ struct gdbarch_selftest : public selftest
{
pass = false;
exception_fprintf (gdb_stderr, ex,
_("Self test failed: arch %s: "), arches[i]);
_("Self test failed: arch %s: "), arch);
}
reset ();