trailing_whitespace.c 1.4 KB

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