X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=tools%2Fccanlint%2Ftests%2Fdepends_accurate.c;h=05ada48a40dc8b829c361beca5ba9d17350abc6c;hp=a6d3a73a85dd94b6b4289f4c14dd1100b260f80b;hb=932d65dd6537250e617516749f03a00fea3b34f6;hpb=016a19d260cd7f4afeb5b2b2cc28c8bbed1cd170 diff --git a/tools/ccanlint/tests/depends_accurate.c b/tools/ccanlint/tests/depends_accurate.c index a6d3a73a..05ada48a 100644 --- a/tools/ccanlint/tests/depends_accurate.c +++ b/tools/ccanlint/tests/depends_accurate.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -15,82 +16,131 @@ #include #include -static char *strip_spaces(const void *ctx, char *line) +static bool has_dep(struct manifest *m, char **deps, bool *used, + const char *depname) { - char *p = talloc_strdup(ctx, line); - unsigned int i, j; + unsigned int i; - for (i = 0, j = 0; p[i]; i++) { - if (!isspace(p[i])) - p[j++] = p[i]; + /* We can include ourselves, of course. */ + if (streq(depname + strlen("ccan/"), m->modname)) + return true; + + for (i = 0; deps[i]; i++) { + if (streq(deps[i], depname)) { + used[i] = true; + return true; + } } - p[j] = '\0'; - return p; + return false; } -static bool has_dep(struct manifest *m, const char *depname) +static bool check_dep_includes(struct manifest *m, + char **deps, bool *used, + struct score *score, + struct ccan_file *f) { - struct manifest *i; + unsigned int i; + char **lines = get_ccan_file_lines(f); + struct line_info *li = get_ccan_line_info(f); + bool ok = true; - /* We can include ourselves, of course. */ - if (streq(depname, m->basename)) - return true; + for (i = 0; lines[i]; i++) { + char *mod; + if (!strreg(f, lines[i], + "^[ \t]*#[ \t]*include[ \t]*[<\"]" + "(ccan/+.+)/+[^/]+\\.h", &mod)) + continue; - list_for_each(&m->deps, i, list) { - if (streq(i->basename, depname)) - return true; + if (has_dep(m, deps, used, mod)) + continue; + + /* FIXME: we can't be sure about + * conditional includes, so don't + * complain. */ + if (!li[i].cond) { + score_file_error(score, f, i+1, + "%s not listed in _info", mod); + ok = false; + } } - return false; + return ok; } static void check_depends_accurate(struct manifest *m, - bool keep, unsigned int *timeleft, struct score *score) { struct list_head *list; + unsigned int i, core_deps, test_deps; + char **deps, **tdeps; + bool *used; + bool ok = true; + + /* Get the *direct* dependencies. */ + if (safe_mode) { + deps = get_safe_ccan_deps(m, m->dir, "depends", false); + tdeps = get_safe_ccan_deps(m, m->dir, "testdepends", false); + } else { + deps = get_deps(m, m->dir, "depends", false, + get_or_compile_info); + tdeps = get_deps(m, m->dir, "testdepends", false, + get_or_compile_info); + } - foreach_ptr(list, &m->c_files, &m->h_files, - &m->run_tests, &m->api_tests, + core_deps = talloc_array_length(deps) - 1; + test_deps = talloc_array_length(tdeps) - 1; + + used = talloc_zero_array(m, bool, core_deps + test_deps + 1); + + foreach_ptr(list, &m->c_files, &m->h_files) { + struct ccan_file *f; + + list_for_each(list, f, list) + ok &= check_dep_includes(m, deps, used, score, f); + } + + for (i = 0; i < core_deps; i++) { + if (!used[i]) + score_file_error(score, m->info_file, 0, + "%s is an unused dependency", + deps[i]); + } + + /* Now append test dependencies to deps. */ + deps = talloc_realloc(NULL, deps, char *, + (core_deps + test_deps + 1) * sizeof(char *)); + memcpy(&deps[core_deps], tdeps, test_deps * sizeof(char *)); + /* ccan/tap is given a free pass. */ + deps[core_deps + test_deps] = (char *)"ccan/tap"; + deps[core_deps + test_deps + 1] = NULL; + + foreach_ptr(list, &m->run_tests, &m->api_tests, &m->compile_ok_tests, &m->compile_fail_tests, &m->other_test_c_files) { struct ccan_file *f; - list_for_each(list, f, list) { - unsigned int i; - char **lines = get_ccan_file_lines(f); - - for (i = 0; lines[i]; i++) { - char *p; - if (lines[i][strspn(lines[i], " \t")] != '#') - continue; - p = strip_spaces(f, lines[i]); - if (!strstarts(p, "#includeerror = "Includes a ccan module" - " not listed in _info"; - score_file_error(score, f, i+1, lines[i]); - } - } + list_for_each(list, f, list) + ok &= check_dep_includes(m, deps, used, score, f); } - if (!score->error) { - score->pass = true; - score->score = score->total; + for (i = core_deps; i < test_deps; i++) { + if (!used[i]) + score_file_error(score, m->info_file, 0, + "%s is an unused test dependency", + deps[i]); } + + if (!score->error) + score->score = score->total; + + /* We don't count unused dependencies as an error (yet!) */ + score->pass = ok; } struct ccanlint depends_accurate = { .key = "depends_accurate", .name = "Module's CCAN dependencies are the only CCAN files #included", .check = check_depends_accurate, - .needs = "depends_exist" + .needs = "depends_exist test_depends_exist" }; REGISTER_TEST(depends_accurate);