]> git.ozlabs.org Git - ccan/blobdiff - tools/ccanlint/tests/examples_compile.c
ccanlint: make compile commands return output.
[ccan] / tools / ccanlint / tests / examples_compile.c
index b34846371cfb8b96bca5e8ae1d383cfa85bc3067..1b2360ed52e3820e13f65e24ece05e22a3dfff67 100644 (file)
@@ -103,7 +103,7 @@ static char *obj_list(const struct manifest *m, struct ccan_file *f)
 static char *lib_list(const struct manifest *m)
 {
        unsigned int i, num;
-       char **libs = get_libs(m, ".", &num, &m->info_file->compiled);
+       char **libs = get_libs(m, m->dir, &num, &m->info_file->compiled);
        char *ret = talloc_strdup(m, "");
 
        for (i = 0; i < num; i++)
@@ -119,14 +119,14 @@ static char *compile(const void *ctx,
        char *errmsg;
 
        file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
-       errmsg = compile_and_link(ctx, file->fullname, ccan_dir,
-                                 obj_list(m, file),
-                                 "", lib_list(m), file->compiled);
-       if (errmsg) {
+       if (!compile_and_link(ctx, file->fullname, ccan_dir,
+                             obj_list(m, file),
+                             "", lib_list(m), file->compiled, &errmsg)) {
                talloc_free(file->compiled);
                file->compiled = NULL;
                return errmsg;
        }
+       talloc_free(errmsg);
        return NULL;
 }
 
@@ -142,7 +142,7 @@ static char *start_main(char *ret, const char *why)
 static char *add_func(char *others, const char *line)
 {
        const char *p, *end = strchr(line, '(') - 1;
-       while (isblank(*end)) {
+       while (isspace(*end)) {
                end--;
                if (end == line)
                        return others;
@@ -185,7 +185,7 @@ static bool looks_internal(char **lines, char **why)
                const char *line = lines[i] + strspn(lines[i], " \t");
                unsigned len = strspn(line, IDENT_CHARS);
 
-               if (!line[0] || isblank(line[0]) || strstarts(line, "//"))
+               if (!line[0] || isspace(line[0]) || strstarts(line, "//"))
                        continue;
 
                /* The winners. */
@@ -342,7 +342,7 @@ static char *mangle(struct manifest *m, char **lines)
                } else {
                        /* Character at start of line, with ( and no ;
                         * == function start.  Ignore comments. */
-                       if (!isblank(lines[i][0])
+                       if (!isspace(lines[i][0])
                            && strchr(lines[i], '(')
                            && !strchr(lines[i], ';')
                            && !strstr(lines[i], "//")) {
@@ -431,6 +431,23 @@ static struct ccan_file *mangle_example(struct manifest *m,
        return f;
 }
 
+/* If an example has expected output, it's complete and should not be
+ * included in future examples. */
+static bool has_expected_output(char **lines)
+{
+       unsigned int i;
+
+       for (i = 0; lines[i]; i++) {
+               char *p = lines[i] + strspn(lines[i], " \t");
+               if (!strstarts(p, "//"))
+                       continue;
+               p += strspn(p, "/ ");
+               if (strncasecmp(p, "given", strlen("given")) == 0)
+                       return true;
+       }
+       return false;
+}
+
 static void build_examples(struct manifest *m, bool keep,
                           unsigned int *timeleft, struct score *score)
 {
@@ -450,33 +467,37 @@ static void build_examples(struct manifest *m, bool keep,
                strip_leading_whitespace(get_ccan_file_lines(i));
                ret = compile(i, m, i, keep);
                if (!ret) {
-                       prev = get_ccan_file_lines(i);
+                       char **lines = get_ccan_file_lines(i);
+                       if (!has_expected_output(lines))
+                               prev = lines;
                        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);
 
                        mangle2 = mangle_example(m, i, new, keep);
                        ret2 = compile(i, m, mangle2, keep);
                        if (!ret2) {
-                               prev = new;
+                               if (!has_expected_output(new))
+                                       prev = new;
                                score->score++;
                                continue;
                        }
                }
 
+               /* Try standalone. */
+               mangle1 = mangle_example(m, i, get_ccan_file_lines(i), keep);
+               ret1 = compile(i, m, mangle1, keep);
+               if (!ret1) {
+                       char **lines = get_ccan_file_lines(i);
+                       if (!has_expected_output(lines))
+                               prev = lines;
+                       score->score++;
+                       continue;
+               }
+
                score->pass = false;
                score->error = "Compiling extracted examples failed";
                if (!verbose) {
@@ -488,7 +509,8 @@ static void build_examples(struct manifest *m, bool keep,
                                err = "Standalone compile and"
                                        " adding headers both failed";
                } else {
-                       err = talloc_asprintf("Standalone example:\n"
+                       err = talloc_asprintf(score,
+                                             "Standalone example:\n"
                                              "%s\n"
                                              "Errors: %s\n\n"
                                              "Adding headers, wrappers:\n"
@@ -520,4 +542,4 @@ struct ccanlint examples_compile = {
        .can_run = can_run,
 };
 
-REGISTER_TEST(examples_compile, &has_examples, NULL);
+REGISTER_TEST(examples_compile, &has_examples, &build_objs, NULL);