Merge branch 'master' of ozlabs.org:ccan
[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, 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         return false;
32 }
33
34 static void check_depends_accurate(struct manifest *m,
35                                    unsigned int *timeleft, struct score *score)
36 {
37         struct list_head *list;
38
39         foreach_ptr(list, &m->c_files, &m->h_files,
40                     &m->run_tests, &m->api_tests,
41                     &m->compile_ok_tests, &m->compile_fail_tests,
42                     &m->other_test_c_files) {
43                 struct ccan_file *f;
44
45                 list_for_each(list, f, list) {
46                         unsigned int i;
47                         char **lines = get_ccan_file_lines(f);
48                         struct line_info *li = get_ccan_line_info(f);
49
50                         for (i = 0; lines[i]; i++) {
51                                 char *mod;
52                                 if (!strreg(f, lines[i],
53                                             "^[ \t]*#[ \t]*include[ \t]*[<\"]"
54                                             "ccan/+([^/]+)/", &mod))
55                                         continue;
56
57                                 if (has_dep(m, mod))
58                                         continue;
59
60                                 /* FIXME: we can't be sure about
61                                  * conditional includes, so don't
62                                  * complain. */
63                                 if (!li[i].cond) {
64                                         score_file_error(score, f, i+1,
65                                                          "%s not listed in _info",
66                                                          mod);
67                                 }
68                         }
69                 }
70         }
71
72         if (!score->error) {
73                 score->score = score->total;
74                 score->pass = true;
75         }
76 }
77
78 struct ccanlint depends_accurate = {
79         .key = "depends_accurate",
80         .name = "Module's CCAN dependencies are the only CCAN files #included",
81         .check = check_depends_accurate,
82         .needs = "depends_exist"
83 };
84
85 REGISTER_TEST(depends_accurate);