From patchwork Sat Oct 21 06:52:33 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Marchi X-Patchwork-Id: 23742 Received: (qmail 1909 invoked by alias); 21 Oct 2017 06:52:53 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 1376 invoked by uid 89); 21 Oct 2017 06:52:53 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_SOFTFAIL autolearn=ham version=3.3.2 spammy=39404 X-HELO: barracuda.ebox.ca Received: from barracuda.ebox.ca (HELO barracuda.ebox.ca) (96.127.255.19) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 21 Oct 2017 06:52:51 +0000 X-ASG-Debug-ID: 1508568756-0c856e65d53809d30001-fS2M51 Received: from smtp.electronicbox.net (smtp.electronicbox.net [96.127.255.82]) by barracuda.ebox.ca with ESMTP id eDbjeDgnoT0oQJEs (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sat, 21 Oct 2017 02:52:36 -0400 (EDT) X-Barracuda-Envelope-From: simon.marchi@polymtl.ca X-Barracuda-RBL-Trusted-Forwarder: 96.127.255.82 Received: from simark.lan (cable-192.222.251.162.electronicbox.net [192.222.251.162]) by smtp.electronicbox.net (Postfix) with ESMTP id 6893C441D64; Sat, 21 Oct 2017 02:52:36 -0400 (EDT) From: Simon Marchi X-Barracuda-Effective-Source-IP: cable-192.222.251.162.electronicbox.net[192.222.251.162] X-Barracuda-Apparent-Source-IP: 192.222.251.162 X-Barracuda-RBL-IP: 192.222.251.162 To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH 1/2] Add tests for filename_language Date: Sat, 21 Oct 2017 02:52:33 -0400 X-ASG-Orig-Subj: [PATCH 1/2] Add tests for filename_language Message-Id: <20171021065234.347-1-simon.marchi@polymtl.ca> X-Barracuda-Connect: smtp.electronicbox.net[96.127.255.82] X-Barracuda-Start-Time: 1508568756 X-Barracuda-Encrypted: DHE-RSA-AES256-SHA X-Barracuda-URL: https://96.127.255.19:443/cgi-mod/mark.cgi X-Barracuda-Scan-Msg-Size: 4727 X-Barracuda-BRTS-Status: 1 X-Barracuda-Spam-Score: 0.00 X-Barracuda-Spam-Status: No, SCORE=0.00 using global scores of TAG_LEVEL=1000.0 QUARANTINE_LEVEL=1000.0 KILL_LEVEL=8.0 tests= X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.3.44079 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- X-IsSubscribed: yes From: Simon Marchi 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/symfile.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/gdb/symfile.c b/gdb/symfile.c index a7d8553bb0..152b29fa8b 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 }