+ score = talloc(m, struct score);
+ list_head_init(&score->per_file_errors);
+ score->error = NULL;
+ score->pass = false;
+ score->score = 0;
+ score->total = 1;
+
+ skip = should_skip(m, i);
+
+ if (skip) {
+ skip:
+ if (verbose && !streq(skip, "not relevant to target"))
+ printf("%s: skipped (%s)\n", i->name, skip);
+
+ /* If we're skipping this because a prereq failed, we fail:
+ * count it as a score of 1. */
+ if (i->skip_fail)
+ (*running_total)++;
+
+ list_del(&i->list);
+ list_add_tail(&finished_tests, &i->list);
+ list_for_each(&i->dependencies, d, node) {
+ if (d->dependent->skip)
+ continue;
+ d->dependent->skip = "dependency was skipped";
+ d->dependent->skip_fail = i->skip_fail;
+ }
+ return i->skip_fail ? false : true;
+ }
+
+ timeleft = timeout ? timeout : default_timeout_ms;
+ i->check(m, i->keep_results, &timeleft, score);
+ if (timeout && timeleft == 0) {
+ skip = "timeout";
+ goto skip;
+ }
+
+ assert(score->score <= score->total);
+ if ((!score->pass && !quiet)
+ || (score->score < score->total && verbose)
+ || verbose > 1) {
+ printf("%s (%s): %s", i->name, i->key, score->pass ? "PASS" : "FAIL");
+ if (score->total > 1)
+ printf(" (+%u/%u)", score->score, score->total);
+ printf("\n");
+ }
+
+ if ((!quiet && !score->pass) || verbose) {
+ if (score->error) {
+ printf("%s%s", score->error,
+ strends(score->error, "\n") ? "" : "\n");
+ }
+ }
+ if (!quiet && score->score < score->total && i->handle)
+ i->handle(m, score);
+
+ *running_score += score->score;
+ *running_total += score->total;
+
+ list_del(&i->list);
+ list_add_tail(&finished_tests, &i->list);
+
+ if (!score->pass) {
+ /* Skip any tests which depend on this one. */
+ list_for_each(&i->dependencies, d, node) {
+ if (d->dependent->skip)
+ continue;
+ d->dependent->skip = "dependency failed";
+ d->dependent->skip_fail = true;
+ }
+ }
+ return score->pass;
+}
+
+static void register_test(struct list_head *h, struct ccanlint *test)
+{
+ list_add(h, &test->list);
+ test->options = talloc_array(NULL, char *, 1);
+ test->options[0] = NULL;
+}
+
+/**
+ * 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 struct ccanlint *find_test(const char *key)
+{
+ struct ccanlint *i;
+
+ list_for_each(&compulsory_tests, i, list)
+ if (streq(i->key, key))
+ return i;
+
+ list_for_each(&normal_tests, i, list)
+ if (streq(i->key, key))
+ return i;
+
+ return NULL;
+}
+
+bool is_excluded(const char *name)
+{
+ return btree_lookup(cmdline_exclude, name) != NULL
+ || btree_lookup(info_exclude, name) != NULL
+ || find_test(name)->skip != NULL;
+}
+
+#undef REGISTER_TEST
+#define REGISTER_TEST(name, ...) extern struct ccanlint name
+#include "generated-normal-tests"
+#include "generated-compulsory-tests"
+
+static void init_tests(void)
+{
+ struct ccanlint *c;
+ struct btree *keys, *names;
+ struct list_head *list;
+
+#undef REGISTER_TEST
+#define REGISTER_TEST(name) register_test(&normal_tests, &name)
+#include "generated-normal-tests"
+#undef REGISTER_TEST
+#define REGISTER_TEST(name) register_test(&compulsory_tests, &name)
+#include "generated-compulsory-tests"
+
+ /* Initialize dependency lists. */
+ foreach_ptr(list, &compulsory_tests, &normal_tests) {
+ list_for_each(list, c, list) {
+ list_head_init(&c->dependencies);
+ }
+ }
+
+ /* Resolve dependencies. */
+ foreach_ptr(list, &compulsory_tests, &normal_tests) {
+ list_for_each(list, c, list) {
+ char **deps = strsplit(NULL, c->needs, " ");
+ unsigned int i;
+
+ for (i = 0; deps[i]; i++) {
+ struct ccanlint *dep;
+ struct dependent *dchild;
+
+ dep = find_test(deps[i]);
+ if (!dep)
+ errx(1, "BUG: unknown dep '%s' for %s",
+ deps[i], c->key);
+ dchild = talloc(NULL, struct dependent);
+ dchild->dependent = c;
+ list_add_tail(&dep->dependencies,
+ &dchild->node);
+ c->num_depends++;
+ }
+ talloc_free(deps);
+ }
+ }
+
+ /* Self-consistency check: make sure no two tests
+ have the same key or name. */
+ keys = btree_new(btree_strcmp);
+ names = btree_new(btree_strcmp);
+ foreach_ptr(list, &compulsory_tests, &normal_tests) {
+ list_for_each(list, c, list) {
+ if (!btree_insert(keys, c->key))
+ errx(1, "BUG: Duplicate test key '%s'",
+ c->key);
+ if (!btree_insert(names, c->name))
+ errx(1, "BUG: Duplicate test name '%s'",
+ c->name);
+ }
+ }
+ btree_delete(keys);
+ btree_delete(names);
+}
+
+static void print_test_depends(void)
+{
+ struct list_head *list;
+
+ foreach_ptr(list, &compulsory_tests, &normal_tests) {
+ struct ccanlint *c;
+ printf("\%s Tests\n",
+ list == &compulsory_tests ? "Compulsory" : "Normal");
+
+ list_for_each(list, c, list) {
+ if (!list_empty(&c->dependencies)) {
+ const struct dependent *d;
+ printf("These depend on %s:\n", c->key);
+ list_for_each(&c->dependencies, d, node)
+ printf("\t%s\n", d->dependent->key);
+ }
+ }
+ }
+}
+
+static int show_tmpdir(const char *dir)
+{
+ printf("You can find ccanlint working files in '%s'\n", dir);
+ return 0;
+}
+
+static char *keep_test(const char *testname, void *unused)
+{
+ struct ccanlint *i;
+
+ if (streq(testname, "all")) {
+ struct list_head *list;
+ foreach_ptr(list, &compulsory_tests, &normal_tests) {
+ list_for_each(list, i, list)
+ i->keep_results = true;
+ }
+ } else {
+ i = find_test(testname);
+ if (!i)
+ errx(1, "No test %s to --keep", testname);
+ i->keep_results = true;
+ }
+
+ /* Don't automatically destroy temporary dir. */
+ talloc_set_destructor(temp_dir(NULL), show_tmpdir);
+ return NULL;
+}
+
+static char *skip_test(const char *testname, void *unused)
+{
+ btree_insert(cmdline_exclude, testname);
+ return NULL;
+}
+
+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 char *list_tests(void *arg)
+{
+ print_tests(&compulsory_tests, "Compulsory");
+ print_tests(&normal_tests, "Normal");
+ exit(0);
+}
+
+static void test_dgraph_vertices(struct list_head *tests, const char *style)
+{
+ const struct ccanlint *i;
+
+ list_for_each(tests, i, list) {
+ /*
+ * todo: escape labels in case ccanlint test keys have
+ * characters interpreted as GraphViz syntax.
+ */
+ printf("\t\"%p\" [label=\"%s\"%s]\n", i, i->key, style);