depends_accurate.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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/str_talloc/str_talloc.h>
  6. #include <ccan/foreach/foreach.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. #include <limits.h>
  12. #include <errno.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <err.h>
  16. #include <string.h>
  17. #include <ctype.h>
  18. static bool has_dep(struct manifest *m, char **deps, bool *used,
  19. const char *depname)
  20. {
  21. unsigned int i;
  22. /* We can include ourselves, of course. */
  23. if (streq(depname + strlen("ccan/"), m->modname))
  24. return true;
  25. for (i = 0; deps[i]; i++) {
  26. if (streq(deps[i], depname)) {
  27. used[i] = true;
  28. return true;
  29. }
  30. }
  31. return false;
  32. }
  33. static bool check_dep_includes(struct manifest *m,
  34. char **deps, bool *used,
  35. struct score *score,
  36. struct ccan_file *f)
  37. {
  38. unsigned int i;
  39. char **lines = get_ccan_file_lines(f);
  40. struct line_info *li = get_ccan_line_info(f);
  41. bool ok = true;
  42. for (i = 0; lines[i]; i++) {
  43. char *mod;
  44. if (!strreg(f, lines[i],
  45. "^[ \t]*#[ \t]*include[ \t]*[<\"]"
  46. "(ccan/+.+)/+[^/]+.h", &mod))
  47. continue;
  48. if (has_dep(m, deps, used, mod))
  49. continue;
  50. /* FIXME: we can't be sure about
  51. * conditional includes, so don't
  52. * complain. */
  53. if (!li[i].cond) {
  54. score_file_error(score, f, i+1,
  55. "%s not listed in _info", mod);
  56. ok = false;
  57. }
  58. }
  59. return ok;
  60. }
  61. static void check_depends_accurate(struct manifest *m,
  62. unsigned int *timeleft, struct score *score)
  63. {
  64. struct list_head *list;
  65. unsigned int i, core_deps, test_deps;
  66. char **deps, **tdeps;
  67. bool *used;
  68. bool ok = true;
  69. /* Get the *direct* dependencies. */
  70. if (safe_mode) {
  71. deps = get_safe_ccan_deps(m, m->dir, "depends", false);
  72. tdeps = get_safe_ccan_deps(m, m->dir, "testdepends", false);
  73. } else {
  74. deps = get_deps(m, m->dir, "depends", false,
  75. get_or_compile_info);
  76. tdeps = get_deps(m, m->dir, "testdepends", false,
  77. get_or_compile_info);
  78. }
  79. core_deps = talloc_array_length(deps) - 1;
  80. test_deps = talloc_array_length(tdeps) - 1;
  81. used = talloc_zero_array(m, bool, core_deps + test_deps + 1);
  82. foreach_ptr(list, &m->c_files, &m->h_files) {
  83. struct ccan_file *f;
  84. list_for_each(list, f, list)
  85. ok &= check_dep_includes(m, deps, used, score, f);
  86. }
  87. for (i = 0; i < core_deps; i++) {
  88. if (!used[i])
  89. score_file_error(score, m->info_file, 0,
  90. "%s is an unused dependency",
  91. deps[i]);
  92. }
  93. /* Now append test dependencies to deps. */
  94. deps = talloc_realloc(NULL, deps, char *,
  95. (core_deps + test_deps + 1) * sizeof(char *));
  96. memcpy(&deps[core_deps], tdeps, test_deps * sizeof(char *));
  97. /* ccan/tap is given a free pass. */
  98. deps[core_deps + test_deps] = (char *)"ccan/tap";
  99. deps[core_deps + test_deps + 1] = NULL;
  100. foreach_ptr(list, &m->run_tests, &m->api_tests,
  101. &m->compile_ok_tests, &m->compile_fail_tests,
  102. &m->other_test_c_files) {
  103. struct ccan_file *f;
  104. list_for_each(list, f, list)
  105. ok &= check_dep_includes(m, deps, used, score, f);
  106. }
  107. for (i = core_deps; i < test_deps; i++) {
  108. if (!used[i])
  109. score_file_error(score, m->info_file, 0,
  110. "%s is an unused test dependency",
  111. deps[i]);
  112. }
  113. if (!score->error)
  114. score->score = score->total;
  115. /* We don't count unused dependencies as an error (yet!) */
  116. score->pass = ok;
  117. }
  118. struct ccanlint depends_accurate = {
  119. .key = "depends_accurate",
  120. .name = "Module's CCAN dependencies are the only CCAN files #included",
  121. .check = check_depends_accurate,
  122. .needs = "depends_exist test_depends_exist"
  123. };
  124. REGISTER_TEST(depends_accurate);