]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/trailing_whitespace.c
ccanlint: use gcov to rate test coverage (score out of 5)
[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                                        bool keep,
24                                        unsigned int *timeleft)
25 {
26         char *report;
27
28         report = report_on_lines(&m->c_files, report_on_trailing_whitespace,
29                                  NULL);
30         report = report_on_lines(&m->h_files, report_on_trailing_whitespace,
31                                  report);
32
33         return report;
34 }
35
36 static const char *describe_trailing_whitespace(struct manifest *m,
37                                                 void *check_result)
38 {
39         return talloc_asprintf(check_result, 
40                                "Some source files have trailing whitespace:\n"
41                                "%s", (char *)check_result);
42 }
43
44 struct ccanlint trailing_whitespace = {
45         .key = "trailing-whitespace",
46         .name = "Module's source code has no trailing whitespace",
47         .total_score = 1,
48         .check = check_trailing_whitespace,
49         .describe = describe_trailing_whitespace,
50 };
51
52
53 REGISTER_TEST(trailing_whitespace, NULL);