file_analysis.c 3.4 KB

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