no_trailing_whitespace.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* Trailing whitespace test. Almost embarrassing, but trivial. */
  2. #include <tools/ccanlint/ccanlint.h>
  3. #include <ccan/talloc/talloc.h>
  4. #include <ccan/foreach/foreach.h>
  5. #include <ccan/str/str.h>
  6. /* FIXME: only print full analysis if verbose >= 2. */
  7. static char *get_trailing_whitespace(const char *line)
  8. {
  9. const char *e = strchr(line, 0);
  10. while (e>line && (e[-1]==' ' || e[-1]=='\t'))
  11. e--;
  12. if (*e == 0)
  13. return NULL; //there were no trailing spaces
  14. if (e == line)
  15. return NULL; //the line only consists of spaces
  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. static void check_trailing_whitespace(struct manifest *m,
  22. bool keep,
  23. unsigned int *timeleft,
  24. struct score *score)
  25. {
  26. struct list_head *list;
  27. struct ccan_file *f;
  28. unsigned int i;
  29. /* We don't fail ccanlint for this. */
  30. score->pass = true;
  31. foreach_ptr(list, &m->c_files, &m->h_files) {
  32. list_for_each(list, f, list) {
  33. char **lines = get_ccan_file_lines(f);
  34. for (i = 0; i < f->num_lines; i++) {
  35. char *err = get_trailing_whitespace(lines[i]);
  36. if (err)
  37. score_file_error(score, f, i+1,
  38. "%s", err);
  39. }
  40. }
  41. }
  42. if (!score->error) {
  43. score->score = score->total;
  44. }
  45. }
  46. struct ccanlint no_trailing_whitespace = {
  47. .key = "no_trailing_whitespace",
  48. .name = "Module's source code has no trailing whitespace",
  49. .check = check_trailing_whitespace,
  50. .needs = ""
  51. };
  52. REGISTER_TEST(no_trailing_whitespace);