examples_exist.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/str/str.h>
  4. #include <ccan/tal/path/path.h>
  5. #include <ccan/take/take.h>
  6. #include <ccan/cast/cast.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. /* Creates and adds an example file. */
  19. static char *add_example(struct manifest *m, struct ccan_file *source,
  20. struct doc_section *example)
  21. {
  22. char *name, *linemarker;
  23. unsigned int i;
  24. int fd;
  25. struct ccan_file *f;
  26. name = tal_fmt(m, "example-%s-%s",
  27. source->name, example->function);
  28. /* example->function == 'struct foo' */
  29. while (strchr(name, ' '))
  30. *strchr(name, ' ') = '_';
  31. name = temp_file(m, ".c", take(name));
  32. f = new_ccan_file(m, take(path_dirname(m, name)),
  33. take(path_basename(m, name)));
  34. tal_steal(f, name);
  35. list_add_tail(&m->examples, &f->list);
  36. fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
  37. if (fd < 0)
  38. return tal_fmt(m, "Creating temporary file %s: %s",
  39. f->fullname, strerror(errno));
  40. /* Add #line to demark where we are from, so errors are correct! */
  41. linemarker = tal_fmt(f, "#line %i \"%s\"\n",
  42. example->srcline+2, source->fullname);
  43. if (write(fd, linemarker, strlen(linemarker)) != (int)strlen(linemarker)) {
  44. close(fd);
  45. return cast_const(char *, "Failure writing to temporary file");
  46. }
  47. for (i = 0; i < example->num_lines; i++) {
  48. if (write(fd, example->lines[i], strlen(example->lines[i]))
  49. != strlen(example->lines[i])
  50. || write(fd, "\n", 1) != 1) {
  51. close(fd);
  52. return cast_const(char *,
  53. "Failure writing to temporary file");
  54. }
  55. }
  56. close(fd);
  57. return NULL;
  58. }
  59. /* FIXME: We should have one example per function in header. */
  60. static void extract_examples(struct manifest *m,
  61. unsigned int *timeleft UNNEEDED,
  62. struct score *score)
  63. {
  64. struct ccan_file *f, *mainh = NULL; /* gcc complains uninitialized */
  65. struct doc_section *d;
  66. bool have_info_example = false, have_header_example = false;
  67. score->total = 2;
  68. list_for_each(get_ccan_file_docs(m->info_file), d, list) {
  69. if (streq(d->type, "example")) {
  70. score->error = add_example(m, m->info_file, d);
  71. if (score->error)
  72. return;
  73. have_info_example = true;
  74. }
  75. }
  76. /* Check all headers for examples. */
  77. list_for_each(&m->h_files, f, list) {
  78. if (strstarts(f->name, m->basename)
  79. && strlen(f->name) == strlen(m->basename) + 2)
  80. mainh = f;
  81. list_for_each(get_ccan_file_docs(f), d, list) {
  82. if (streq(d->type, "example")) {
  83. score->error = add_example(m, f, d);
  84. if (score->error)
  85. return;
  86. have_header_example = true;
  87. }
  88. }
  89. }
  90. /* We don't fail ccanlint for this. */
  91. score->pass = true;
  92. if (have_info_example && have_header_example) {
  93. score->score = score->total;
  94. return;
  95. }
  96. if (!have_info_example)
  97. score_file_error(score, m->info_file, 0, "No Example: section");
  98. if (!have_header_example)
  99. score_file_error(score, mainh, 0, "No Example: section");
  100. score->score = have_info_example + have_header_example;
  101. }
  102. struct ccanlint examples_exist = {
  103. .key = "examples_exist",
  104. .name = "_info and main header file have Example: sections",
  105. .check = extract_examples,
  106. .needs = "info_exists main_header_exists"
  107. };
  108. REGISTER_TEST(examples_exist);