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
/* 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. */
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 ());
}
#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. */
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.