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>
37 #include <ccan/strmap/strmap.h>
40 STRMAP_MEMBERS(struct ccanlint *);
44 static struct ccanlint_map tests;
45 bool safe_mode = false;
46 static bool targeting = false;
47 static struct btree *cmdline_exclude;
48 static struct btree *info_exclude;
49 static unsigned int timeout;
51 /* These are overridden at runtime if we can find config.h */
52 const char *compiler = NULL;
53 const char *cflags = NULL;
55 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 static const char *should_skip(struct manifest *m, struct ccanlint *i)
85 if (btree_lookup(cmdline_exclude, i->key))
86 return "excluded on command line";
88 if (btree_lookup(info_exclude, i->key))
89 return "excluded in _info file";
95 return "dependency failed";
102 static bool skip_node(struct dgraph_node *to, const char *failmsg)
104 struct ccanlint *c = container_of(to, struct ccanlint, node);
110 c->skip = "dependency was skipped";
116 static bool run_test(struct ccanlint *i,
118 unsigned int *running_score,
119 unsigned int *running_total,
123 unsigned int timeleft;
127 strmap_del(&tests, i->key, NULL);
129 if (!i->should_run) {
130 dgraph_clear_node(&i->node);
134 score = talloc(m, struct score);
135 list_head_init(&score->per_file_errors);
141 skip = should_skip(m, i);
146 printf("%s%s: skipped (%s)\n", prefix, i->name, skip);
148 /* If we're skipping this because a prereq failed, we fail:
149 * count it as a score of 1. */
153 dgraph_traverse_from(&i->node, skip_node,
154 i->skip_fail ? "dependency failed" : NULL);
155 dgraph_clear_node(&i->node);
156 return i->skip_fail ? false : true;
159 timeleft = timeout ? timeout : default_timeout_ms;
160 i->check(m, i->keep_results, &timeleft, score);
161 if (timeout && timeleft == 0) {
166 assert(score->score <= score->total);
167 if ((!score->pass && !quiet)
168 || (score->score < score->total && verbose)
170 printf("%s%s (%s): %s",
171 prefix, i->name, i->key, score->pass ? "PASS" : "FAIL");
172 if (score->total > 1)
173 printf(" (+%u/%u)", score->score, score->total);
177 if ((!quiet && !score->pass) || verbose) {
179 printf("%s%s", score->error,
180 strends(score->error, "\n") ? "" : "\n");
183 if (!quiet && score->score < score->total && i->handle)
186 *running_score += score->score;
187 *running_total += score->total;
190 /* Skip any tests which depend on this one. */
191 dgraph_traverse_from(&i->node, skip_node, "dependency failed");
193 dgraph_clear_node(&i->node);
197 static void register_test(struct ccanlint *test)
199 if (!strmap_add(&tests, test->key, test))
200 err(1, "Adding test %s", test->key);
201 test->options = talloc_array(NULL, char *, 1);
202 test->options[0] = NULL;
203 dgraph_init_node(&test->node);
206 static bool get_test(const char *member, struct ccanlint *i,
207 struct ccanlint **ret)
209 if (tlist_empty(&i->node.edge[DGRAPH_TO])) {
217 * get_next_test - retrieves the next test to be processed
219 static inline struct ccanlint *get_next_test(void)
221 struct ccanlint *i = NULL;
223 strmap_iterate(&tests, get_test, &i);
227 if (strmap_empty(&tests))
230 errx(1, "Can't make process; test dependency cycle");
233 static struct ccanlint *find_test(const char *key)
235 return strmap_get(&tests, key);
238 bool is_excluded(const char *name)
240 return btree_lookup(cmdline_exclude, name) != NULL
241 || btree_lookup(info_exclude, name) != NULL
242 || find_test(name)->skip != NULL;
245 static bool reset_deps(const char *member, struct ccanlint *c, void *unused)
247 char **deps = strsplit(NULL, c->needs, " ");
251 c->skip_fail = false;
252 for (i = 0; deps[i]; i++) {
253 struct ccanlint *dep;
255 dep = find_test(deps[i]);
257 errx(1, "BUG: unknown dep '%s' for %s",
259 dgraph_add_edge(&dep->node, &c->node);
265 static bool check_names(const char *member, struct ccanlint *c,
266 struct ccanlint_map *names)
268 if (!strmap_add(names, c->name, c))
269 err(1, "Duplicate name %s", c->name);
274 #define REGISTER_TEST(name, ...) extern struct ccanlint name
275 #include "generated-testlist"
277 static void init_tests(void)
279 struct ccanlint_map names;
284 #define REGISTER_TEST(name) register_test(&name)
285 #include "generated-testlist"
287 strmap_iterate(&tests, reset_deps, NULL);
289 /* Check for duplicate names. */
291 strmap_iterate(&tests, check_names, &names);
292 strmap_clear(&names);
295 static bool print_deps(const char *member, struct ccanlint *c, void *unused)
297 if (!tlist_empty(&c->node.edge[DGRAPH_FROM])) {
298 struct dgraph_edge *e;
300 printf("These depend on %s:\n", c->key);
301 dgraph_for_each_edge(&c->node, e, DGRAPH_FROM) {
302 struct ccanlint *to = container_of(e->n[DGRAPH_TO],
305 printf("\t%s\n", to->key);
311 static void print_test_depends(void)
315 strmap_iterate(&tests, print_deps, NULL);
319 static int show_tmpdir(const char *dir)
321 printf("You can find ccanlint working files in '%s'\n", dir);
325 static bool keep_one_test(const char *member, struct ccanlint *c, void *unused)
327 c->keep_results = true;
331 static char *keep_test(const char *testname, void *unused)
333 if (streq(testname, "all")) {
334 strmap_iterate(&tests, keep_one_test, NULL);
336 struct ccanlint *i = find_test(testname);
338 errx(1, "No test %s to --keep", testname);
339 keep_one_test(testname, i, NULL);
342 /* Don't automatically destroy temporary dir. */
343 talloc_set_destructor(temp_dir(NULL), show_tmpdir);
347 static char *skip_test(const char *testname, void *unused)
349 btree_insert(cmdline_exclude, testname);
353 static char *list_tests(void *arg)
358 /* This makes them print in topological order. */
359 while ((i = get_next_test()) != NULL) {
360 printf(" %-25s %s\n", i->key, i->name);
361 dgraph_clear_node(&i->node);
362 strmap_del(&tests, i->key, NULL);
367 static bool draw_test(const char *member, struct ccanlint *c, const char *style)
370 * todo: escape labels in case ccanlint test keys have
371 * characters interpreted as GraphViz syntax.
373 printf("\t\"%p\" [label=\"%s\"%s]\n", c, c->key, style);
377 static void test_dgraph_vertices(const char *style)
379 strmap_iterate(&tests, draw_test, style);
382 static bool draw_edges(const char *member, struct ccanlint *c, void *unused)
384 struct dgraph_edge *e;
386 dgraph_for_each_edge(&c->node, e, DGRAPH_FROM) {
387 struct ccanlint *to = container_of(e->n[DGRAPH_TO],
390 printf("\t\"%p\" -> \"%p\"\n", c->name, to->name);
395 static void test_dgraph_edges(void)
397 strmap_iterate(&tests, draw_edges, NULL);
400 static char *test_dependency_graph(void *arg)
404 test_dgraph_vertices("");
412 /* Remove empty lines. */
413 static char **collapse(char **lines, unsigned int *nump)
416 for (i = j = 0; lines[i]; i++) {
418 lines[j++] = lines[i];
427 static void add_options(struct ccanlint *test, char **options,
428 unsigned int num_options)
435 /* -1, because last one is NULL. */
436 num = talloc_array_length(test->options) - 1;
438 test->options = talloc_realloc(NULL, test->options,
440 num + num_options + 1);
441 memcpy(&test->options[num], options, (num_options + 1)*sizeof(char *));
444 void add_info_options(struct ccan_file *info)
446 struct doc_section *d;
448 struct ccanlint *test;
450 list_for_each(get_ccan_file_docs(info), d, list) {
451 if (!streq(d->type, "ccanlint"))
454 for (i = 0; i < d->num_lines; i++) {
455 unsigned int num_words;
456 char **words = collapse(strsplit(d, d->lines[i], " \t"),
461 if (strncmp(words[0], "//", 2) == 0)
464 test = find_test(words[0]);
466 warnx("%s: unknown ccanlint test '%s'",
467 info->fullname, words[0]);
472 warnx("%s: no argument to test '%s'",
473 info->fullname, words[0]);
478 if (strcasecmp(words[1], "FAIL") == 0) {
480 btree_insert(info_exclude, words[0]);
482 if (!test->takes_options)
483 warnx("%s: %s doesn't take options",
484 info->fullname, words[0]);
485 add_options(test, words+1, num_words-1);
491 /* If options are of form "filename:<option>" they only apply to that file */
492 char **per_file_options(const struct ccanlint *test, struct ccan_file *f)
495 unsigned int i, j = 0;
498 if (!test->options[0])
499 return test->options;
501 ret = talloc_array(f, char *, talloc_array_length(test->options));
502 for (i = 0; test->options[i]; i++) {
505 if (!test->options[i] || !strchr(test->options[i], ':')) {
506 optname = test->options[i];
507 } else if (strstarts(test->options[i], f->name)
508 && test->options[i][strlen(f->name)] == ':') {
509 optname = test->options[i] + strlen(f->name) + 1;
513 /* FAIL overrides anything else. */
514 if (streq(optname, "FAIL")) {
515 ret = talloc_array(f, char *, 2);
516 ret[0] = (char *)"FAIL";
524 /* Shrink it to size so talloc_array_length() works as expected. */
525 return talloc_realloc(NULL, ret, char *, j + 1);
528 static char *demangle_string(char *string)
531 const char mapfrom[] = "abfnrtv";
532 const char mapto[] = "\a\b\f\n\r\t\v";
534 if (!strchr(string, '"'))
536 string = strchr(string, '"') + 1;
537 if (!strrchr(string, '"'))
539 *strrchr(string, '"') = '\0';
541 for (i = 0; i < strlen(string); i++) {
542 if (string[i] == '\\') {
545 const char *p = strchr(mapfrom, string[i+1]);
547 repl = mapto[p - mapfrom];
549 } else if (strlen(string+i+1) >= 3) {
550 if (string[i+1] == 'x') {
551 repl = (string[i+2]-'0')*16
554 } else if (cisdigit(string[i+1])) {
555 repl = (string[i+2]-'0')*8*8
556 + (string[i+3]-'0')*8
567 memmove(string + i + 1, string + i + len + 1,
568 strlen(string + i + len + 1) + 1);
576 static void read_config_header(void)
578 char *fname = talloc_asprintf(NULL, "%s/config.h", ccan_dir);
582 config_header = grab_file(NULL, fname, NULL);
583 if (!config_header) {
588 lines = strsplit(config_header, config_header, "\n");
589 for (i = 0; i < talloc_array_length(lines) - 1; i++) {
591 const char **line = (const char **)&lines[i];
593 if (!get_token(line, "#"))
595 if (!get_token(line, "define"))
597 sym = get_symbol_token(lines, line);
598 if (streq(sym, "CCAN_COMPILER") && !compiler) {
599 compiler = demangle_string(lines[i]);
601 errx(1, "%s:%u:could not parse CCAN_COMPILER",
604 printf("%s: compiler set to '%s'\n",
606 } else if (streq(sym, "CCAN_CFLAGS") && !cflags) {
607 cflags = demangle_string(lines[i]);
609 errx(1, "%s:%u:could not parse CCAN_CFLAGS",
612 printf("%s: compiler flags set to '%s'\n",
617 compiler = CCAN_COMPILER;
619 compiler = CCAN_CFLAGS;
622 static char *opt_set_const_charp(const char *arg, const char **p)
624 return opt_set_charp(arg, cast_const2(char **, p));
627 static bool should_run_ccanlint(const char *name, struct ccanlint *c,
630 c->should_run = true;
634 static bool should_run_node(struct dgraph_node *to, void *unused)
636 should_run_ccanlint(NULL, container_of(to, struct ccanlint, node),
641 static char *add_target(const char *arg, bool *targeting)
643 struct ccanlint *c = find_test(arg);
645 return talloc_asprintf(NULL, "Unknown test %s", arg);
647 dgraph_traverse_to(&c->node, should_run_node, NULL);
652 int main(int argc, char *argv[])
654 bool summary = false, pass = true, targeting = false;
658 const char *prefix = "";
659 char *dir = talloc_getcwd(NULL), *base_dir = dir, *testlink;
661 cmdline_exclude = btree_new(btree_strcmp);
662 info_exclude = btree_new(btree_strcmp);
664 opt_register_early_noarg("--verbose|-v", opt_inc_intval, &verbose,
665 "verbose mode (up to -vvvv)");
666 opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
667 "do not compile anything");
668 opt_register_noarg("-l|--list-tests", list_tests, NULL,
669 "list tests ccanlint performs (and exit)");
670 opt_register_noarg("--test-dep-graph", test_dependency_graph, NULL,
671 "print dependency graph of tests in Graphviz .dot format");
672 opt_register_arg("-k|--keep <testname>", keep_test, NULL, NULL,
673 "keep results of <testname>"
674 " (can be used multiple times, or 'all')");
675 opt_register_noarg("--summary|-s", opt_set_bool, &summary,
676 "simply give one line summary");
677 opt_register_arg("-x|--exclude <testname>", skip_test, NULL, NULL,
678 "exclude <testname> (can be used multiple times)");
679 opt_register_arg("-t|--timeout <milleseconds>", opt_set_uintval,
681 "ignore (terminate) tests that are slower than this");
682 opt_register_arg("--target <testname>", add_target, NULL, &targeting,
683 "run this test and prerequisites (can specify multiple times)");
684 opt_register_arg("--compiler <compiler>", opt_set_const_charp,
685 NULL, &compiler, "set the compiler");
686 opt_register_arg("--cflags <flags>", opt_set_const_charp,
687 NULL, &cflags, "set the compiler flags");
688 opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
689 "\nA program for checking and guiding development"
691 "This usage message");
693 /* Do verbose before anything else... */
694 opt_early_parse(argc, argv, opt_log_stderr_exit);
696 /* We move into temporary directory, so gcov dumps its files there. */
697 if (chdir(temp_dir(talloc_autofree_context())) != 0)
698 err(1, "Error changing to %s temporary dir", temp_dir(NULL));
703 compile_verbose = true;
704 print_test_depends();
707 tools_verbose = true;
709 opt_parse(&argc, argv, opt_log_stderr_exit);
711 /* This links back to the module's test dir. */
712 testlink = talloc_asprintf(NULL, "%s/test", temp_dir(NULL));
714 /* If no --target, run all tests. */
716 strmap_iterate(&tests, should_run_ccanlint, NULL);
718 /* Defaults to pwd. */
724 for (i = 1; i < argc; i++) {
725 unsigned int score, total_score;
730 dir = talloc_asprintf_append(NULL, "%s/%s",
732 while (strends(dir, "/"))
733 dir[strlen(dir)-1] = '\0';
737 prefix = talloc_append_string(talloc_basename(NULL,dir),
742 m = get_manifest(talloc_autofree_context(), dir);
744 /* FIXME: This has to come after we've got manifest. */
746 read_config_header();
748 /* Create a symlink from temp dir back to src dir's
751 if (symlink(talloc_asprintf(m, "%s/test", dir), testlink) != 0)
752 err(1, "Creating test symlink in %s", temp_dir(NULL));
754 score = total_score = 0;
755 while ((t = get_next_test()) != NULL) {
756 if (!run_test(t, summary, &score, &total_score, m,
760 warnx("%s%s failed", prefix, t->name);
761 printf("%sTotal score: 0/%u\n",
762 prefix, total_score);
768 printf("%sTotal score: %u/%u\n", prefix, score, total_score);