]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/ccanlint.c
ee8c8b78634aab7e5eb3dfb9fb5374b07014e88d
[ccan] / tools / ccanlint / ccanlint.c
1 /*
2  * ccanlint: assorted checks and advice for a ccan package
3  * Copyright (C) 2008 Rusty Russell, Idris Soule
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the Free
7  * Software Foundation; either version 2 of the License, or (at your option)
8  * any later version.
9  *
10  *   This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 #include "ccanlint.h"
20 #include "../tools.h"
21 #include <unistd.h>
22 #include <getopt.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <err.h>
28 #include <ctype.h>
29 #include <ccan/btree/btree.h>
30 #include <ccan/str/str.h>
31 #include <ccan/str_talloc/str_talloc.h>
32 #include <ccan/talloc/talloc.h>
33
34 static unsigned int verbose = 0;
35 static LIST_HEAD(compulsory_tests);
36 static LIST_HEAD(normal_tests);
37 static LIST_HEAD(finished_tests);
38 bool safe_mode = false;
39 static struct btree *exclude;
40
41 static void usage(const char *name)
42 {
43         fprintf(stderr, "Usage: %s [-s] [-n] [-v] [-d <dirname>]\n"
44                 "   -v: verbose mode\n"
45                 "   -s: simply give one line summary\n"
46                 "   -d: use this directory instead of the current one\n"
47                 "   -n: do not compile anything\n"
48                 "   -l: list tests ccanlint performs\n"
49                 "   -x: exclude tests (e.g. -x trailing_whitespace,valgrind)\n",
50                 name);
51         exit(1);
52 }
53
54 #if 0
55 static void indent_print(const char *string)
56 {
57         while (*string) {
58                 unsigned int line = strcspn(string, "\n");
59                 printf("\t%.*s", line, string);
60                 if (string[line] == '\n') {
61                         printf("\n");
62                         line++;
63                 }
64                 string += line;
65         }
66 }
67 #endif
68
69 bool ask(const char *question)
70 {
71         char reply[2];
72
73         printf("%s ", question);
74         fflush(stdout);
75
76         return fgets(reply, sizeof(reply), stdin) != NULL
77                 && toupper(reply[0]) == 'Y';
78 }
79
80 static const char *should_skip(struct manifest *m, struct ccanlint *i)
81 {
82         if (btree_lookup(exclude, i->key))
83                 return "excluded on command line";
84         
85         if (i->skip_fail)
86                 return "dependency failed";
87
88         if (i->skip)
89                 return "dependency was skipped";
90
91         if (i->can_run)
92                 return i->can_run(m);
93         return NULL;
94 }
95
96 static bool run_test(struct ccanlint *i,
97                      bool quiet,
98                      unsigned int *score,
99                      unsigned int *total_score,
100                      struct manifest *m)
101 {
102         void *result;
103         unsigned int this_score;
104         const struct dependent *d;
105         const char *skip;
106
107         //one less test to run through
108         list_for_each(&i->dependencies, d, node)
109                 d->dependent->num_depends--;
110
111         skip = should_skip(m, i);
112         if (skip) {
113                 if (verbose)
114                         printf("  %s: skipped (%s)\n", i->name, skip);
115
116                 /* If we're skipping this because a prereq failed, we fail. */
117                 if (i->skip_fail)
118                         *total_score += i->total_score;
119                         
120                 list_del(&i->list);
121                 list_add_tail(&finished_tests, &i->list);
122                 list_for_each(&i->dependencies, d, node) {
123                         d->dependent->skip = true;
124                         d->dependent->skip_fail = i->skip_fail;
125                 }
126                 return true;
127         }
128
129         result = i->check(m);
130         if (!result) {
131                 if (verbose) {
132                         printf("  %s: OK", i->name);
133                         if (i->total_score)
134                                 printf(" (+%u/%u)",
135                                        i->total_score, i->total_score);
136                         printf("\n");
137                 }
138                 if (i->total_score) {
139                         *score += i->total_score;
140                         *total_score += i->total_score;
141                 }
142
143                 list_del(&i->list);
144                 list_add_tail(&finished_tests, &i->list);
145                 return true;
146         }
147
148         if (i->score)
149                 this_score = i->score(m, result);
150         else
151                 this_score = 0;
152
153         list_del(&i->list);
154         list_add_tail(&finished_tests, &i->list);
155
156         *total_score += i->total_score;
157         *score += this_score;
158         if (verbose) {
159                 printf("  %s: FAIL (+%u/%u)\n",
160                        i->name, this_score, i->total_score);
161         }
162         if (!quiet) {
163                 printf("%s\n", i->describe(m, result));
164
165                 if (i->handle)
166                         i->handle(m, result);
167         }
168
169         /* Skip any tests which depend on this one. */
170         list_for_each(&i->dependencies, d, node) {
171                 d->dependent->skip = true;
172                 d->dependent->skip_fail = true;
173         }
174
175         return false;
176 }
177
178 static void register_test(struct list_head *h, struct ccanlint *test, ...)
179 {
180         va_list ap;
181         struct ccanlint *depends;
182         struct dependent *dchild;
183
184         list_add(h, &test->list);
185
186         va_start(ap, test);
187         /* Careful: we might have been initialized by a dependent. */
188         if (test->dependencies.n.next == NULL)
189                 list_head_init(&test->dependencies);
190
191         //dependent(s) args (if any), last one is NULL
192         while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
193                 dchild = malloc(sizeof(*dchild));
194                 dchild->dependent = test;
195                 /* The thing we depend on might not be initialized yet! */
196                 if (depends->dependencies.n.next == NULL)
197                         list_head_init(&depends->dependencies);
198                 list_add_tail(&depends->dependencies, &dchild->node);
199                 test->num_depends++;
200         }
201         va_end(ap);
202 }
203
204 /**
205  * get_next_test - retrieves the next test to be processed
206  **/
207 static inline struct ccanlint *get_next_test(struct list_head *test)
208 {
209         struct ccanlint *i;
210
211         if (list_empty(test))
212                 return NULL;
213
214         list_for_each(test, i, list) {
215                 if (i->num_depends == 0)
216                         return i;
217         }
218         errx(1, "Can't make process; test dependency cycle");
219 }
220
221 static void init_tests(void)
222 {
223         const struct ccanlint *i;
224         struct btree *keys, *names;
225
226 #undef REGISTER_TEST
227 #define REGISTER_TEST(name, ...) register_test(&normal_tests, &name, __VA_ARGS__)
228 #include "generated-normal-tests"
229 #undef REGISTER_TEST
230 #define REGISTER_TEST(name, ...) register_test(&compulsory_tests, &name, __VA_ARGS__)
231 #include "generated-compulsory-tests"
232
233         /* Self-consistency check: make sure no two tests
234            have the same key or name. */
235         keys = btree_new(btree_strcmp);
236         names = btree_new(btree_strcmp);
237         list_for_each(&compulsory_tests, i, list) {
238                 if (!btree_insert(keys, i->key))
239                         errx(1, "BUG: Duplicate test key '%s'", i->key);
240                 if (!btree_insert(keys, i->name))
241                         errx(1, "BUG: Duplicate test name '%s'", i->name);
242         }
243         list_for_each(&normal_tests, i, list) {
244                 if (!btree_insert(keys, i->key))
245                         errx(1, "BUG: Duplicate test key '%s'", i->key);
246                 if (!btree_insert(keys, i->name))
247                         errx(1, "BUG: Duplicate test name '%s'", i->name);
248         }
249         btree_delete(keys);
250         btree_delete(names);
251         
252         if (!verbose)
253                 return;
254
255         printf("\nCompulsory Tests\n");
256         list_for_each(&compulsory_tests, i, list) {
257                 printf("%s depends on %u others\n", i->name, i->num_depends);
258                 if (!list_empty(&i->dependencies)) {
259                         const struct dependent *d;
260                         printf("These depend on us:\n");
261                         list_for_each(&i->dependencies, d, node)
262                                 printf("\t%s\n", d->dependent->name);
263                 }
264         }
265
266         printf("\nNormal Tests\n");
267         list_for_each(&normal_tests, i, list) {
268                 printf("%s depends on %u others\n", i->name, i->num_depends);
269                 if (!list_empty(&i->dependencies)) {
270                         const struct dependent *d;
271                         printf("These depend on us:\n");
272                         list_for_each(&i->dependencies, d, node)
273                                 printf("\t%s\n", d->dependent->name);
274                 }
275         }
276 }
277
278 static void print_tests(struct list_head *tests, const char *type)
279 {
280         struct ccanlint *i;
281
282         printf("%s tests:\n", type);
283         /* This makes them print in topological order. */
284         while ((i = get_next_test(tests)) != NULL) {
285                 const struct dependent *d;
286                 printf("   %-25s %s\n", i->key, i->name);
287                 list_del(&i->list);
288                 list_for_each(&i->dependencies, d, node)
289                         d->dependent->num_depends--;
290         }
291 }
292
293 static void list_tests(void)
294 {
295         init_tests();
296         print_tests(&compulsory_tests, "Compulsory");
297         print_tests(&normal_tests, "Normal");
298         exit(0);
299 }
300
301 int main(int argc, char *argv[])
302 {
303         int c;
304         bool summary = false;
305         unsigned int score = 0, total_score = 0;
306         struct manifest *m;
307         struct ccanlint *i;
308         const char *prefix = "", *dir = ".";
309         
310         exclude = btree_new(btree_strcmp);
311
312         /* I'd love to use long options, but that's not standard. */
313         /* FIXME: getopt_long ccan package? */
314         while ((c = getopt(argc, argv, "sd:vnlx:")) != -1) {
315                 switch (c) {
316                 case 'd':
317                         dir = optarg;
318                         prefix = talloc_append_string(talloc_basename(NULL,
319                                                                       optarg),
320                                                       ": ");
321                         break;
322                 case 'l':
323                         list_tests();
324                 case 's':
325                         summary = true;
326                         break;
327                 case 'v':
328                         verbose++;
329                         break;
330                 case 'n':
331                         safe_mode = true;
332                         break;
333                 case 'x': {
334                         char **exclude_strs = strsplit(NULL, optarg, ",", NULL);
335                         size_t i;
336                         for (i = 0; exclude_strs[i]; i++)
337                                 btree_insert(exclude, exclude_strs[i]);
338                 } break;
339                 default:
340                         usage(argv[0]);
341                 }
342         }
343
344         if (optind < argc)
345                 usage(argv[0]);
346
347         m = get_manifest(talloc_autofree_context(), dir);
348
349         init_tests();
350
351         /* If you don't pass the compulsory tests, you don't even get a score */
352         if (verbose)
353                 printf("Compulsory tests:\n");
354
355         while ((i = get_next_test(&compulsory_tests)) != NULL) {
356                 if (!run_test(i, summary, &score, &total_score, m)) {
357                         errx(1, "%s%s failed", prefix, i->name);
358                 }
359         }
360
361         if (verbose)
362                 printf("\nNormal tests:\n");
363         while ((i = get_next_test(&normal_tests)) != NULL)
364                 run_test(i, summary, &score, &total_score, m);
365
366         printf("%sTotal score: %u/%u\n", prefix, score, total_score);
367         return 0;
368 }