2 * ccanlint: assorted checks and advice for a ccan package
3 * Copyright (C) 2008 Rusty Russell, Idris Soule
4 * Copyright (C) 2010 Rusty Russell, Idris Soule
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 51
18 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <ccan/btree/btree.h>
29 #include <ccan/str/str.h>
30 #include <ccan/str_talloc/str_talloc.h>
31 #include <ccan/talloc/talloc.h>
32 #include <ccan/opt/opt.h>
33 #include <ccan/foreach/foreach.h>
34 #include <ccan/grab_file/grab_file.h>
35 #include <ccan/cast/cast.h>
38 static struct list_head compulsory_tests;
39 static struct list_head normal_tests;
40 bool safe_mode = false;
41 static struct btree *cmdline_exclude;
42 static struct btree *info_exclude;
43 static unsigned int timeout;
45 /* These are overridden at runtime if we can find config.h */
46 const char *compiler = NULL;
47 const char *cflags = NULL;
49 const char *config_header;
52 static void indent_print(const char *string)
55 unsigned int line = strcspn(string, "\n");
56 printf("\t%.*s", line, string);
57 if (string[line] == '\n') {
66 bool ask(const char *question)
70 printf("%s ", question);
73 return fgets(reply, sizeof(reply), stdin) != NULL
74 && toupper(reply[0]) == 'Y';
77 static const char *should_skip(struct manifest *m, struct ccanlint *i)
79 if (btree_lookup(cmdline_exclude, i->key))
80 return "excluded on command line";
82 if (btree_lookup(info_exclude, i->key))
83 return "excluded in _info file";
89 return "dependency failed";
96 static bool run_test(struct ccanlint *i,
98 unsigned int *running_score,
99 unsigned int *running_total,
103 unsigned int timeleft;
104 const struct dependent *d;
108 //one less test to run through
109 list_for_each(&i->dependencies, d, node)
110 d->dependent->num_depends--;
112 score = talloc(m, struct score);
113 list_head_init(&score->per_file_errors);
119 skip = should_skip(m, i);
123 if (verbose && !streq(skip, "not relevant to target"))
124 printf("%s%s: skipped (%s)\n", prefix, i->name, skip);
126 /* If we're skipping this because a prereq failed, we fail:
127 * count it as a score of 1. */
132 list_for_each(&i->dependencies, d, node) {
133 if (d->dependent->skip)
135 d->dependent->skip = "dependency was skipped";
136 d->dependent->skip_fail = i->skip_fail;
138 return i->skip_fail ? false : true;
141 timeleft = timeout ? timeout : default_timeout_ms;
142 i->check(m, i->keep_results, &timeleft, score);
143 if (timeout && timeleft == 0) {
148 assert(score->score <= score->total);
149 if ((!score->pass && !quiet)
150 || (score->score < score->total && verbose)
152 printf("%s%s (%s): %s",
153 prefix, i->name, i->key, score->pass ? "PASS" : "FAIL");
154 if (score->total > 1)
155 printf(" (+%u/%u)", score->score, score->total);
159 if ((!quiet && !score->pass) || verbose) {
161 printf("%s%s", score->error,
162 strends(score->error, "\n") ? "" : "\n");
165 if (!quiet && score->score < score->total && i->handle)
168 *running_score += score->score;
169 *running_total += score->total;
174 /* Skip any tests which depend on this one. */
175 list_for_each(&i->dependencies, d, node) {
176 if (d->dependent->skip)
178 d->dependent->skip = "dependency failed";
179 d->dependent->skip_fail = true;
185 static void register_test(struct list_head *h, struct ccanlint *test)
187 list_add(h, &test->list);
188 test->options = talloc_array(NULL, char *, 1);
189 test->options[0] = NULL;
191 test->skip_fail = false;
195 * get_next_test - retrieves the next test to be processed
197 static inline struct ccanlint *get_next_test(struct list_head *test)
201 if (list_empty(test))
204 list_for_each(test, i, list) {
205 if (i->num_depends == 0)
208 errx(1, "Can't make process; test dependency cycle");
211 static struct ccanlint *find_test(const char *key)
215 list_for_each(&compulsory_tests, i, list)
216 if (streq(i->key, key))
219 list_for_each(&normal_tests, i, list)
220 if (streq(i->key, key))
226 bool is_excluded(const char *name)
228 return btree_lookup(cmdline_exclude, name) != NULL
229 || btree_lookup(info_exclude, name) != NULL
230 || find_test(name)->skip != NULL;
234 #define REGISTER_TEST(name, ...) extern struct ccanlint name
235 #include "generated-normal-tests"
236 #include "generated-compulsory-tests"
238 static void init_tests(void)
241 struct btree *keys, *names;
242 struct list_head *list;
244 list_head_init(&normal_tests);
245 list_head_init(&compulsory_tests);
248 #define REGISTER_TEST(name) register_test(&normal_tests, &name)
249 #include "generated-normal-tests"
251 #define REGISTER_TEST(name) register_test(&compulsory_tests, &name)
252 #include "generated-compulsory-tests"
254 /* Initialize dependency lists. */
255 foreach_ptr(list, &compulsory_tests, &normal_tests) {
256 list_for_each(list, c, list) {
257 list_head_init(&c->dependencies);
262 /* Resolve dependencies. */
263 foreach_ptr(list, &compulsory_tests, &normal_tests) {
264 list_for_each(list, c, list) {
265 char **deps = strsplit(NULL, c->needs, " ");
268 for (i = 0; deps[i]; i++) {
269 struct ccanlint *dep;
270 struct dependent *dchild;
272 dep = find_test(deps[i]);
274 errx(1, "BUG: unknown dep '%s' for %s",
276 dchild = talloc(NULL, struct dependent);
277 dchild->dependent = c;
278 list_add_tail(&dep->dependencies,
286 /* Self-consistency check: make sure no two tests
287 have the same key or name. */
288 keys = btree_new(btree_strcmp);
289 names = btree_new(btree_strcmp);
290 foreach_ptr(list, &compulsory_tests, &normal_tests) {
291 list_for_each(list, c, list) {
292 if (!btree_insert(keys, c->key))
293 errx(1, "BUG: Duplicate test key '%s'",
295 if (!btree_insert(names, c->name))
296 errx(1, "BUG: Duplicate test name '%s'",
304 static void print_test_depends(void)
306 struct list_head *list;
308 foreach_ptr(list, &compulsory_tests, &normal_tests) {
310 printf("\%s Tests\n",
311 list == &compulsory_tests ? "Compulsory" : "Normal");
313 list_for_each(list, c, list) {
314 if (!list_empty(&c->dependencies)) {
315 const struct dependent *d;
316 printf("These depend on %s:\n", c->key);
317 list_for_each(&c->dependencies, d, node)
318 printf("\t%s\n", d->dependent->key);
324 static int show_tmpdir(const char *dir)
326 printf("You can find ccanlint working files in '%s'\n", dir);
330 static char *keep_test(const char *testname, void *unused)
335 if (streq(testname, "all")) {
336 struct list_head *list;
337 foreach_ptr(list, &compulsory_tests, &normal_tests) {
338 list_for_each(list, i, list)
339 i->keep_results = true;
342 i = find_test(testname);
344 errx(1, "No test %s to --keep", testname);
345 i->keep_results = true;
348 /* Don't automatically destroy temporary dir. */
349 talloc_set_destructor(temp_dir(NULL), show_tmpdir);
353 static char *skip_test(const char *testname, void *unused)
355 btree_insert(cmdline_exclude, testname);
359 static void print_tests(struct list_head *tests, const char *type)
363 printf("%s tests:\n", type);
364 /* This makes them print in topological order. */
365 while ((i = get_next_test(tests)) != NULL) {
366 const struct dependent *d;
367 printf(" %-25s %s\n", i->key, i->name);
369 list_for_each(&i->dependencies, d, node)
370 d->dependent->num_depends--;
374 static char *list_tests(void *arg)
377 print_tests(&compulsory_tests, "Compulsory");
378 print_tests(&normal_tests, "Normal");
382 static void test_dgraph_vertices(struct list_head *tests, const char *style)
384 const struct ccanlint *i;
386 list_for_each(tests, i, list) {
388 * todo: escape labels in case ccanlint test keys have
389 * characters interpreted as GraphViz syntax.
391 printf("\t\"%p\" [label=\"%s\"%s]\n", i, i->key, style);
395 static void test_dgraph_edges(struct list_head *tests)
397 const struct ccanlint *i;
398 const struct dependent *d;
400 list_for_each(tests, i, list)
401 list_for_each(&i->dependencies, d, node)
402 printf("\t\"%p\" -> \"%p\"\n", d->dependent, i);
405 static char *test_dependency_graph(void *arg)
410 test_dgraph_vertices(&compulsory_tests, ", style=filled, fillcolor=yellow");
411 test_dgraph_vertices(&normal_tests, "");
413 test_dgraph_edges(&compulsory_tests);
414 test_dgraph_edges(&normal_tests);
421 /* Remove empty lines. */
422 static char **collapse(char **lines, unsigned int *nump)
425 for (i = j = 0; lines[i]; i++) {
427 lines[j++] = lines[i];
436 static void add_options(struct ccanlint *test, char **options,
437 unsigned int num_options)
444 /* -1, because last one is NULL. */
445 num = talloc_array_length(test->options) - 1;
447 test->options = talloc_realloc(NULL, test->options,
449 num + num_options + 1);
450 memcpy(&test->options[num], options, (num_options + 1)*sizeof(char *));
453 static void add_info_options(struct ccan_file *info, bool mark_fails)
455 struct doc_section *d;
457 struct ccanlint *test;
459 list_for_each(get_ccan_file_docs(info), d, list) {
460 if (!streq(d->type, "ccanlint"))
463 for (i = 0; i < d->num_lines; i++) {
464 unsigned int num_words;
465 char **words = collapse(strsplit(d, d->lines[i], " \t"),
470 if (strncmp(words[0], "//", 2) == 0)
473 test = find_test(words[0]);
475 warnx("%s: unknown ccanlint test '%s'",
476 info->fullname, words[0]);
481 warnx("%s: no argument to test '%s'",
482 info->fullname, words[0]);
487 if (strcasecmp(words[1], "FAIL") == 0) {
489 btree_insert(info_exclude, words[0]);
491 if (!test->takes_options)
492 warnx("%s: %s doesn't take options",
493 info->fullname, words[0]);
494 add_options(test, words+1, num_words-1);
500 /* If options are of form "filename:<option>" they only apply to that file */
501 char **per_file_options(const struct ccanlint *test, struct ccan_file *f)
504 unsigned int i, j = 0;
507 if (!test->options[0])
508 return test->options;
510 ret = talloc_array(f, char *, talloc_array_length(test->options));
511 for (i = 0; test->options[i]; i++) {
514 if (!test->options[i] || !strchr(test->options[i], ':')) {
515 optname = test->options[i];
516 } else if (strstarts(test->options[i], f->name)
517 && test->options[i][strlen(f->name)] == ':') {
518 optname = test->options[i] + strlen(f->name) + 1;
522 /* FAIL overrides anything else. */
523 if (streq(optname, "FAIL")) {
524 ret = talloc_array(f, char *, 2);
525 ret[0] = (char *)"FAIL";
533 /* Shrink it to size so talloc_array_length() works as expected. */
534 return talloc_realloc(NULL, ret, char *, j + 1);
537 static bool depends_on(struct ccanlint *i, struct ccanlint *target)
539 const struct dependent *d;
544 list_for_each(&i->dependencies, d, node) {
545 if (depends_on(d->dependent, target))
551 /* O(N^2), who cares? */
552 static void skip_unrelated_tests(struct ccanlint *target)
555 struct list_head *list;
557 foreach_ptr(list, &compulsory_tests, &normal_tests)
558 list_for_each(list, i, list)
559 if (!depends_on(i, target))
560 i->skip = "not relevant to target";
563 static char *demangle_string(char *string)
566 const char mapfrom[] = "abfnrtv";
567 const char mapto[] = "\a\b\f\n\r\t\v";
569 if (!strchr(string, '"'))
571 string = strchr(string, '"') + 1;
572 if (!strrchr(string, '"'))
574 *strrchr(string, '"') = '\0';
576 for (i = 0; i < strlen(string); i++) {
577 if (string[i] == '\\') {
580 const char *p = strchr(mapfrom, string[i+1]);
582 repl = mapto[p - mapfrom];
584 } else if (strlen(string+i+1) >= 3) {
585 if (string[i+1] == 'x') {
586 repl = (string[i+2]-'0')*16
589 } else if (cisdigit(string[i+1])) {
590 repl = (string[i+2]-'0')*8*8
591 + (string[i+3]-'0')*8
602 memmove(string + i + 1, string + i + len + 1,
603 strlen(string + i + len + 1) + 1);
611 static void read_config_header(void)
613 char *fname = talloc_asprintf(NULL, "%s/config.h", ccan_dir);
617 config_header = grab_file(NULL, fname, NULL);
618 if (!config_header) {
623 lines = strsplit(config_header, config_header, "\n");
624 for (i = 0; i < talloc_array_length(lines) - 1; i++) {
626 const char **line = (const char **)&lines[i];
628 if (!get_token(line, "#"))
630 if (!get_token(line, "define"))
632 sym = get_symbol_token(lines, line);
633 if (streq(sym, "CCAN_COMPILER") && !compiler) {
634 compiler = demangle_string(lines[i]);
636 errx(1, "%s:%u:could not parse CCAN_COMPILER",
639 printf("%s: compiler set to '%s'\n",
641 } else if (streq(sym, "CCAN_CFLAGS") && !cflags) {
642 cflags = demangle_string(lines[i]);
644 errx(1, "%s:%u:could not parse CCAN_CFLAGS",
647 printf("%s: compiler flags set to '%s'\n",
652 compiler = CCAN_COMPILER;
654 compiler = CCAN_CFLAGS;
657 static char *opt_set_const_charp(const char *arg, const char **p)
659 return opt_set_charp(arg, cast_const2(char **, p));
662 int main(int argc, char *argv[])
664 bool summary = false, pass = true;
668 const char *prefix = "";
669 char *dir = talloc_getcwd(NULL), *base_dir = dir, *target = NULL,
672 cmdline_exclude = btree_new(btree_strcmp);
673 info_exclude = btree_new(btree_strcmp);
675 opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
676 "do not compile anything");
677 opt_register_noarg("-l|--list-tests", list_tests, NULL,
678 "list tests ccanlint performs (and exit)");
679 opt_register_noarg("--test-dep-graph", test_dependency_graph, NULL,
680 "print dependency graph of tests in Graphviz .dot format");
681 opt_register_arg("-k|--keep <testname>", keep_test, NULL, NULL,
682 "keep results of <testname>"
683 " (can be used multiple times, or 'all')");
684 opt_register_noarg("--summary|-s", opt_set_bool, &summary,
685 "simply give one line summary");
686 opt_register_noarg("--verbose|-v", opt_inc_intval, &verbose,
687 "verbose mode (up to -vvvv)");
688 opt_register_arg("-x|--exclude <testname>", skip_test, NULL, NULL,
689 "exclude <testname> (can be used multiple times)");
690 opt_register_arg("-t|--timeout <milleseconds>", opt_set_uintval,
692 "ignore (terminate) tests that are slower than this");
693 opt_register_arg("--target <testname>", opt_set_charp,
695 "only run one test (and its prerequisites)");
696 opt_register_arg("--compiler <compiler>", opt_set_const_charp,
697 NULL, &compiler, "set the compiler");
698 opt_register_arg("--cflags <flags>", opt_set_const_charp,
699 NULL, &cflags, "set the compiler flags");
700 opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
701 "\nA program for checking and guiding development"
703 "This usage message");
705 /* We move into temporary directory, so gcov dumps its files there. */
706 if (chdir(temp_dir(talloc_autofree_context())) != 0)
707 err(1, "Error changing to %s temporary dir", temp_dir(NULL));
709 opt_parse(&argc, argv, opt_log_stderr_exit);
712 compile_verbose = true;
713 print_test_depends();
716 tools_verbose = true;
718 /* This links back to the module's test dir. */
719 testlink = talloc_asprintf(NULL, "%s/test", temp_dir(NULL));
721 /* Defaults to pwd. */
727 for (i = 1; i < argc; i++) {
728 unsigned int score, total_score;
732 dir = talloc_asprintf_append(NULL, "%s/%s",
734 while (strends(dir, "/"))
735 dir[strlen(dir)-1] = '\0';
739 prefix = talloc_append_string(talloc_basename(NULL,dir),
745 struct ccanlint *test;
747 test = find_test(target);
749 errx(1, "Unknown test to run '%s'", target);
750 skip_unrelated_tests(test);
753 m = get_manifest(talloc_autofree_context(), dir);
755 /* FIXME: This has to come after we've got manifest. */
757 read_config_header();
759 /* Create a symlink from temp dir back to src dir's
762 if (symlink(talloc_asprintf(m, "%s/test", dir), testlink) != 0)
763 err(1, "Creating test symlink in %s", temp_dir(NULL));
765 /* If you don't pass the compulsory tests, score is 0. */
766 score = total_score = 0;
767 while ((t = get_next_test(&compulsory_tests)) != NULL) {
768 if (!run_test(t, summary, &score, &total_score, m,
770 warnx("%s%s failed", prefix, t->name);
771 printf("%sTotal score: 0/%u\n",
772 prefix, total_score);
778 /* --target overrides known FAIL from _info */
780 add_info_options(m->info_file, !target);
782 while ((t = get_next_test(&normal_tests)) != NULL)
783 pass &= run_test(t, summary, &score, &total_score, m,
786 printf("%sTotal score: %u/%u\n", prefix, score, total_score);