From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7833) id BD2D23858D1E; Tue, 19 Apr 2022 08:17:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BD2D23858D1E 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: Allow lazy registration X-Act-Checkin: binutils-gdb X-Git-Author: Lancelot SIX X-Git-Refname: refs/heads/master X-Git-Oldrev: c57207c15c4fa980263e6849d0e6472c33e647fc X-Git-Newrev: 9a0f7f634e8eb98a8b2ff48eb68f90fefed83ac1 Message-Id: <20220419081756.BD2D23858D1E@sourceware.org> Date: Tue, 19 Apr 2022 08:17:56 +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:56 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D9a0f7f634e8e= b98a8b2ff48eb68f90fefed83ac1 commit 9a0f7f634e8eb98a8b2ff48eb68f90fefed83ac1 Author: Lancelot SIX Date: Tue Mar 22 08:09:40 2022 -0400 gdbsupport/selftest: Allow lazy registration =20 This patch adds a way to delay the registration of tests until the latest possible moment. This is intended for situations where GDB needs to be fully initialized in order to decide if a particular selftest can be executed or not. =20 This mechanism will be used in the next patch. =20 Change-Id: I7f6b061f4c0a6832226c7080ab4e3a2523e1b0b0 Diff: --- gdbsupport/selftest.cc | 21 +++++++++++++++++++++ gdbsupport/selftest.h | 11 +++++++++++ 2 files changed, 32 insertions(+) diff --git a/gdbsupport/selftest.cc b/gdbsupport/selftest.cc index 7077f113f97..1b862bb9ce3 100644 --- a/gdbsupport/selftest.cc +++ b/gdbsupport/selftest.cc @@ -30,6 +30,11 @@ namespace selftests =20 static selftests_registry tests; =20 +/* Set of callback functions used to register selftests after GDB is fully + initialized. */ + +static std::vector lazy_generators; + /* See selftest.h. */ =20 void @@ -44,6 +49,14 @@ register_test (const std::string &name, =20 /* See selftest.h. */ =20 +void +add_lazy_generator (selftests_generator generator) +{ + lazy_generators.push_back (std::move (generator)); +} + +/* See selftest.h. */ + static bool run_verbose_ =3D false; =20 /* See selftest.h. */ @@ -104,6 +117,14 @@ run_tests (gdb::array_view filters,= bool verbose) selftests_range all_selftests () { + /* Execute any function which might still want to register tests. Once = each + function has been executed, clear lazy_generators to ensure that + callback functions are only executed once. */ + for (const auto &generator : lazy_generators) + for (selftest &test : generator ()) + register_test (std::move (test.name), std::move (test.test)); + lazy_generators.clear (); + return selftests_range (tests.cbegin (), tests.cend ()); } =20 diff --git a/gdbsupport/selftest.h b/gdbsupport/selftest.h index 5ca9bdcc598..661431dbfca 100644 --- a/gdbsupport/selftest.h +++ b/gdbsupport/selftest.h @@ -23,6 +23,7 @@ #include "gdbsupport/function-view.h" #include "gdbsupport/iterator-range.h" #include +#include =20 /* A test is just a function that does some checks and throws an exception if something has gone wrong. */ @@ -61,6 +62,16 @@ extern bool run_verbose (); extern void register_test (const std::string &name, std::function function); =20 +/* A selftest generator is a callback function used to delay the generation + of selftests. */ + +using selftests_generator =3D std::function (void)>; + +/* Register a function which can lazily register selftests once GDB is ful= ly + initialized. */ + +extern void add_lazy_generator (selftests_generator generator); + /* Run all the self tests. This print a message describing the number of test and the number of failures.