ccanlint: keep mapping back to original source line for doc_extract
[ccan] / tools / ccanlint / tests / info_summary_single_line.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <stdio.h>
4 #include <ccan/talloc/talloc.h>
5 #include <ccan/str/str.h>
6
7 /* Summary line is form '<identifier> - ' (spaces for 'struct foo -') */
8 /* slightly modified from doc_extract-core.c */
9 static unsigned int is_summary_line(const char *line)
10 {
11         unsigned int id_len;
12
13         id_len = strspn(line, IDENT_CHARS" *");
14         if (id_len == 0)
15                 return 0;
16         if (strspn(line, " ") == id_len)
17                 return 0;
18         if (!strstarts(line + id_len-1, " - "))
19                 return 0;
20         return id_len - 1;
21 }
22
23 static void check_info_summary_single_line(struct manifest *m,
24                                            bool keep,
25                                            unsigned int *timeleft,
26                                            struct score *score)
27 {
28         int i = 0;
29         get_ccan_line_info(m->info_file);
30         score->total = 1;
31         for (i = 0; i < m->info_file->num_lines; ++i) {
32                 if (is_summary_line(m->info_file->lines[i])) {
33                         if (strspn(m->info_file->lines[i+1], " *") == strlen(m->info_file->lines[i+1])) {
34                                 /* valid summary line */
35                                 score->error = NULL;
36                                 score->pass = true;
37                                 score->score = 1;
38                         } else {
39                                 /* invalid summary line - line following summary line should be empty */
40                                 score->pass = false;
41                                 score->score = 0;
42                                 score->error = "invalid summary line - not on a single line:";
43                                 score_file_error(score, m->info_file, i+1, "summary is not on a single line");
44                         }
45                         break;
46                 }
47         }
48 }
49
50
51 struct ccanlint info_summary_single_line = {
52         .key = "info_summary_single_line",
53         .name = "Module has a single line summary in _info",
54         .check = check_info_summary_single_line,
55         .needs = "info_exists"
56 };
57
58 REGISTER_TEST(info_summary_single_line);