ccanlint: with -vv, give details on attempts to compile examples.
[ccan] / tools / ccanlint / tests / has_examples.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 struct score {
60         bool info_example, header_example;
61         char *error;
62 };
63
64 static void *extract_examples(struct manifest *m,
65                               bool keep,
66                               unsigned int *timeleft)
67 {
68         struct ccan_file *f;
69         struct doc_section *d;
70         struct score *score = talloc(m, struct score);
71
72         score->info_example = score->header_example = false;
73         score->error = NULL;
74
75         list_for_each(get_ccan_file_docs(m->info_file), d, list) {
76                 if (streq(d->type, "example")) {
77                         score->error = add_example(m, m->info_file, keep, d);
78                         if (score->error)
79                                 return score;
80                         score->info_example = true;
81                 }
82         }
83
84         /* Check main header. */
85         list_for_each(&m->h_files, f, list) {
86                 if (!strstarts(f->name, m->basename)
87                     || strlen(f->name) != strlen(m->basename) + 2)
88                         continue;
89
90                 list_for_each(get_ccan_file_docs(f), d, list) {
91                         if (streq(d->type, "example")) {
92                                 score->error = add_example(m, f, keep, d);
93                                 if (score->error)
94                                         return score;
95                                 score->header_example = true;
96                         }
97                 }
98         }
99         return score;
100 }
101
102 static unsigned int score_examples(struct manifest *m, void *check_result)
103 {
104         struct score *score = check_result;
105         int total = 0;
106
107         if (score->error)
108                 return 0;
109         total += score->info_example;
110         total += score->header_example;
111         return total;
112 }
113
114 static const char *describe_examples(struct manifest *m,
115                                      void *check_result)
116 {
117         struct score *score = check_result;
118         char *descrip = NULL;
119
120         if (score->error)
121                 return score->error;
122
123         if (!score->info_example)
124                 descrip = talloc_asprintf(score,
125                 "Your _info file has no module example.\n\n"
126                 "There should be an Example: section of the _info documentation\n"
127                 "which provides a concise toy program which uses your module\n");
128
129         if (!score->header_example)
130                 descrip = talloc_asprintf(score,
131                  "%sMain header file file has no examples\n\n"
132                  "There should be an Example: section for each public function\n"
133                   "demonstrating its use\n", descrip ? descrip : "");
134
135         return descrip;
136 }
137
138 struct ccanlint has_examples = {
139         .key = "has-examples",
140         .name = "_info and header files have examples",
141         .score = score_examples,
142         .check = extract_examples,
143         .describe = describe_examples,
144         .total_score = 2,
145 };
146
147 REGISTER_TEST(has_examples, &has_info, NULL);