utf8: don't allow NUL in decoded strings.
[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 UNNEEDED,
74                                    struct score *score)
75 {
76         struct list_head *list;
77         unsigned int i, core_deps, test_deps;
78         char **deps, **tdeps;
79         bool *used;
80         bool ok = true;
81
82         /* Get the *direct* dependencies. */
83         if (safe_mode) {
84                 deps = get_safe_ccan_deps(m, m->dir, "depends", false);
85                 tdeps = get_safe_ccan_deps(m, m->dir, "testdepends", false);
86         } else {
87                 deps = get_deps(m, m->dir, "depends", false,
88                                 get_or_compile_info);
89                 tdeps = get_deps(m, m->dir, "testdepends", false,
90                                  get_or_compile_info);
91         }
92
93         core_deps = tal_count(deps) - 1;
94         test_deps = tal_count(tdeps) - 1;
95
96         used = tal_arrz(m, bool, core_deps + test_deps + 1);
97
98         foreach_ptr(list, &m->c_files, &m->h_files) {
99                 struct ccan_file *f;
100
101                 list_for_each(list, f, list)
102                         ok &= check_dep_includes(m, deps, used, score, f);
103         }
104
105         for (i = 0; i < core_deps; i++) {
106                 if (!used[i] && strstarts(deps[i], "ccan/"))
107                         score_file_error(score, m->info_file, 0,
108                                          "%s is an unused dependency",
109                                          deps[i]);
110         }
111
112         /* Now remove NUL and append test dependencies to deps. */
113         deps = tal_dup_arr(m, char *, take(deps), core_deps, test_deps + 2);
114         memcpy(deps + core_deps, tdeps, sizeof(tdeps[0]) * test_deps);
115         /* ccan/tap is given a free pass. */
116         deps[core_deps + test_deps] = (char *)"ccan/tap";
117         deps[core_deps + test_deps + 1] = NULL;
118
119         foreach_ptr(list, &m->run_tests, &m->api_tests,
120                     &m->compile_ok_tests, &m->compile_fail_tests,
121                     &m->other_test_c_files) {
122                 struct ccan_file *f;
123
124                 list_for_each(list, f, list)
125                         ok &= check_dep_includes(m, deps, used, score, f);
126         }
127
128         for (i = core_deps; i < test_deps; i++) {
129                 if (!used[i])
130                         score_file_error(score, m->info_file, 0,
131                                          "%s is an unused test dependency",
132                                          deps[i]);
133         }
134
135         if (!score->error)
136                 score->score = score->total;
137
138         /* We don't count unused dependencies as an error (yet!) */
139         score->pass = ok;
140 }
141
142 struct ccanlint depends_accurate = {
143         .key = "depends_accurate",
144         .name = "Module's CCAN dependencies are the only CCAN files #included",
145         .check = check_depends_accurate,
146         .needs = "depends_exist info_compiles test_depends_exist headers_idempotent"
147 };
148
149 REGISTER_TEST(depends_accurate);