ccanlint: add simple check for comment referring to LICENSE file.
[ccan] / tools / ccanlint / tests / license_comment.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <ccan/foreach/foreach.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <limits.h>
8 #include <errno.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <err.h>
12 #include <ccan/talloc/talloc.h>
13 #include <ccan/str/str.h>
14 #include <ccan/str_talloc/str_talloc.h>
15
16 static void check_license_comment(struct manifest *m,
17                                   bool keep,
18                                   unsigned int *timeleft, struct score *score)
19 {
20         struct list_head *list;
21
22         /* No requirements on public domain. */
23         if (m->license == LICENSE_PUBLIC_DOMAIN
24             || m->license == LICENSE_UNKNOWN) {
25                 score->pass = true;
26                 score->score = score->total;
27                 return;
28         }
29
30         foreach_ptr(list, &m->c_files, &m->h_files) {
31                 struct ccan_file *f;
32
33                 list_for_each(list, f, list) {
34                         unsigned int i;
35                         char **lines = get_ccan_file_lines(f);
36                         struct line_info *info = get_ccan_line_info(f);
37                         bool found_license = false, found_flavor = false;
38
39                         for (i = 0; lines[i]; i++) {
40                                 if (info[i].type == CODE_LINE)
41                                         break;
42                                 if (strstr(lines[i], "LICENSE"))
43                                         found_license = true;
44                                 if (strstr(lines[i],
45                                            licenses[m->license].shortname))
46                                         found_flavor = true;
47                         }
48                         if ((!found_license || !found_flavor)
49                             && !find_boilerplate(f, m->license)) {
50                                 score_file_error(score, f, lines[i] ? i : 0,
51                                                  "No reference to license"
52                                                  " found");
53                         }
54                 }
55         }
56
57         if (list_empty(&score->per_file_errors)) {
58                 score->pass = true;
59                 score->score = score->total;
60         }
61 }
62
63 struct ccanlint license_comment = {
64         .key = "license_comment",
65         .name = "Source and header files refer to LICENSE",
66         .check = check_license_comment,
67         .needs = "license_exists"
68 };
69 REGISTER_TEST(license_comment);