examples_exist.c 3.1 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, path_dirname(m, name), path_basename(m, name));
  33. tal_steal(f, name);
  34. list_add_tail(&m->examples, &f->list);
  35. fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
  36. if (fd < 0)
  37. return tal_fmt(m, "Creating temporary file %s: %s",
  38. f->fullname, strerror(errno));
  39. /* Add #line to demark where we are from, so errors are correct! */
  40. linemarker = tal_fmt(f, "#line %i \"%s\"\n",
  41. example->srcline+2, source->fullname);
  42. write(fd, linemarker, strlen(linemarker));
  43. for (i = 0; i < example->num_lines; i++) {
  44. if (write(fd, example->lines[i], strlen(example->lines[i]))
  45. != strlen(example->lines[i])
  46. || write(fd, "\n", 1) != 1) {
  47. close(fd);
  48. return cast_const(char *,
  49. "Failure writing to temporary file");
  50. }
  51. }
  52. close(fd);
  53. return NULL;
  54. }
  55. /* FIXME: We should have one example per function in header. */
  56. static void extract_examples(struct manifest *m,
  57. unsigned int *timeleft,
  58. struct score *score)
  59. {
  60. struct ccan_file *f, *mainh = NULL; /* gcc complains uninitialized */
  61. struct doc_section *d;
  62. bool have_info_example = false, have_header_example = false;
  63. score->total = 2;
  64. list_for_each(get_ccan_file_docs(m->info_file), d, list) {
  65. if (streq(d->type, "example")) {
  66. score->error = add_example(m, m->info_file, d);
  67. if (score->error)
  68. return;
  69. have_info_example = true;
  70. }
  71. }
  72. /* Check main header. */
  73. list_for_each(&m->h_files, f, list) {
  74. if (!strstarts(f->name, m->basename)
  75. || strlen(f->name) != strlen(m->basename) + 2)
  76. continue;
  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"
  104. };
  105. REGISTER_TEST(examples_exist);