0ed1e654cd9324199e045a867b41b4d219593a0f
[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, bool test_depend, const char *depname)
20 {
21         struct manifest *i;
22
23         /* We can include ourselves, of course. */
24         if (streq(depname, m->basename))
25                 return true;
26
27         list_for_each(&m->deps, i, list) {
28                 if (streq(i->basename, depname))
29                         return true;
30         }
31
32         if (test_depend) {
33                 list_for_each(&m->test_deps, i, list) {
34                         if (streq(i->basename, depname))
35                                 return true;
36                 }
37         }
38
39         return false;
40 }
41
42 static void check_dep_includes(struct manifest *m, struct score *score,
43                                struct ccan_file *f, bool test_depend)
44 {
45         unsigned int i;
46         char **lines = get_ccan_file_lines(f);
47         struct line_info *li = get_ccan_line_info(f);
48
49         for (i = 0; lines[i]; i++) {
50                 char *mod;
51                 if (!strreg(f, lines[i],
52                             "^[ \t]*#[ \t]*include[ \t]*[<\"]"
53                             "ccan/+([^/]+)/", &mod))
54                         continue;
55
56                 if (has_dep(m, test_depend, mod))
57                         continue;
58
59                 /* FIXME: we can't be sure about
60                  * conditional includes, so don't
61                  * complain. */
62                 if (!li[i].cond) {
63                         score_file_error(score, f, i+1,
64                                          "%s not listed in _info", mod);
65                 }
66         }
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
74         foreach_ptr(list, &m->c_files, &m->h_files) {
75                 struct ccan_file *f;
76
77                 list_for_each(list, f, list)
78                         check_dep_includes(m, score, f, false);
79         }
80
81         foreach_ptr(list, &m->run_tests, &m->api_tests,
82                     &m->compile_ok_tests, &m->compile_fail_tests,
83                     &m->other_test_c_files) {
84                 struct ccan_file *f;
85
86                 list_for_each(list, f, list)
87                         check_dep_includes(m, score, f, true);
88         }
89
90         if (!score->error) {
91                 score->score = score->total;
92                 score->pass = true;
93         }
94 }
95
96 struct ccanlint depends_accurate = {
97         .key = "depends_accurate",
98         .name = "Module's CCAN dependencies are the only CCAN files #included",
99         .check = check_depends_accurate,
100         .needs = "depends_exist test_depends_exist"
101 };
102
103 REGISTER_TEST(depends_accurate);