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 bool targeting = false;
45 static struct btree *cmdline_exclude;
46 static struct btree *info_exclude;
47 static unsigned int timeout;
49 /* These are overridden at runtime if we can find config.h */
50 const char *compiler = NULL;
51 const char *cflags = NULL;
53 const char *config_header;
56 static void indent_print(const char *string)
59 unsigned int line = strcspn(string, "\n");
60 printf("\t%.*s", line, string);
61 if (string[line] == '\n') {
70 bool ask(const char *question)
74 printf("%s ", question);
77 return fgets(reply, sizeof(reply), stdin) != NULL
78 && toupper(reply[0]) == 'Y';
81 static const char *should_skip(struct manifest *m, struct ccanlint *i)
83 if (btree_lookup(cmdline_exclude, i->key))
84 return "excluded on command line";
86 if (btree_lookup(info_exclude, i->key))
87 return "excluded in _info file";
93 return "dependency failed";
100 static bool run_test(struct ccanlint *i,
102 unsigned int *running_score,
103 unsigned int *running_total,
107 unsigned int timeleft;
108 const struct dependent *d;
112 //one less test to run through
113 list_for_each(&i->dependencies, d, node)
114 d->dependent->num_depends--;
116 score = talloc(m, struct score);
117 list_head_init(&score->per_file_errors);
123 skip = should_skip(m, i);
127 if (verbose && !streq(skip, "not relevant to target"))
128 printf("%s%s: skipped (%s)\n", prefix, i->name, skip);
130 /* If we're skipping this because a prereq failed, we fail:
131 * count it as a score of 1. */
136 list_for_each(&i->dependencies, d, node) {
137 if (d->dependent->skip)
139 d->dependent->skip = "dependency was skipped";
140 d->dependent->skip_fail = i->skip_fail;
142 return i->skip_fail ? false : true;
145 timeleft = timeout ? timeout : default_timeout_ms;
146 i->check(m, i->keep_results, &timeleft, score);
147 if (timeout && timeleft == 0) {
152 assert(score->score <= score->total);
153 if ((!score->pass && !quiet)
154 || (score->score < score->total && verbose)
156 printf("%s%s (%s): %s",
157 prefix, i->name, i->key, score->pass ? "PASS" : "FAIL");
158 if (score->total > 1)
159 printf(" (+%u/%u)", score->score, score->total);
163 if ((!quiet && !score->pass) || verbose) {
165 printf("%s%s", score->error,
166 strends(score->error, "\n") ? "" : "\n");
169 if (!quiet && score->score < score->total && i->handle)
172 *running_score += score->score;
173 *running_total += score->total;
178 /* Skip any tests which depend on this one. */
179 list_for_each(&i->dependencies, d, node) {
180 if (d->dependent->skip)
182 d->dependent->skip = "dependency failed";
183 d->dependent->skip_fail = true;
189 static void register_test(struct ccanlint *test)
191 tlist_add(&tests, test, list);
192 test->options = talloc_array(NULL, char *, 1);
193 test->options[0] = NULL;
195 test->skip_fail = false;
199 * get_next_test - retrieves the next test to be processed
201 static inline struct ccanlint *get_next_test(void)
205 if (tlist_empty(&tests))
208 tlist_for_each(&tests, i, list) {
209 if (i->num_depends == 0)
212 errx(1, "Can't make process; test dependency cycle");
215 static struct ccanlint *find_test(const char *key)
219 tlist_for_each(&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-testlist"
237 static void init_tests(void)
240 struct btree *keys, *names;
245 #define REGISTER_TEST(name) register_test(&name)
246 #include "generated-testlist"
248 /* Initialize dependency lists. */
249 tlist_for_each(&tests, c, list) {
250 list_head_init(&c->dependencies);
254 /* Resolve dependencies. */
255 tlist_for_each(&tests, c, list) {
256 char **deps = strsplit(NULL, c->needs, " ");
259 for (i = 0; deps[i]; i++) {
260 struct ccanlint *dep;
261 struct dependent *dchild;
263 dep = find_test(deps[i]);
265 errx(1, "BUG: unknown dep '%s' for %s",
267 dchild = talloc(NULL, struct dependent);
268 dchild->dependent = c;
269 list_add_tail(&dep->dependencies, &dchild->node);
275 /* Self-consistency check: make sure no two tests
276 have the same key or name. */
277 keys = btree_new(btree_strcmp);
278 names = btree_new(btree_strcmp);
279 tlist_for_each(&tests, c, list) {
280 if (!btree_insert(keys, c->key))
281 errx(1, "BUG: Duplicate test key '%s'",
283 if (!btree_insert(names, c->name))
284 errx(1, "BUG: Duplicate test name '%s'",
292 static void print_test_depends(void)
297 tlist_for_each(&tests, c, list) {
298 if (!list_empty(&c->dependencies)) {
299 const struct dependent *d;
300 printf("These depend on %s:\n", c->key);
301 list_for_each(&c->dependencies, d, node)
302 printf("\t%s\n", d->dependent->key);
307 static int show_tmpdir(const char *dir)
309 printf("You can find ccanlint working files in '%s'\n", dir);
313 static char *keep_test(const char *testname, void *unused)
318 if (streq(testname, "all")) {
319 tlist_for_each(&tests, i, list)
320 i->keep_results = true;
322 i = find_test(testname);
324 errx(1, "No test %s to --keep", testname);
325 i->keep_results = true;
328 /* Don't automatically destroy temporary dir. */
329 talloc_set_destructor(temp_dir(NULL), show_tmpdir);
333 static char *skip_test(const char *testname, void *unused)
335 btree_insert(cmdline_exclude, testname);
339 static char *list_tests(void *arg)
346 /* This makes them print in topological order. */
347 while ((i = get_next_test()) != NULL) {
348 const struct dependent *d;
349 printf(" %-25s %s\n", i->key, i->name);
351 list_for_each(&i->dependencies, d, node)
352 d->dependent->num_depends--;
357 static void test_dgraph_vertices(const char *style)
359 const struct ccanlint *i;
361 tlist_for_each(&tests, i, list) {
363 * todo: escape labels in case ccanlint test keys have
364 * characters interpreted as GraphViz syntax.
366 printf("\t\"%p\" [label=\"%s\"%s]\n", i, i->key, style);
370 static void test_dgraph_edges(void)
372 const struct ccanlint *i;
373 const struct dependent *d;
375 tlist_for_each(&tests, i, list)
376 list_for_each(&i->dependencies, d, node)
377 printf("\t\"%p\" -> \"%p\"\n", d->dependent, i);
380 static char *test_dependency_graph(void *arg)
385 test_dgraph_vertices("");
393 /* Remove empty lines. */
394 static char **collapse(char **lines, unsigned int *nump)
397 for (i = j = 0; lines[i]; i++) {
399 lines[j++] = lines[i];
408 static void add_options(struct ccanlint *test, char **options,
409 unsigned int num_options)
416 /* -1, because last one is NULL. */
417 num = talloc_array_length(test->options) - 1;
419 test->options = talloc_realloc(NULL, test->options,
421 num + num_options + 1);
422 memcpy(&test->options[num], options, (num_options + 1)*sizeof(char *));
425 void add_info_options(struct ccan_file *info)
427 struct doc_section *d;
429 struct ccanlint *test;
431 list_for_each(get_ccan_file_docs(info), d, list) {
432 if (!streq(d->type, "ccanlint"))
435 for (i = 0; i < d->num_lines; i++) {
436 unsigned int num_words;
437 char **words = collapse(strsplit(d, d->lines[i], " \t"),
442 if (strncmp(words[0], "//", 2) == 0)
445 test = find_test(words[0]);
447 warnx("%s: unknown ccanlint test '%s'",
448 info->fullname, words[0]);
453 warnx("%s: no argument to test '%s'",
454 info->fullname, words[0]);
459 if (strcasecmp(words[1], "FAIL") == 0) {
461 btree_insert(info_exclude, words[0]);
463 if (!test->takes_options)
464 warnx("%s: %s doesn't take options",
465 info->fullname, words[0]);
466 add_options(test, words+1, num_words-1);
472 /* If options are of form "filename:<option>" they only apply to that file */
473 char **per_file_options(const struct ccanlint *test, struct ccan_file *f)
476 unsigned int i, j = 0;
479 if (!test->options[0])
480 return test->options;
482 ret = talloc_array(f, char *, talloc_array_length(test->options));
483 for (i = 0; test->options[i]; i++) {
486 if (!test->options[i] || !strchr(test->options[i], ':')) {
487 optname = test->options[i];
488 } else if (strstarts(test->options[i], f->name)
489 && test->options[i][strlen(f->name)] == ':') {
490 optname = test->options[i] + strlen(f->name) + 1;
494 /* FAIL overrides anything else. */
495 if (streq(optname, "FAIL")) {
496 ret = talloc_array(f, char *, 2);
497 ret[0] = (char *)"FAIL";
505 /* Shrink it to size so talloc_array_length() works as expected. */
506 return talloc_realloc(NULL, ret, char *, j + 1);
509 static bool depends_on(struct ccanlint *i, struct ccanlint *target)
511 const struct dependent *d;
516 list_for_each(&i->dependencies, d, node) {
517 if (depends_on(d->dependent, target))
523 /* O(N^2), who cares? */
524 static void skip_unrelated_tests(struct ccanlint *target)
528 tlist_for_each(&tests, i, list)
529 if (!depends_on(i, target))
530 i->skip = "not relevant to target";
533 static char *demangle_string(char *string)
536 const char mapfrom[] = "abfnrtv";
537 const char mapto[] = "\a\b\f\n\r\t\v";
539 if (!strchr(string, '"'))
541 string = strchr(string, '"') + 1;
542 if (!strrchr(string, '"'))
544 *strrchr(string, '"') = '\0';
546 for (i = 0; i < strlen(string); i++) {
547 if (string[i] == '\\') {
550 const char *p = strchr(mapfrom, string[i+1]);
552 repl = mapto[p - mapfrom];
554 } else if (strlen(string+i+1) >= 3) {
555 if (string[i+1] == 'x') {
556 repl = (string[i+2]-'0')*16
559 } else if (cisdigit(string[i+1])) {
560 repl = (string[i+2]-'0')*8*8
561 + (string[i+3]-'0')*8
572 memmove(string + i + 1, string + i + len + 1,
573 strlen(string + i + len + 1) + 1);
581 static void read_config_header(void)
583 char *fname = talloc_asprintf(NULL, "%s/config.h", ccan_dir);
587 config_header = grab_file(NULL, fname, NULL);
588 if (!config_header) {
593 lines = strsplit(config_header, config_header, "\n");
594 for (i = 0; i < talloc_array_length(lines) - 1; i++) {
596 const char **line = (const char **)&lines[i];
598 if (!get_token(line, "#"))
600 if (!get_token(line, "define"))
602 sym = get_symbol_token(lines, line);
603 if (streq(sym, "CCAN_COMPILER") && !compiler) {
604 compiler = demangle_string(lines[i]);
606 errx(1, "%s:%u:could not parse CCAN_COMPILER",
609 printf("%s: compiler set to '%s'\n",
611 } else if (streq(sym, "CCAN_CFLAGS") && !cflags) {
612 cflags = demangle_string(lines[i]);
614 errx(1, "%s:%u:could not parse CCAN_CFLAGS",
617 printf("%s: compiler flags set to '%s'\n",
622 compiler = CCAN_COMPILER;
624 compiler = CCAN_CFLAGS;
627 static char *opt_set_const_charp(const char *arg, const char **p)
629 return opt_set_charp(arg, cast_const2(char **, p));
632 int main(int argc, char *argv[])
634 bool summary = false, pass = true;
638 const char *prefix = "";
639 char *dir = talloc_getcwd(NULL), *base_dir = dir, *target = NULL,
642 cmdline_exclude = btree_new(btree_strcmp);
643 info_exclude = btree_new(btree_strcmp);
645 opt_register_early_noarg("--verbose|-v", opt_inc_intval, &verbose,
646 "verbose mode (up to -vvvv)");
647 opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
648 "do not compile anything");
649 opt_register_noarg("-l|--list-tests", list_tests, NULL,
650 "list tests ccanlint performs (and exit)");
651 opt_register_noarg("--test-dep-graph", test_dependency_graph, NULL,
652 "print dependency graph of tests in Graphviz .dot format");
653 opt_register_arg("-k|--keep <testname>", keep_test, NULL, NULL,
654 "keep results of <testname>"
655 " (can be used multiple times, or 'all')");
656 opt_register_noarg("--summary|-s", opt_set_bool, &summary,
657 "simply give one line summary");
658 opt_register_arg("-x|--exclude <testname>", skip_test, NULL, NULL,
659 "exclude <testname> (can be used multiple times)");
660 opt_register_arg("-t|--timeout <milleseconds>", opt_set_uintval,
662 "ignore (terminate) tests that are slower than this");
663 opt_register_arg("--target <testname>", opt_set_charp,
665 "only run one test (and its prerequisites)");
666 opt_register_arg("--compiler <compiler>", opt_set_const_charp,
667 NULL, &compiler, "set the compiler");
668 opt_register_arg("--cflags <flags>", opt_set_const_charp,
669 NULL, &cflags, "set the compiler flags");
670 opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
671 "\nA program for checking and guiding development"
673 "This usage message");
675 /* Do verbose before anything else... */
676 opt_early_parse(argc, argv, opt_log_stderr_exit);
678 /* We move into temporary directory, so gcov dumps its files there. */
679 if (chdir(temp_dir(talloc_autofree_context())) != 0)
680 err(1, "Error changing to %s temporary dir", temp_dir(NULL));
682 opt_parse(&argc, argv, opt_log_stderr_exit);
685 compile_verbose = true;
686 print_test_depends();
689 tools_verbose = true;
691 /* This links back to the module's test dir. */
692 testlink = talloc_asprintf(NULL, "%s/test", temp_dir(NULL));
694 /* Defaults to pwd. */
700 for (i = 1; i < argc; i++) {
701 unsigned int score, total_score;
706 dir = talloc_asprintf_append(NULL, "%s/%s",
708 while (strends(dir, "/"))
709 dir[strlen(dir)-1] = '\0';
713 prefix = talloc_append_string(talloc_basename(NULL,dir),
719 struct ccanlint *test;
721 test = find_test(target);
723 errx(1, "Unknown test to run '%s'", target);
724 skip_unrelated_tests(test);
727 m = get_manifest(talloc_autofree_context(), dir);
729 /* FIXME: This has to come after we've got manifest. */
731 read_config_header();
733 /* Create a symlink from temp dir back to src dir's
736 if (symlink(talloc_asprintf(m, "%s/test", dir), testlink) != 0)
737 err(1, "Creating test symlink in %s", temp_dir(NULL));
739 score = total_score = 0;
740 while ((t = get_next_test()) != NULL) {
741 if (!run_test(t, summary, &score, &total_score, m,
745 warnx("%s%s failed", prefix, t->name);
746 printf("%sTotal score: 0/%u\n",
747 prefix, total_score);
753 printf("%sTotal score: %u/%u\n", prefix, score, total_score);