]> git.ozlabs.org Git - ccan/blobdiff - tools/ccanlint/tests/examples_compile.c
ccanlint: fix abort when -vv used and examples don't compile.
[ccan] / tools / ccanlint / tests / examples_compile.c
index e66379f5e7fee45e9036a5cda09f664dac568b09..a3a3d1d595c88a5e8cfb7384c99b087aa416df64 100644 (file)
@@ -15,6 +15,8 @@ static const char *can_run(struct manifest *m)
 {
        if (safe_mode)
                return "Safe mode enabled";
+       if (list_empty(&m->examples))
+               return "No examples to compile";
        return NULL;
 }
 
@@ -122,16 +124,12 @@ static char *compile(const void *ctx,
                                  "", lib_list(m), file->compiled);
        if (errmsg) {
                talloc_free(file->compiled);
+               file->compiled = NULL;
                return errmsg;
        }
        return NULL;
 }
 
-struct score {
-       unsigned int score;
-       char *errors;
-};
-
 static char *start_main(char *ret, const char *why)
 {
        return talloc_asprintf_append(ret,
@@ -429,113 +427,97 @@ static struct ccan_file *mangle_example(struct manifest *m,
        }
        close(fd);
        f->contents = talloc_steal(f, contents);
+       list_add(&m->mangled_examples, &f->list);
        return f;
 }
 
-static void *build_examples(struct manifest *m, bool keep,
-                           unsigned int *timeleft)
+static void build_examples(struct manifest *m, bool keep,
+                          unsigned int *timeleft, struct score *score)
 {
        struct ccan_file *i;
-       struct score *score = talloc(m, struct score);
        char **prev = NULL;
 
-       score->score = 0;
-       score->errors = talloc_strdup(score, "");
+       score->total = 0;
+       score->pass = true;
 
-       examples_compile.total_score = 0;
        list_for_each(&m->examples, i, list) {
-               char *ret, *ret1 = NULL, *ret2;
-               struct ccan_file *mangle1 = NULL, *mangle2;
+               char *ret, *ret1, *ret2 = NULL;
+               struct ccan_file *mangle1, *mangle2 = NULL;
+               char *err;
 
-               examples_compile.total_score++;
+               score->total++;
                /* Simplify our dumb parsing. */
                strip_leading_whitespace(get_ccan_file_lines(i));
-               ret = compile(score, m, i, keep);
+               ret = compile(i, m, i, keep);
                if (!ret) {
                        prev = get_ccan_file_lines(i);
                        score->score++;
                        continue;
                }
 
+               /* Try standalone. */
+               mangle1 = mangle_example(m, i, get_ccan_file_lines(i), keep);
+               ret1 = compile(i, m, mangle1, keep);
+               if (!ret1) {
+                       prev = get_ccan_file_lines(i);
+                       score->score++;
+                       continue;
+               }
+
                /* Try combining with previous (successful) example... */
                if (prev) {
                        char **new = combine(i, get_ccan_file_lines(i), prev);
 
-                       mangle1 = mangle_example(m, i, new, keep);
-                       ret1 = compile(score, m, mangle1, keep);
-                       if (!ret1) {
+                       mangle2 = mangle_example(m, i, new, keep);
+                       ret2 = compile(i, m, mangle2, keep);
+                       if (!ret2) {
                                prev = new;
                                score->score++;
                                continue;
                        }
                }
 
-               /* Try standalone. */
-               mangle2 = mangle_example(m, i, get_ccan_file_lines(i), keep);
-               ret2 = compile(score, m, mangle2, keep);
-               if (!ret2) {
-                       prev = get_ccan_file_lines(i);
-                       score->score++;
-                       continue;
-               }
-
-               score->errors = talloc_asprintf_append(score->errors,
-                                      "%s: tried standalone example:\n"
-                                      "%s\n"
-                                      "Errors: %s\n\n",
-                                      i->name,
-                                      get_ccan_file_contents(i),
-                                      ret);
-               if (mangle1) {
-                       score->errors = talloc_asprintf_append(score->errors,
-                                              "%s: tried combining with"
-                                              " previous example:\n"
-                                              "%s\n"
-                                              "Errors: %s\n\n",
-                                              i->name,
-                                              get_ccan_file_contents(mangle1),
-                                              ret1);
-               }
-               score->errors = talloc_asprintf_append(score->errors,
-                                      "%s: tried adding headers, wrappers:\n"
+               score->pass = false;
+               score->error = "Compiling extracted examples failed";
+               if (!verbose) {
+                       if (mangle2) 
+                               err = "Standalone, adding headers, "
+                                       "and including previous "
+                                       "example all failed";
+                       else
+                               err = "Standalone compile and"
+                                       " adding headers both failed";
+               } else {
+                       err = talloc_asprintf(score,
+                                             "Standalone example:\n"
+                                             "%s\n"
+                                             "Errors: %s\n\n"
+                                             "Adding headers, wrappers:\n"
+                                             "%s\n"
+                                             "Errors: %s\n\n",
+                                             get_ccan_file_contents(i),
+                                             ret,
+                                             get_ccan_file_contents(mangle1),
+                                             ret1);
+
+                       if (mangle2)
+                               err = talloc_asprintf_append(err, 
+                                      "Combining with previous example:\n"
                                       "%s\n"
                                       "Errors: %s\n\n",
-                                      i->name,
                                       get_ccan_file_contents(mangle2),
                                       ret2);
+               }
+               score_file_error(score, i, 0, err);
                /* This didn't work, so not a candidate for combining. */
                prev = NULL;
        }
-
-       if (strcmp(score->errors, "") == 0) {
-               talloc_free(score);
-               return NULL;
-       }
-       return score;
-}
-
-static unsigned int score_examples(struct manifest *m, void *check_result)
-{
-       struct score *score = check_result;
-       return score->score;
-}
-
-static const char *describe(struct manifest *m, void *check_result)
-{
-       struct score *score = check_result;
-       if (verbose >= 2 && score->errors)
-               return talloc_asprintf(m, "Compile errors building examples:\n"
-                                      "%s", score->errors);
-       return NULL;
 }
 
 struct ccanlint examples_compile = {
        .key = "examples-compile",
        .name = "Module examples compile",
-       .score = score_examples,
-       .total_score = 3, /* This gets changed to # examples, if they exist */
        .check = build_examples,
-       .describe = describe,
        .can_run = can_run,
 };