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 (strstarts(depname + strlen("ccan/"), m->modname)) {
25 const char *p = depname + strlen("ccan/") + strlen(m->modname);
26 /* And our own tests! */
27 if (streq(p, "/test") || streq(p, ""))
29 /* But not any submodules; they need an explicit dep */
32 for (i = 0; deps[i]; i++) {
33 if (streq(deps[i], depname)) {
41 static bool check_dep_includes(struct manifest *m,
42 char **deps, bool *used,
47 char **lines = get_ccan_file_lines(f);
48 struct line_info *li = get_ccan_line_info(f);
51 for (i = 0; lines[i]; i++) {
53 if (!tal_strreg(f, lines[i],
54 "^[ \t]*#[ \t]*include[ \t]*[<\"]"
55 "(ccan/+.+)/+[^/]+\\.[ch][\">]", &mod))
58 if (has_dep(m, deps, used, mod))
61 /* FIXME: we can't be sure about conditional includes,
62 * so don't complain (handle common case of idempotent wrap) */
63 if (!li[i].cond || li[i].cond == f->idempotent_cond) {
64 score_file_error(score, f, i+1,
65 "%s not listed in _info", mod);
72 static void check_depends_accurate(struct manifest *m,
73 unsigned int *timeleft UNNEEDED,
76 struct list_head *list;
77 unsigned int i, core_deps, test_deps;
82 /* Get the *direct* dependencies. */
84 deps = get_safe_ccan_deps(m, m->dir, "depends", false);
85 tdeps = get_safe_ccan_deps(m, m->dir, "testdepends", false);
87 deps = get_deps(m, m->dir, "depends", false,
89 tdeps = get_deps(m, m->dir, "testdepends", false,
93 core_deps = tal_count(deps) - 1;
94 test_deps = tal_count(tdeps) - 1;
96 used = tal_arrz(m, bool, core_deps + test_deps + 1);
98 foreach_ptr(list, &m->c_files, &m->h_files) {
101 list_for_each(list, f, list)
102 ok &= check_dep_includes(m, deps, used, score, f);
105 for (i = 0; i < core_deps; i++) {
106 if (!used[i] && strstarts(deps[i], "ccan/"))
107 score_file_error(score, m->info_file, 0,
108 "%s is an unused dependency",
112 /* Now remove NUL and append test dependencies to deps. */
113 deps = tal_dup_arr(m, char *, take(deps), core_deps, test_deps + 2);
114 memcpy(deps + core_deps, tdeps, sizeof(tdeps[0]) * test_deps);
115 /* ccan/tap is given a free pass. */
116 deps[core_deps + test_deps] = (char *)"ccan/tap";
117 deps[core_deps + test_deps + 1] = NULL;
119 foreach_ptr(list, &m->run_tests, &m->api_tests,
120 &m->compile_ok_tests, &m->compile_fail_tests,
121 &m->other_test_c_files) {
124 list_for_each(list, f, list)
125 ok &= check_dep_includes(m, deps, used, score, f);
128 for (i = core_deps; i < test_deps; i++) {
130 score_file_error(score, m->info_file, 0,
131 "%s is an unused test dependency",
136 score->score = score->total;
138 /* We don't count unused dependencies as an error (yet!) */
142 struct ccanlint depends_accurate = {
143 .key = "depends_accurate",
144 .name = "Module's CCAN dependencies are the only CCAN files #included",
145 .check = check_depends_accurate,
146 .needs = "depends_exist info_compiles test_depends_exist headers_idempotent"
149 REGISTER_TEST(depends_accurate);