]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/no_trailing_whitespace.c
ccanlint: give a point per compile_ok/compile_fail test
[ccan] / tools / ccanlint / tests / no_trailing_whitespace.c
1 /* Trailing whitespace test.  Almost embarrassing, but trivial. */
2 #include <tools/ccanlint/ccanlint.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/foreach/foreach.h>
5 #include <ccan/str/str.h>
6
7 /* FIXME: only print full analysis if verbose >= 2.  */
8 static char *get_trailing_whitespace(const char *line)
9 {
10         const char *e = strchr(line, 0);
11         while (e>line && (e[-1]==' ' || e[-1]=='\t'))
12                 e--;
13         if (*e == 0)
14                 return NULL; //there were no trailing spaces
15         if (e == line)
16                 return NULL; //the line only consists of spaces
17
18         if (strlen(line) > 20)
19                 return talloc_asprintf(line, "...'%s'",
20                                        line + strlen(line) - 20);
21         return talloc_asprintf(line, "'%s'", line);
22 }
23
24 static void check_trailing_whitespace(struct manifest *m,
25                                       bool keep,
26                                       unsigned int *timeleft,
27                                       struct score *score)
28 {
29         struct list_head *list;
30         struct ccan_file *f;
31         unsigned int i;
32
33         foreach_ptr(list, &m->c_files, &m->h_files) {
34                 list_for_each(list, f, list) {
35                         char **lines = get_ccan_file_lines(f);
36                         for (i = 0; i < f->num_lines; i++) {
37                                 char *err = get_trailing_whitespace(lines[i]);
38                                 if (err)
39                                         score_file_error(score, f, i+1,
40                                                          "%s", err);
41                         }
42                 }
43         }
44         if (!score->error) {
45                 score->pass = true;
46                 score->score = score->total;
47         }
48 }
49
50 struct ccanlint no_trailing_whitespace = {
51         .key = "no_trailing_whitespace",
52         .name = "Module's source code has no trailing whitespace",
53         .check = check_trailing_whitespace,
54         .needs = ""
55 };
56
57
58 REGISTER_TEST(no_trailing_whitespace);