]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/ccanlint.c
alloc: reduce number of large pages to 256.
[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 static unsigned int timeout;
41
42 static void usage(const char *name)
43 {
44         fprintf(stderr, "Usage: %s [-s] [-n] [-v] [-t <ms>] [-d <dirname>] [-x <tests>] [-k <test>]*\n"
45                 "   -v: verbose mode\n"
46                 "   -s: simply give one line summary\n"
47                 "   -d: use this directory instead of the current one\n"
48                 "   -n: do not compile anything\n"
49                 "   -l: list tests ccanlint performs\n"
50                 "   -k: keep results of this test (can be used multiple times)\n"
51                 "   -x: exclude tests (e.g. -x trailing_whitespace,valgrind)\n"
52                 "   -t: ignore (terminate) tests that are slower than this\n",
53                 name);
54         exit(1);
55 }
56
57 #if 0
58 static void indent_print(const char *string)
59 {
60         while (*string) {
61                 unsigned int line = strcspn(string, "\n");
62                 printf("\t%.*s", line, string);
63                 if (string[line] == '\n') {
64                         printf("\n");
65                         line++;
66                 }
67                 string += line;
68         }
69 }
70 #endif
71
72 bool ask(const char *question)
73 {
74         char reply[2];
75
76         printf("%s ", question);
77         fflush(stdout);
78
79         return fgets(reply, sizeof(reply), stdin) != NULL
80                 && toupper(reply[0]) == 'Y';
81 }
82
83 static const char *should_skip(struct manifest *m, struct ccanlint *i)
84 {
85         if (btree_lookup(exclude, i->key))
86                 return "excluded on command line";
87         
88         if (i->skip_fail)
89                 return "dependency failed";
90
91         if (i->skip)
92                 return "dependency was skipped";
93
94         if (i->can_run)
95                 return i->can_run(m);
96         return NULL;
97 }
98
99 static bool run_test(struct ccanlint *i,
100                      bool quiet,
101                      unsigned int *score,
102                      unsigned int *total_score,
103                      struct manifest *m)
104 {
105         void *result;
106         unsigned int this_score, timeleft;
107         const struct dependent *d;
108         const char *skip;
109
110         //one less test to run through
111         list_for_each(&i->dependencies, d, node)
112                 d->dependent->num_depends--;
113
114         skip = should_skip(m, i);
115
116         if (skip) {
117         skip:
118                 if (verbose)
119                         printf("  %s: skipped (%s)\n", i->name, skip);
120
121                 /* If we're skipping this because a prereq failed, we fail. */
122                 if (i->skip_fail)
123                         *total_score += i->total_score;
124                         
125                 list_del(&i->list);
126                 list_add_tail(&finished_tests, &i->list);
127                 list_for_each(&i->dependencies, d, node) {
128                         d->dependent->skip = true;
129                         d->dependent->skip_fail = i->skip_fail;
130                 }
131                 return true;
132         }
133
134         timeleft = timeout ? timeout : default_timeout_ms;
135         result = i->check(m, i->keep_results, &timeleft);
136         if (timeout && timeleft == 0) {
137                 skip = "timeout";
138                 goto skip;
139         }
140         if (!result) {
141                 if (verbose) {
142                         printf("  %s: OK", i->name);
143                         if (i->total_score)
144                                 printf(" (+%u/%u)",
145                                        i->total_score, i->total_score);
146                         printf("\n");
147                 }
148                 if (i->total_score) {
149                         *score += i->total_score;
150                         *total_score += i->total_score;
151                 }
152
153                 list_del(&i->list);
154                 list_add_tail(&finished_tests, &i->list);
155                 return true;
156         }
157
158         if (i->score)
159                 this_score = i->score(m, result);
160         else
161                 this_score = 0;
162
163         list_del(&i->list);
164         list_add_tail(&finished_tests, &i->list);
165
166         *total_score += i->total_score;
167         *score += this_score;
168         if (verbose) {
169                 printf("  %s: FAIL (+%u/%u)\n",
170                        i->name, this_score, i->total_score);
171         }
172         if (!quiet) {
173                 printf("%s\n", i->describe(m, result));
174
175                 if (i->handle)
176                         i->handle(m, result);
177         }
178
179         /* Skip any tests which depend on this one. */
180         list_for_each(&i->dependencies, d, node) {
181                 d->dependent->skip = true;
182                 d->dependent->skip_fail = true;
183         }
184
185         return false;
186 }
187
188 static void register_test(struct list_head *h, struct ccanlint *test, ...)
189 {
190         va_list ap;
191         struct ccanlint *depends;
192         struct dependent *dchild;
193
194         list_add(h, &test->list);
195
196         va_start(ap, test);
197         /* Careful: we might have been initialized by a dependent. */
198         if (test->dependencies.n.next == NULL)
199                 list_head_init(&test->dependencies);
200
201         //dependent(s) args (if any), last one is NULL
202         while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
203                 dchild = malloc(sizeof(*dchild));
204                 dchild->dependent = test;
205                 /* The thing we depend on might not be initialized yet! */
206                 if (depends->dependencies.n.next == NULL)
207                         list_head_init(&depends->dependencies);
208                 list_add_tail(&depends->dependencies, &dchild->node);
209                 test->num_depends++;
210         }
211         va_end(ap);
212 }
213
214 /**
215  * get_next_test - retrieves the next test to be processed
216  **/
217 static inline struct ccanlint *get_next_test(struct list_head *test)
218 {
219         struct ccanlint *i;
220
221         if (list_empty(test))
222                 return NULL;
223
224         list_for_each(test, i, list) {
225                 if (i->num_depends == 0)
226                         return i;
227         }
228         errx(1, "Can't make process; test dependency cycle");
229 }
230
231 static void init_tests(void)
232 {
233         const struct ccanlint *i;
234         struct btree *keys, *names;
235
236 #undef REGISTER_TEST
237 #define REGISTER_TEST(name, ...) register_test(&normal_tests, &name, __VA_ARGS__)
238 #include "generated-normal-tests"
239 #undef REGISTER_TEST
240 #define REGISTER_TEST(name, ...) register_test(&compulsory_tests, &name, __VA_ARGS__)
241 #include "generated-compulsory-tests"
242
243         /* Self-consistency check: make sure no two tests
244            have the same key or name. */
245         keys = btree_new(btree_strcmp);
246         names = btree_new(btree_strcmp);
247         list_for_each(&compulsory_tests, i, list) {
248                 if (!btree_insert(keys, i->key))
249                         errx(1, "BUG: Duplicate test key '%s'", i->key);
250                 if (!btree_insert(keys, i->name))
251                         errx(1, "BUG: Duplicate test name '%s'", i->name);
252         }
253         list_for_each(&normal_tests, i, list) {
254                 if (!btree_insert(keys, i->key))
255                         errx(1, "BUG: Duplicate test key '%s'", i->key);
256                 if (!btree_insert(keys, i->name))
257                         errx(1, "BUG: Duplicate test name '%s'", i->name);
258         }
259         btree_delete(keys);
260         btree_delete(names);
261         
262         if (!verbose)
263                 return;
264
265         printf("\nCompulsory Tests\n");
266         list_for_each(&compulsory_tests, i, list) {
267                 printf("%s depends on %u others\n", i->name, i->num_depends);
268                 if (!list_empty(&i->dependencies)) {
269                         const struct dependent *d;
270                         printf("These depend on us:\n");
271                         list_for_each(&i->dependencies, d, node)
272                                 printf("\t%s\n", d->dependent->name);
273                 }
274         }
275
276         printf("\nNormal Tests\n");
277         list_for_each(&normal_tests, i, list) {
278                 printf("%s depends on %u others\n", i->name, i->num_depends);
279                 if (!list_empty(&i->dependencies)) {
280                         const struct dependent *d;
281                         printf("These depend on us:\n");
282                         list_for_each(&i->dependencies, d, node)
283                                 printf("\t%s\n", d->dependent->name);
284                 }
285         }
286 }
287
288 static struct ccanlint *find_test(const char *key)
289 {
290         struct ccanlint *i;
291
292         list_for_each(&compulsory_tests, i, list)
293                 if (streq(i->key, key))
294                         return i;
295
296         list_for_each(&normal_tests, i, list)
297                 if (streq(i->key, key))
298                         return i;
299
300         return NULL;
301 }
302
303 static void keep_test(const char *testname)
304 {
305         struct ccanlint *i = find_test(testname);
306         if (!i)
307                 errx(1, "No test %s to --keep", testname);
308         i->keep_results = true;
309 }
310
311 static void print_tests(struct list_head *tests, const char *type)
312 {
313         struct ccanlint *i;
314
315         printf("%s tests:\n", type);
316         /* This makes them print in topological order. */
317         while ((i = get_next_test(tests)) != NULL) {
318                 const struct dependent *d;
319                 printf("   %-25s %s\n", i->key, i->name);
320                 list_del(&i->list);
321                 list_for_each(&i->dependencies, d, node)
322                         d->dependent->num_depends--;
323         }
324 }
325
326 static void list_tests(void)
327 {
328         print_tests(&compulsory_tests, "Compulsory");
329         print_tests(&normal_tests, "Normal");
330         exit(0);
331 }
332
333 int main(int argc, char *argv[])
334 {
335         int c;
336         bool summary = false;
337         unsigned int score = 0, total_score = 0;
338         struct manifest *m;
339         struct ccanlint *i;
340         const char *prefix = "", *dir = ".";
341         
342         init_tests();
343
344         exclude = btree_new(btree_strcmp);
345
346         /* I'd love to use long options, but that's not standard. */
347         /* FIXME: getopt_long ccan package? */
348         while ((c = getopt(argc, argv, "sd:vnlx:t:k:")) != -1) {
349                 switch (c) {
350                 case 'd':
351                         dir = optarg;
352                         prefix = talloc_append_string(talloc_basename(NULL,
353                                                                       optarg),
354                                                       ": ");
355                         break;
356                 case 'l':
357                         list_tests();
358                 case 's':
359                         summary = true;
360                         break;
361                 case 'v':
362                         verbose++;
363                         break;
364                 case 'n':
365                         safe_mode = true;
366                         break;
367                 case 'k':
368                         keep_test(optarg);
369                         break;
370                 case 'x': {
371                         char **exclude_strs = strsplit(NULL, optarg, ",", NULL);
372                         size_t i;
373                         for (i = 0; exclude_strs[i]; i++)
374                                 btree_insert(exclude, exclude_strs[i]);
375                 } break;
376                 case 't':
377                         timeout = atoi(optarg);
378                         if (!timeout)
379                                 errx(1, "Invalid timeout %s: 1 ms minumum",
380                                      optarg);
381                         break;
382                 default:
383                         usage(argv[0]);
384                 }
385         }
386
387         if (optind < argc)
388                 usage(argv[0]);
389
390         m = get_manifest(talloc_autofree_context(), dir);
391
392         /* If you don't pass the compulsory tests, you don't even get a score */
393         if (verbose)
394                 printf("Compulsory tests:\n");
395
396         while ((i = get_next_test(&compulsory_tests)) != NULL) {
397                 if (!run_test(i, summary, &score, &total_score, m)) {
398                         errx(1, "%s%s failed", prefix, i->name);
399                 }
400         }
401
402         if (verbose)
403                 printf("\nNormal tests:\n");
404         while ((i = get_next_test(&normal_tests)) != NULL)
405                 run_test(i, summary, &score, &total_score, m);
406
407         printf("%sTotal score: %u/%u\n", prefix, score, total_score);
408         return 0;
409 }