From: Lancelot SIX <lancelot.six@amd.com>
To: <gdb-patches@sourceware.org>
Cc: <lsix@lancelotsix.com>, Lancelot SIX <lancelot.six@amd.com>
Subject: [PATCH 2/3] gdbsupport/selftest: Allow lazy registration
Date: Fri, 1 Apr 2022 14:00:14 +0100 [thread overview]
Message-ID: <20220401130015.513187-3-lancelot.six@amd.com> (raw)
In-Reply-To: <20220401130015.513187-1-lancelot.six@amd.com>
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.
This mechanism will be used in the next patch.
Change-Id: I7f6b061f4c0a6832226c7080ab4e3a2523e1b0b0
---
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
static selftests_registry tests;
+/* Set of callback functions used to register selftests after GDB is fully
+ initialized. */
+
+static std::vector<selftests_generator> lazy_generators;
+
/* See selftest.h. */
void
@@ -44,6 +49,14 @@ register_test (const std::string &name,
/* See selftest.h. */
+void
+add_lazy_generator (selftests_generator generator)
+{
+ lazy_generators.push_back (std::move (generator));
+}
+
+/* See selftest.h. */
+
static bool run_verbose_ = false;
/* See selftest.h. */
@@ -104,6 +117,14 @@ run_tests (gdb::array_view<const char *const> 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 ());
}
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 <set>
+#include <vector>
/* 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<void(void)> function);
+/* A selftest generator is a callback function used to delay the generation
+ of selftests. */
+
+using selftests_generator = std::function<std::vector<selftest> (void)>;
+
+/* Register a function which can lazily register selftests once GDB is fully
+ 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.
--
2.25.1
next prev parent reply other threads:[~2022-04-01 13:00 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-01 13:00 [PATCH 0/3] Fix register_test_foreach_arch Lancelot SIX
2022-04-01 13:00 ` [PATCH 1/3] gdbsupport/selftest: Replace for_each_selftest with an iterator_range Lancelot SIX
2022-04-01 13:00 ` Lancelot SIX [this message]
2022-04-01 13:00 ` [PATCH 3/3] gdb/selftest-arch: Make register_test_foreach_arch generate arch tests lazily Lancelot SIX
2022-04-15 17:52 ` Tom Tromey
2022-04-15 18:10 ` Lancelot SIX
2022-04-15 18:50 ` Tom Tromey
2022-04-19 18:55 ` Simon Marchi
2022-04-19 21:10 ` Lancelot SIX
2022-04-12 8:17 ` [PING] [PATCH 0/3] Fix register_test_foreach_arch Lancelot SIX
2022-04-15 17:53 ` Tom Tromey
2022-04-19 8:18 ` Lancelot SIX
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220401130015.513187-3-lancelot.six@amd.com \
--to=lancelot.six@amd.com \
--cc=gdb-patches@sourceware.org \
--cc=lsix@lancelotsix.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).