d7082fac296c68730383542dce0a82085aee03a3
[ccan] / tools / ccanlint / tests / depends_accurate.c
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>
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 bool has_dep(struct manifest *m, char **deps, bool *used,
19                     const char *depname)
20 {
21         unsigned int i;
22
23         /* We can include ourselves, of course. */
24         if (streq(depname + strlen("ccan/"), m->modname))
25                 return true;
26
27         for (i = 0; deps[i]; i++) {
28                 if (streq(deps[i], depname)) {
29                         used[i] = true;
30                         return true;
31                 }
32         }
33         return false;
34 }
35
36 static bool check_dep_includes(struct manifest *m,
37                                char **deps, bool *used,
38                                struct score *score,
39                                struct ccan_file *f)
40 {
41         unsigned int i;
42         char **lines = get_ccan_file_lines(f);
43         struct line_info *li = get_ccan_line_info(f);
44         bool ok = true;
45
46         for (i = 0; lines[i]; i++) {
47                 char *mod;
48                 if (!tal_strreg(f, lines[i],
49                                 "^[ \t]*#[ \t]*include[ \t]*[<\"]"
50                                 "(ccan/+.+)/+[^/]+\\.h", &mod))
51                         continue;
52
53                 if (has_dep(m, deps, used, mod))
54                         continue;
55
56                 /* FIXME: we can't be sure about
57                  * conditional includes, so don't
58                  * complain. */
59                 if (!li[i].cond) {
60                         score_file_error(score, f, i+1,
61                                          "%s not listed in _info", mod);
62                         ok = false;
63                 }
64         }
65         return ok;
66 }
67
68 static void check_depends_accurate(struct manifest *m,
69                                    unsigned int *timeleft, struct score *score)
70 {
71         struct list_head *list;
72         unsigned int i, core_deps, test_deps;
73         char **deps, **tdeps;
74         bool *used;
75         bool ok = true;
76
77         /* Get the *direct* dependencies. */
78         if (safe_mode) {
79                 deps = get_safe_ccan_deps(m, m->dir, "depends", false);
80                 tdeps = get_safe_ccan_deps(m, m->dir, "testdepends", false);
81         } else {
82                 deps = get_deps(m, m->dir, "depends", false,
83                                 get_or_compile_info);
84                 tdeps = get_deps(m, m->dir, "testdepends", false,
85                                  get_or_compile_info);
86         }
87
88         core_deps = tal_count(deps) - 1;
89         test_deps = tal_count(tdeps) - 1;
90
91         used = tal_arrz(m, bool, core_deps + test_deps + 1);
92
93         foreach_ptr(list, &m->c_files, &m->h_files) {
94                 struct ccan_file *f;
95
96                 list_for_each(list, f, list)
97                         ok &= check_dep_includes(m, deps, used, score, f);
98         }
99
100         for (i = 0; i < core_deps; i++) {
101                 if (!used[i] && strstarts(deps[i], "ccan/"))
102                         score_file_error(score, m->info_file, 0,
103                                          "%s is an unused dependency",
104                                          deps[i]);
105         }
106
107         /* Now remove NUL and append test dependencies to deps. */
108         deps = tal_dup(m, char *, take(deps), core_deps, test_deps + 2);
109         memcpy(deps + core_deps, tdeps, sizeof(tdeps[0]) * test_deps);
110         /* ccan/tap is given a free pass. */
111         deps[core_deps + test_deps] = (char *)"ccan/tap";
112         deps[core_deps + test_deps + 1] = NULL;
113
114         foreach_ptr(list, &m->run_tests, &m->api_tests,
115                     &m->compile_ok_tests, &m->compile_fail_tests,
116                     &m->other_test_c_files) {
117                 struct ccan_file *f;
118
119                 list_for_each(list, f, list)
120                         ok &= check_dep_includes(m, deps, used, score, f);
121         }
122
123         for (i = core_deps; i < test_deps; i++) {
124                 if (!used[i])
125                         score_file_error(score, m->info_file, 0,
126                                          "%s is an unused test dependency",
127                                          deps[i]);
128         }
129
130         if (!score->error)
131                 score->score = score->total;
132
133         /* We don't count unused dependencies as an error (yet!) */
134         score->pass = ok;
135 }
136
137 struct ccanlint depends_accurate = {
138         .key = "depends_accurate",
139         .name = "Module's CCAN dependencies are the only CCAN files #included",
140         .check = check_depends_accurate,
141         .needs = "depends_exist test_depends_exist"
142 };
143
144 REGISTER_TEST(depends_accurate);