trailing_whitespace.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. unsigned int *timeleft)
  21. {
  22. char *report;
  23. report = report_on_lines(&m->c_files, report_on_trailing_whitespace,
  24. NULL);
  25. report = report_on_lines(&m->h_files, report_on_trailing_whitespace,
  26. report);
  27. return report;
  28. }
  29. static const char *describe_trailing_whitespace(struct manifest *m,
  30. void *check_result)
  31. {
  32. return talloc_asprintf(check_result,
  33. "Some source files have trailing whitespace:\n"
  34. "%s", (char *)check_result);
  35. }
  36. struct ccanlint trailing_whitespace = {
  37. .key = "trailing-whitespace",
  38. .name = "Module's source code has no trailing whitespace",
  39. .total_score = 1,
  40. .check = check_trailing_whitespace,
  41. .describe = describe_trailing_whitespace,
  42. };
  43. REGISTER_TEST(trailing_whitespace, NULL);