]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/file_analysis.c
Fix namespacize after grab_file changes.
[ccan] / tools / ccanlint / file_analysis.c
1 #include "ccanlint.h"
2 #include "get_file_lines.h"
3 #include <talloc/talloc.h>
4 #include <string/string.h>
5 #include <noerr/noerr.h>
6 #include <unistd.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <err.h>
11 #include <errno.h>
12 #include <dirent.h>
13
14 char **get_ccan_file_lines(struct ccan_file *f)
15 {
16         if (!f->lines) {
17                 char *buffer = grab_file(f, f->name, NULL);
18                 if (!buffer)
19                         err(1, "Getting file %s", f->name);
20                 f->lines = strsplit(f, buffer, "\n", &f->num_lines);
21         }
22         return f->lines;
23 }
24
25 static void add_files(struct manifest *m, const char *dir)
26 {
27         DIR *d;
28         struct dirent *ent;
29
30         if (dir[0])
31                 d = opendir(dir);
32         else
33                 d = opendir(".");
34         if (!d)
35                 err(1, "Opening directory %s", dir[0] ? dir : ".");
36
37         while ((ent = readdir(d)) != NULL) {
38                 struct stat st;
39                 struct ccan_file *f;
40                 struct list_head *dest;
41                 bool is_c_src;
42
43                 if (ent->d_name[0] == '.')
44                         continue;
45
46                 f = talloc(m, struct ccan_file);
47                 f->lines = NULL;
48                 f->name = talloc_asprintf(f, "%s%s", dir, ent->d_name);
49                 if (lstat(f->name, &st) != 0)
50                         err(1, "lstat %s", f->name);
51
52                 if (S_ISDIR(st.st_mode)) {
53                         f->name = talloc_append_string(f->name, "/");
54                         add_files(m, f->name);
55                         continue;
56                 }
57                 if (!S_ISREG(st.st_mode)) {
58                         talloc_free(f);
59                         continue;
60                 }
61
62                 if (streq(f->name, "_info.c")) {
63                         m->info_file = f;
64                         continue;
65                 }
66
67                 is_c_src = strends(f->name, ".c");
68                 if (!is_c_src && !strends(f->name, ".h"))
69                         dest = &m->other_files;
70                 else if (!strchr(f->name, '/')) {
71                         if (is_c_src)
72                                 dest = &m->c_files;
73                         else
74                                 dest = &m->h_files;
75                 } else if (strstarts(f->name, "test/")) {
76                         if (is_c_src) {
77                                 if (strstarts(f->name, "test/run"))
78                                         dest = &m->run_tests;
79                                 else if (strstarts(f->name, "test/compile_ok"))
80                                         dest = &m->compile_ok_tests;
81                                 else if (strstarts(f->name, "test/compile_fail"))
82                                         dest = &m->compile_fail_tests;
83                                 else
84                                         dest = &m->other_test_files;
85                         } else
86                                 dest = &m->other_test_files;
87                 } else
88                         dest = &m->other_files;
89
90                 list_add(dest, &f->list);
91         }
92         closedir(d);
93 }
94
95 char *report_on_lines(struct list_head *files,
96                       char *(*report)(const char *),
97                       char *sofar)
98 {
99         struct ccan_file *f;
100
101         list_for_each(files, f, list) {
102                 unsigned int i;
103                 char **lines = get_ccan_file_lines(f);
104
105                 for (i = 0; i < f->num_lines; i++) {
106                         char *r = report(lines[i]);
107                         if (!r)
108                                 continue;
109
110                         sofar = talloc_asprintf_append(sofar,
111                                                        "%s:%u:%s\n",
112                                                        f->name, i+1, r);
113                         talloc_free(r);
114                 }
115         }
116         return sofar;
117 }
118
119 struct manifest *get_manifest(void)
120 {
121         struct manifest *m = talloc(NULL, struct manifest);
122         unsigned int len;
123
124         m->info_file = NULL;
125         list_head_init(&m->c_files);
126         list_head_init(&m->h_files);
127         list_head_init(&m->run_tests);
128         list_head_init(&m->compile_ok_tests);
129         list_head_init(&m->compile_fail_tests);
130         list_head_init(&m->other_test_files);
131         list_head_init(&m->other_files);
132
133         /* *This* is why people hate C. */
134         len = 32;
135         m->basename = talloc_array(m, char, len);
136         while (!getcwd(m->basename, len)) {
137                 if (errno != ERANGE)
138                         err(1, "Getting current directory");
139                 m->basename = talloc_realloc(m, m->basename, char, len *= 2);
140         }
141
142         len = strlen(m->basename);
143         while (len && m->basename[len-1] == '/')
144                 m->basename[--len] = '\0';
145
146         m->basename = strrchr(m->basename, '/');
147         if (!m->basename)
148                 errx(1, "I don't expect to be run from the root directory");
149         m->basename++;
150
151         add_files(m, "");
152         return m;
153 }