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/foreach/foreach.h>
18 static char *strip_spaces(const void *ctx, char *line)
20 char *p = talloc_strdup(ctx, line);
23 for (i = 0, j = 0; p[i]; i++) {
31 static bool has_dep(struct manifest *m, const char *depname, bool tap_ok)
35 if (tap_ok && streq(depname, "ccan/tap"))
38 /* We can include ourselves, of course. */
39 if (streq(depname + strlen("ccan/"), m->basename))
42 list_for_each(&m->dep_dirs, f, list) {
43 if (streq(f->name, depname))
49 static void *check_depends_accurate(struct manifest *m,
51 unsigned int *timeleft)
53 struct list_head *list;
54 char *report = talloc_strdup(m, "");
56 foreach_ptr(list, &m->c_files, &m->h_files,
57 &m->run_tests, &m->api_tests,
58 &m->compile_ok_tests, &m->compile_fail_tests,
59 &m->other_test_c_files) {
63 /* Including ccan/tap is fine for tests. */
64 tap_ok = (list != &m->c_files && list != &m->h_files);
66 list_for_each(list, f, list) {
68 char **lines = get_ccan_file_lines(f);
70 for (i = 0; lines[i]; i++) {
72 if (lines[i][strspn(lines[i], " \t")] != '#')
74 p = strip_spaces(f, lines[i]);
75 if (!strstarts(p, "#include<ccan/")
76 && !strstarts(p, "#include\"ccan/"))
78 p += strlen("#include\"");
79 if (!strchr(strchr(p, '/') + 1, '/'))
81 *strchr(strchr(p, '/') + 1, '/') = '\0';
82 if (!has_dep(m, p, tap_ok))
83 report = talloc_asprintf_append(report,
85 f->name, i+1, lines[i]);
90 if (streq(report, "")) {
97 static const char *describe_depends_accurage(struct manifest *m,
100 return talloc_asprintf(check_result,
101 "You include ccan modules you don't list as dependencies:\n"
102 "%s", (char *)check_result);
105 struct ccanlint depends_accurate = {
106 .key = "depends-accurate",
107 .name = "Module's CCAN dependencies are the only ccan files #included",
109 .check = check_depends_accurate,
110 .describe = describe_depends_accurage,
113 REGISTER_TEST(depends_accurate, &depends_exist, NULL);