file_analysis.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "ccanlint.h"
  2. #include "get_file_lines.h"
  3. #include <talloc/talloc.h>
  4. #include <string/string.h>
  5. #include <noerr/noerr.h>
  6. #include <unistd.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <err.h>
  11. #include <errno.h>
  12. #include <dirent.h>
  13. char **get_ccan_file_lines(struct ccan_file *f)
  14. {
  15. if (!f->lines)
  16. f->lines = get_file_lines(f, f->name, &f->num_lines);
  17. return f->lines;
  18. }
  19. static void add_files(struct manifest *m, const char *dir)
  20. {
  21. DIR *d;
  22. struct dirent *ent;
  23. if (dir[0])
  24. d = opendir(dir);
  25. else
  26. d = opendir(".");
  27. if (!d)
  28. err(1, "Opening directory %s", dir[0] ? dir : ".");
  29. while ((ent = readdir(d)) != NULL) {
  30. struct stat st;
  31. struct ccan_file *f;
  32. struct list_head *dest;
  33. bool is_c_src;
  34. if (ent->d_name[0] == '.')
  35. continue;
  36. f = talloc(m, struct ccan_file);
  37. f->lines = NULL;
  38. f->name = talloc_asprintf(f, "%s%s", dir, ent->d_name);
  39. if (lstat(f->name, &st) != 0)
  40. err(1, "lstat %s", f->name);
  41. if (S_ISDIR(st.st_mode)) {
  42. f->name = talloc_append_string(f->name, "/");
  43. add_files(m, f->name);
  44. continue;
  45. }
  46. if (!S_ISREG(st.st_mode)) {
  47. talloc_free(f);
  48. continue;
  49. }
  50. if (streq(f->name, "_info.c")) {
  51. m->info_file = f;
  52. continue;
  53. }
  54. is_c_src = strends(f->name, ".c");
  55. if (!is_c_src && !strends(f->name, ".h"))
  56. dest = &m->other_files;
  57. else if (!strchr(f->name, '/')) {
  58. if (is_c_src)
  59. dest = &m->c_files;
  60. else
  61. dest = &m->h_files;
  62. } else if (strstarts(f->name, "test/")) {
  63. if (is_c_src) {
  64. if (strstarts(f->name, "test/run"))
  65. dest = &m->run_tests;
  66. else if (strstarts(f->name, "test/compile_ok"))
  67. dest = &m->compile_ok_tests;
  68. else if (strstarts(f->name, "test/compile_fail"))
  69. dest = &m->compile_fail_tests;
  70. else
  71. dest = &m->other_test_files;
  72. } else
  73. dest = &m->other_test_files;
  74. } else
  75. dest = &m->other_files;
  76. list_add(dest, &f->list);
  77. }
  78. closedir(d);
  79. }
  80. char *report_on_lines(struct list_head *files,
  81. char *(*report)(const char *),
  82. char *sofar)
  83. {
  84. struct ccan_file *f;
  85. list_for_each(files, f, list) {
  86. unsigned int i;
  87. char **lines = get_ccan_file_lines(f);
  88. for (i = 0; i < f->num_lines; i++) {
  89. char *r = report(lines[i]);
  90. if (!r)
  91. continue;
  92. sofar = talloc_asprintf_append(sofar,
  93. "%s:%u:%s\n",
  94. f->name, i+1, r);
  95. talloc_free(r);
  96. }
  97. }
  98. return sofar;
  99. }
  100. struct manifest *get_manifest(void)
  101. {
  102. struct manifest *m = talloc(NULL, struct manifest);
  103. unsigned int len;
  104. m->info_file = NULL;
  105. list_head_init(&m->c_files);
  106. list_head_init(&m->h_files);
  107. list_head_init(&m->run_tests);
  108. list_head_init(&m->compile_ok_tests);
  109. list_head_init(&m->compile_fail_tests);
  110. list_head_init(&m->other_test_files);
  111. list_head_init(&m->other_files);
  112. /* *This* is why people hate C. */
  113. len = 32;
  114. m->basename = talloc_array(m, char, len);
  115. while (!getcwd(m->basename, len)) {
  116. if (errno != ERANGE)
  117. err(1, "Getting current directory");
  118. m->basename = talloc_realloc(m, m->basename, char, len *= 2);
  119. }
  120. len = strlen(m->basename);
  121. while (len && m->basename[len-1] == '/')
  122. m->basename[--len] = '\0';
  123. m->basename = strrchr(m->basename, '/');
  124. if (!m->basename)
  125. errx(1, "I don't expect to be run from the root directory");
  126. m->basename++;
  127. add_files(m, "");
  128. return m;
  129. }