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