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