Commit f3305bc0 authored by Rusty Russell's avatar Rusty Russell

ccanlint: load file contents on demand, fix names for extracted examples.

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".
parent 4d10049d
......@@ -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);
......
......@@ -18,10 +18,21 @@
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;
......
......@@ -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;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment