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/str/str.h>
29 #include <ccan/str_talloc/str_talloc.h>
30 #include <ccan/talloc/talloc.h>
31 #include <ccan/opt/opt.h>
32 #include <ccan/foreach/foreach.h>
33 #include <ccan/grab_file/grab_file.h>
34 #include <ccan/cast/cast.h>
35 #include <ccan/tlist/tlist.h>
36 #include <ccan/strmap/strmap.h>
39 STRMAP_MEMBERS(struct ccanlint *);
43 static struct ccanlint_map tests;
44 bool safe_mode = false;
45 bool keep_results = false;
46 static bool targeting = false;
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 /* Skip, but don't remove. */
82 static bool skip_test(struct dgraph_node *node, const char *why)
84 struct ccanlint *c = container_of(node, struct ccanlint, node);
89 static const char *dep_failed(struct manifest *m)
91 return "dependency couldn't run";
94 static bool cannot_run(struct dgraph_node *node, void *unused)
96 struct ccanlint *c = container_of(node, struct ccanlint, node);
97 c->can_run = dep_failed;
103 unsigned int score, total;
109 static bool run_test(struct dgraph_node *n, struct run_info *run)
111 struct ccanlint *i = container_of(n, struct ccanlint, node);
112 unsigned int timeleft;
118 score = talloc(run->m, struct score);
119 list_head_init(&score->per_file_errors);
125 /* We can see skipped things in two cases:
126 * (1) _info excluded them (presumably because they fail).
127 * (2) A prerequisite failed.
131 printf("%s%s: skipped (%s)\n",
132 run->prefix, i->name, i->skip);
133 /* Pass us up to the test which failed, not us. */
139 i->skip = i->can_run(run->m);
141 /* Test doesn't apply, or can't run? That's OK. */
143 printf("%s%s: skipped (%s)\n",
144 run->prefix, i->name, i->skip);
145 /* Mark our dependencies to skip. */
146 dgraph_traverse_from(&i->node, cannot_run, NULL);
153 timeleft = timeout ? timeout : default_timeout_ms;
154 i->check(run->m, &timeleft, score);
155 if (timeout && timeleft == 0) {
158 printf("%s%s: skipped (%s)\n",
159 run->prefix, i->name, i->skip);
160 /* Mark our dependencies to skip. */
161 dgraph_traverse_from(&i->node, skip_test,
162 "dependency timed out");
168 assert(score->score <= score->total);
169 if ((!score->pass && !run->quiet)
170 || (score->score < score->total && verbose)
172 printf("%s%s (%s): %s",
173 run->prefix, i->name, i->key,
174 score->pass ? "PASS" : "FAIL");
175 if (score->total > 1)
176 printf(" (+%u/%u)", score->score, score->total);
180 if ((!run->quiet && !score->pass) || verbose) {
182 printf("%s%s", score->error,
183 strends(score->error, "\n") ? "" : "\n");
186 if (!run->quiet && score->score < score->total && i->handle)
187 i->handle(run->m, score);
190 /* Skip any tests which depend on this one. */
191 dgraph_traverse_from(&i->node, skip_test, "dependency failed");
195 run->score += score->score;
196 run->total += score->total;
198 /* FIXME: Free score. */
199 run->pass &= score->pass;
202 if (!score->pass && i->compulsory) {
203 warnx("%s%s failed", run->prefix, i->name);
210 static void register_test(struct ccanlint *test)
212 if (!strmap_add(&tests, test->key, test))
213 err(1, "Adding test %s", test->key);
214 test->options = talloc_array(NULL, char *, 1);
215 test->options[0] = NULL;
216 dgraph_init_node(&test->node);
219 static bool get_test(const char *member, struct ccanlint *i,
220 struct ccanlint **ret)
222 if (tlist_empty(&i->node.edge[DGRAPH_TO])) {
230 * get_next_test - retrieves the next test to be processed
232 static inline struct ccanlint *get_next_test(void)
234 struct ccanlint *i = NULL;
236 strmap_iterate(&tests, get_test, &i);
240 if (strmap_empty(&tests))
243 errx(1, "Can't make process; test dependency cycle");
246 static struct ccanlint *find_test(const char *key)
248 return strmap_get(&tests, key);
251 bool is_excluded(const char *name)
253 return find_test(name)->skip != NULL;
256 static bool init_deps(const char *member, struct ccanlint *c, void *unused)
258 char **deps = strsplit(NULL, c->needs, " ");
261 for (i = 0; deps[i]; i++) {
262 struct ccanlint *dep;
264 dep = find_test(deps[i]);
266 errx(1, "BUG: unknown dep '%s' for %s",
268 dgraph_add_edge(&dep->node, &c->node);
274 static bool check_names(const char *member, struct ccanlint *c,
275 struct ccanlint_map *names)
277 if (!strmap_add(names, c->name, c))
278 err(1, "Duplicate name %s", c->name);
283 #define REGISTER_TEST(name, ...) extern struct ccanlint name
284 #include "generated-testlist"
286 static void init_tests(void)
288 struct ccanlint_map names;
293 #define REGISTER_TEST(name) register_test(&name)
294 #include "generated-testlist"
296 strmap_iterate(&tests, init_deps, NULL);
298 /* Check for duplicate names. */
300 strmap_iterate(&tests, check_names, &names);
301 strmap_clear(&names);
304 static bool reset_test(struct dgraph_node *node, void *unused)
306 struct ccanlint *c = container_of(node, struct ccanlint, node);
312 static void reset_tests(struct dgraph_node *all)
314 dgraph_traverse_to(all, reset_test, NULL);
317 static bool print_deps(const char *member, struct ccanlint *c, void *unused)
319 if (!tlist_empty(&c->node.edge[DGRAPH_FROM])) {
320 struct dgraph_edge *e;
322 printf("These depend on %s:\n", c->key);
323 dgraph_for_each_edge(&c->node, e, DGRAPH_FROM) {
324 struct ccanlint *to = container_of(e->n[DGRAPH_TO],
327 printf("\t%s\n", to->key);
333 static void print_test_depends(void)
337 strmap_iterate(&tests, print_deps, NULL);
341 static int show_tmpdir(const char *dir)
343 printf("You can find ccanlint working files in '%s'\n", dir);
347 static char *keep_tests(void *unused)
351 /* Don't automatically destroy temporary dir. */
352 talloc_set_destructor(temp_dir(NULL), show_tmpdir);
356 static bool remove_test(struct dgraph_node *node, const char *why)
358 struct ccanlint *c = container_of(node, struct ccanlint, node);
360 dgraph_clear_node(node);
364 static char *exclude_test(const char *testname, void *unused)
366 struct ccanlint *i = find_test(testname);
368 return talloc_asprintf(NULL, "No test %s to --exclude",
371 /* Remove this, and everything which depends on it. */
372 dgraph_traverse_from(&i->node, remove_test, "excluded on command line");
373 remove_test(&i->node, "excluded on command line");
377 static void skip_test_and_deps(struct ccanlint *c, const char *why)
379 /* Skip this, and everything which depends on us. */
380 dgraph_traverse_from(&c->node, skip_test, why);
381 skip_test(&c->node, why);
384 static char *list_tests(void *arg)
389 /* This makes them print in topological order. */
390 while ((i = get_next_test()) != NULL) {
391 printf(" %-25s %s\n", i->key, i->name);
392 dgraph_clear_node(&i->node);
393 strmap_del(&tests, i->key, NULL);
398 static bool draw_test(const char *member, struct ccanlint *c, const char *style)
401 * todo: escape labels in case ccanlint test keys have
402 * characters interpreted as GraphViz syntax.
404 printf("\t\"%p\" [label=\"%s\"%s]\n", c, c->key, style);
408 static void test_dgraph_vertices(const char *style)
410 strmap_iterate(&tests, draw_test, style);
413 static bool draw_edges(const char *member, struct ccanlint *c, void *unused)
415 struct dgraph_edge *e;
417 dgraph_for_each_edge(&c->node, e, DGRAPH_FROM) {
418 struct ccanlint *to = container_of(e->n[DGRAPH_TO],
421 printf("\t\"%p\" -> \"%p\"\n", c->name, to->name);
426 static void test_dgraph_edges(void)
428 strmap_iterate(&tests, draw_edges, NULL);
431 static char *test_dependency_graph(void *arg)
435 test_dgraph_vertices("");
443 /* Remove empty lines. */
444 static char **collapse(char **lines, unsigned int *nump)
447 for (i = j = 0; lines[i]; i++) {
449 lines[j++] = lines[i];
458 static void add_options(struct ccanlint *test, char **options,
459 unsigned int num_options)
466 /* -1, because last one is NULL. */
467 num = talloc_array_length(test->options) - 1;
469 test->options = talloc_realloc(NULL, test->options,
471 num + num_options + 1);
472 memcpy(&test->options[num], options, (num_options + 1)*sizeof(char *));
475 void add_info_options(struct ccan_file *info)
477 struct doc_section *d;
479 struct ccanlint *test;
481 list_for_each(get_ccan_file_docs(info), d, list) {
482 if (!streq(d->type, "ccanlint"))
485 for (i = 0; i < d->num_lines; i++) {
486 unsigned int num_words;
487 char **words = collapse(strsplit(d, d->lines[i], " \t"),
492 if (strncmp(words[0], "//", 2) == 0)
495 test = find_test(words[0]);
497 warnx("%s: unknown ccanlint test '%s'",
498 info->fullname, words[0]);
503 warnx("%s: no argument to test '%s'",
504 info->fullname, words[0]);
509 if (strcasecmp(words[1], "FAIL") == 0) {
511 skip_test_and_deps(test,
515 if (!test->takes_options)
516 warnx("%s: %s doesn't take options",
517 info->fullname, words[0]);
518 add_options(test, words+1, num_words-1);
524 /* If options are of form "filename:<option>" they only apply to that file */
525 char **per_file_options(const struct ccanlint *test, struct ccan_file *f)
528 unsigned int i, j = 0;
531 if (!test->options[0])
532 return test->options;
534 ret = talloc_array(f, char *, talloc_array_length(test->options));
535 for (i = 0; test->options[i]; i++) {
538 if (!test->options[i] || !strchr(test->options[i], ':')) {
539 optname = test->options[i];
540 } else if (strstarts(test->options[i], f->name)
541 && test->options[i][strlen(f->name)] == ':') {
542 optname = test->options[i] + strlen(f->name) + 1;
546 /* FAIL overrides anything else. */
547 if (streq(optname, "FAIL")) {
548 ret = talloc_array(f, char *, 2);
549 ret[0] = (char *)"FAIL";
557 /* Shrink it to size so talloc_array_length() works as expected. */
558 return talloc_realloc(NULL, ret, char *, j + 1);
561 static char *demangle_string(char *string)
564 const char mapfrom[] = "abfnrtv";
565 const char mapto[] = "\a\b\f\n\r\t\v";
567 if (!strchr(string, '"'))
569 string = strchr(string, '"') + 1;
570 if (!strrchr(string, '"'))
572 *strrchr(string, '"') = '\0';
574 for (i = 0; i < strlen(string); i++) {
575 if (string[i] == '\\') {
578 const char *p = strchr(mapfrom, string[i+1]);
580 repl = mapto[p - mapfrom];
582 } else if (strlen(string+i+1) >= 3) {
583 if (string[i+1] == 'x') {
584 repl = (string[i+2]-'0')*16
587 } else if (cisdigit(string[i+1])) {
588 repl = (string[i+2]-'0')*8*8
589 + (string[i+3]-'0')*8
600 memmove(string + i + 1, string + i + len + 1,
601 strlen(string + i + len + 1) + 1);
609 static void read_config_header(void)
611 char *fname = talloc_asprintf(NULL, "%s/config.h", ccan_dir);
615 config_header = grab_file(NULL, fname, NULL);
616 if (!config_header) {
621 lines = strsplit(config_header, config_header, "\n");
622 for (i = 0; i < talloc_array_length(lines) - 1; i++) {
624 const char **line = (const char **)&lines[i];
626 if (!get_token(line, "#"))
628 if (!get_token(line, "define"))
630 sym = get_symbol_token(lines, line);
631 if (streq(sym, "CCAN_COMPILER") && !compiler) {
632 compiler = demangle_string(lines[i]);
634 errx(1, "%s:%u:could not parse CCAN_COMPILER",
637 printf("%s: compiler set to '%s'\n",
639 } else if (streq(sym, "CCAN_CFLAGS") && !cflags) {
640 cflags = demangle_string(lines[i]);
642 errx(1, "%s:%u:could not parse CCAN_CFLAGS",
645 printf("%s: compiler flags set to '%s'\n",
650 compiler = CCAN_COMPILER;
652 compiler = CCAN_CFLAGS;
655 static char *opt_set_const_charp(const char *arg, const char **p)
657 return opt_set_charp(arg, cast_const2(char **, p));
660 static char *opt_set_target(const char *arg, struct dgraph_node *all)
662 struct ccanlint *t = find_test(arg);
664 return talloc_asprintf(NULL, "unknown --target %s", arg);
667 dgraph_add_edge(&t->node, all);
671 static bool run_tests(struct dgraph_node *all,
681 run.score = run.total = 0;
684 dgraph_traverse_to(all, run_test, &run);
686 printf("%sTotal score: %u/%u\n", prefix, run.score, run.total);
690 static bool add_to_all(const char *member, struct ccanlint *c,
691 struct dgraph_node *all)
693 dgraph_add_edge(&c->node, all);
697 int main(int argc, char *argv[])
699 bool summary = false, pass = true;
702 const char *prefix = "";
703 char *dir = talloc_getcwd(NULL), *base_dir = dir, *testlink;
704 struct dgraph_node all;
706 /* Empty graph node to which we attach everything else. */
707 dgraph_init_node(&all);
709 opt_register_early_noarg("--verbose|-v", opt_inc_intval, &verbose,
710 "verbose mode (up to -vvvv)");
711 opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
712 "do not compile anything");
713 opt_register_noarg("-l|--list-tests", list_tests, NULL,
714 "list tests ccanlint performs (and exit)");
715 opt_register_noarg("--test-dep-graph", test_dependency_graph, NULL,
716 "print dependency graph of tests in Graphviz .dot format");
717 opt_register_noarg("-k|--keep", keep_tests, NULL,
718 "do not delete ccanlint working files");
719 opt_register_noarg("--summary|-s", opt_set_bool, &summary,
720 "simply give one line summary");
721 opt_register_arg("-x|--exclude <testname>", exclude_test, NULL, NULL,
722 "exclude <testname> (can be used multiple times)");
723 opt_register_arg("--timeout <milleseconds>", opt_set_uintval,
725 "ignore (terminate) tests that are slower than this");
726 opt_register_arg("-t|--target <testname>", opt_set_target, NULL, &all,
727 "only run one test (and its prerequisites)");
728 opt_register_arg("--compiler <compiler>", opt_set_const_charp,
729 NULL, &compiler, "set the compiler");
730 opt_register_arg("--cflags <flags>", opt_set_const_charp,
731 NULL, &cflags, "set the compiler flags");
732 opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
733 "\nA program for checking and guiding development"
735 "This usage message");
737 /* Do verbose before anything else... */
738 opt_early_parse(argc, argv, opt_log_stderr_exit);
740 /* We move into temporary directory, so gcov dumps its files there. */
741 if (chdir(temp_dir(talloc_autofree_context())) != 0)
742 err(1, "Error changing to %s temporary dir", temp_dir(NULL));
747 compile_verbose = true;
748 print_test_depends();
751 tools_verbose = true;
753 opt_parse(&argc, argv, opt_log_stderr_exit);
756 strmap_iterate(&tests, add_to_all, &all);
758 /* This links back to the module's test dir. */
759 testlink = talloc_asprintf(NULL, "%s/test", temp_dir(NULL));
761 /* Defaults to pwd. */
767 for (i = 1; i < argc; i++) {
768 unsigned int score, total_score;
773 dir = talloc_asprintf_append(NULL, "%s/%s",
775 while (strends(dir, "/"))
776 dir[strlen(dir)-1] = '\0';
780 prefix = talloc_append_string(talloc_basename(NULL,dir),
783 m = get_manifest(talloc_autofree_context(), dir);
785 /* FIXME: This has to come after we've got manifest. */
787 read_config_header();
789 /* Create a symlink from temp dir back to src dir's
792 if (symlink(talloc_asprintf(m, "%s/test", dir), testlink) != 0)
793 err(1, "Creating test symlink in %s", temp_dir(NULL));
795 score = total_score = 0;
796 if (!run_tests(&all, summary, m, prefix))