no_trailing_whitespace.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* Trailing whitespace test. Almost embarrassing, but trivial. */
  2. #include <tools/ccanlint/ccanlint.h>
  3. #include <ccan/foreach/foreach.h>
  4. #include <ccan/str/str.h>
  5. #include <ccan/tal/str/str.h>
  6. /* FIXME: only print full analysis if verbose >= 2. */
  7. static char *get_trailing_whitespace(const tal_t *ctx, 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 tal_fmt(ctx, "...'%s'", line + strlen(line) - 20);
  18. return tal_fmt(ctx, "'%s'", line);
  19. }
  20. static void check_trailing_whitespace(struct manifest *m,
  21. unsigned int *timeleft,
  22. struct score *score)
  23. {
  24. struct list_head *list;
  25. struct ccan_file *f;
  26. unsigned int i;
  27. /* We don't fail ccanlint for this. */
  28. score->pass = true;
  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(score,
  34. lines[i]);
  35. if (err)
  36. score_file_error(score, f, i+1,
  37. "%s", err);
  38. }
  39. }
  40. }
  41. if (!score->error) {
  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 = "info_exists"
  50. };
  51. REGISTER_TEST(no_trailing_whitespace);