no_trailing_whitespace.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. foreach_ptr(list, &m->c_files, &m->h_files) {
  30. list_for_each(list, f, list) {
  31. char **lines = get_ccan_file_lines(f);
  32. for (i = 0; i < f->num_lines; i++) {
  33. char *err = get_trailing_whitespace(lines[i]);
  34. if (err)
  35. score_file_error(score, f, i+1,
  36. "%s", err);
  37. }
  38. }
  39. }
  40. if (!score->error) {
  41. score->pass = true;
  42. score->score = score->total;
  43. }
  44. }
  45. struct ccanlint no_trailing_whitespace = {
  46. .key = "no_trailing_whitespace",
  47. .name = "Module's source code has no trailing whitespace",
  48. .check = check_trailing_whitespace,
  49. .needs = ""
  50. };
  51. REGISTER_TEST(no_trailing_whitespace);