+static void register_test(struct list_head *h, struct ccanlint *test, ...)
+{
+ va_list ap;
+ struct ccanlint *depends;
+ struct dependent *dchild;
+
+ list_add(h, &test->list);
+
+ va_start(ap, test);
+ /* Careful: we might have been initialized by a dependent. */
+ if (test->dependencies.n.next == NULL)
+ list_head_init(&test->dependencies);
+
+ //dependent(s) args (if any), last one is NULL
+ while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
+ dchild = malloc(sizeof(*dchild));
+ dchild->dependent = test;
+ /* The thing we depend on might not be initialized yet! */
+ if (depends->dependencies.n.next == NULL)
+ list_head_init(&depends->dependencies);
+ list_add_tail(&depends->dependencies, &dchild->node);
+ test->num_depends++;
+ }
+ va_end(ap);
+}
+
+/**
+ * get_next_test - retrieves the next test to be processed
+ **/
+static inline struct ccanlint *get_next_test(struct list_head *test)
+{
+ struct ccanlint *i;
+
+ if (list_empty(test))
+ return NULL;
+
+ list_for_each(test, i, list) {
+ if (i->num_depends == 0)
+ return i;
+ }
+ errx(1, "Can't make process; test dependency cycle");
+}
+
+static void init_tests(void)
+{
+ const struct ccanlint *i;
+ struct btree *keys, *names;
+
+#undef REGISTER_TEST
+#define REGISTER_TEST(name, ...) register_test(&normal_tests, &name, __VA_ARGS__)
+#include "generated-normal-tests"
+#undef REGISTER_TEST
+#define REGISTER_TEST(name, ...) register_test(&compulsory_tests, &name, __VA_ARGS__)
+#include "generated-compulsory-tests"
+
+ /* Self-consistency check: make sure no two tests
+ have the same key or name. */
+ keys = btree_new(btree_strcmp);
+ names = btree_new(btree_strcmp);
+ list_for_each(&compulsory_tests, i, list) {
+ if (!btree_insert(keys, i->key))
+ errx(1, "BUG: Duplicate test key '%s'", i->key);
+ if (!btree_insert(keys, i->name))
+ errx(1, "BUG: Duplicate test name '%s'", i->name);
+ }
+ list_for_each(&normal_tests, i, list) {
+ if (!btree_insert(keys, i->key))
+ errx(1, "BUG: Duplicate test key '%s'", i->key);
+ if (!btree_insert(keys, i->name))
+ errx(1, "BUG: Duplicate test name '%s'", i->name);
+ }
+ btree_delete(keys);
+ btree_delete(names);
+
+ if (!verbose)
+ return;
+
+ printf("\nCompulsory Tests\n");
+ list_for_each(&compulsory_tests, i, list) {
+ printf("%s depends on %u others\n", i->name, i->num_depends);
+ if (!list_empty(&i->dependencies)) {
+ const struct dependent *d;
+ printf("These depend on us:\n");
+ list_for_each(&i->dependencies, d, node)
+ printf("\t%s\n", d->dependent->name);
+ }
+ }
+
+ printf("\nNormal Tests\n");
+ list_for_each(&normal_tests, i, list) {
+ printf("%s depends on %u others\n", i->name, i->num_depends);
+ if (!list_empty(&i->dependencies)) {
+ const struct dependent *d;
+ printf("These depend on us:\n");
+ list_for_each(&i->dependencies, d, node)
+ printf("\t%s\n", d->dependent->name);
+ }
+ }
+}
+
+static void print_tests(struct list_head *tests, const char *type)
+{
+ struct ccanlint *i;
+
+ printf("%s tests:\n", type);
+ /* This makes them print in topological order. */
+ while ((i = get_next_test(tests)) != NULL) {
+ const struct dependent *d;
+ printf(" %-25s %s\n", i->key, i->name);
+ list_del(&i->list);
+ list_for_each(&i->dependencies, d, node)
+ d->dependent->num_depends--;
+ }
+}
+
+static void list_tests(void)
+{
+ init_tests();
+ print_tests(&compulsory_tests, "Compulsory");
+ print_tests(&normal_tests, "Normal");
+ exit(0);
+}
+