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