trailing_whitespace.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. static char *report_on_trailing_whitespace(const char *line)
  6. {
  7. const char *e = strchr(line, 0);
  8. while (e>line && (e[-1]==' ' || e[-1]=='\t'))
  9. e--;
  10. if (*e == 0)
  11. return NULL; //there were no trailing spaces
  12. if (e == line)
  13. return NULL; //the line only consists of spaces
  14. if (strlen(line) > 20)
  15. return talloc_asprintf(line, "...'%s'",
  16. line + strlen(line) - 20);
  17. return talloc_asprintf(line, "'%s'", line);
  18. }
  19. static void *check_trailing_whitespace(struct manifest *m)
  20. {
  21. char *report;
  22. report = report_on_lines(&m->c_files, report_on_trailing_whitespace,
  23. NULL);
  24. report = report_on_lines(&m->h_files, report_on_trailing_whitespace,
  25. report);
  26. return report;
  27. }
  28. static const char *describe_trailing_whitespace(struct manifest *m,
  29. void *check_result)
  30. {
  31. return talloc_asprintf(check_result,
  32. "Some source files have trailing whitespace:\n"
  33. "%s", (char *)check_result);
  34. }
  35. struct ccanlint trailing_whitespace = {
  36. .name = "No lines with unnecessary trailing whitespace",
  37. .total_score = 1,
  38. .check = check_trailing_whitespace,
  39. .describe = describe_trailing_whitespace,
  40. };