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>
38 static LIST_HEAD(compulsory_tests);
39 static LIST_HEAD(normal_tests);
40 static LIST_HEAD(finished_tests);
41 bool safe_mode = false;
42 static struct btree *cmdline_exclude;
43 static struct btree *info_exclude;
44 static unsigned int timeout;
46 /* These are overridden at runtime if we can find config.h */
47 const char *compiler = NULL;
48 const char *cflags = NULL;
50 const char *config_header;
53 static void indent_print(const char *string)
56 unsigned int line = strcspn(string, "\n");
57 printf("\t%.*s", line, string);
58 if (string[line] == '\n') {
67 bool ask(const char *question)
71 printf("%s ", question);
74 return fgets(reply, sizeof(reply), stdin) != NULL
75 && toupper(reply[0]) == 'Y';
78 static const char *should_skip(struct manifest *m, struct ccanlint *i)
80 if (btree_lookup(cmdline_exclude, i->key))
81 return "excluded on command line";
83 if (btree_lookup(info_exclude, i->key))
84 return "excluded in _info file";
90 return "dependency failed";
97 static bool run_test(struct ccanlint *i,
99 unsigned int *running_score,
100 unsigned int *running_total,
103 unsigned int timeleft;
104 const struct dependent *d;
108 //one less test to run through
109 list_for_each(&i->dependencies, d, node)
110 d->dependent->num_depends--;
112 score = talloc(m, struct score);
113 list_head_init(&score->per_file_errors);
119 skip = should_skip(m, i);
123 if (verbose && !streq(skip, "not relevant to target"))
124 printf("%s: skipped (%s)\n", i->name, skip);
126 /* If we're skipping this because a prereq failed, we fail:
127 * count it as a score of 1. */
132 list_add_tail(&finished_tests, &i->list);
133 list_for_each(&i->dependencies, d, node) {
134 if (d->dependent->skip)
136 d->dependent->skip = "dependency was skipped";
137 d->dependent->skip_fail = i->skip_fail;
139 return i->skip_fail ? false : true;
142 timeleft = timeout ? timeout : default_timeout_ms;
143 i->check(m, i->keep_results, &timeleft, score);
144 if (timeout && timeleft == 0) {
149 assert(score->score <= score->total);
150 if ((!score->pass && !quiet)
151 || (score->score < score->total && verbose)
153 printf("%s (%s): %s", i->name, i->key, score->pass ? "PASS" : "FAIL");
154 if (score->total > 1)
155 printf(" (+%u/%u)", score->score, score->total);
159 if ((!quiet && !score->pass) || verbose) {
161 printf("%s%s", score->error,
162 strends(score->error, "\n") ? "" : "\n");
165 if (!quiet && score->score < score->total && i->handle)
168 *running_score += score->score;
169 *running_total += score->total;
172 list_add_tail(&finished_tests, &i->list);
175 /* Skip any tests which depend on this one. */
176 list_for_each(&i->dependencies, d, node) {
177 if (d->dependent->skip)
179 d->dependent->skip = "dependency failed";
180 d->dependent->skip_fail = true;
186 static void register_test(struct list_head *h, struct ccanlint *test)
188 list_add(h, &test->list);
189 test->options = talloc_array(NULL, char *, 1);
190 test->options[0] = NULL;
194 * get_next_test - retrieves the next test to be processed
196 static inline struct ccanlint *get_next_test(struct list_head *test)
200 if (list_empty(test))
203 list_for_each(test, i, list) {
204 if (i->num_depends == 0)
207 errx(1, "Can't make process; test dependency cycle");
210 static struct ccanlint *find_test(const char *key)
214 list_for_each(&compulsory_tests, i, list)
215 if (streq(i->key, key))
218 list_for_each(&normal_tests, i, list)
219 if (streq(i->key, key))
226 #define REGISTER_TEST(name, ...) extern struct ccanlint name
227 #include "generated-normal-tests"
228 #include "generated-compulsory-tests"
230 static void init_tests(void)
233 struct btree *keys, *names;
234 struct list_head *list;
237 #define REGISTER_TEST(name) register_test(&normal_tests, &name)
238 #include "generated-normal-tests"
240 #define REGISTER_TEST(name) register_test(&compulsory_tests, &name)
241 #include "generated-compulsory-tests"
243 /* Initialize dependency lists. */
244 foreach_ptr(list, &compulsory_tests, &normal_tests) {
245 list_for_each(list, c, list) {
246 list_head_init(&c->dependencies);
250 /* Resolve dependencies. */
251 foreach_ptr(list, &compulsory_tests, &normal_tests) {
252 list_for_each(list, c, list) {
253 char **deps = strsplit(NULL, c->needs, " ");
256 for (i = 0; deps[i]; i++) {
257 struct ccanlint *dep;
258 struct dependent *dchild;
260 dep = find_test(deps[i]);
262 errx(1, "BUG: unknown dep '%s' for %s",
264 dchild = talloc(NULL, struct dependent);
265 dchild->dependent = c;
266 list_add_tail(&dep->dependencies,
274 /* Self-consistency check: make sure no two tests
275 have the same key or name. */
276 keys = btree_new(btree_strcmp);
277 names = btree_new(btree_strcmp);
278 foreach_ptr(list, &compulsory_tests, &normal_tests) {
279 list_for_each(list, c, list) {
280 if (!btree_insert(keys, c->key))
281 errx(1, "BUG: Duplicate test key '%s'",
283 if (!btree_insert(names, c->name))
284 errx(1, "BUG: Duplicate test name '%s'",
292 static void print_test_depends(void)
294 struct list_head *list;
296 foreach_ptr(list, &compulsory_tests, &normal_tests) {
298 printf("\%s Tests\n",
299 list == &compulsory_tests ? "Compulsory" : "Normal");
301 list_for_each(list, c, list) {
302 if (!list_empty(&c->dependencies)) {
303 const struct dependent *d;
304 printf("These depend on %s:\n", c->key);
305 list_for_each(&c->dependencies, d, node)
306 printf("\t%s\n", d->dependent->key);
312 static int show_tmpdir(const char *dir)
314 printf("You can find ccanlint working files in '%s'\n", dir);
318 static char *keep_test(const char *testname, void *unused)
322 if (streq(testname, "all")) {
323 struct list_head *list;
324 foreach_ptr(list, &compulsory_tests, &normal_tests) {
325 list_for_each(list, i, list)
326 i->keep_results = true;
329 i = find_test(testname);
331 errx(1, "No test %s to --keep", testname);
332 i->keep_results = true;
335 /* Don't automatically destroy temporary dir. */
336 talloc_set_destructor(temp_dir(NULL), show_tmpdir);
340 static char *skip_test(const char *testname, void *unused)
342 btree_insert(cmdline_exclude, testname);
346 static void print_tests(struct list_head *tests, const char *type)
350 printf("%s tests:\n", type);
351 /* This makes them print in topological order. */
352 while ((i = get_next_test(tests)) != NULL) {
353 const struct dependent *d;
354 printf(" %-25s %s\n", i->key, i->name);
356 list_for_each(&i->dependencies, d, node)
357 d->dependent->num_depends--;
361 static char *list_tests(void *arg)
363 print_tests(&compulsory_tests, "Compulsory");
364 print_tests(&normal_tests, "Normal");
368 static void test_dgraph_vertices(struct list_head *tests, const char *style)
370 const struct ccanlint *i;
372 list_for_each(tests, i, list) {
374 * todo: escape labels in case ccanlint test keys have
375 * characters interpreted as GraphViz syntax.
377 printf("\t\"%p\" [label=\"%s\"%s]\n", i, i->key, style);
381 static void test_dgraph_edges(struct list_head *tests)
383 const struct ccanlint *i;
384 const struct dependent *d;
386 list_for_each(tests, i, list)
387 list_for_each(&i->dependencies, d, node)
388 printf("\t\"%p\" -> \"%p\"\n", d->dependent, i);
391 static char *test_dependency_graph(void *arg)
395 test_dgraph_vertices(&compulsory_tests, ", style=filled, fillcolor=yellow");
396 test_dgraph_vertices(&normal_tests, "");
398 test_dgraph_edges(&compulsory_tests);
399 test_dgraph_edges(&normal_tests);
406 /* Remove empty lines. */
407 static char **collapse(char **lines, unsigned int *nump)
410 for (i = j = 0; lines[i]; i++) {
412 lines[j++] = lines[i];
421 static void add_options(struct ccanlint *test, char **options,
422 unsigned int num_options)
429 /* -1, because last one is NULL. */
430 num = talloc_array_length(test->options) - 1;
432 test->options = talloc_realloc(NULL, test->options,
434 num + num_options + 1);
435 memcpy(&test->options[num], options, (num_options + 1)*sizeof(char *));
438 static void add_info_options(struct ccan_file *info, bool mark_fails)
440 struct doc_section *d;
442 struct ccanlint *test;
444 list_for_each(get_ccan_file_docs(info), d, list) {
445 if (!streq(d->type, "ccanlint"))
448 for (i = 0; i < d->num_lines; i++) {
449 unsigned int num_words;
450 char **words = collapse(strsplit(d, d->lines[i], " \t"),
455 if (strncmp(words[0], "//", 2) == 0)
458 test = find_test(words[0]);
460 warnx("%s: unknown ccanlint test '%s'",
461 info->fullname, words[0]);
466 warnx("%s: no argument to test '%s'",
467 info->fullname, words[0]);
472 if (strcasecmp(words[1], "FAIL") == 0) {
474 btree_insert(info_exclude, words[0]);
476 if (!test->takes_options)
477 warnx("%s: %s doesn't take options",
478 info->fullname, words[0]);
479 add_options(test, words+1, num_words-1);
485 /* If options are of form "filename:<option>" they only apply to that file */
486 char **per_file_options(const struct ccanlint *test, struct ccan_file *f)
489 unsigned int i, j = 0;
492 if (!test->options[0])
493 return test->options;
495 ret = talloc_array(f, char *, talloc_array_length(test->options));
496 for (i = 0; test->options[i]; i++) {
499 if (!test->options[i] || !strchr(test->options[i], ':')) {
500 optname = test->options[i];
501 } else if (strstarts(test->options[i], f->name)
502 && test->options[i][strlen(f->name)] == ':') {
503 optname = test->options[i] + strlen(f->name) + 1;
507 /* FAIL overrides anything else. */
508 if (streq(optname, "FAIL")) {
509 ret = talloc_array(f, char *, 2);
510 ret[0] = (char *)"FAIL";
518 /* Shrink it to size so talloc_array_length() works as expected. */
519 return talloc_realloc(NULL, ret, char *, j + 1);
522 static bool depends_on(struct ccanlint *i, struct ccanlint *target)
524 const struct dependent *d;
529 list_for_each(&i->dependencies, d, node) {
530 if (depends_on(d->dependent, target))
536 /* O(N^2), who cares? */
537 static void skip_unrelated_tests(struct ccanlint *target)
540 struct list_head *list;
542 foreach_ptr(list, &compulsory_tests, &normal_tests)
543 list_for_each(list, i, list)
544 if (!depends_on(i, target))
545 i->skip = "not relevant to target";
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 int main(int argc, char *argv[])
649 bool summary = false, pass = true;
650 unsigned int score = 0, total_score = 0;
653 const char *prefix = "";
654 char *dir = talloc_getcwd(NULL), *base_dir = dir, *target = NULL;
658 cmdline_exclude = btree_new(btree_strcmp);
659 info_exclude = btree_new(btree_strcmp);
661 opt_register_arg("--dir|-d", opt_set_charp, opt_show_charp, &dir,
662 "use this directory");
663 opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
664 "do not compile anything");
665 opt_register_noarg("-l|--list-tests", list_tests, NULL,
666 "list tests ccanlint performs (and exit)");
667 opt_register_noarg("--test-dep-graph", test_dependency_graph, NULL,
668 "print dependency graph of tests in Graphviz .dot format");
669 opt_register_arg("-k|--keep <testname>", keep_test, NULL, NULL,
670 "keep results of <testname>"
671 " (can be used multiple times, or 'all')");
672 opt_register_noarg("--summary|-s", opt_set_bool, &summary,
673 "simply give one line summary");
674 opt_register_noarg("--verbose|-v", opt_inc_intval, &verbose,
675 "verbose mode (up to -vvvv)");
676 opt_register_arg("-x|--exclude <testname>", skip_test, NULL, NULL,
677 "exclude <testname> (can be used multiple times)");
678 opt_register_arg("-t|--timeout <milleseconds>", opt_set_uintval,
680 "ignore (terminate) tests that are slower than this");
681 opt_register_arg("--target <testname>", opt_set_charp,
683 "only run one test (and its prerequisites)");
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 /* We move into temporary directory, so gcov dumps its files there. */
694 if (chdir(temp_dir(talloc_autofree_context())) != 0)
695 err(1, "Error changing to %s temporary dir", temp_dir(NULL));
697 opt_parse(&argc, argv, opt_log_stderr_exit);
700 dir = talloc_asprintf_append(NULL, "%s/%s", base_dir, dir);
701 while (strends(dir, "/"))
702 dir[strlen(dir)-1] = '\0';
704 prefix = talloc_append_string(talloc_basename(NULL, dir), ": ");
706 compile_verbose = true;
707 print_test_depends();
710 tools_verbose = true;
712 m = get_manifest(talloc_autofree_context(), dir);
713 read_config_header();
715 /* Create a symlink from temp dir back to src dir's test directory. */
716 if (symlink(talloc_asprintf(m, "%s/test", dir),
717 talloc_asprintf(m, "%s/test", temp_dir(NULL))) != 0)
718 err(1, "Creating test symlink in %s", temp_dir(NULL));
721 struct ccanlint *test;
723 test = find_test(target);
725 errx(1, "Unknown test to run '%s'", target);
726 skip_unrelated_tests(test);
729 /* If you don't pass the compulsory tests, you get a score of 0. */
730 while ((i = get_next_test(&compulsory_tests)) != NULL) {
731 if (!run_test(i, summary, &score, &total_score, m)) {
732 printf("%sTotal score: 0/%u\n", prefix, total_score);
733 errx(1, "%s%s failed", prefix, i->name);
737 /* --target overrides known FAIL from _info */
739 add_info_options(m->info_file, !target);
741 while ((i = get_next_test(&normal_tests)) != NULL)
742 pass &= run_test(i, summary, &score, &total_score, m);
744 printf("%sTotal score: %u/%u\n", prefix, score, total_score);