]> git.ozlabs.org Git - ccan/blobdiff - tools/ccanlint/ccanlint.c
ccanlint: fix segfault caused by tests not depending on the "info" test.
[ccan] / tools / ccanlint / ccanlint.c
index 31406cccbfa0386a8cda1b3f451a094b11984150..ee34dd71438d179fe72999bca15c1967c8e039a8 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * ccanlint: assorted checks and advice for a ccan package
  * Copyright (C) 2008 Rusty Russell, Idris Soule
+ * Copyright (C) 2010 Rusty Russell, Idris Soule
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
@@ -58,7 +59,7 @@ static void indent_print(const char *string)
 
 bool ask(const char *question)
 {
-       char reply[2];
+       char reply[80];
 
        printf("%s ", question);
        fflush(stdout);
@@ -148,11 +149,13 @@ static bool run_test(struct ccanlint *i,
                printf("\n");
        }
 
-       if (!quiet && !score->pass) {
+       if ((!quiet && !score->pass) || verbose) {
                struct file_error *f;
+               unsigned int lines = 1;
 
                if (score->error)
-                       printf("%s:\n", score->error);
+                       printf("%s%s\n", score->error,
+                              list_empty(&score->per_file_errors) ? "" : ":");
 
                list_for_each(&score->per_file_errors, f, list) {
                        if (f->line)
@@ -162,8 +165,12 @@ static bool run_test(struct ccanlint *i,
                                printf("%s:%s\n", f->file->fullname, f->error);
                        else
                                printf("%s\n", f->error);
+                       if (verbose < 2 && ++lines > 5) {
+                               printf("... more (use -vv to see them all)\n");
+                               break;
+                       }
                }
-               if (i->handle)
+               if (!quiet && !score->pass && i->handle)
                        i->handle(m, score);
        }
 
@@ -311,7 +318,7 @@ static char *keep_test(const char *testname, void *unused)
 
 static char *skip_test(const char *testname, void *unused)
 {
-       btree_insert(cmdline_exclude, optarg);
+       btree_insert(cmdline_exclude, testname);
        return NULL;
 }
 
@@ -337,24 +344,64 @@ static char *list_tests(void *arg)
        exit(0);
 }
 
-static char *strip(const void *ctx, const char *line)
+/* Remove empty lines. */
+static char **collapse(char **lines, unsigned int *nump)
 {
-       line += strcspn(line, IDENT_CHARS "-");
-       return talloc_strndup(ctx, line, strspn(line, IDENT_CHARS "-"));
+       unsigned int i, j;
+       for (i = j = 0; lines[i]; i++) {
+               if (lines[i][0])
+                       lines[j++] = lines[i];
+       }
+       if (nump)
+               *nump = j;
+       return lines;
 }
 
-static void add_info_fails(struct ccan_file *info)
+static void add_info_options(struct ccan_file *info, bool mark_fails)
 {
        struct doc_section *d;
        unsigned int i;
+       struct ccanlint *test;
 
        list_for_each(get_ccan_file_docs(info), d, list) {
-               if (!streq(d->type, "fails"))
+               if (!streq(d->type, "ccanlint"))
                        continue;
 
-               for (i = 0; i < d->num_lines; i++)
-                       btree_insert(info_exclude, strip(info, d->lines[i]));
-               break;
+               for (i = 0; i < d->num_lines; i++) {
+                       char **words = collapse(strsplit(d, d->lines[i], " \t",
+                                                        NULL), NULL);
+                       if (!words[0])
+                               continue;
+
+                       if (strncmp(words[0], "//", 2) == 0)
+                               continue;
+
+                       test = find_test(words[0]);
+                       if (!test) {
+                               warnx("%s: unknown ccanlint test '%s'",
+                                     info->fullname, words[0]);
+                               continue;
+                       }
+
+                       if (!words[1]) {
+                               warnx("%s: no argument to test '%s'",
+                                     info->fullname, words[0]);
+                               continue;
+                       }
+
+                       /* Known failure? */
+                       if (strcasecmp(words[1], "FAIL") == 0) {
+                               if (mark_fails)
+                                       btree_insert(info_exclude, words[0]);
+                       } else {
+                               if (!test->takes_options)
+                                       warnx("%s: %s doesn't take options",
+                                             info->fullname, words[0]);
+                               /* Copy line exactly into options. */
+                               test->options = strstr(d->lines[i], words[0])
+                                       + strlen(words[0]);
+                       }
+               }
        }
 }
 
@@ -409,7 +456,7 @@ int main(int argc, char *argv[])
        opt_register_noarg("--summary|-s", opt_set_bool, &summary,
                           "simply give one line summary");
        opt_register_noarg("--verbose|-v", opt_inc_intval, &verbose,
-                          "verbose mode (can specify more than once)");
+                          "verbose mode (up to -vvvv)");
        opt_register_arg("-x|--exclude <testname>", skip_test, NULL, NULL,
                         "exclude <testname> (can be used multiple times)");
        opt_register_arg("-t|--timeout <milleseconds>", opt_set_uintval,
@@ -429,9 +476,9 @@ int main(int argc, char *argv[])
                dir = talloc_asprintf_append(NULL, "%s/%s", base_dir, dir);
        if (dir != base_dir)
                prefix = talloc_append_string(talloc_basename(NULL, dir), ": ");
-       if (verbose >= 2)
-               compile_verbose = true;
        if (verbose >= 3)
+               compile_verbose = true;
+       if (verbose >= 4)
                tools_verbose = true;
 
        /* We move into temporary directory, so gcov dumps its files there. */
@@ -450,7 +497,7 @@ int main(int argc, char *argv[])
 
                test = find_test(target);
                if (!test)
-                       err(1, "Unknown test to run '%s'", target);
+                       errx(1, "Unknown test to run '%s'", target);
                skip_unrelated_tests(test);
        }
 
@@ -462,7 +509,9 @@ int main(int argc, char *argv[])
                }
        }
 
-       add_info_fails(m->info_file);
+       /* --target overrides known FAIL from _info */
+       add_info_options(m->info_file, !target);
+
        while ((i = get_next_test(&normal_tests)) != NULL)
                run_test(i, summary, &score, &total_score, m);