depends_accurate.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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, const char *depname)
  19. {
  20. struct manifest *i;
  21. /* We can include ourselves, of course. */
  22. if (streq(depname, m->basename))
  23. return true;
  24. list_for_each(&m->deps, i, list) {
  25. if (streq(i->basename, depname))
  26. return true;
  27. }
  28. return false;
  29. }
  30. static void check_depends_accurate(struct manifest *m,
  31. unsigned int *timeleft, struct score *score)
  32. {
  33. struct list_head *list;
  34. /* FIXME: This isn't reliable enough with #ifdefs, so we don't fail. */
  35. score->pass = true;
  36. foreach_ptr(list, &m->c_files, &m->h_files,
  37. &m->run_tests, &m->api_tests,
  38. &m->compile_ok_tests, &m->compile_fail_tests,
  39. &m->other_test_c_files) {
  40. struct ccan_file *f;
  41. list_for_each(list, f, list) {
  42. unsigned int i;
  43. char **lines = get_ccan_file_lines(f);
  44. for (i = 0; lines[i]; i++) {
  45. char *mod;
  46. if (!strreg(f, lines[i],
  47. "^[ \t]*#[ \t]*include[ \t]*[<\"]"
  48. "ccan/+([^/]+)/", &mod))
  49. continue;
  50. if (has_dep(m, mod))
  51. continue;
  52. score_file_error(score, f, i+1,
  53. "%s not listed in _info",
  54. mod);
  55. }
  56. }
  57. }
  58. if (!score->error) {
  59. score->score = score->total;
  60. }
  61. }
  62. struct ccanlint depends_accurate = {
  63. .key = "depends_accurate",
  64. .name = "Module's CCAN dependencies are the only CCAN files #included",
  65. .check = check_depends_accurate,
  66. .needs = "depends_exist"
  67. };
  68. REGISTER_TEST(depends_accurate);