1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/str/str.h>
5 #include <ccan/cast/cast.h>
18 /* Creates and adds an example file. */
19 static char *add_example(struct manifest *m, struct ccan_file *source,
21 struct doc_section *example)
28 name = talloc_asprintf(m, "%s/example-%s-%s.c",
33 /* example->function == 'struct foo' */
34 while (strchr(name, ' '))
35 *strchr(name, ' ') = '_';
37 name = maybe_temp_file(m, ".c", keep, name);
38 f = new_ccan_file(m, talloc_dirname(m, name), talloc_basename(m, name));
39 talloc_steal(f, name);
40 list_add_tail(&m->examples, &f->list);
42 fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
44 return talloc_asprintf(m, "Creating temporary file %s: %s",
45 f->fullname, strerror(errno));
47 for (i = 0; i < example->num_lines; i++) {
48 if (write(fd, example->lines[i], strlen(example->lines[i]))
49 != strlen(example->lines[i])
50 || write(fd, "\n", 1) != 1) {
52 return cast_const(char *,
53 "Failure writing to temporary file");
60 /* FIXME: We should have one example per function in header. */
61 static void extract_examples(struct manifest *m,
63 unsigned int *timeleft,
66 struct ccan_file *f, *mainh = NULL; /* gcc complains uninitialized */
67 struct doc_section *d;
68 bool have_info_example = false, have_header_example = false;
71 list_for_each(get_ccan_file_docs(m->info_file), d, list) {
72 if (streq(d->type, "example")) {
73 score->error = add_example(m, m->info_file, keep, d);
76 have_info_example = true;
80 /* Check main header. */
81 list_for_each(&m->h_files, f, list) {
82 if (!strstarts(f->name, m->basename)
83 || strlen(f->name) != strlen(m->basename) + 2)
87 list_for_each(get_ccan_file_docs(f), d, list) {
88 if (streq(d->type, "example")) {
89 score->error = add_example(m, f, keep, d);
92 have_header_example = true;
97 /* We don't fail ccanlint for this. */
99 if (have_info_example && have_header_example) {
100 score->score = score->total;
104 if (!have_info_example)
105 score_file_error(score, m->info_file, 0, "No Example: section");
106 if (!have_header_example)
107 score_file_error(score, mainh, 0, "No Example: section");
109 score->score = have_info_example + have_header_example;
112 struct ccanlint examples_exist = {
113 .key = "examples_exist",
114 .name = "_info and main header file have Example: sections",
115 .check = extract_examples,
116 .needs = "info_exists"
119 REGISTER_TEST(examples_exist);