]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/trailing_whitespace.c
f522d316e5c406742dc0e344be450114d83ca5f1
[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 {
24         char *report;
25
26         report = report_on_lines(&m->c_files, report_on_trailing_whitespace,
27                                  NULL);
28         report = report_on_lines(&m->h_files, report_on_trailing_whitespace,
29                                  report);
30
31         return report;
32 }
33
34 static const char *describe_trailing_whitespace(struct manifest *m,
35                                                 void *check_result)
36 {
37         return talloc_asprintf(check_result, 
38                                "Some source files have trailing whitespace:\n"
39                                "%s", (char *)check_result);
40 }
41
42 struct ccanlint trailing_whitespace = {
43         .name = "No lines with unnecessary trailing whitespace",
44         .total_score = 1,
45         .check = check_trailing_whitespace,
46         .describe = describe_trailing_whitespace,
47 };