3ba19f60a3240e0385dd720b509887bd4e4b493e
[ccan] / tools / ccanlint / tests / depends_accurate.c
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>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <limits.h>
11 #include <errno.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <err.h>
15 #include <string.h>
16 #include <ctype.h>
17
18 static char *strip_spaces(const void *ctx, char *line)
19 {
20         char *p = talloc_strdup(ctx, line);
21         unsigned int i, j;
22
23         for (i = 0, j = 0; p[i]; i++) {
24                 if (!isspace(p[i]))
25                         p[j++] = p[i];
26         }
27         p[j] = '\0';
28         return p;
29 }
30
31 static bool has_dep(struct manifest *m, const char *depname, bool tap_ok)
32 {
33         struct ccan_file *f;
34
35         if (tap_ok && streq(depname, "ccan/tap"))
36                 return true;
37
38         /* We can include ourselves, of course. */
39         if (streq(depname + strlen("ccan/"), m->basename))
40                 return true;
41
42         list_for_each(&m->dep_dirs, f, list) {
43                 if (streq(f->name, depname))
44                         return true;
45         }
46         return false;
47 }
48
49 static void check_depends_accurate(struct manifest *m,
50                                    bool keep,
51                                    unsigned int *timeleft, struct score *score)
52 {
53         struct list_head *list;
54
55         foreach_ptr(list, &m->c_files, &m->h_files,
56                     &m->run_tests, &m->api_tests,
57                     &m->compile_ok_tests, &m->compile_fail_tests,
58                     &m->other_test_c_files) {
59                 struct ccan_file *f;
60                 bool tap_ok;
61
62                 /* Including ccan/tap is fine for tests. */
63                 tap_ok = (list != &m->c_files && list != &m->h_files);
64
65                 list_for_each(list, f, list) {
66                         unsigned int i;
67                         char **lines = get_ccan_file_lines(f);
68
69                         for (i = 0; lines[i]; i++) {
70                                 char *p;
71                                 if (lines[i][strspn(lines[i], " \t")] != '#')
72                                         continue;
73                                 p = strip_spaces(f, lines[i]);
74                                 if (!strstarts(p, "#include<ccan/")
75                                     && !strstarts(p, "#include\"ccan/"))
76                                         continue;
77                                 p += strlen("#include\"");
78                                 if (!strchr(strchr(p, '/') + 1, '/'))
79                                         continue;
80                                 *strchr(strchr(p, '/') + 1, '/') = '\0';
81                                 if (has_dep(m, p, tap_ok))
82                                         continue;
83                                 score->error = "Includes a ccan module"
84                                         " not listed in _info";
85                                 score_file_error(score, f, i+1, lines[i]);
86                         }
87                 }
88         }
89
90         if (!score->error) {
91                 score->pass = true;
92                 score->score = score->total;
93         }
94 }
95
96 struct ccanlint depends_accurate = {
97         .key = "depends-accurate",
98         .name = "Module's CCAN dependencies are the only ccan files #included",
99         .check = check_depends_accurate,
100 };
101
102 REGISTER_TEST(depends_accurate, &depends_exist, NULL);