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