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;
58 static void indent_print(const char *string)
61 unsigned int line = strcspn(string, "\n");
62 printf("\t%.*s", line, string);
63 if (string[line] == '\n') {
72 bool ask(const char *question)
76 printf("%s ", question);
79 return fgets(reply, sizeof(reply), stdin) != NULL
80 && toupper(reply[0]) == 'Y';
83 /* Skip, but don't remove. */
84 static bool skip_test(struct dgraph_node *node, const char *why)
86 struct ccanlint *c = container_of(node, struct ccanlint, node);
91 static const char *dep_failed(struct manifest *m)
93 return "dependency couldn't run";
96 static bool cannot_run(struct dgraph_node *node, void *unused)
98 struct ccanlint *c = container_of(node, struct ccanlint, node);
99 c->can_run = dep_failed;
105 unsigned int score, total;
111 static bool run_test(struct dgraph_node *n, struct run_info *run)
113 struct ccanlint *i = container_of(n, struct ccanlint, node);
114 unsigned int timeleft;
120 score = talloc(run->m, struct score);
121 list_head_init(&score->per_file_errors);
127 /* We can see skipped things in two cases:
128 * (1) _info excluded them (presumably because they fail).
129 * (2) A prerequisite failed.
133 printf("%s%s: skipped (%s)\n",
134 run->prefix, i->name, i->skip);
135 /* Pass us up to the test which failed, not us. */
141 i->skip = i->can_run(run->m);
143 /* Test doesn't apply, or can't run? That's OK. */
145 printf("%s%s: skipped (%s)\n",
146 run->prefix, i->name, i->skip);
147 /* Mark our dependencies to skip. */
148 dgraph_traverse_from(&i->node, cannot_run, NULL);
155 timeleft = timeout ? timeout : default_timeout_ms;
156 i->check(run->m, &timeleft, score);
157 if (timeout && timeleft == 0) {
160 printf("%s%s: skipped (%s)\n",
161 run->prefix, i->name, i->skip);
162 /* Mark our dependencies to skip. */
163 dgraph_traverse_from(&i->node, skip_test,
164 "dependency timed out");
170 assert(score->score <= score->total);
171 if ((!score->pass && !run->quiet)
172 || (score->score < score->total && verbose)
174 printf("%s%s (%s): %s",
175 run->prefix, i->name, i->key,
176 score->pass ? "PASS" : "FAIL");
177 if (score->total > 1)
178 printf(" (+%u/%u)", score->score, score->total);
182 if ((!run->quiet && !score->pass) || verbose) {
184 printf("%s%s", score->error,
185 strends(score->error, "\n") ? "" : "\n");
188 if (!run->quiet && score->score < score->total && i->handle)
189 i->handle(run->m, score);
192 /* Skip any tests which depend on this one. */
193 dgraph_traverse_from(&i->node, skip_test, "dependency failed");
197 run->score += score->score;
198 run->total += score->total;
200 /* FIXME: Free score. */
201 run->pass &= score->pass;
204 if (!score->pass && i->compulsory) {
205 warnx("%s%s failed", run->prefix, i->name);
212 static void register_test(struct ccanlint *test)
214 if (!strmap_add(&tests, test->key, test))
215 err(1, "Adding test %s", test->key);
216 test->options = talloc_array(NULL, char *, 1);
217 test->options[0] = NULL;
218 dgraph_init_node(&test->node);
221 static bool get_test(const char *member, struct ccanlint *i,
222 struct ccanlint **ret)
224 if (tlist_empty(&i->node.edge[DGRAPH_TO])) {
232 * get_next_test - retrieves the next test to be processed
234 static inline struct ccanlint *get_next_test(void)
236 struct ccanlint *i = NULL;
238 strmap_iterate(&tests, get_test, &i);
242 if (strmap_empty(&tests))
245 errx(1, "Can't make process; test dependency cycle");
248 static struct ccanlint *find_test(const char *key)
250 return strmap_get(&tests, key);
253 bool is_excluded(const char *name)
255 return find_test(name)->skip != NULL;
258 static bool init_deps(const char *member, struct ccanlint *c, void *unused)
260 char **deps = strsplit(NULL, c->needs, " ");
263 for (i = 0; deps[i]; i++) {
264 struct ccanlint *dep;
266 dep = find_test(deps[i]);
268 errx(1, "BUG: unknown dep '%s' for %s",
270 dgraph_add_edge(&dep->node, &c->node);
276 static bool check_names(const char *member, struct ccanlint *c,
277 struct ccanlint_map *names)
279 if (!strmap_add(names, c->name, c))
280 err(1, "Duplicate name %s", c->name);
284 static void init_tests(void)
286 struct ccanlint_map names;
287 struct ccanlint **table;
292 table = autodata_get(ccanlint_tests, &num);
293 for (i = 0; i < num; i++)
294 register_test(table[i]);
295 autodata_free(table);
297 strmap_iterate(&tests, init_deps, NULL);
299 /* Check for duplicate names. */
301 strmap_iterate(&tests, check_names, &names);
302 strmap_clear(&names);
305 static bool reset_test(struct dgraph_node *node, void *unused)
307 struct ccanlint *c = container_of(node, struct ccanlint, node);
313 static void reset_tests(struct dgraph_node *all)
315 dgraph_traverse_to(all, reset_test, NULL);
318 static bool print_deps(const char *member, struct ccanlint *c, void *unused)
320 if (!tlist_empty(&c->node.edge[DGRAPH_FROM])) {
321 struct dgraph_edge *e;
323 printf("These depend on %s:\n", c->key);
324 dgraph_for_each_edge(&c->node, e, DGRAPH_FROM) {
325 struct ccanlint *to = container_of(e->n[DGRAPH_TO],
328 printf("\t%s\n", to->key);
334 static void print_test_depends(void)
338 strmap_iterate(&tests, print_deps, NULL);
342 static int show_tmpdir(const char *dir)
344 printf("You can find ccanlint working files in '%s'\n", dir);
348 static char *keep_tests(void *unused)
352 /* Don't automatically destroy temporary dir. */
353 talloc_set_destructor(temp_dir(NULL), show_tmpdir);
357 static bool remove_test(struct dgraph_node *node, const char *why)
359 struct ccanlint *c = container_of(node, struct ccanlint, node);
361 dgraph_clear_node(node);
365 static char *exclude_test(const char *testname, void *unused)
367 struct ccanlint *i = find_test(testname);
369 return talloc_asprintf(NULL, "No test %s to --exclude",
372 /* Remove this, and everything which depends on it. */
373 dgraph_traverse_from(&i->node, remove_test, "excluded on command line");
374 remove_test(&i->node, "excluded on command line");
378 static void skip_test_and_deps(struct ccanlint *c, const char *why)
380 /* Skip this, and everything which depends on us. */
381 dgraph_traverse_from(&c->node, skip_test, why);
382 skip_test(&c->node, why);
385 static char *list_tests(void *arg)
390 /* This makes them print in topological order. */
391 while ((i = get_next_test()) != NULL) {
392 printf(" %-25s %s\n", i->key, i->name);
393 dgraph_clear_node(&i->node);
394 strmap_del(&tests, i->key, NULL);
399 static bool draw_test(const char *member, struct ccanlint *c, const char *style)
402 * todo: escape labels in case ccanlint test keys have
403 * characters interpreted as GraphViz syntax.
405 printf("\t\"%p\" [label=\"%s\"%s]\n", c, c->key, style);
409 static void test_dgraph_vertices(const char *style)
411 strmap_iterate(&tests, draw_test, style);
414 static bool draw_edges(const char *member, struct ccanlint *c, void *unused)
416 struct dgraph_edge *e;
418 dgraph_for_each_edge(&c->node, e, DGRAPH_FROM) {
419 struct ccanlint *to = container_of(e->n[DGRAPH_TO],
422 printf("\t\"%p\" -> \"%p\"\n", c->name, to->name);
427 static void test_dgraph_edges(void)
429 strmap_iterate(&tests, draw_edges, NULL);
432 static char *test_dependency_graph(void *arg)
436 test_dgraph_vertices("");
444 /* Remove empty lines. */
445 static char **collapse(char **lines, unsigned int *nump)
448 for (i = j = 0; lines[i]; i++) {
450 lines[j++] = lines[i];
459 static void add_options(struct ccanlint *test, char **options,
460 unsigned int num_options)
467 /* -1, because last one is NULL. */
468 num = talloc_array_length(test->options) - 1;
470 test->options = talloc_realloc(NULL, test->options,
472 num + num_options + 1);
473 memcpy(&test->options[num], options, (num_options + 1)*sizeof(char *));
476 void add_info_options(struct ccan_file *info)
478 struct doc_section *d;
480 struct ccanlint *test;
482 list_for_each(get_ccan_file_docs(info), d, list) {
483 if (!streq(d->type, "ccanlint"))
486 for (i = 0; i < d->num_lines; i++) {
487 unsigned int num_words;
488 char **words = collapse(strsplit(d, d->lines[i], " \t"),
493 if (strncmp(words[0], "//", 2) == 0)
496 test = find_test(words[0]);
498 warnx("%s: unknown ccanlint test '%s'",
499 info->fullname, words[0]);
504 warnx("%s: no argument to test '%s'",
505 info->fullname, words[0]);
510 if (strcasecmp(words[1], "FAIL") == 0) {
512 skip_test_and_deps(test,
516 if (!test->takes_options)
517 warnx("%s: %s doesn't take options",
518 info->fullname, words[0]);
519 add_options(test, words+1, num_words-1);
525 /* If options are of form "filename:<option>" they only apply to that file */
526 char **per_file_options(const struct ccanlint *test, struct ccan_file *f)
529 unsigned int i, j = 0;
532 if (!test->options[0])
533 return test->options;
535 ret = talloc_array(f, char *, talloc_array_length(test->options));
536 for (i = 0; test->options[i]; i++) {
539 if (!test->options[i] || !strchr(test->options[i], ':')) {
540 optname = test->options[i];
541 } else if (strstarts(test->options[i], f->name)
542 && test->options[i][strlen(f->name)] == ':') {
543 optname = test->options[i] + strlen(f->name) + 1;
547 /* FAIL overrides anything else. */
548 if (streq(optname, "FAIL")) {
549 ret = talloc_array(f, char *, 2);
550 ret[0] = (char *)"FAIL";
558 /* Shrink it to size so talloc_array_length() works as expected. */
559 return talloc_realloc(NULL, ret, char *, j + 1);
562 static char *demangle_string(char *string)
565 const char mapfrom[] = "abfnrtv";
566 const char mapto[] = "\a\b\f\n\r\t\v";
568 if (!strchr(string, '"'))
570 string = strchr(string, '"') + 1;
571 if (!strrchr(string, '"'))
573 *strrchr(string, '"') = '\0';
575 for (i = 0; i < strlen(string); i++) {
576 if (string[i] == '\\') {
579 const char *p = strchr(mapfrom, string[i+1]);
581 repl = mapto[p - mapfrom];
583 } else if (strlen(string+i+1) >= 3) {
584 if (string[i+1] == 'x') {
585 repl = (string[i+2]-'0')*16
588 } else if (cisdigit(string[i+1])) {
589 repl = (string[i+2]-'0')*8*8
590 + (string[i+3]-'0')*8
601 memmove(string + i + 1, string + i + len + 1,
602 strlen(string + i + len + 1) + 1);
610 static void read_config_header(void)
612 char *fname = talloc_asprintf(NULL, "%s/config.h", ccan_dir);
616 config_header = grab_file(NULL, fname, NULL);
617 if (!config_header) {
622 lines = strsplit(config_header, config_header, "\n");
623 for (i = 0; i < talloc_array_length(lines) - 1; i++) {
625 const char **line = (const char **)&lines[i];
627 if (!get_token(line, "#"))
629 if (!get_token(line, "define"))
631 sym = get_symbol_token(lines, line);
632 if (streq(sym, "CCAN_COMPILER") && !compiler) {
633 compiler = demangle_string(lines[i]);
635 errx(1, "%s:%u:could not parse CCAN_COMPILER",
638 printf("%s: compiler set to '%s'\n",
640 } else if (streq(sym, "CCAN_CFLAGS") && !cflags) {
641 cflags = demangle_string(lines[i]);
643 errx(1, "%s:%u:could not parse CCAN_CFLAGS",
646 printf("%s: compiler flags set to '%s'\n",
651 compiler = CCAN_COMPILER;
653 compiler = CCAN_CFLAGS;
656 static char *opt_set_const_charp(const char *arg, const char **p)
658 return opt_set_charp(arg, cast_const2(char **, p));
661 static char *opt_set_target(const char *arg, struct dgraph_node *all)
663 struct ccanlint *t = find_test(arg);
665 return talloc_asprintf(NULL, "unknown --target %s", arg);
668 dgraph_add_edge(&t->node, all);
672 static bool run_tests(struct dgraph_node *all,
682 run.score = run.total = 0;
685 dgraph_traverse_to(all, run_test, &run);
687 printf("%sTotal score: %u/%u\n", prefix, run.score, run.total);
691 static bool add_to_all(const char *member, struct ccanlint *c,
692 struct dgraph_node *all)
694 /* If we're excluded on cmdline, don't add. */
696 dgraph_add_edge(&c->node, all);
700 int main(int argc, char *argv[])
702 bool summary = false, pass = true;
705 const char *prefix = "";
706 char *dir = talloc_getcwd(NULL), *base_dir = dir, *testlink;
707 struct dgraph_node all;
709 /* Empty graph node to which we attach everything else. */
710 dgraph_init_node(&all);
712 opt_register_early_noarg("--verbose|-v", opt_inc_intval, &verbose,
713 "verbose mode (up to -vvvv)");
714 opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
715 "do not compile anything");
716 opt_register_noarg("-l|--list-tests", list_tests, NULL,
717 "list tests ccanlint performs (and exit)");
718 opt_register_noarg("--test-dep-graph", test_dependency_graph, NULL,
719 "print dependency graph of tests in Graphviz .dot format");
720 opt_register_noarg("-k|--keep", keep_tests, NULL,
721 "do not delete ccanlint working files");
722 opt_register_noarg("--summary|-s", opt_set_bool, &summary,
723 "simply give one line summary");
724 opt_register_arg("-x|--exclude <testname>", exclude_test, NULL, NULL,
725 "exclude <testname> (can be used multiple times)");
726 opt_register_arg("--timeout <milleseconds>", opt_set_uintval,
728 "ignore (terminate) tests that are slower than this");
729 opt_register_arg("-t|--target <testname>", opt_set_target, NULL, &all,
730 "only run one test (and its prerequisites)");
731 opt_register_arg("--compiler <compiler>", opt_set_const_charp,
732 NULL, &compiler, "set the compiler");
733 opt_register_arg("--cflags <flags>", opt_set_const_charp,
734 NULL, &cflags, "set the compiler flags");
735 opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
736 "\nA program for checking and guiding development"
738 "This usage message");
740 /* Do verbose before anything else... */
741 opt_early_parse(argc, argv, opt_log_stderr_exit);
743 /* We move into temporary directory, so gcov dumps its files there. */
744 if (chdir(temp_dir(talloc_autofree_context())) != 0)
745 err(1, "Error changing to %s temporary dir", temp_dir(NULL));
750 compile_verbose = true;
751 print_test_depends();
754 tools_verbose = true;
756 opt_parse(&argc, argv, opt_log_stderr_exit);
759 strmap_iterate(&tests, add_to_all, &all);
761 /* This links back to the module's test dir. */
762 testlink = talloc_asprintf(NULL, "%s/test", temp_dir(NULL));
764 /* Defaults to pwd. */
770 for (i = 1; i < argc; i++) {
771 unsigned int score, total_score;
776 dir = talloc_asprintf_append(NULL, "%s/%s",
778 while (strends(dir, "/"))
779 dir[strlen(dir)-1] = '\0';
782 /* We assume there's a ccan/ in there somewhere... */
784 ccan_dir = find_ccan_dir(dir);
786 errx(1, "Cannot find ccan/ base directory in %s",
788 read_config_header();
792 prefix = talloc_append_string(talloc_basename(NULL,dir),
795 m = get_manifest(talloc_autofree_context(), dir);
797 /* Create a symlink from temp dir back to src dir's
800 if (symlink(talloc_asprintf(m, "%s/test", dir), testlink) != 0)
801 err(1, "Creating test symlink in %s", temp_dir(NULL));
803 score = total_score = 0;
804 if (!run_tests(&all, summary, m, prefix))