From: Rusty Russell Date: Wed, 6 Oct 2010 01:45:31 +0000 (+1030) Subject: ccanlint: load file contents on demand, fix names for extracted examples. X-Git-Url: https://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=f3305bc06847d022831f8ffd7beaccab316d8143 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". --- diff --git a/tools/ccanlint/ccanlint.h b/tools/ccanlint/ccanlint.h index 46d1fc9d..7d9bd7c8 100644 --- a/tools/ccanlint/ccanlint.h +++ b/tools/ccanlint/ccanlint.h @@ -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); diff --git a/tools/ccanlint/file_analysis.c b/tools/ccanlint/file_analysis.c index 4908a895..9abd6228 100644 --- a/tools/ccanlint/file_analysis.c +++ b/tools/ccanlint/file_analysis.c @@ -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; diff --git a/tools/ccanlint/tests/has_examples.c b/tools/ccanlint/tests/has_examples.c index 6c8fff4d..22d18e9d 100644 --- a/tools/ccanlint/tests/has_examples.c +++ b/tools/ccanlint/tests/has_examples.c @@ -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;