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