]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/examples_exist.c
tools: use tal instead of talloc.
[ccan] / tools / ccanlint / tests / examples_exist.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/str/str.h>
4 #include <ccan/cast/cast.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <limits.h>
10 #include <errno.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <err.h>
14 #include <string.h>
15 #include <ctype.h>
16
17 /* Creates and adds an example file. */
18 static char *add_example(struct manifest *m, struct ccan_file *source,
19                          struct doc_section *example)
20 {
21         char *name, *linemarker;
22         unsigned int i;
23         int fd;
24         struct ccan_file *f;
25
26         name = tal_fmt(m, "%s/example-%s-%s.c",
27                        tal_dirname(m, source->fullname),
28                        source->name,
29                        example->function);
30         /* example->function == 'struct foo' */
31         while (strchr(name, ' '))
32                 *strchr(name, ' ') = '_';
33
34         name = temp_file(m, ".c", name);
35         f = new_ccan_file(m, tal_dirname(m, name), tal_basename(m, name));
36         tal_steal(f, name);
37         list_add_tail(&m->examples, &f->list);
38
39         fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
40         if (fd < 0)
41                 return tal_fmt(m, "Creating temporary file %s: %s",
42                                f->fullname, strerror(errno));
43
44         /* Add #line to demark where we are from, so errors are correct! */
45         linemarker = tal_fmt(f, "#line %i \"%s\"\n",
46                              example->srcline+2, source->fullname);
47         write(fd, linemarker, strlen(linemarker));
48
49         for (i = 0; i < example->num_lines; i++) {
50                 if (write(fd, example->lines[i], strlen(example->lines[i]))
51                     != strlen(example->lines[i])
52                     || write(fd, "\n", 1) != 1) {
53                         close(fd);
54                         return cast_const(char *,
55                                           "Failure writing to temporary file");
56                 }
57         }
58         close(fd);
59         return NULL;
60 }
61
62 /* FIXME: We should have one example per function in header. */
63 static void extract_examples(struct manifest *m,
64                              unsigned int *timeleft,
65                              struct score *score)
66 {
67         struct ccan_file *f, *mainh = NULL; /* gcc complains uninitialized */
68         struct doc_section *d;
69         bool have_info_example = false, have_header_example = false;
70
71         score->total = 2;
72         list_for_each(get_ccan_file_docs(m->info_file), d, list) {
73                 if (streq(d->type, "example")) {
74                         score->error = add_example(m, m->info_file, d);
75                         if (score->error)
76                                 return;
77                         have_info_example = true;
78                 }
79         }
80
81         /* Check main header. */
82         list_for_each(&m->h_files, f, list) {
83                 if (!strstarts(f->name, m->basename)
84                     || strlen(f->name) != strlen(m->basename) + 2)
85                         continue;
86
87                 mainh = f;
88                 list_for_each(get_ccan_file_docs(f), d, list) {
89                         if (streq(d->type, "example")) {
90                                 score->error = add_example(m, f, d);
91                                 if (score->error)
92                                         return;
93                                 have_header_example = true;
94                         }
95                 }
96         }
97
98         /* We don't fail ccanlint for this. */
99         score->pass = true;
100         if (have_info_example && have_header_example) {
101                 score->score = score->total;
102                 return;
103         }
104
105         if (!have_info_example)
106                 score_file_error(score, m->info_file, 0, "No Example: section");
107         if (!have_header_example)
108                 score_file_error(score, mainh, 0, "No Example: section");
109
110         score->score = have_info_example + have_header_example;
111 }
112
113 struct ccanlint examples_exist = {
114         .key = "examples_exist",
115         .name = "_info and main header file have Example: sections",
116         .check = extract_examples,
117         .needs = "info_exists"
118 };
119
120 REGISTER_TEST(examples_exist);