ccanlint: rename test keys
[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/foreach/foreach.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <limits.h>
11 #include <errno.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <err.h>
15 #include <string.h>
16 #include <ctype.h>
17
18 static char *strip_spaces(const void *ctx, char *line)
19 {
20         char *p = talloc_strdup(ctx, line);
21         unsigned int i, j;
22
23         for (i = 0, j = 0; p[i]; i++) {
24                 if (!isspace(p[i]))
25                         p[j++] = p[i];
26         }
27         p[j] = '\0';
28         return p;
29 }
30
31 static bool has_dep(struct manifest *m, const char *depname)
32 {
33         struct manifest *i;
34
35         /* We can include ourselves, of course. */
36         if (streq(depname, m->basename))
37                 return true;
38
39         list_for_each(&m->deps, i, list) {
40                 if (streq(i->basename, depname))
41                         return true;
42         }
43         return false;
44 }
45
46 static void check_depends_accurate(struct manifest *m,
47                                    bool keep,
48                                    unsigned int *timeleft, struct score *score)
49 {
50         struct list_head *list;
51
52         foreach_ptr(list, &m->c_files, &m->h_files,
53                     &m->run_tests, &m->api_tests,
54                     &m->compile_ok_tests, &m->compile_fail_tests,
55                     &m->other_test_c_files) {
56                 struct ccan_file *f;
57
58                 list_for_each(list, f, list) {
59                         unsigned int i;
60                         char **lines = get_ccan_file_lines(f);
61
62                         for (i = 0; lines[i]; i++) {
63                                 char *p;
64                                 if (lines[i][strspn(lines[i], " \t")] != '#')
65                                         continue;
66                                 p = strip_spaces(f, lines[i]);
67                                 if (!strstarts(p, "#include<ccan/")
68                                     && !strstarts(p, "#include\"ccan/"))
69                                         continue;
70                                 p += strlen("#include\"ccan/");
71                                 if (!strchr(strchr(p, '/') + 1, '/'))
72                                         continue;
73                                 *strchr(strchr(p, '/') + 1, '/') = '\0';
74                                 if (has_dep(m, p))
75                                         continue;
76                                 score->error = "Includes a ccan module"
77                                         " not listed in _info";
78                                 score_file_error(score, f, i+1, lines[i]);
79                         }
80                 }
81         }
82
83         if (!score->error) {
84                 score->pass = true;
85                 score->score = score->total;
86         }
87 }
88
89 struct ccanlint depends_accurate = {
90         .key = "depends_accurate",
91         .name = "Module's CCAN dependencies are the only CCAN files #included",
92         .check = check_depends_accurate,
93 };
94
95 REGISTER_TEST(depends_accurate, &depends_exist, NULL);