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 static bool targeting = false;
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 /* Skip, but don't remove. */
81 static bool skip_test(struct dgraph_node *node, const char *why)
83 struct ccanlint *c = container_of(node, struct ccanlint, node);
88 static const char *dep_failed(struct manifest *m)
90 return "dependency couldn't run";
93 static bool cannot_run(struct dgraph_node *node, void *unused)
95 struct ccanlint *c = container_of(node, struct ccanlint, node);
96 c->can_run = dep_failed;
102 unsigned int score, total;
108 static bool run_test(struct dgraph_node *n, struct run_info *run)
110 struct ccanlint *i = container_of(n, struct ccanlint, node);
111 unsigned int timeleft;
117 score = talloc(run->m, struct score);
118 list_head_init(&score->per_file_errors);
124 /* We can see skipped things in two cases:
125 * (1) _info excluded them (presumably because they fail).
126 * (2) A prerequisite failed.
130 printf("%s%s: skipped (%s)\n",
131 run->prefix, i->name, i->skip);
132 /* Pass us up to the test which failed, not us. */
138 i->skip = i->can_run(run->m);
140 /* Test doesn't apply, or can't run? That's OK. */
142 printf("%s%s: skipped (%s)\n",
143 run->prefix, i->name, i->skip);
144 /* Mark our dependencies to skip. */
145 dgraph_traverse_from(&i->node, cannot_run, NULL);
152 timeleft = timeout ? timeout : default_timeout_ms;
153 i->check(run->m, i->keep_results, &timeleft, score);
154 if (timeout && timeleft == 0) {
157 printf("%s%s: skipped (%s)\n",
158 run->prefix, i->name, i->skip);
159 /* Mark our dependencies to skip. */
160 dgraph_traverse_from(&i->node, skip_test,
161 "dependency timed out");
167 assert(score->score <= score->total);
168 if ((!score->pass && !run->quiet)
169 || (score->score < score->total && verbose)
171 printf("%s%s (%s): %s",
172 run->prefix, i->name, i->key,
173 score->pass ? "PASS" : "FAIL");
174 if (score->total > 1)
175 printf(" (+%u/%u)", score->score, score->total);
179 if ((!run->quiet && !score->pass) || verbose) {
181 printf("%s%s", score->error,
182 strends(score->error, "\n") ? "" : "\n");
185 if (!run->quiet && score->score < score->total && i->handle)
186 i->handle(run->m, score);
189 /* Skip any tests which depend on this one. */
190 dgraph_traverse_from(&i->node, skip_test, "dependency failed");
194 run->score += score->score;
195 run->total += score->total;
197 /* FIXME: Free score. */
198 run->pass &= score->pass;
201 if (!score->pass && i->compulsory) {
202 warnx("%s%s failed", run->prefix, i->name);
209 static void register_test(struct ccanlint *test)
211 if (!strmap_add(&tests, test->key, test))
212 err(1, "Adding test %s", test->key);
213 test->options = talloc_array(NULL, char *, 1);
214 test->options[0] = NULL;
215 dgraph_init_node(&test->node);
218 static bool get_test(const char *member, struct ccanlint *i,
219 struct ccanlint **ret)
221 if (tlist_empty(&i->node.edge[DGRAPH_TO])) {
229 * get_next_test - retrieves the next test to be processed
231 static inline struct ccanlint *get_next_test(void)
233 struct ccanlint *i = NULL;
235 strmap_iterate(&tests, get_test, &i);
239 if (strmap_empty(&tests))
242 errx(1, "Can't make process; test dependency cycle");
245 static struct ccanlint *find_test(const char *key)
247 return strmap_get(&tests, key);
250 bool is_excluded(const char *name)
252 return find_test(name)->skip != NULL;
255 static bool init_deps(const char *member, struct ccanlint *c, void *unused)
257 char **deps = strsplit(NULL, c->needs, " ");
260 for (i = 0; deps[i]; i++) {
261 struct ccanlint *dep;
263 dep = find_test(deps[i]);
265 errx(1, "BUG: unknown dep '%s' for %s",
267 dgraph_add_edge(&dep->node, &c->node);
273 static bool check_names(const char *member, struct ccanlint *c,
274 struct ccanlint_map *names)
276 if (!strmap_add(names, c->name, c))
277 err(1, "Duplicate name %s", c->name);
282 #define REGISTER_TEST(name, ...) extern struct ccanlint name
283 #include "generated-testlist"
285 static void init_tests(void)
287 struct ccanlint_map names;
292 #define REGISTER_TEST(name) register_test(&name)
293 #include "generated-testlist"
295 strmap_iterate(&tests, init_deps, NULL);
297 /* Check for duplicate names. */
299 strmap_iterate(&tests, check_names, &names);
300 strmap_clear(&names);
303 static bool reset_test(struct dgraph_node *node, void *unused)
305 struct ccanlint *c = container_of(node, struct ccanlint, node);
311 static void reset_tests(struct dgraph_node *all)
313 dgraph_traverse_to(all, reset_test, NULL);
316 static bool print_deps(const char *member, struct ccanlint *c, void *unused)
318 if (!tlist_empty(&c->node.edge[DGRAPH_FROM])) {
319 struct dgraph_edge *e;
321 printf("These depend on %s:\n", c->key);
322 dgraph_for_each_edge(&c->node, e, DGRAPH_FROM) {
323 struct ccanlint *to = container_of(e->n[DGRAPH_TO],
326 printf("\t%s\n", to->key);
332 static void print_test_depends(void)
336 strmap_iterate(&tests, print_deps, NULL);
340 static int show_tmpdir(const char *dir)
342 printf("You can find ccanlint working files in '%s'\n", dir);
346 static bool keep_one_test(const char *member, struct ccanlint *c, void *unused)
348 c->keep_results = true;
352 static char *keep_test(const char *testname, void *unused)
354 if (streq(testname, "all")) {
355 strmap_iterate(&tests, keep_one_test, NULL);
357 struct ccanlint *i = find_test(testname);
359 errx(1, "No test %s to --keep", testname);
360 keep_one_test(testname, i, NULL);
363 /* Don't automatically destroy temporary dir. */
364 talloc_set_destructor(temp_dir(NULL), show_tmpdir);
368 static bool remove_test(struct dgraph_node *node, const char *why)
370 struct ccanlint *c = container_of(node, struct ccanlint, node);
372 dgraph_clear_node(node);
376 static char *exclude_test(const char *testname, void *unused)
378 struct ccanlint *i = find_test(testname);
380 return talloc_asprintf(NULL, "No test %s to --exclude",
383 /* Remove this, and everything which depends on it. */
384 dgraph_traverse_from(&i->node, remove_test, "excluded on command line");
385 remove_test(&i->node, "excluded on command line");
389 static void skip_test_and_deps(struct ccanlint *c, const char *why)
391 /* Skip this, and everything which depends on us. */
392 dgraph_traverse_from(&c->node, skip_test, why);
393 skip_test(&c->node, why);
396 static char *list_tests(void *arg)
401 /* This makes them print in topological order. */
402 while ((i = get_next_test()) != NULL) {
403 printf(" %-25s %s\n", i->key, i->name);
404 dgraph_clear_node(&i->node);
405 strmap_del(&tests, i->key, NULL);
410 static bool draw_test(const char *member, struct ccanlint *c, const char *style)
413 * todo: escape labels in case ccanlint test keys have
414 * characters interpreted as GraphViz syntax.
416 printf("\t\"%p\" [label=\"%s\"%s]\n", c, c->key, style);
420 static void test_dgraph_vertices(const char *style)
422 strmap_iterate(&tests, draw_test, style);
425 static bool draw_edges(const char *member, struct ccanlint *c, void *unused)
427 struct dgraph_edge *e;
429 dgraph_for_each_edge(&c->node, e, DGRAPH_FROM) {
430 struct ccanlint *to = container_of(e->n[DGRAPH_TO],
433 printf("\t\"%p\" -> \"%p\"\n", c->name, to->name);
438 static void test_dgraph_edges(void)
440 strmap_iterate(&tests, draw_edges, NULL);
443 static char *test_dependency_graph(void *arg)
447 test_dgraph_vertices("");
455 /* Remove empty lines. */
456 static char **collapse(char **lines, unsigned int *nump)
459 for (i = j = 0; lines[i]; i++) {
461 lines[j++] = lines[i];
470 static void add_options(struct ccanlint *test, char **options,
471 unsigned int num_options)
478 /* -1, because last one is NULL. */
479 num = talloc_array_length(test->options) - 1;
481 test->options = talloc_realloc(NULL, test->options,
483 num + num_options + 1);
484 memcpy(&test->options[num], options, (num_options + 1)*sizeof(char *));
487 void add_info_options(struct ccan_file *info)
489 struct doc_section *d;
491 struct ccanlint *test;
493 list_for_each(get_ccan_file_docs(info), d, list) {
494 if (!streq(d->type, "ccanlint"))
497 for (i = 0; i < d->num_lines; i++) {
498 unsigned int num_words;
499 char **words = collapse(strsplit(d, d->lines[i], " \t"),
504 if (strncmp(words[0], "//", 2) == 0)
507 test = find_test(words[0]);
509 warnx("%s: unknown ccanlint test '%s'",
510 info->fullname, words[0]);
515 warnx("%s: no argument to test '%s'",
516 info->fullname, words[0]);
521 if (strcasecmp(words[1], "FAIL") == 0) {
523 skip_test_and_deps(test,
527 if (!test->takes_options)
528 warnx("%s: %s doesn't take options",
529 info->fullname, words[0]);
530 add_options(test, words+1, num_words-1);
536 /* If options are of form "filename:<option>" they only apply to that file */
537 char **per_file_options(const struct ccanlint *test, struct ccan_file *f)
540 unsigned int i, j = 0;
543 if (!test->options[0])
544 return test->options;
546 ret = talloc_array(f, char *, talloc_array_length(test->options));
547 for (i = 0; test->options[i]; i++) {
550 if (!test->options[i] || !strchr(test->options[i], ':')) {
551 optname = test->options[i];
552 } else if (strstarts(test->options[i], f->name)
553 && test->options[i][strlen(f->name)] == ':') {
554 optname = test->options[i] + strlen(f->name) + 1;
558 /* FAIL overrides anything else. */
559 if (streq(optname, "FAIL")) {
560 ret = talloc_array(f, char *, 2);
561 ret[0] = (char *)"FAIL";
569 /* Shrink it to size so talloc_array_length() works as expected. */
570 return talloc_realloc(NULL, ret, char *, j + 1);
573 static char *demangle_string(char *string)
576 const char mapfrom[] = "abfnrtv";
577 const char mapto[] = "\a\b\f\n\r\t\v";
579 if (!strchr(string, '"'))
581 string = strchr(string, '"') + 1;
582 if (!strrchr(string, '"'))
584 *strrchr(string, '"') = '\0';
586 for (i = 0; i < strlen(string); i++) {
587 if (string[i] == '\\') {
590 const char *p = strchr(mapfrom, string[i+1]);
592 repl = mapto[p - mapfrom];
594 } else if (strlen(string+i+1) >= 3) {
595 if (string[i+1] == 'x') {
596 repl = (string[i+2]-'0')*16
599 } else if (cisdigit(string[i+1])) {
600 repl = (string[i+2]-'0')*8*8
601 + (string[i+3]-'0')*8
612 memmove(string + i + 1, string + i + len + 1,
613 strlen(string + i + len + 1) + 1);
621 static void read_config_header(void)
623 char *fname = talloc_asprintf(NULL, "%s/config.h", ccan_dir);
627 config_header = grab_file(NULL, fname, NULL);
628 if (!config_header) {
633 lines = strsplit(config_header, config_header, "\n");
634 for (i = 0; i < talloc_array_length(lines) - 1; i++) {
636 const char **line = (const char **)&lines[i];
638 if (!get_token(line, "#"))
640 if (!get_token(line, "define"))
642 sym = get_symbol_token(lines, line);
643 if (streq(sym, "CCAN_COMPILER") && !compiler) {
644 compiler = demangle_string(lines[i]);
646 errx(1, "%s:%u:could not parse CCAN_COMPILER",
649 printf("%s: compiler set to '%s'\n",
651 } else if (streq(sym, "CCAN_CFLAGS") && !cflags) {
652 cflags = demangle_string(lines[i]);
654 errx(1, "%s:%u:could not parse CCAN_CFLAGS",
657 printf("%s: compiler flags set to '%s'\n",
662 compiler = CCAN_COMPILER;
664 compiler = CCAN_CFLAGS;
667 static char *opt_set_const_charp(const char *arg, const char **p)
669 return opt_set_charp(arg, cast_const2(char **, p));
672 static char *opt_set_target(const char *arg, struct dgraph_node *all)
674 struct ccanlint *t = find_test(arg);
676 return talloc_asprintf(NULL, "unknown --target %s", arg);
679 dgraph_add_edge(&t->node, all);
683 static bool run_tests(struct dgraph_node *all,
693 run.score = run.total = 0;
696 dgraph_traverse_to(all, run_test, &run);
698 printf("%sTotal score: %u/%u\n", prefix, run.score, run.total);
702 static bool add_to_all(const char *member, struct ccanlint *c,
703 struct dgraph_node *all)
705 dgraph_add_edge(&c->node, all);
709 int main(int argc, char *argv[])
711 bool summary = false, pass = true;
714 const char *prefix = "";
715 char *dir = talloc_getcwd(NULL), *base_dir = dir, *testlink;
716 struct dgraph_node all;
718 /* Empty graph node to which we attach everything else. */
719 dgraph_init_node(&all);
721 opt_register_early_noarg("--verbose|-v", opt_inc_intval, &verbose,
722 "verbose mode (up to -vvvv)");
723 opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
724 "do not compile anything");
725 opt_register_noarg("-l|--list-tests", list_tests, NULL,
726 "list tests ccanlint performs (and exit)");
727 opt_register_noarg("--test-dep-graph", test_dependency_graph, NULL,
728 "print dependency graph of tests in Graphviz .dot format");
729 opt_register_arg("-k|--keep <testname>", keep_test, NULL, NULL,
730 "keep results of <testname>"
731 " (can be used multiple times, or 'all')");
732 opt_register_noarg("--summary|-s", opt_set_bool, &summary,
733 "simply give one line summary");
734 opt_register_arg("-x|--exclude <testname>", exclude_test, NULL, NULL,
735 "exclude <testname> (can be used multiple times)");
736 opt_register_arg("--timeout <milleseconds>", opt_set_uintval,
738 "ignore (terminate) tests that are slower than this");
739 opt_register_arg("-t|--target <testname>", opt_set_target, NULL, &all,
740 "only run one test (and its prerequisites)");
741 opt_register_arg("--compiler <compiler>", opt_set_const_charp,
742 NULL, &compiler, "set the compiler");
743 opt_register_arg("--cflags <flags>", opt_set_const_charp,
744 NULL, &cflags, "set the compiler flags");
745 opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
746 "\nA program for checking and guiding development"
748 "This usage message");
750 /* Do verbose before anything else... */
751 opt_early_parse(argc, argv, opt_log_stderr_exit);
753 /* We move into temporary directory, so gcov dumps its files there. */
754 if (chdir(temp_dir(talloc_autofree_context())) != 0)
755 err(1, "Error changing to %s temporary dir", temp_dir(NULL));
760 compile_verbose = true;
761 print_test_depends();
764 tools_verbose = true;
766 opt_parse(&argc, argv, opt_log_stderr_exit);
769 strmap_iterate(&tests, add_to_all, &all);
771 /* This links back to the module's test dir. */
772 testlink = talloc_asprintf(NULL, "%s/test", temp_dir(NULL));
774 /* Defaults to pwd. */
780 for (i = 1; i < argc; i++) {
781 unsigned int score, total_score;
786 dir = talloc_asprintf_append(NULL, "%s/%s",
788 while (strends(dir, "/"))
789 dir[strlen(dir)-1] = '\0';
793 prefix = talloc_append_string(talloc_basename(NULL,dir),
796 m = get_manifest(talloc_autofree_context(), dir);
798 /* FIXME: This has to come after we've got manifest. */
800 read_config_header();
802 /* Create a symlink from temp dir back to src dir's
805 if (symlink(talloc_asprintf(m, "%s/test", dir), testlink) != 0)
806 err(1, "Creating test symlink in %s", temp_dir(NULL));
808 score = total_score = 0;
809 if (!run_tests(&all, summary, m, prefix))