Browse Source

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".
Rusty Russell 15 years ago
parent
commit
f3305bc068
3 changed files with 20 additions and 12 deletions
  1. 4 1
      tools/ccanlint/ccanlint.h
  2. 14 9
      tools/ccanlint/file_analysis.c
  3. 2 2
      tools/ccanlint/tests/has_examples.c

+ 4 - 1
tools/ccanlint/ccanlint.h

@@ -132,7 +132,7 @@ struct ccan_file {
 	char *fullname;
 	char *fullname;
 
 
 	/* Pristine version of the original file.
 	/* 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;
 	const char *contents;
 	size_t contents_size;
 	size_t contents_size;
 
 
@@ -153,6 +153,9 @@ struct ccan_file {
 /* A new ccan_file, with the given name (talloc_steal onto returned value). */
 /* 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);
 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. */
 /* Use this rather than accessing f->lines directly: loads on demand. */
 char **get_ccan_file_lines(struct ccan_file *f);
 char **get_ccan_file_lines(struct ccan_file *f);
 
 

+ 14 - 9
tools/ccanlint/file_analysis.c

@@ -18,10 +18,21 @@
 
 
 const char *ccan_dir;
 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)
 char **get_ccan_file_lines(struct ccan_file *f)
 {
 {
 	if (!f->lines)
 	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;
 	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->compiled = NULL;
 	f->name = talloc_steal(f, name);
 	f->name = talloc_steal(f, name);
 	f->fullname = talloc_asprintf(f, "%s/%s", dir, f->name);
 	f->fullname = talloc_asprintf(f, "%s/%s", dir, f->name);
+	f->contents = NULL;
+	f->cov_compiled = NULL;
 	return f;
 	return f;
 }
 }
 
 
@@ -90,23 +103,15 @@ static void add_files(struct manifest *m, const char *dir)
 
 
 		if (streq(f->name, "_info")) {
 		if (streq(f->name, "_info")) {
 			m->info_file = f;
 			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;
 			continue;
 		}
 		}
 
 
 		is_c_src = strends(f->name, ".c");
 		is_c_src = strends(f->name, ".c");
 		if (!is_c_src && !strends(f->name, ".h")) {
 		if (!is_c_src && !strends(f->name, ".h")) {
-			/* We don't pull in contents of non-source files */
 			dest = &m->other_files;
 			dest = &m->other_files;
 			continue;
 			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 (!strchr(f->name, '/')) {
 			if (is_c_src)
 			if (is_c_src)
 				dest = &m->c_files;
 				dest = &m->c_files;

+ 2 - 2
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;
 	struct ccan_file *f;
 
 
 	name = maybe_temp_file(m, ".c", keep, 
 	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,
 					       talloc_dirname(m,
 							      source->fullname),
 							      source->fullname),
 					       source->name,
 					       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) {
 	list_for_each(get_ccan_file_docs(m->info_file), d, list) {
 		if (streq(d->type, "example")) {
 		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)
 			if (score->error)
 				return score;
 				return score;
 			score->info_example = true;
 			score->info_example = true;