From 32fa66eb88d029576cfd8f5698d0579662d2f045 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Fri, 27 Oct 2017 21:46:49 -0400 Subject: [PATCH] Add tests for filename_language The next patch touches the filename_language area, but I noticed there is no test exercising that. This patch adds some selftests for add_filename_language, deduce_language_from_filename and set_ext_lang_command. Because these tests add entries to the global filename_language_table vector, it is not possible to run them successfully multiple times in a same GDB instance. They can potentially interfere with each other for the same reason. I therefore added the scoped_restore_filename_language_table class that is used to make sure tests leave that global vector in the same state they found it (it is replaced in the following patch by a simple scoped_restore). gdb/ChangeLog: * symfile.c: Include selftest.h. (class scoped_restore_filename_language_table): New. (test_filename_language): New test. (test_set_ext_lang_command): New test. (_initialize_symfile): Register tests. --- gdb/ChangeLog | 8 +++++ gdb/symfile.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 4b292e0d1d4..d529a363a73 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,11 @@ +2017-10-27 Simon Marchi + + * symfile.c: Include selftest.h. + (class scoped_restore_filename_language_table): New. + (test_filename_language): New test. + (test_set_ext_lang_command): New test. + (_initialize_symfile): Register tests. + 2017-10-27 Keith Seitz * breakpoint.c (print_breakpoint_location): Use the symbol saved diff --git a/gdb/symfile.c b/gdb/symfile.c index a7d8553bb00..152b29fa8bb 100644 --- a/gdb/symfile.c +++ b/gdb/symfile.c @@ -57,6 +57,7 @@ #include "gdb_bfd.h" #include "cli/cli-utils.h" #include "common/byte-vector.h" +#include "selftest.h" #include #include @@ -3829,6 +3830,86 @@ map_symbol_filenames (symbol_filename_ftype *fun, void *data, } } +#if GDB_SELF_TEST + +namespace selftests { +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 () +{ + /* This test messes up the filename_language_table global. */ + scoped_restore_filename_language_table restore_flt; + + /* Test deducing an unknown extension. */ + language lang = deduce_language_from_filename ("myfile.blah"); + SELF_CHECK (lang == language_unknown); + + /* Test deducing a known extension. */ + lang = deduce_language_from_filename ("myfile.c"); + SELF_CHECK (lang == language_c); + + /* Test adding a new extension using the internal API. */ + add_filename_language (".blah", language_pascal); + lang = deduce_language_from_filename ("myfile.blah"); + SELF_CHECK (lang == language_pascal); +} + +static void +test_set_ext_lang_command () +{ + /* This test messes up the filename_language_table global. */ + scoped_restore_filename_language_table restore_flt; + + /* Confirm that the .hello extension is not known. */ + language lang = deduce_language_from_filename ("cake.hello"); + SELF_CHECK (lang == language_unknown); + + /* Test adding a new extension using the CLI command. */ + gdb::unique_xmalloc_ptr args_holder (xstrdup (".hello rust")); + ext_args = args_holder.get (); + set_ext_lang_command (NULL, 1, NULL); + + lang = deduce_language_from_filename ("cake.hello"); + SELF_CHECK (lang == language_rust); + + /* Test overriding an existing extension using the CLI command. */ + int size_before = VEC_length (filename_language, filename_language_table); + args_holder.reset (xstrdup (".hello pascal")); + ext_args = args_holder.get (); + set_ext_lang_command (NULL, 1, NULL); + int size_after = VEC_length (filename_language, filename_language_table); + + lang = deduce_language_from_filename ("cake.hello"); + SELF_CHECK (lang == language_pascal); + SELF_CHECK (size_before == size_after); +} + +} /* namespace filename_language */ +} /* namespace selftests */ + +#endif /* GDB_SELF_TEST */ + void _initialize_symfile (void) { @@ -3940,4 +4021,12 @@ Set printing of separate debug info file search debug."), _("\ Show printing of separate debug info file search debug."), _("\ When on, GDB prints the searched locations while looking for separate debug \ info files."), NULL, NULL, &setdebuglist, &showdebuglist); + +#if GDB_SELF_TEST + selftests::register_test + ("filename_language", selftests::filename_language::test_filename_language); + selftests::register_test + ("set_ext_lang_command", + selftests::filename_language::test_set_ext_lang_command); +#endif } -- 2.30.2