1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/str/str.h>
5 #include <ccan/str_talloc/str_talloc.h>
6 #include <ccan/foreach/foreach.h>
19 static bool has_dep(struct manifest *m, char **deps, bool *used,
24 /* We can include ourselves, of course. */
25 if (streq(depname + strlen("ccan/"), m->modname))
28 for (i = 0; deps[i]; i++) {
29 if (streq(deps[i], depname)) {
37 static bool check_dep_includes(struct manifest *m,
38 char **deps, bool *used,
43 char **lines = get_ccan_file_lines(f);
44 struct line_info *li = get_ccan_line_info(f);
47 for (i = 0; lines[i]; i++) {
49 if (!strreg(f, lines[i],
50 "^[ \t]*#[ \t]*include[ \t]*[<\"]"
51 "(ccan/+.+)/+[^/]+.h", &mod))
54 if (has_dep(m, deps, used, mod))
57 /* FIXME: we can't be sure about
58 * conditional includes, so don't
61 score_file_error(score, f, i+1,
62 "%s not listed in _info", mod);
69 static void check_depends_accurate(struct manifest *m,
70 unsigned int *timeleft, struct score *score)
72 struct list_head *list;
73 unsigned int i, core_deps, test_deps;
78 /* Get the *direct* dependencies. */
80 deps = get_safe_ccan_deps(m, m->dir, "depends", false);
81 tdeps = get_safe_ccan_deps(m, m->dir, "testdepends", false);
83 deps = get_deps(m, m->dir, "depends", false,
85 tdeps = get_deps(m, m->dir, "testdepends", false,
89 core_deps = talloc_array_length(deps) - 1;
90 test_deps = talloc_array_length(tdeps) - 1;
92 used = talloc_zero_array(m, bool, core_deps + test_deps + 1);
94 foreach_ptr(list, &m->c_files, &m->h_files) {
97 list_for_each(list, f, list)
98 ok &= check_dep_includes(m, deps, used, score, f);
101 for (i = 0; i < core_deps; i++) {
103 score_file_error(score, m->info_file, 0,
104 "%s is an unused dependency",
108 /* Now append test dependencies to deps. */
109 deps = talloc_realloc(NULL, deps, char *,
110 (core_deps + test_deps + 1) * sizeof(char *));
111 memcpy(&deps[core_deps], tdeps, test_deps * sizeof(char *));
112 /* ccan/tap is given a free pass. */
113 deps[core_deps + test_deps] = (char *)"ccan/tap";
114 deps[core_deps + test_deps + 1] = NULL;
116 foreach_ptr(list, &m->run_tests, &m->api_tests,
117 &m->compile_ok_tests, &m->compile_fail_tests,
118 &m->other_test_c_files) {
121 list_for_each(list, f, list)
122 ok &= check_dep_includes(m, deps, used, score, f);
125 for (i = core_deps; i < test_deps; i++) {
127 score_file_error(score, m->info_file, 0,
128 "%s is an unused test dependency",
133 score->score = score->total;
135 /* We don't count unused dependencies as an error (yet!) */
139 struct ccanlint depends_accurate = {
140 .key = "depends_accurate",
141 .name = "Module's CCAN dependencies are the only CCAN files #included",
142 .check = check_depends_accurate,
143 .needs = "depends_exist test_depends_exist"
146 REGISTER_TEST(depends_accurate);