depends_accurate.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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)
  45. {
  46. struct list_head *list;
  47. char *report = talloc_strdup(m, "");
  48. foreach_ptr(list, &m->c_files, &m->h_files,
  49. &m->run_tests, &m->api_tests,
  50. &m->compile_ok_tests, &m->compile_fail_tests,
  51. &m->other_test_c_files) {
  52. struct ccan_file *f;
  53. bool tap_ok;
  54. /* Including ccan/tap is fine for tests. */
  55. tap_ok = (list != &m->c_files && list != &m->h_files);
  56. list_for_each(list, f, list) {
  57. unsigned int i;
  58. char **lines = get_ccan_file_lines(f);
  59. for (i = 0; lines[i]; i++) {
  60. char *p;
  61. if (lines[i][strspn(lines[i], " \t")] != '#')
  62. continue;
  63. p = strip_spaces(f, lines[i]);
  64. if (!strstarts(p, "#include<ccan/")
  65. && !strstarts(p, "#include\"ccan/"))
  66. continue;
  67. p += strlen("#include\"");
  68. if (!strchr(strchr(p, '/') + 1, '/'))
  69. continue;
  70. *strchr(strchr(p, '/') + 1, '/') = '\0';
  71. if (!has_dep(m, p, tap_ok))
  72. report = talloc_asprintf_append(report,
  73. "%s:%u:%s\n",
  74. f->name, i+1, lines[i]);
  75. }
  76. }
  77. }
  78. if (streq(report, "")) {
  79. talloc_free(report);
  80. report = NULL;
  81. }
  82. return report;
  83. }
  84. static const char *describe_depends_accurage(struct manifest *m,
  85. void *check_result)
  86. {
  87. return talloc_asprintf(check_result,
  88. "You include ccan modules you don't list as dependencies:\n"
  89. "%s", (char *)check_result);
  90. }
  91. struct ccanlint depends_accurate = {
  92. .key = "depends-accurate",
  93. .name = "Module's CCAN dependencies are the only ccan files #included",
  94. .total_score = 1,
  95. .check = check_depends_accurate,
  96. .describe = describe_depends_accurage,
  97. };
  98. REGISTER_TEST(depends_accurate, &depends_exist, NULL);