Get rid of VEC(filename_language)

This patch removes VEC(filename_language), replacing its usage with
std::vector.  filename_language::ext is changed to an std::string at the
same time.

Regtested on the buildbot.

gdb/ChangeLog:

	* symfile.c (filename_language): Make struct, not typedef.  Add
	constructor.
	<ext>: Change type to std::string.
	(DEF_VEC_O (filename_language)): Remove.
	(filename_language_table): Change type to std::vector.
	(add_filename_language): Adjust.
	(set_ext_lang_command): Adjust.
	(info_ext_lang_command): Adjust.
	(deduce_language_from_filename): Adjust.
	(class scoped_restore_filename_language_table): Remove.
	(test_filename_language): Use scoped_restore.
	(test_set_ext_lang_command): Use scoped_restore, adjust to
	std::vector change.
This commit is contained in:
Simon Marchi 2017-10-27 21:47:30 -04:00
parent 32fa66eb88
commit 593e3209f3
2 changed files with 40 additions and 62 deletions

View File

@ -1,3 +1,19 @@
2017-10-27 Simon Marchi <simon.marchi@ericsson.com>
* symfile.c (filename_language): Make struct, not typedef. Add
constructor.
<ext>: Change type to std::string.
(DEF_VEC_O (filename_language)): Remove.
(filename_language_table): Change type to std::vector.
(add_filename_language): Adjust.
(set_ext_lang_command): Adjust.
(info_ext_lang_command): Adjust.
(deduce_language_from_filename): Adjust.
(class scoped_restore_filename_language_table): Remove.
(test_filename_language): Use scoped_restore.
(test_set_ext_lang_command): Use scoped_restore, adjust to
std::vector change.
2017-10-27 Simon Marchi <simon.marchi@ericsson.com> 2017-10-27 Simon Marchi <simon.marchi@ericsson.com>
* symfile.c: Include selftest.h. * symfile.c: Include selftest.h.

View File

@ -2637,27 +2637,24 @@ reread_symbols (void)
} }
typedef struct struct filename_language
{ {
char *ext; filename_language (const std::string &ext_, enum language lang_)
: ext (ext_), lang (lang_)
{}
std::string ext;
enum language lang; enum language lang;
} filename_language; };
DEF_VEC_O (filename_language); static std::vector<filename_language> filename_language_table;
static VEC (filename_language) *filename_language_table;
/* See symfile.h. */ /* See symfile.h. */
void void
add_filename_language (const char *ext, enum language lang) add_filename_language (const char *ext, enum language lang)
{ {
filename_language entry; filename_language_table.emplace_back (ext, lang);
entry.ext = xstrdup (ext);
entry.lang = lang;
VEC_safe_push (filename_language, filename_language_table, &entry);
} }
static char *ext_args; static char *ext_args;
@ -2674,10 +2671,8 @@ show_ext_args (struct ui_file *file, int from_tty,
static void static void
set_ext_lang_command (char *args, int from_tty, struct cmd_list_element *e) set_ext_lang_command (char *args, int from_tty, struct cmd_list_element *e)
{ {
int i;
char *cp = ext_args; char *cp = ext_args;
enum language lang; enum language lang;
filename_language *entry;
/* First arg is filename extension, starting with '.' */ /* First arg is filename extension, starting with '.' */
if (*cp != '.') if (*cp != '.')
@ -2706,16 +2701,15 @@ set_ext_lang_command (char *args, int from_tty, struct cmd_list_element *e)
/* Lookup the language from among those we know. */ /* Lookup the language from among those we know. */
lang = language_enum (cp); lang = language_enum (cp);
auto it = filename_language_table.begin ();
/* Now lookup the filename extension: do we already know it? */ /* Now lookup the filename extension: do we already know it? */
for (i = 0; for (; it != filename_language_table.end (); it++)
VEC_iterate (filename_language, filename_language_table, i, entry);
++i)
{ {
if (0 == strcmp (ext_args, entry->ext)) if (it->ext == ext_args)
break; break;
} }
if (entry == NULL) if (it == filename_language_table.end ())
{ {
/* New file extension. */ /* New file extension. */
add_filename_language (ext_args, lang); add_filename_language (ext_args, lang);
@ -2728,42 +2722,31 @@ set_ext_lang_command (char *args, int from_tty, struct cmd_list_element *e)
/* query ("Really make files of type %s '%s'?", */ /* query ("Really make files of type %s '%s'?", */
/* ext_args, language_str (lang)); */ /* ext_args, language_str (lang)); */
xfree (entry->ext); it->lang = lang;
entry->ext = xstrdup (ext_args);
entry->lang = lang;
} }
} }
static void static void
info_ext_lang_command (char *args, int from_tty) info_ext_lang_command (char *args, int from_tty)
{ {
int i;
filename_language *entry;
printf_filtered (_("Filename extensions and the languages they represent:")); printf_filtered (_("Filename extensions and the languages they represent:"));
printf_filtered ("\n\n"); printf_filtered ("\n\n");
for (i = 0; for (const filename_language &entry : filename_language_table)
VEC_iterate (filename_language, filename_language_table, i, entry); printf_filtered ("\t%s\t- %s\n", entry.ext.c_str (),
++i) language_str (entry.lang));
printf_filtered ("\t%s\t- %s\n", entry->ext, language_str (entry->lang));
} }
enum language enum language
deduce_language_from_filename (const char *filename) deduce_language_from_filename (const char *filename)
{ {
int i;
const char *cp; const char *cp;
if (filename != NULL) if (filename != NULL)
if ((cp = strrchr (filename, '.')) != NULL) if ((cp = strrchr (filename, '.')) != NULL)
{ {
filename_language *entry; for (const filename_language &entry : filename_language_table)
if (entry.ext == cp)
for (i = 0; return entry.lang;
VEC_iterate (filename_language, filename_language_table, i, entry);
++i)
if (strcmp (cp, entry->ext) == 0)
return entry->lang;
} }
return language_unknown; return language_unknown;
@ -3835,31 +3818,10 @@ map_symbol_filenames (symbol_filename_ftype *fun, void *data,
namespace selftests { namespace selftests {
namespace filename_language { namespace filename_language {
/* Save the content of the filename_language_table global and restore it when
going out of scope. */
class scoped_restore_filename_language_table
{
public:
scoped_restore_filename_language_table ()
{
m_saved_table = VEC_copy (filename_language, filename_language_table);
}
~scoped_restore_filename_language_table ()
{
VEC_free (filename_language, filename_language_table);
filename_language_table = VEC_copy (filename_language, m_saved_table);
}
private:
VEC(filename_language) *m_saved_table;
};
static void test_filename_language () static void test_filename_language ()
{ {
/* This test messes up the filename_language_table global. */ /* This test messes up the filename_language_table global. */
scoped_restore_filename_language_table restore_flt; scoped_restore restore_flt = make_scoped_restore (&filename_language_table);
/* Test deducing an unknown extension. */ /* Test deducing an unknown extension. */
language lang = deduce_language_from_filename ("myfile.blah"); language lang = deduce_language_from_filename ("myfile.blah");
@ -3879,7 +3841,7 @@ static void
test_set_ext_lang_command () test_set_ext_lang_command ()
{ {
/* This test messes up the filename_language_table global. */ /* This test messes up the filename_language_table global. */
scoped_restore_filename_language_table restore_flt; scoped_restore restore_flt = make_scoped_restore (&filename_language_table);
/* Confirm that the .hello extension is not known. */ /* Confirm that the .hello extension is not known. */
language lang = deduce_language_from_filename ("cake.hello"); language lang = deduce_language_from_filename ("cake.hello");
@ -3894,11 +3856,11 @@ test_set_ext_lang_command ()
SELF_CHECK (lang == language_rust); SELF_CHECK (lang == language_rust);
/* Test overriding an existing extension using the CLI command. */ /* Test overriding an existing extension using the CLI command. */
int size_before = VEC_length (filename_language, filename_language_table); int size_before = filename_language_table.size ();
args_holder.reset (xstrdup (".hello pascal")); args_holder.reset (xstrdup (".hello pascal"));
ext_args = args_holder.get (); ext_args = args_holder.get ();
set_ext_lang_command (NULL, 1, NULL); set_ext_lang_command (NULL, 1, NULL);
int size_after = VEC_length (filename_language, filename_language_table); int size_after = filename_language_table.size ();
lang = deduce_language_from_filename ("cake.hello"); lang = deduce_language_from_filename ("cake.hello");
SELF_CHECK (lang == language_pascal); SELF_CHECK (lang == language_pascal);