]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/examples_relevant.c
ccanlint: check examples actually mention thing they are demonstrating.
[ccan] / tools / ccanlint / tests / examples_relevant.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 static void examples_relevant_check(struct manifest *m,
18                                     bool keep,
19                                     unsigned int *timeleft,
20                                     struct score *score)
21 {
22         struct ccan_file *f;
23         struct doc_section *d;
24
25         list_for_each(&m->h_files, f, list) {
26                 list_for_each(get_ccan_file_docs(f), d, list) {
27                         unsigned int i;
28                         bool found = false;
29
30                         if (!streq(d->type, "example"))
31                                 continue;
32
33                         for (i = 0; i < d->num_lines; i++) {
34                                 if (strstr(d->lines[i], d->function))
35                                         found = true;
36                         }
37
38                         if (!found) {
39                                 score_file_error(score, f, d->srcline+1,
40                                                  "Example for %s doesn't"
41                                                  " mention it", d->function);
42                         }
43                 }
44         }
45
46         if (!score->error) {
47                 score->score = score->total;
48                 score->pass = true;
49                 return;
50         }
51 }
52
53 struct ccanlint examples_relevant = {
54         .key = "examples_relevant",
55         .name = "Example: sections demonstrate appropriate function",
56         .check = examples_relevant_check,
57         .needs = "examples_exist"
58 };
59
60 REGISTER_TEST(examples_relevant);