ccanlint: load file contents on demand, fix names for extracted examples.
authorRusty Russell <rusty@rustcorp.com.au>
Wed, 6 Oct 2010 01:45:31 +0000 (12:15 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Wed, 6 Oct 2010 01:45:31 +0000 (12:15 +1030)
We had example names like "example-opt.h-opt_parse", which got trimmed to
"example-opt".  By using a basename of example-opt.h-opt_parse.c, we only
strip the final ".c" not the middle ".h".

tools/ccanlint/ccanlint.h
tools/ccanlint/file_analysis.c
tools/ccanlint/tests/has_examples.c

index 46d1fc9da9c6106058f6ef1673a90f66962fbc0a..7d9bd7c821ec6149e7c296333e062c14b3c88e55 100644 (file)
@@ -132,7 +132,7 @@ struct ccan_file {
        char *fullname;
 
        /* Pristine version of the original file.
-        * Use get_ccan_file_lines to fill this. */
+        * Use get_ccan_file_contents to fill this. */
        const char *contents;
        size_t contents_size;
 
@@ -153,6 +153,9 @@ struct ccan_file {
 /* A new ccan_file, with the given name (talloc_steal onto returned value). */
 struct ccan_file *new_ccan_file(const void *ctx, const char *dir, char *name);
 
+/* Use this rather than accessing f->contents directly: loads on demand. */
+const char *get_ccan_file_contents(struct ccan_file *f);
+
 /* Use this rather than accessing f->lines directly: loads on demand. */
 char **get_ccan_file_lines(struct ccan_file *f);
 
index 4908a895423b98eddcd70fc5d973dc60e871d4b9..9abd62288e6923307f68716d34ea7d7b2a96da04 100644 (file)
 
 const char *ccan_dir;
 
+const char *get_ccan_file_contents(struct ccan_file *f)
+{
+       if (!f->contents) {
+               f->contents = grab_file(f, f->fullname, &f->contents_size);
+               if (!f->contents)
+                       err(1, "Reading file %s", f->fullname);
+       }
+       return f->contents;
+}
+
 char **get_ccan_file_lines(struct ccan_file *f)
 {
        if (!f->lines)
-               f->lines = strsplit(f, f->contents, "\n", &f->num_lines);
+               f->lines = strsplit(f, get_ccan_file_contents(f),
+                                   "\n", &f->num_lines);
 
        return f->lines;
 }
@@ -48,6 +59,8 @@ struct ccan_file *new_ccan_file(const void *ctx, const char *dir, char *name)
        f->compiled = NULL;
        f->name = talloc_steal(f, name);
        f->fullname = talloc_asprintf(f, "%s/%s", dir, f->name);
+       f->contents = NULL;
+       f->cov_compiled = NULL;
        return f;
 }
 
@@ -90,23 +103,15 @@ static void add_files(struct manifest *m, const char *dir)
 
                if (streq(f->name, "_info")) {
                        m->info_file = f;
-                       f->contents = grab_file(f, f->name, &f->contents_size);
-                       if (!f->contents)
-                               err(1, "Reading file %s", f->name);
                        continue;
                }
 
                is_c_src = strends(f->name, ".c");
                if (!is_c_src && !strends(f->name, ".h")) {
-                       /* We don't pull in contents of non-source files */
                        dest = &m->other_files;
                        continue;
                }
 
-               f->contents = grab_file(f, f->name, &f->contents_size);
-               if (!f->contents)
-                       err(1, "Reading file %s", f->name);
-
                if (!strchr(f->name, '/')) {
                        if (is_c_src)
                                dest = &m->c_files;
index 6c8fff4d68bfd558b38b2ad398ee652ef185b11b..22d18e9d5a1440004c341d2bde93f3b735707a7b 100644 (file)
@@ -25,7 +25,7 @@ static char *add_example(struct manifest *m, struct ccan_file *source,
        struct ccan_file *f;
 
        name = maybe_temp_file(m, ".c", keep, 
-                              talloc_asprintf(m, "%s/example-%s-%s",
+                              talloc_asprintf(m, "%s/example-%s-%s.c",
                                               talloc_dirname(m,
                                                              source->fullname),
                                               source->name,
@@ -70,7 +70,7 @@ static void *extract_examples(struct manifest *m,
 
        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, keep, d);
                        if (score->error)
                                return score;
                        score->info_example = true;