depends_accurate.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/talloc/talloc.h>
  4. #include <ccan/str/str.h>
  5. #include <ccan/foreach/foreach.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include <limits.h>
  11. #include <errno.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <err.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. static char *strip_spaces(const void *ctx, char *line)
  18. {
  19. char *p = talloc_strdup(ctx, line);
  20. unsigned int i, j;
  21. for (i = 0, j = 0; p[i]; i++) {
  22. if (!isspace(p[i]))
  23. p[j++] = p[i];
  24. }
  25. p[j] = '\0';
  26. return p;
  27. }
  28. static bool has_dep(struct manifest *m, const char *depname, bool tap_ok)
  29. {
  30. struct ccan_file *f;
  31. if (tap_ok && streq(depname, "ccan/tap"))
  32. return true;
  33. /* We can include ourselves, of course. */
  34. if (streq(depname + strlen("ccan/"), m->basename))
  35. return true;
  36. list_for_each(&m->dep_dirs, f, list) {
  37. if (streq(f->name, depname))
  38. return true;
  39. }
  40. return false;
  41. }
  42. static void check_depends_accurate(struct manifest *m,
  43. bool keep,
  44. unsigned int *timeleft, struct score *score)
  45. {
  46. struct list_head *list;
  47. foreach_ptr(list, &m->c_files, &m->h_files,
  48. &m->run_tests, &m->api_tests,
  49. &m->compile_ok_tests, &m->compile_fail_tests,
  50. &m->other_test_c_files) {
  51. struct ccan_file *f;
  52. bool tap_ok;
  53. /* Including ccan/tap is fine for tests. */
  54. tap_ok = (list != &m->c_files && list != &m->h_files);
  55. list_for_each(list, f, list) {
  56. unsigned int i;
  57. char **lines = get_ccan_file_lines(f);
  58. for (i = 0; lines[i]; i++) {
  59. char *p;
  60. if (lines[i][strspn(lines[i], " \t")] != '#')
  61. continue;
  62. p = strip_spaces(f, lines[i]);
  63. if (!strstarts(p, "#include<ccan/")
  64. && !strstarts(p, "#include\"ccan/"))
  65. continue;
  66. p += strlen("#include\"");
  67. if (!strchr(strchr(p, '/') + 1, '/'))
  68. continue;
  69. *strchr(strchr(p, '/') + 1, '/') = '\0';
  70. if (has_dep(m, p, tap_ok))
  71. continue;
  72. score->error = "Includes a ccan module"
  73. " not listed in _info";
  74. score_file_error(score, f, i+1, lines[i]);
  75. }
  76. }
  77. }
  78. if (!score->error) {
  79. score->pass = true;
  80. score->score = score->total;
  81. }
  82. }
  83. struct ccanlint depends_accurate = {
  84. .key = "depends-accurate",
  85. .name = "Module's CCAN dependencies are the only ccan files #included",
  86. .check = check_depends_accurate,
  87. };
  88. REGISTER_TEST(depends_accurate, &depends_exist, NULL);