]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/depends_accurate.c
78dad0b13a9c904cacf661a44658948e84ddfc83
[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, bool tap_ok)
32 {
33         struct ccan_file *f;
34
35         if (tap_ok && streq(depname, "ccan/tap"))
36                 return true;
37
38         /* We can include ourselves, of course. */
39         if (streq(depname + strlen("ccan/"), m->basename))
40                 return true;
41
42         list_for_each(&m->dep_dirs, f, list) {
43                 if (streq(f->name, depname))
44                         return true;
45         }
46         return false;
47 }
48
49 static void *check_depends_accurate(struct manifest *m,
50                                     bool keep,
51                                     unsigned int *timeleft)
52 {
53         struct list_head *list;
54         char *report = talloc_strdup(m, "");
55
56         foreach_ptr(list, &m->c_files, &m->h_files,
57                     &m->run_tests, &m->api_tests,
58                     &m->compile_ok_tests, &m->compile_fail_tests,
59                     &m->other_test_c_files) {
60                 struct ccan_file *f;
61                 bool tap_ok;
62
63                 /* Including ccan/tap is fine for tests. */
64                 tap_ok =  (list != &m->c_files && list != &m->h_files);
65
66                 list_for_each(list, f, list) {
67                         unsigned int i;
68                         char **lines = get_ccan_file_lines(f);
69
70                         for (i = 0; lines[i]; i++) {
71                                 char *p;
72                                 if (lines[i][strspn(lines[i], " \t")] != '#')
73                                         continue;
74                                 p = strip_spaces(f, lines[i]);
75                                 if (!strstarts(p, "#include<ccan/")
76                                     && !strstarts(p, "#include\"ccan/"))
77                                         continue;
78                                 p += strlen("#include\"");
79                                 if (!strchr(strchr(p, '/') + 1, '/'))
80                                         continue;
81                                 *strchr(strchr(p, '/') + 1, '/') = '\0';
82                                 if (!has_dep(m, p, tap_ok))
83                                         report = talloc_asprintf_append(report,
84                                                "%s:%u:%s\n",
85                                                f->name, i+1, lines[i]);
86                         }
87                 }
88         }
89
90         if (streq(report, "")) {
91                 talloc_free(report);
92                 report = NULL;
93         }
94         return report;
95 }
96
97 static const char *describe_depends_accurage(struct manifest *m,
98                                              void *check_result)
99 {
100         return talloc_asprintf(check_result, 
101                                "You include ccan modules you don't list as dependencies:\n"
102                                "%s", (char *)check_result);
103 }
104
105 struct ccanlint depends_accurate = {
106         .key = "depends-accurate",
107         .name = "Module's CCAN dependencies are the only ccan files #included",
108         .total_score = 1,
109         .check = check_depends_accurate,
110         .describe = describe_depends_accurage,
111 };
112
113 REGISTER_TEST(depends_accurate, &depends_exist, NULL);