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>
36 #include <ccan/tlist/tlist.h>
38 /* Defines struct tlist_ccanlint. */
39 TLIST_TYPE(ccanlint, struct ccanlint);
42 static struct tlist_ccanlint tests = TLIST_INIT(tests);
43 bool safe_mode = false;
44 static struct btree *cmdline_exclude;
45 static struct btree *info_exclude;
46 static unsigned int timeout;
48 /* These are overridden at runtime if we can find config.h */
49 const char *compiler = NULL;
50 const char *cflags = NULL;
52 const char *config_header;
55 static void indent_print(const char *string)
58 unsigned int line = strcspn(string, "\n");
59 printf("\t%.*s", line, string);
60 if (string[line] == '\n') {
69 bool ask(const char *question)
73 printf("%s ", question);
76 return fgets(reply, sizeof(reply), stdin) != NULL
77 && toupper(reply[0]) == 'Y';
80 static const char *should_skip(struct manifest *m, struct ccanlint *i)
82 if (btree_lookup(cmdline_exclude, i->key))
83 return "excluded on command line";
85 if (btree_lookup(info_exclude, i->key))
86 return "excluded in _info file";
92 return "dependency failed";
99 static bool run_test(struct ccanlint *i,
101 unsigned int *running_score,
102 unsigned int *running_total,
106 unsigned int timeleft;
107 const struct dependent *d;
111 //one less test to run through
112 list_for_each(&i->dependencies, d, node)
113 d->dependent->num_depends--;
115 score = talloc(m, struct score);
116 list_head_init(&score->per_file_errors);
122 skip = should_skip(m, i);
126 if (verbose && !streq(skip, "not relevant to target"))
127 printf("%s%s: skipped (%s)\n", prefix, i->name, skip);
129 /* If we're skipping this because a prereq failed, we fail:
130 * count it as a score of 1. */
135 list_for_each(&i->dependencies, d, node) {
136 if (d->dependent->skip)
138 d->dependent->skip = "dependency was skipped";
139 d->dependent->skip_fail = i->skip_fail;
141 return i->skip_fail ? false : true;
144 timeleft = timeout ? timeout : default_timeout_ms;
145 i->check(m, i->keep_results, &timeleft, score);
146 if (timeout && timeleft == 0) {
151 assert(score->score <= score->total);
152 if ((!score->pass && !quiet)
153 || (score->score < score->total && verbose)
155 printf("%s%s (%s): %s",
156 prefix, i->name, i->key, score->pass ? "PASS" : "FAIL");
157 if (score->total > 1)
158 printf(" (+%u/%u)", score->score, score->total);
162 if ((!quiet && !score->pass) || verbose) {
164 printf("%s%s", score->error,
165 strends(score->error, "\n") ? "" : "\n");
168 if (!quiet && score->score < score->total && i->handle)
171 *running_score += score->score;
172 *running_total += score->total;
177 /* Skip any tests which depend on this one. */
178 list_for_each(&i->dependencies, d, node) {
179 if (d->dependent->skip)
181 d->dependent->skip = "dependency failed";
182 d->dependent->skip_fail = true;
188 static void register_test(struct ccanlint *test)
190 tlist_add(&tests, test, list);
191 test->options = talloc_array(NULL, char *, 1);
192 test->options[0] = NULL;
194 test->skip_fail = false;
198 * get_next_test - retrieves the next test to be processed
200 static inline struct ccanlint *get_next_test(void)
204 if (tlist_empty(&tests))
207 tlist_for_each(&tests, i, list) {
208 if (i->num_depends == 0)
211 errx(1, "Can't make process; test dependency cycle");
214 static struct ccanlint *find_test(const char *key)
218 tlist_for_each(&tests, i, list)
219 if (streq(i->key, key))
225 bool is_excluded(const char *name)
227 return btree_lookup(cmdline_exclude, name) != NULL
228 || btree_lookup(info_exclude, name) != NULL
229 || find_test(name)->skip != NULL;
233 #define REGISTER_TEST(name, ...) extern struct ccanlint name
234 #include "generated-testlist"
236 static void init_tests(void)
239 struct btree *keys, *names;
244 #define REGISTER_TEST(name) register_test(&name)
245 #include "generated-testlist"
247 /* Initialize dependency lists. */
248 tlist_for_each(&tests, c, list) {
249 list_head_init(&c->dependencies);
253 /* Resolve dependencies. */
254 tlist_for_each(&tests, c, list) {
255 char **deps = strsplit(NULL, c->needs, " ");
258 for (i = 0; deps[i]; i++) {
259 struct ccanlint *dep;
260 struct dependent *dchild;
262 dep = find_test(deps[i]);
264 errx(1, "BUG: unknown dep '%s' for %s",
266 dchild = talloc(NULL, struct dependent);
267 dchild->dependent = c;
268 list_add_tail(&dep->dependencies, &dchild->node);
274 /* Self-consistency check: make sure no two tests
275 have the same key or name. */
276 keys = btree_new(btree_strcmp);
277 names = btree_new(btree_strcmp);
278 tlist_for_each(&tests, c, list) {
279 if (!btree_insert(keys, c->key))
280 errx(1, "BUG: Duplicate test key '%s'",
282 if (!btree_insert(names, c->name))
283 errx(1, "BUG: Duplicate test name '%s'",
291 static void print_test_depends(void)
296 tlist_for_each(&tests, c, list) {
297 if (!list_empty(&c->dependencies)) {
298 const struct dependent *d;
299 printf("These depend on %s:\n", c->key);
300 list_for_each(&c->dependencies, d, node)
301 printf("\t%s\n", d->dependent->key);
306 static int show_tmpdir(const char *dir)
308 printf("You can find ccanlint working files in '%s'\n", dir);
312 static char *keep_test(const char *testname, void *unused)
317 if (streq(testname, "all")) {
318 tlist_for_each(&tests, i, list)
319 i->keep_results = true;
321 i = find_test(testname);
323 errx(1, "No test %s to --keep", testname);
324 i->keep_results = true;
327 /* Don't automatically destroy temporary dir. */
328 talloc_set_destructor(temp_dir(NULL), show_tmpdir);
332 static char *skip_test(const char *testname, void *unused)
334 btree_insert(cmdline_exclude, testname);
338 static char *list_tests(void *arg)
345 /* This makes them print in topological order. */
346 while ((i = get_next_test()) != NULL) {
347 const struct dependent *d;
348 printf(" %-25s %s\n", i->key, i->name);
350 list_for_each(&i->dependencies, d, node)
351 d->dependent->num_depends--;
356 static void test_dgraph_vertices(const char *style)
358 const struct ccanlint *i;
360 tlist_for_each(&tests, i, list) {
362 * todo: escape labels in case ccanlint test keys have
363 * characters interpreted as GraphViz syntax.
365 printf("\t\"%p\" [label=\"%s\"%s]\n", i, i->key, style);
369 static void test_dgraph_edges(void)
371 const struct ccanlint *i;
372 const struct dependent *d;
374 tlist_for_each(&tests, i, list)
375 list_for_each(&i->dependencies, d, node)
376 printf("\t\"%p\" -> \"%p\"\n", d->dependent, i);
379 static char *test_dependency_graph(void *arg)
384 test_dgraph_vertices("");
392 /* Remove empty lines. */
393 static char **collapse(char **lines, unsigned int *nump)
396 for (i = j = 0; lines[i]; i++) {
398 lines[j++] = lines[i];
407 static void add_options(struct ccanlint *test, char **options,
408 unsigned int num_options)
415 /* -1, because last one is NULL. */
416 num = talloc_array_length(test->options) - 1;
418 test->options = talloc_realloc(NULL, test->options,
420 num + num_options + 1);
421 memcpy(&test->options[num], options, (num_options + 1)*sizeof(char *));
424 static void add_info_options(struct ccan_file *info, bool mark_fails)
426 struct doc_section *d;
428 struct ccanlint *test;
430 list_for_each(get_ccan_file_docs(info), d, list) {
431 if (!streq(d->type, "ccanlint"))
434 for (i = 0; i < d->num_lines; i++) {
435 unsigned int num_words;
436 char **words = collapse(strsplit(d, d->lines[i], " \t"),
441 if (strncmp(words[0], "//", 2) == 0)
444 test = find_test(words[0]);
446 warnx("%s: unknown ccanlint test '%s'",
447 info->fullname, words[0]);
452 warnx("%s: no argument to test '%s'",
453 info->fullname, words[0]);
458 if (strcasecmp(words[1], "FAIL") == 0) {
460 btree_insert(info_exclude, words[0]);
462 if (!test->takes_options)
463 warnx("%s: %s doesn't take options",
464 info->fullname, words[0]);
465 add_options(test, words+1, num_words-1);
471 /* If options are of form "filename:<option>" they only apply to that file */
472 char **per_file_options(const struct ccanlint *test, struct ccan_file *f)
475 unsigned int i, j = 0;
478 if (!test->options[0])
479 return test->options;
481 ret = talloc_array(f, char *, talloc_array_length(test->options));
482 for (i = 0; test->options[i]; i++) {
485 if (!test->options[i] || !strchr(test->options[i], ':')) {
486 optname = test->options[i];
487 } else if (strstarts(test->options[i], f->name)
488 && test->options[i][strlen(f->name)] == ':') {
489 optname = test->options[i] + strlen(f->name) + 1;
493 /* FAIL overrides anything else. */
494 if (streq(optname, "FAIL")) {
495 ret = talloc_array(f, char *, 2);
496 ret[0] = (char *)"FAIL";
504 /* Shrink it to size so talloc_array_length() works as expected. */
505 return talloc_realloc(NULL, ret, char *, j + 1);
508 static bool depends_on(struct ccanlint *i, struct ccanlint *target)
510 const struct dependent *d;
515 list_for_each(&i->dependencies, d, node) {
516 if (depends_on(d->dependent, target))
522 /* O(N^2), who cares? */
523 static void skip_unrelated_tests(struct ccanlint *target)
527 tlist_for_each(&tests, i, list)
528 if (!depends_on(i, target))
529 i->skip = "not relevant to target";
532 static char *demangle_string(char *string)
535 const char mapfrom[] = "abfnrtv";
536 const char mapto[] = "\a\b\f\n\r\t\v";
538 if (!strchr(string, '"'))
540 string = strchr(string, '"') + 1;
541 if (!strrchr(string, '"'))
543 *strrchr(string, '"') = '\0';
545 for (i = 0; i < strlen(string); i++) {
546 if (string[i] == '\\') {
549 const char *p = strchr(mapfrom, string[i+1]);
551 repl = mapto[p - mapfrom];
553 } else if (strlen(string+i+1) >= 3) {
554 if (string[i+1] == 'x') {
555 repl = (string[i+2]-'0')*16
558 } else if (cisdigit(string[i+1])) {
559 repl = (string[i+2]-'0')*8*8
560 + (string[i+3]-'0')*8
571 memmove(string + i + 1, string + i + len + 1,
572 strlen(string + i + len + 1) + 1);
580 static void read_config_header(void)
582 char *fname = talloc_asprintf(NULL, "%s/config.h", ccan_dir);
586 config_header = grab_file(NULL, fname, NULL);
587 if (!config_header) {
592 lines = strsplit(config_header, config_header, "\n");
593 for (i = 0; i < talloc_array_length(lines) - 1; i++) {
595 const char **line = (const char **)&lines[i];
597 if (!get_token(line, "#"))
599 if (!get_token(line, "define"))
601 sym = get_symbol_token(lines, line);
602 if (streq(sym, "CCAN_COMPILER") && !compiler) {
603 compiler = demangle_string(lines[i]);
605 errx(1, "%s:%u:could not parse CCAN_COMPILER",
608 printf("%s: compiler set to '%s'\n",
610 } else if (streq(sym, "CCAN_CFLAGS") && !cflags) {
611 cflags = demangle_string(lines[i]);
613 errx(1, "%s:%u:could not parse CCAN_CFLAGS",
616 printf("%s: compiler flags set to '%s'\n",
621 compiler = CCAN_COMPILER;
623 compiler = CCAN_CFLAGS;
626 static char *opt_set_const_charp(const char *arg, const char **p)
628 return opt_set_charp(arg, cast_const2(char **, p));
631 int main(int argc, char *argv[])
633 bool summary = false, pass = true;
637 const char *prefix = "";
638 char *dir = talloc_getcwd(NULL), *base_dir = dir, *target = NULL,
641 cmdline_exclude = btree_new(btree_strcmp);
642 info_exclude = btree_new(btree_strcmp);
644 opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
645 "do not compile anything");
646 opt_register_noarg("-l|--list-tests", list_tests, NULL,
647 "list tests ccanlint performs (and exit)");
648 opt_register_noarg("--test-dep-graph", test_dependency_graph, NULL,
649 "print dependency graph of tests in Graphviz .dot format");
650 opt_register_arg("-k|--keep <testname>", keep_test, NULL, NULL,
651 "keep results of <testname>"
652 " (can be used multiple times, or 'all')");
653 opt_register_noarg("--summary|-s", opt_set_bool, &summary,
654 "simply give one line summary");
655 opt_register_noarg("--verbose|-v", opt_inc_intval, &verbose,
656 "verbose mode (up to -vvvv)");
657 opt_register_arg("-x|--exclude <testname>", skip_test, NULL, NULL,
658 "exclude <testname> (can be used multiple times)");
659 opt_register_arg("-t|--timeout <milleseconds>", opt_set_uintval,
661 "ignore (terminate) tests that are slower than this");
662 opt_register_arg("--target <testname>", opt_set_charp,
664 "only run one test (and its prerequisites)");
665 opt_register_arg("--compiler <compiler>", opt_set_const_charp,
666 NULL, &compiler, "set the compiler");
667 opt_register_arg("--cflags <flags>", opt_set_const_charp,
668 NULL, &cflags, "set the compiler flags");
669 opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
670 "\nA program for checking and guiding development"
672 "This usage message");
674 /* We move into temporary directory, so gcov dumps its files there. */
675 if (chdir(temp_dir(talloc_autofree_context())) != 0)
676 err(1, "Error changing to %s temporary dir", temp_dir(NULL));
678 opt_parse(&argc, argv, opt_log_stderr_exit);
681 compile_verbose = true;
682 print_test_depends();
685 tools_verbose = true;
687 /* This links back to the module's test dir. */
688 testlink = talloc_asprintf(NULL, "%s/test", temp_dir(NULL));
690 /* Defaults to pwd. */
696 for (i = 1; i < argc; i++) {
697 unsigned int score, total_score;
698 bool added_info_options = false;
703 dir = talloc_asprintf_append(NULL, "%s/%s",
705 while (strends(dir, "/"))
706 dir[strlen(dir)-1] = '\0';
710 prefix = talloc_append_string(talloc_basename(NULL,dir),
716 struct ccanlint *test;
718 test = find_test(target);
720 errx(1, "Unknown test to run '%s'", target);
721 skip_unrelated_tests(test);
724 m = get_manifest(talloc_autofree_context(), dir);
726 /* FIXME: This has to come after we've got manifest. */
728 read_config_header();
730 /* Create a symlink from temp dir back to src dir's
733 if (symlink(talloc_asprintf(m, "%s/test", dir), testlink) != 0)
734 err(1, "Creating test symlink in %s", temp_dir(NULL));
736 score = total_score = 0;
737 while ((t = get_next_test()) != NULL) {
738 if (!run_test(t, summary, &score, &total_score, m,
742 warnx("%s%s failed", prefix, t->name);
743 printf("%sTotal score: 0/%u\n",
744 prefix, total_score);
749 /* --target overrides known FAIL from _info */
750 if (!added_info_options && m->info_file) {
751 add_info_options(m->info_file, !target);
752 added_info_options = true;
756 printf("%sTotal score: %u/%u\n", prefix, score, total_score);