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/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. unsigned int *timeleft,
  23. struct score *score)
  24. {
  25. struct list_head *list;
  26. struct ccan_file *f;
  27. unsigned int i;
  28. /* We don't fail ccanlint for this. */
  29. score->pass = true;
  30. foreach_ptr(list, &m->c_files, &m->h_files) {
  31. list_for_each(list, f, list) {
  32. char **lines = get_ccan_file_lines(f);
  33. for (i = 0; i < f->num_lines; i++) {
  34. char *err = get_trailing_whitespace(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);