1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/str/str.h>
4 #include <ccan/take/take.h>
5 #include <ccan/foreach/foreach.h>
18 static bool has_dep(struct manifest *m, char **deps, bool *used,
23 /* We can include ourselves, of course. */
24 if (streq(depname + strlen("ccan/"), m->modname))
27 for (i = 0; deps[i]; i++) {
28 if (streq(deps[i], depname)) {
36 static bool check_dep_includes(struct manifest *m,
37 char **deps, bool *used,
42 char **lines = get_ccan_file_lines(f);
43 struct line_info *li = get_ccan_line_info(f);
46 for (i = 0; lines[i]; i++) {
48 if (!tal_strreg(f, lines[i],
49 "^[ \t]*#[ \t]*include[ \t]*[<\"]"
50 "(ccan/+.+)/+[^/]+\\.h", &mod))
53 if (has_dep(m, deps, used, mod))
56 /* FIXME: we can't be sure about conditional includes,
57 * so don't complain (handle common case of idempotent wrap) */
58 if (!li[i].cond || li[i].cond == f->idempotent_cond) {
59 score_file_error(score, f, i+1,
60 "%s not listed in _info", mod);
67 static void check_depends_accurate(struct manifest *m,
68 unsigned int *timeleft, struct score *score)
70 struct list_head *list;
71 unsigned int i, core_deps, test_deps;
76 /* Get the *direct* dependencies. */
78 deps = get_safe_ccan_deps(m, m->dir, "depends", false);
79 tdeps = get_safe_ccan_deps(m, m->dir, "testdepends", false);
81 deps = get_deps(m, m->dir, "depends", false,
83 tdeps = get_deps(m, m->dir, "testdepends", false,
87 core_deps = tal_count(deps) - 1;
88 test_deps = tal_count(tdeps) - 1;
90 used = tal_arrz(m, bool, core_deps + test_deps + 1);
92 foreach_ptr(list, &m->c_files, &m->h_files) {
95 list_for_each(list, f, list)
96 ok &= check_dep_includes(m, deps, used, score, f);
99 for (i = 0; i < core_deps; i++) {
100 if (!used[i] && strstarts(deps[i], "ccan/"))
101 score_file_error(score, m->info_file, 0,
102 "%s is an unused dependency",
106 /* Now remove NUL and append test dependencies to deps. */
107 deps = tal_dup_arr(m, char *, take(deps), core_deps, test_deps + 2);
108 memcpy(deps + core_deps, tdeps, sizeof(tdeps[0]) * test_deps);
109 /* ccan/tap is given a free pass. */
110 deps[core_deps + test_deps] = (char *)"ccan/tap";
111 deps[core_deps + test_deps + 1] = NULL;
113 foreach_ptr(list, &m->run_tests, &m->api_tests,
114 &m->compile_ok_tests, &m->compile_fail_tests,
115 &m->other_test_c_files) {
118 list_for_each(list, f, list)
119 ok &= check_dep_includes(m, deps, used, score, f);
122 for (i = core_deps; i < test_deps; i++) {
124 score_file_error(score, m->info_file, 0,
125 "%s is an unused test dependency",
130 score->score = score->total;
132 /* We don't count unused dependencies as an error (yet!) */
136 struct ccanlint depends_accurate = {
137 .key = "depends_accurate",
138 .name = "Module's CCAN dependencies are the only CCAN files #included",
139 .check = check_depends_accurate,
140 .needs = "depends_exist test_depends_exist headers_idempotent"
143 REGISTER_TEST(depends_accurate);