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;
50 static struct dgraph_node all;
52 /* These are overridden at runtime if we can find config.h */
53 const char *compiler = NULL;
54 const char *cflags = NULL;
56 const char *config_header;
59 static void indent_print(const char *string)
62 unsigned int line = strcspn(string, "\n");
63 printf("\t%.*s", line, string);
64 if (string[line] == '\n') {
73 bool ask(const char *question)
77 printf("%s ", question);
80 return fgets(reply, sizeof(reply), stdin) != NULL
81 && toupper(reply[0]) == 'Y';
84 static const char *should_skip(struct manifest *m, struct ccanlint *i)
86 if (btree_lookup(cmdline_exclude, i->key))
87 return "excluded on command line";
89 if (btree_lookup(info_exclude, i->key))
90 return "excluded in _info file";
96 return "dependency failed";
103 static bool skip_node(struct dgraph_node *to, const char *failmsg)
105 struct ccanlint *c = container_of(to, struct ccanlint, node);
111 c->skip = "dependency was skipped";
119 unsigned int score, total;
125 static bool run_test(struct dgraph_node *n, struct run_info *run)
127 struct ccanlint *i = container_of(n, struct ccanlint, node);
128 unsigned int timeleft;
135 score = talloc(run->m, struct score);
136 list_head_init(&score->per_file_errors);
142 skip = should_skip(run->m, i);
147 printf("%s%s: skipped (%s)\n",
148 run->prefix, i->name, skip);
150 /* If we're skipping this because a prereq failed, we fail:
151 * count it as a score of 1. */
155 dgraph_traverse_from(&i->node, skip_node,
156 i->skip_fail ? "dependency failed" : NULL);
157 dgraph_clear_node(&i->node);
158 score->pass = i->skip_fail ? false : true;
162 timeleft = timeout ? timeout : default_timeout_ms;
163 i->check(run->m, i->keep_results, &timeleft, score);
164 if (timeout && timeleft == 0) {
169 assert(score->score <= score->total);
170 if ((!score->pass && !run->quiet)
171 || (score->score < score->total && verbose)
173 printf("%s%s (%s): %s",
174 run->prefix, i->name, i->key,
175 score->pass ? "PASS" : "FAIL");
176 if (score->total > 1)
177 printf(" (+%u/%u)", score->score, score->total);
181 if ((!run->quiet && !score->pass) || verbose) {
183 printf("%s%s", score->error,
184 strends(score->error, "\n") ? "" : "\n");
187 if (!run->quiet && score->score < score->total && i->handle)
188 i->handle(run->m, score);
190 run->score += score->score;
191 run->total += score->total;
194 /* Skip any tests which depend on this one. */
195 dgraph_traverse_from(&i->node, skip_node, "dependency failed");
197 dgraph_clear_node(&i->node);
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);
219 dgraph_add_edge(&test->node, &all);
223 static bool get_test(const char *member, struct ccanlint *i,
224 struct ccanlint **ret)
226 if (tlist_empty(&i->node.edge[DGRAPH_TO])) {
234 * get_next_test - retrieves the next test to be processed
236 static inline struct ccanlint *get_next_test(void)
238 struct ccanlint *i = NULL;
240 strmap_iterate(&tests, get_test, &i);
244 if (strmap_empty(&tests))
247 errx(1, "Can't make process; test dependency cycle");
250 static struct ccanlint *find_test(const char *key)
252 return strmap_get(&tests, key);
255 bool is_excluded(const char *name)
257 return btree_lookup(cmdline_exclude, name) != NULL
258 || btree_lookup(info_exclude, name) != NULL
259 || find_test(name)->skip != NULL;
262 static bool reset_deps(const char *member, struct ccanlint *c, void *unused)
264 char **deps = strsplit(NULL, c->needs, " ");
268 c->skip_fail = false;
269 for (i = 0; deps[i]; i++) {
270 struct ccanlint *dep;
272 dep = find_test(deps[i]);
274 errx(1, "BUG: unknown dep '%s' for %s",
276 dgraph_add_edge(&dep->node, &c->node);
282 static bool check_names(const char *member, struct ccanlint *c,
283 struct ccanlint_map *names)
285 if (!strmap_add(names, c->name, c))
286 err(1, "Duplicate name %s", c->name);
291 #define REGISTER_TEST(name, ...) extern struct ccanlint name
292 #include "generated-testlist"
294 static void init_tests(void)
296 struct ccanlint_map names;
298 /* This is the default target. */
299 dgraph_init_node(&all);
304 #define REGISTER_TEST(name) register_test(&name)
305 #include "generated-testlist"
307 strmap_iterate(&tests, reset_deps, NULL);
309 /* Check for duplicate names. */
311 strmap_iterate(&tests, check_names, &names);
312 strmap_clear(&names);
315 static bool print_deps(const char *member, struct ccanlint *c, void *unused)
317 if (!tlist_empty(&c->node.edge[DGRAPH_FROM])) {
318 struct dgraph_edge *e;
320 printf("These depend on %s:\n", c->key);
321 dgraph_for_each_edge(&c->node, e, DGRAPH_FROM) {
322 struct ccanlint *to = container_of(e->n[DGRAPH_TO],
325 printf("\t%s\n", to->key);
331 static void print_test_depends(void)
335 strmap_iterate(&tests, print_deps, NULL);
339 static int show_tmpdir(const char *dir)
341 printf("You can find ccanlint working files in '%s'\n", dir);
345 static bool keep_one_test(const char *member, struct ccanlint *c, void *unused)
347 c->keep_results = true;
351 static char *keep_test(const char *testname, void *unused)
353 if (streq(testname, "all")) {
354 strmap_iterate(&tests, keep_one_test, NULL);
356 struct ccanlint *i = find_test(testname);
358 errx(1, "No test %s to --keep", testname);
359 keep_one_test(testname, i, NULL);
362 /* Don't automatically destroy temporary dir. */
363 talloc_set_destructor(temp_dir(NULL), show_tmpdir);
367 static char *skip_test(const char *testname, void *unused)
369 btree_insert(cmdline_exclude, testname);
373 static char *list_tests(void *arg)
378 /* This makes them print in topological order. */
379 while ((i = get_next_test()) != NULL) {
380 printf(" %-25s %s\n", i->key, i->name);
381 dgraph_clear_node(&i->node);
382 strmap_del(&tests, i->key, NULL);
387 static bool draw_test(const char *member, struct ccanlint *c, const char *style)
390 * todo: escape labels in case ccanlint test keys have
391 * characters interpreted as GraphViz syntax.
393 printf("\t\"%p\" [label=\"%s\"%s]\n", c, c->key, style);
397 static void test_dgraph_vertices(const char *style)
399 strmap_iterate(&tests, draw_test, style);
402 static bool draw_edges(const char *member, struct ccanlint *c, void *unused)
404 struct dgraph_edge *e;
406 dgraph_for_each_edge(&c->node, e, DGRAPH_FROM) {
407 struct ccanlint *to = container_of(e->n[DGRAPH_TO],
410 printf("\t\"%p\" -> \"%p\"\n", c->name, to->name);
415 static void test_dgraph_edges(void)
417 strmap_iterate(&tests, draw_edges, NULL);
420 static char *test_dependency_graph(void *arg)
424 test_dgraph_vertices("");
432 /* Remove empty lines. */
433 static char **collapse(char **lines, unsigned int *nump)
436 for (i = j = 0; lines[i]; i++) {
438 lines[j++] = lines[i];
447 static void add_options(struct ccanlint *test, char **options,
448 unsigned int num_options)
455 /* -1, because last one is NULL. */
456 num = talloc_array_length(test->options) - 1;
458 test->options = talloc_realloc(NULL, test->options,
460 num + num_options + 1);
461 memcpy(&test->options[num], options, (num_options + 1)*sizeof(char *));
464 void add_info_options(struct ccan_file *info)
466 struct doc_section *d;
468 struct ccanlint *test;
470 list_for_each(get_ccan_file_docs(info), d, list) {
471 if (!streq(d->type, "ccanlint"))
474 for (i = 0; i < d->num_lines; i++) {
475 unsigned int num_words;
476 char **words = collapse(strsplit(d, d->lines[i], " \t"),
481 if (strncmp(words[0], "//", 2) == 0)
484 test = find_test(words[0]);
486 warnx("%s: unknown ccanlint test '%s'",
487 info->fullname, words[0]);
492 warnx("%s: no argument to test '%s'",
493 info->fullname, words[0]);
498 if (strcasecmp(words[1], "FAIL") == 0) {
500 btree_insert(info_exclude, words[0]);
502 if (!test->takes_options)
503 warnx("%s: %s doesn't take options",
504 info->fullname, words[0]);
505 add_options(test, words+1, num_words-1);
511 /* If options are of form "filename:<option>" they only apply to that file */
512 char **per_file_options(const struct ccanlint *test, struct ccan_file *f)
515 unsigned int i, j = 0;
518 if (!test->options[0])
519 return test->options;
521 ret = talloc_array(f, char *, talloc_array_length(test->options));
522 for (i = 0; test->options[i]; i++) {
525 if (!test->options[i] || !strchr(test->options[i], ':')) {
526 optname = test->options[i];
527 } else if (strstarts(test->options[i], f->name)
528 && test->options[i][strlen(f->name)] == ':') {
529 optname = test->options[i] + strlen(f->name) + 1;
533 /* FAIL overrides anything else. */
534 if (streq(optname, "FAIL")) {
535 ret = talloc_array(f, char *, 2);
536 ret[0] = (char *)"FAIL";
544 /* Shrink it to size so talloc_array_length() works as expected. */
545 return talloc_realloc(NULL, ret, char *, j + 1);
548 static char *demangle_string(char *string)
551 const char mapfrom[] = "abfnrtv";
552 const char mapto[] = "\a\b\f\n\r\t\v";
554 if (!strchr(string, '"'))
556 string = strchr(string, '"') + 1;
557 if (!strrchr(string, '"'))
559 *strrchr(string, '"') = '\0';
561 for (i = 0; i < strlen(string); i++) {
562 if (string[i] == '\\') {
565 const char *p = strchr(mapfrom, string[i+1]);
567 repl = mapto[p - mapfrom];
569 } else if (strlen(string+i+1) >= 3) {
570 if (string[i+1] == 'x') {
571 repl = (string[i+2]-'0')*16
574 } else if (cisdigit(string[i+1])) {
575 repl = (string[i+2]-'0')*8*8
576 + (string[i+3]-'0')*8
587 memmove(string + i + 1, string + i + len + 1,
588 strlen(string + i + len + 1) + 1);
596 static void read_config_header(void)
598 char *fname = talloc_asprintf(NULL, "%s/config.h", ccan_dir);
602 config_header = grab_file(NULL, fname, NULL);
603 if (!config_header) {
608 lines = strsplit(config_header, config_header, "\n");
609 for (i = 0; i < talloc_array_length(lines) - 1; i++) {
611 const char **line = (const char **)&lines[i];
613 if (!get_token(line, "#"))
615 if (!get_token(line, "define"))
617 sym = get_symbol_token(lines, line);
618 if (streq(sym, "CCAN_COMPILER") && !compiler) {
619 compiler = demangle_string(lines[i]);
621 errx(1, "%s:%u:could not parse CCAN_COMPILER",
624 printf("%s: compiler set to '%s'\n",
626 } else if (streq(sym, "CCAN_CFLAGS") && !cflags) {
627 cflags = demangle_string(lines[i]);
629 errx(1, "%s:%u:could not parse CCAN_CFLAGS",
632 printf("%s: compiler flags set to '%s'\n",
637 compiler = CCAN_COMPILER;
639 compiler = CCAN_CFLAGS;
642 static char *opt_set_const_charp(const char *arg, const char **p)
644 return opt_set_charp(arg, cast_const2(char **, p));
647 static char *opt_set_target(const char *arg, struct ccanlint **t)
651 return talloc_asprintf(NULL, "unknown --target %s", arg);
655 static bool run_tests(struct ccanlint *target,
665 run.score = run.total = 0;
669 dgraph_traverse_to(&target->node, run_test, &run);
671 run_test(&target->node, &run);
673 dgraph_traverse_to(&all, run_test, &run);
675 printf("%sTotal score: %u/%u\n",
676 prefix, run.score, run.total);
681 int main(int argc, char *argv[])
683 bool summary = false, pass = true;
686 struct ccanlint *target = NULL;
687 const char *prefix = "";
688 char *dir = talloc_getcwd(NULL), *base_dir = dir, *testlink;
690 cmdline_exclude = btree_new(btree_strcmp);
691 info_exclude = btree_new(btree_strcmp);
693 opt_register_early_noarg("--verbose|-v", opt_inc_intval, &verbose,
694 "verbose mode (up to -vvvv)");
695 opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
696 "do not compile anything");
697 opt_register_noarg("-l|--list-tests", list_tests, NULL,
698 "list tests ccanlint performs (and exit)");
699 opt_register_noarg("--test-dep-graph", test_dependency_graph, NULL,
700 "print dependency graph of tests in Graphviz .dot format");
701 opt_register_arg("-k|--keep <testname>", keep_test, NULL, NULL,
702 "keep results of <testname>"
703 " (can be used multiple times, or 'all')");
704 opt_register_noarg("--summary|-s", opt_set_bool, &summary,
705 "simply give one line summary");
706 opt_register_arg("-x|--exclude <testname>", skip_test, NULL, NULL,
707 "exclude <testname> (can be used multiple times)");
708 opt_register_arg("-t|--timeout <milleseconds>", opt_set_uintval,
710 "ignore (terminate) tests that are slower than this");
711 opt_register_arg("--target <testname>", opt_set_target,
713 "only run one test (and its prerequisites)");
714 opt_register_arg("--compiler <compiler>", opt_set_const_charp,
715 NULL, &compiler, "set the compiler");
716 opt_register_arg("--cflags <flags>", opt_set_const_charp,
717 NULL, &cflags, "set the compiler flags");
718 opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
719 "\nA program for checking and guiding development"
721 "This usage message");
723 /* Do verbose before anything else... */
724 opt_early_parse(argc, argv, opt_log_stderr_exit);
726 /* We move into temporary directory, so gcov dumps its files there. */
727 if (chdir(temp_dir(talloc_autofree_context())) != 0)
728 err(1, "Error changing to %s temporary dir", temp_dir(NULL));
733 compile_verbose = true;
734 print_test_depends();
737 tools_verbose = true;
739 opt_parse(&argc, argv, opt_log_stderr_exit);
741 /* This links back to the module's test dir. */
742 testlink = talloc_asprintf(NULL, "%s/test", temp_dir(NULL));
744 /* Defaults to pwd. */
750 for (i = 1; i < argc; i++) {
751 unsigned int score, total_score;
756 dir = talloc_asprintf_append(NULL, "%s/%s",
758 while (strends(dir, "/"))
759 dir[strlen(dir)-1] = '\0';
763 prefix = talloc_append_string(talloc_basename(NULL,dir),
768 m = get_manifest(talloc_autofree_context(), dir);
770 /* FIXME: This has to come after we've got manifest. */
772 read_config_header();
774 /* Create a symlink from temp dir back to src dir's
777 if (symlink(talloc_asprintf(m, "%s/test", dir), testlink) != 0)
778 err(1, "Creating test symlink in %s", temp_dir(NULL));
780 score = total_score = 0;
781 if (!run_tests(target, summary, m, prefix))