file_analysis.c 3.6 KB

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