]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/no_trailing_whitespace.c
ccanlint: remove redundant num_lines in struct ccan_file.
[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/foreach/foreach.h>
4 #include <ccan/str/str.h>
5 #include <ccan/tal/str/str.h>
6
7 /* FIXME: only print full analysis if verbose >= 2.  */
8 static char *get_trailing_whitespace(const tal_t *ctx, 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 tal_fmt(ctx, "...'%s'", line + strlen(line) - 20);
20         return tal_fmt(ctx, "'%s'", line);
21 }
22
23 static void check_trailing_whitespace(struct manifest *m,
24                                       unsigned int *timeleft,
25                                       struct score *score)
26 {
27         struct list_head *list;
28         struct ccan_file *f;
29         unsigned int i;
30
31         /* We don't fail ccanlint for this. */
32         score->pass = true;
33
34         foreach_ptr(list, &m->c_files, &m->h_files) {
35                 list_for_each(list, f, list) {
36                         char **lines = get_ccan_file_lines(f);
37                         for (i = 0; f->lines[i]; i++) {
38                                 char *err = get_trailing_whitespace(score,
39                                                                     lines[i]);
40                                 if (err)
41                                         score_file_error(score, f, i+1,
42                                                          "%s", err);
43                         }
44                 }
45         }
46         if (!score->error) {
47                 score->score = score->total;
48         }
49 }
50
51 struct ccanlint no_trailing_whitespace = {
52         .key = "no_trailing_whitespace",
53         .name = "Module's source code has no trailing whitespace",
54         .check = check_trailing_whitespace,
55         .needs = "info_exists"
56 };
57
58
59 REGISTER_TEST(no_trailing_whitespace);