trailing_whitespace.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. /* FIXME: only print full analysis if verbose >= 2. */
  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. if (strlen(line) > 20)
  16. return talloc_asprintf(line, "...'%s'",
  17. line + strlen(line) - 20);
  18. return talloc_asprintf(line, "'%s'", line);
  19. }
  20. static void *check_trailing_whitespace(struct manifest *m,
  21. bool keep,
  22. unsigned int *timeleft)
  23. {
  24. char *report;
  25. report = report_on_lines(&m->c_files, report_on_trailing_whitespace,
  26. NULL);
  27. report = report_on_lines(&m->h_files, report_on_trailing_whitespace,
  28. report);
  29. return report;
  30. }
  31. static const char *describe_trailing_whitespace(struct manifest *m,
  32. void *check_result)
  33. {
  34. if (!verbose)
  35. return NULL;
  36. return talloc_asprintf(check_result,
  37. "Some source files have trailing whitespace:\n"
  38. "%s", (char *)check_result);
  39. }
  40. struct ccanlint trailing_whitespace = {
  41. .key = "trailing-whitespace",
  42. .name = "Module's source code has no trailing whitespace",
  43. .total_score = 1,
  44. .check = check_trailing_whitespace,
  45. .describe = describe_trailing_whitespace,
  46. };
  47. REGISTER_TEST(trailing_whitespace, NULL);