examples_exist.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. write(fd, linemarker, strlen(linemarker));
  44. for (i = 0; i < example->num_lines; i++) {
  45. if (write(fd, example->lines[i], strlen(example->lines[i]))
  46. != strlen(example->lines[i])
  47. || write(fd, "\n", 1) != 1) {
  48. close(fd);
  49. return cast_const(char *,
  50. "Failure writing to temporary file");
  51. }
  52. }
  53. close(fd);
  54. return NULL;
  55. }
  56. /* FIXME: We should have one example per function in header. */
  57. static void extract_examples(struct manifest *m,
  58. unsigned int *timeleft UNNEEDED,
  59. struct score *score)
  60. {
  61. struct ccan_file *f, *mainh = NULL; /* gcc complains uninitialized */
  62. struct doc_section *d;
  63. bool have_info_example = false, have_header_example = false;
  64. score->total = 2;
  65. list_for_each(get_ccan_file_docs(m->info_file), d, list) {
  66. if (streq(d->type, "example")) {
  67. score->error = add_example(m, m->info_file, d);
  68. if (score->error)
  69. return;
  70. have_info_example = true;
  71. }
  72. }
  73. /* Check all headers for examples. */
  74. list_for_each(&m->h_files, f, list) {
  75. if (strstarts(f->name, m->basename)
  76. && strlen(f->name) == strlen(m->basename) + 2)
  77. mainh = f;
  78. list_for_each(get_ccan_file_docs(f), d, list) {
  79. if (streq(d->type, "example")) {
  80. score->error = add_example(m, f, d);
  81. if (score->error)
  82. return;
  83. have_header_example = true;
  84. }
  85. }
  86. }
  87. /* We don't fail ccanlint for this. */
  88. score->pass = true;
  89. if (have_info_example && have_header_example) {
  90. score->score = score->total;
  91. return;
  92. }
  93. if (!have_info_example)
  94. score_file_error(score, m->info_file, 0, "No Example: section");
  95. if (!have_header_example)
  96. score_file_error(score, mainh, 0, "No Example: section");
  97. score->score = have_info_example + have_header_example;
  98. }
  99. struct ccanlint examples_exist = {
  100. .key = "examples_exist",
  101. .name = "_info and main header file have Example: sections",
  102. .check = extract_examples,
  103. .needs = "info_exists main_header_exists"
  104. };
  105. REGISTER_TEST(examples_exist);