]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/trailing_whitespace.c
0fdd0cf847fa9653617bc89df0a40e0abd039282
[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/str/str.h>
5
6 static char *report_on_trailing_whitespace(const char *line)
7 {
8         const char *e = strchr(line, 0);
9         while (e>line && (e[-1]==' ' || e[-1]=='\t'))
10                 e--;
11         if (*e == 0)
12                 return NULL; //there were no trailing spaces
13         if (e == line)
14                 return NULL; //the line only consists of spaces
15
16         if (strlen(line) > 20)
17                 return talloc_asprintf(line, "...'%s'",
18                                        line + strlen(line) - 20);
19         return talloc_asprintf(line, "'%s'", line);
20 }
21
22 static void *check_trailing_whitespace(struct manifest *m,
23                                        unsigned int *timeleft)
24 {
25         char *report;
26
27         report = report_on_lines(&m->c_files, report_on_trailing_whitespace,
28                                  NULL);
29         report = report_on_lines(&m->h_files, report_on_trailing_whitespace,
30                                  report);
31
32         return report;
33 }
34
35 static const char *describe_trailing_whitespace(struct manifest *m,
36                                                 void *check_result)
37 {
38         return talloc_asprintf(check_result, 
39                                "Some source files have trailing whitespace:\n"
40                                "%s", (char *)check_result);
41 }
42
43 struct ccanlint trailing_whitespace = {
44         .key = "trailing-whitespace",
45         .name = "Module's source code has no trailing whitespace",
46         .total_score = 1,
47         .check = check_trailing_whitespace,
48         .describe = describe_trailing_whitespace,
49 };
50
51
52 REGISTER_TEST(trailing_whitespace, NULL);