]> git.ozlabs.org Git - ccan/blobdiff - tools/ccanlint/tests/examples_exist.c
tools: use tal/path instead of writing own path handlers.
[ccan] / tools / ccanlint / tests / examples_exist.c
index 085f673a7e5ed506a6abe819911ef8e21af5a444..02ddb486268f15e62b686e2b86c675166b728554 100644 (file)
@@ -1,7 +1,8 @@
 #include <tools/ccanlint/ccanlint.h>
 #include <tools/tools.h>
-#include <ccan/talloc/talloc.h>
 #include <ccan/str/str.h>
+#include <ccan/tal/path/path.h>
+#include <ccan/take/take.h>
 #include <ccan/cast/cast.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 
 /* Creates and adds an example file. */
 static char *add_example(struct manifest *m, struct ccan_file *source,
-                        bool keep,
                         struct doc_section *example)
 {
-       char *name;
+       char *name, *linemarker;
        unsigned int i;
        int fd;
        struct ccan_file *f;
 
-       name = talloc_asprintf(m, "%s/example-%s-%s.c",
-                              talloc_dirname(m,
-                                             source->fullname),
-                              source->name,
-                              example->function);
+       name = tal_fmt(m, "example-%s-%s",
+                      source->name, example->function);
        /* example->function == 'struct foo' */
        while (strchr(name, ' '))
                *strchr(name, ' ') = '_';
 
-       name = maybe_temp_file(m, ".c", keep, name);
-       f = new_ccan_file(m, talloc_dirname(m, name), talloc_basename(m, name));
-       talloc_steal(f, name);
+       name = temp_file(m, ".c", take(name));
+       f = new_ccan_file(m, path_dirname(m, name), path_basename(m, name));
+       tal_steal(f, name);
        list_add_tail(&m->examples, &f->list);
 
        fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
        if (fd < 0)
-               return talloc_asprintf(m, "Creating temporary file %s: %s",
-                                      f->fullname, strerror(errno));
+               return tal_fmt(m, "Creating temporary file %s: %s",
+                              f->fullname, strerror(errno));
+
+       /* Add #line to demark where we are from, so errors are correct! */
+       linemarker = tal_fmt(f, "#line %i \"%s\"\n",
+                            example->srcline+2, source->fullname);
+       write(fd, linemarker, strlen(linemarker));
 
        for (i = 0; i < example->num_lines; i++) {
                if (write(fd, example->lines[i], strlen(example->lines[i]))
@@ -59,7 +61,6 @@ static char *add_example(struct manifest *m, struct ccan_file *source,
 
 /* FIXME: We should have one example per function in header. */
 static void extract_examples(struct manifest *m,
-                            bool keep,
                             unsigned int *timeleft,
                             struct score *score)
 {
@@ -70,7 +71,7 @@ static void extract_examples(struct manifest *m,
        score->total = 2;
        list_for_each(get_ccan_file_docs(m->info_file), d, list) {
                if (streq(d->type, "example")) {
-                       score->error = add_example(m, m->info_file, keep, d);
+                       score->error = add_example(m, m->info_file, d);
                        if (score->error)
                                return;
                        have_info_example = true;
@@ -86,7 +87,7 @@ static void extract_examples(struct manifest *m,
                mainh = f;
                list_for_each(get_ccan_file_docs(f), d, list) {
                        if (streq(d->type, "example")) {
-                               score->error = add_example(m, f, keep, d);
+                               score->error = add_example(m, f, d);
                                if (score->error)
                                        return;
                                have_header_example = true;
@@ -94,9 +95,10 @@ static void extract_examples(struct manifest *m,
                }
        }
 
+       /* We don't fail ccanlint for this. */
+       score->pass = true;
        if (have_info_example && have_header_example) {
                score->score = score->total;
-               score->pass = true;
                return;
        }
 
@@ -106,8 +108,6 @@ static void extract_examples(struct manifest *m,
                score_file_error(score, mainh, 0, "No Example: section");
 
        score->score = have_info_example + have_header_example;
-       /* We pass if we find any example. */
-       score->pass = score->score != 0;
 }
 
 struct ccanlint examples_exist = {