]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/examples_exist.c
ccanlint: fix uninitialized variable
[ccan] / tools / ccanlint / tests / examples_exist.c
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 <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                          bool keep,
20                          struct doc_section *example)
21 {
22         char *name;
23         unsigned int i;
24         int fd;
25         struct ccan_file *f;
26
27         name = talloc_asprintf(m, "%s/example-%s-%s.c",
28                                talloc_dirname(m,
29                                               source->fullname),
30                                source->name,
31                                example->function);
32         /* example->function == 'struct foo' */
33         while (strchr(name, ' '))
34                 *strchr(name, ' ') = '_';
35
36         name = maybe_temp_file(m, ".c", keep, name);
37         f = new_ccan_file(m, talloc_dirname(m, name), talloc_basename(m, name));
38         talloc_steal(f, name);
39         list_add_tail(&m->examples, &f->list);
40
41         fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
42         if (fd < 0)
43                 return talloc_asprintf(m, "Creating temporary file %s: %s",
44                                        f->fullname, strerror(errno));
45
46         for (i = 0; i < example->num_lines; i++) {
47                 if (write(fd, example->lines[i], strlen(example->lines[i]))
48                     != strlen(example->lines[i])
49                     || write(fd, "\n", 1) != 1) {
50                         close(fd);
51                         return "Failure writing to temporary file";
52                 }
53         }
54         close(fd);
55         return NULL;
56 }
57
58 /* FIXME: We should have one example per function in header. */
59 static void extract_examples(struct manifest *m,
60                              bool keep,
61                              unsigned int *timeleft,
62                              struct score *score)
63 {
64         struct ccan_file *f, *mainh = NULL; /* gcc complains uninitialized */
65         struct doc_section *d;
66         bool have_info_example = false, have_header_example = false;
67
68         score->total = 2;
69         list_for_each(get_ccan_file_docs(m->info_file), d, list) {
70                 if (streq(d->type, "example")) {
71                         score->error = add_example(m, m->info_file, keep, d);
72                         if (score->error)
73                                 return;
74                         have_info_example = true;
75                 }
76         }
77
78         /* Check main header. */
79         list_for_each(&m->h_files, f, list) {
80                 if (!strstarts(f->name, m->basename)
81                     || strlen(f->name) != strlen(m->basename) + 2)
82                         continue;
83
84                 mainh = f;
85                 list_for_each(get_ccan_file_docs(f), d, list) {
86                         if (streq(d->type, "example")) {
87                                 score->error = add_example(m, f, keep, d);
88                                 if (score->error)
89                                         return;
90                                 have_header_example = true;
91                         }
92                 }
93         }
94
95         if (have_info_example && have_header_example) {
96                 score->score = score->total;
97                 score->pass = true;
98                 return;
99         }
100
101         score->error = "Expect examples in header and _info";
102         if (!have_info_example)
103                 score_file_error(score, m->info_file, 0, "No Example: section");
104         if (!have_header_example)
105                 score_file_error(score, mainh, 0, "No Example: section");
106
107         score->score = have_info_example + have_header_example;
108         /* We pass if we find any example. */
109         score->pass = score->score != 0;
110 }
111
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"
117 };
118
119 REGISTER_TEST(examples_exist);