]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/depends_accurate.c
08760333eafcb6c0d757fc6f5c2e234c0137633a
[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 conditional includes,
57                  * so don't complain (handle common case of idempotent wrap) */
58                 if (!li[i].cond || li[i].cond == f->idempotent_cond) {
59                         score_file_error(score, f, i+1,
60                                          "%s not listed in _info", mod);
61                         ok = false;
62                 }
63         }
64         return ok;
65 }
66
67 static void check_depends_accurate(struct manifest *m,
68                                    unsigned int *timeleft, struct score *score)
69 {
70         struct list_head *list;
71         unsigned int i, core_deps, test_deps;
72         char **deps, **tdeps;
73         bool *used;
74         bool ok = true;
75
76         /* Get the *direct* dependencies. */
77         if (safe_mode) {
78                 deps = get_safe_ccan_deps(m, m->dir, "depends", false);
79                 tdeps = get_safe_ccan_deps(m, m->dir, "testdepends", false);
80         } else {
81                 deps = get_deps(m, m->dir, "depends", false,
82                                 get_or_compile_info);
83                 tdeps = get_deps(m, m->dir, "testdepends", false,
84                                  get_or_compile_info);
85         }
86
87         core_deps = tal_count(deps) - 1;
88         test_deps = tal_count(tdeps) - 1;
89
90         used = tal_arrz(m, bool, core_deps + test_deps + 1);
91
92         foreach_ptr(list, &m->c_files, &m->h_files) {
93                 struct ccan_file *f;
94
95                 list_for_each(list, f, list)
96                         ok &= check_dep_includes(m, deps, used, score, f);
97         }
98
99         for (i = 0; i < core_deps; i++) {
100                 if (!used[i] && strstarts(deps[i], "ccan/"))
101                         score_file_error(score, m->info_file, 0,
102                                          "%s is an unused dependency",
103                                          deps[i]);
104         }
105
106         /* Now remove NUL and append test dependencies to deps. */
107         deps = tal_dup_arr(m, char *, take(deps), core_deps, test_deps + 2);
108         memcpy(deps + core_deps, tdeps, sizeof(tdeps[0]) * test_deps);
109         /* ccan/tap is given a free pass. */
110         deps[core_deps + test_deps] = (char *)"ccan/tap";
111         deps[core_deps + test_deps + 1] = NULL;
112
113         foreach_ptr(list, &m->run_tests, &m->api_tests,
114                     &m->compile_ok_tests, &m->compile_fail_tests,
115                     &m->other_test_c_files) {
116                 struct ccan_file *f;
117
118                 list_for_each(list, f, list)
119                         ok &= check_dep_includes(m, deps, used, score, f);
120         }
121
122         for (i = core_deps; i < test_deps; i++) {
123                 if (!used[i])
124                         score_file_error(score, m->info_file, 0,
125                                          "%s is an unused test dependency",
126                                          deps[i]);
127         }
128
129         if (!score->error)
130                 score->score = score->total;
131
132         /* We don't count unused dependencies as an error (yet!) */
133         score->pass = ok;
134 }
135
136 struct ccanlint depends_accurate = {
137         .key = "depends_accurate",
138         .name = "Module's CCAN dependencies are the only CCAN files #included",
139         .check = check_depends_accurate,
140         .needs = "depends_exist test_depends_exist headers_idempotent"
141 };
142
143 REGISTER_TEST(depends_accurate);