]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/depends_accurate.c
configurator: HAVE_PROC_SELF_MAPS
[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         /* FIXME: This isn't reliable enough with #ifdefs, so we don't fail. */
40         score->pass = true;
41
42         foreach_ptr(list, &m->c_files, &m->h_files,
43                     &m->run_tests, &m->api_tests,
44                     &m->compile_ok_tests, &m->compile_fail_tests,
45                     &m->other_test_c_files) {
46                 struct ccan_file *f;
47
48                 list_for_each(list, f, list) {
49                         unsigned int i;
50                         char **lines = get_ccan_file_lines(f);
51
52                         for (i = 0; lines[i]; i++) {
53                                 char *mod;
54                                 if (!strreg(f, lines[i],
55                                             "^[ \t]*#[ \t]*include[ \t]*[<\"]"
56                                             "ccan/+([^/]+)/", &mod))
57                                         continue;
58                                 if (has_dep(m, mod))
59                                         continue;
60                                 score_file_error(score, f, i+1,
61                                                  "%s not listed in _info",
62                                                  mod);
63                         }
64                 }
65         }
66
67         if (!score->error) {
68                 score->score = score->total;
69         }
70 }
71
72 struct ccanlint depends_accurate = {
73         .key = "depends_accurate",
74         .name = "Module's CCAN dependencies are the only CCAN files #included",
75         .check = check_depends_accurate,
76         .needs = "depends_exist"
77 };
78
79 REGISTER_TEST(depends_accurate);