2a301ea8b1c077af8bb369e1d2c64cf893080a8d
[ccan] / tools / ccanlint / tests / 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->error = "Trailing whitespace"
40                                                 " found";
41                                         score_file_error(score, f, i+1, err);
42                                 }
43                         }
44                 }
45         }
46         if (!score->error) {
47                 score->pass = true;
48                 score->score = score->total;
49         }
50 }
51
52 struct ccanlint trailing_whitespace = {
53         .key = "no_trailing_whitespace",
54         .name = "Module's source code has no trailing whitespace",
55         .check = check_trailing_whitespace,
56 };
57
58
59 REGISTER_TEST(trailing_whitespace, NULL);