From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7833) id A8F0C3858D1E; Tue, 19 Apr 2022 08:17:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A8F0C3858D1E Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Lancelot SIX To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdbsupport/selftest: Replace for_each_selftest with an iterator_range X-Act-Checkin: binutils-gdb X-Git-Author: Lancelot SIX X-Git-Refname: refs/heads/master X-Git-Oldrev: 2aaee75f81a130011c96a0ab38475dba894114c3 X-Git-Newrev: c57207c15c4fa980263e6849d0e6472c33e647fc Message-Id: <20220419081751.A8F0C3858D1E@sourceware.org> Date: Tue, 19 Apr 2022 08:17:51 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 19 Apr 2022 08:17:51 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dc57207c15c4f= a980263e6849d0e6472c33e647fc commit c57207c15c4fa980263e6849d0e6472c33e647fc Author: Lancelot SIX Date: Wed Mar 23 15:29:53 2022 +0000 gdbsupport/selftest: Replace for_each_selftest with an iterator_range =20 Remove the callback-based selftests::for_each_selftest function and use an iterator_range instead. =20 Also use this iterator range in run_tests so all iterations over the selftests are done in a consistent way. This will become useful in a later commit. =20 Change-Id: I0b3a5349a7987fbcb0071f11c394e353df986583 Diff: --- gdb/maint.c | 13 ++++++------- gdbsupport/selftest.cc | 27 ++++++++++++--------------- gdbsupport/selftest.h | 31 ++++++++++++++++++++++++------- 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/gdb/maint.c b/gdb/maint.c index 60e183efdd1..7b726c2bc9f 100644 --- a/gdb/maint.c +++ b/gdb/maint.c @@ -1181,11 +1181,11 @@ maintenance_selftest_completer (cmd_list_element *c= md, return; =20 #if GDB_SELF_TEST - selftests::for_each_selftest ([&tracker, text] (const std::string &name) + for (const auto &test : selftests::all_selftests ()) { - if (startswith (name.c_str (), text)) - tracker.add_completion (make_unique_xstrdup (name.c_str ())); - }); + if (startswith (test.name.c_str (), text)) + tracker.add_completion (make_unique_xstrdup (test.name.c_str ())); + } #endif } =20 @@ -1194,9 +1194,8 @@ maintenance_info_selftests (const char *arg, int from= _tty) { #if GDB_SELF_TEST gdb_printf ("Registered selftests:\n"); - selftests::for_each_selftest ([] (const std::string &name) { - gdb_printf (" - %s\n", name.c_str ()); - }); + for (const auto &test : selftests::all_selftests ()) + gdb_printf (" - %s\n", test.name.c_str ()); #else gdb_printf (_("\ Selftests have been disabled for this build.\n")); diff --git a/gdbsupport/selftest.cc b/gdbsupport/selftest.cc index 466d7cfeab5..7077f113f97 100644 --- a/gdbsupport/selftest.cc +++ b/gdbsupport/selftest.cc @@ -20,16 +20,15 @@ #include "common-exceptions.h" #include "common-debug.h" #include "selftest.h" -#include #include =20 namespace selftests { -/* All the tests that have been registered. Using an std::map allows keep= ing +/* All the tests that have been registered. Using an std::set allows keep= ing the order of tests stable and easily looking up whether a test name exists. */ =20 -static std::map> tests; +static selftests_registry tests; =20 /* See selftest.h. */ =20 @@ -38,9 +37,9 @@ register_test (const std::string &name, std::function function) { /* Check that no test with this name already exist. */ - gdb_assert (tests.find (name) =3D=3D tests.end ()); - - tests[name] =3D function; + auto status =3D tests.emplace (name, std::move (function)); + if (!status.second) + gdb_assert_not_reached ("Test already registered"); } =20 /* See selftest.h. */ @@ -63,10 +62,8 @@ run_tests (gdb::array_view filters, b= ool verbose) int ran =3D 0, failed =3D 0; run_verbose_ =3D verbose; =20 - for (const auto &pair : tests) + for (const auto &test : all_selftests ()) { - const std::string &name =3D pair.first; - const auto &test =3D pair.second; bool run =3D false; =20 if (filters.empty ()) @@ -75,7 +72,7 @@ run_tests (gdb::array_view filters, bo= ol verbose) { for (const char *filter : filters) { - if (name.find (filter) !=3D std::string::npos) + if (test.name.find (filter) !=3D std::string::npos) run =3D true; } } @@ -85,9 +82,9 @@ run_tests (gdb::array_view filters, bo= ol verbose) =20 try { - debug_printf (_("Running selftest %s.\n"), name.c_str ()); + debug_printf (_("Running selftest %s.\n"), test.name.c_str ()); ++ran; - test (); + test.test (); } catch (const gdb_exception_error &ex) { @@ -104,10 +101,10 @@ run_tests (gdb::array_view filters= , bool verbose) =20 /* See selftest.h. */ =20 -void for_each_selftest (for_each_selftest_ftype func) +selftests_range +all_selftests () { - for (const auto &pair : tests) - func (pair.first); + return selftests_range (tests.cbegin (), tests.cend ()); } =20 } // namespace selftests diff --git a/gdbsupport/selftest.h b/gdbsupport/selftest.h index 3c343e1a761..5ca9bdcc598 100644 --- a/gdbsupport/selftest.h +++ b/gdbsupport/selftest.h @@ -21,6 +21,8 @@ =20 #include "gdbsupport/array-view.h" #include "gdbsupport/function-view.h" +#include "gdbsupport/iterator-range.h" +#include =20 /* A test is just a function that does some checks and throws an exception if something has gone wrong. */ @@ -28,6 +30,28 @@ namespace selftests { =20 +/* Selftests are registered under a unique name. */ + +struct selftest +{ + selftest (std::string name, std::function test) + : name { std::move (name) }, test { std::move (test) } + { } + bool operator< (const selftest &rhs) const + { return name < rhs.name; } + + std::string name; + std::function test; +}; + +/* Type of the container of all the registered selftests. */ +using selftests_registry =3D std::set; +using selftests_range =3D iterator_range; + +/* Create a range to iterate over all registered tests. */ + +selftests_range all_selftests (); + /* True if selftest should run verbosely. */ =20 extern bool run_verbose (); @@ -48,13 +72,6 @@ extern void run_tests (gdb::array_view filters, =20 /* Reset GDB or GDBserver's internal state. */ extern void reset (); - -using for_each_selftest_ftype - =3D gdb::function_view; - -/* Call FUNC for each registered selftest. */ - -extern void for_each_selftest (for_each_selftest_ftype func); } =20 /* Check that VALUE is true, and, if not, throw an exception. */