]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/depends_accurate.c
ccanlint: score_file_error() takes printf-format
[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         foreach_ptr(list, &m->c_files, &m->h_files,
41                     &m->run_tests, &m->api_tests,
42                     &m->compile_ok_tests, &m->compile_fail_tests,
43                     &m->other_test_c_files) {
44                 struct ccan_file *f;
45
46                 list_for_each(list, f, list) {
47                         unsigned int i;
48                         char **lines = get_ccan_file_lines(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                                 if (has_dep(m, mod))
57                                         continue;
58                                 score_file_error(score, f, i+1,
59                                                  "%s not listed in _info",
60                                                  mod);
61                         }
62                 }
63         }
64
65         if (!score->error) {
66                 score->pass = true;
67                 score->score = score->total;
68         }
69 }
70
71 struct ccanlint depends_accurate = {
72         .key = "depends_accurate",
73         .name = "Module's CCAN dependencies are the only CCAN files #included",
74         .check = check_depends_accurate,
75         .needs = "depends_exist"
76 };
77
78 REGISTER_TEST(depends_accurate);