examples_exist.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/talloc/talloc.h>
  4. #include <ccan/str/str.h>
  5. #include <ccan/cast/cast.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include <limits.h>
  11. #include <errno.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <err.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. /* Creates and adds an example file. */
  18. static char *add_example(struct manifest *m, struct ccan_file *source,
  19. struct doc_section *example)
  20. {
  21. char *name;
  22. unsigned int i;
  23. int fd;
  24. struct ccan_file *f;
  25. name = talloc_asprintf(m, "%s/example-%s-%s.c",
  26. talloc_dirname(m,
  27. source->fullname),
  28. source->name,
  29. example->function);
  30. /* example->function == 'struct foo' */
  31. while (strchr(name, ' '))
  32. *strchr(name, ' ') = '_';
  33. name = temp_file(m, ".c", name);
  34. f = new_ccan_file(m, talloc_dirname(m, name), talloc_basename(m, name));
  35. talloc_steal(f, name);
  36. list_add_tail(&m->examples, &f->list);
  37. fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
  38. if (fd < 0)
  39. return talloc_asprintf(m, "Creating temporary file %s: %s",
  40. f->fullname, strerror(errno));
  41. for (i = 0; i < example->num_lines; i++) {
  42. if (write(fd, example->lines[i], strlen(example->lines[i]))
  43. != strlen(example->lines[i])
  44. || write(fd, "\n", 1) != 1) {
  45. close(fd);
  46. return cast_const(char *,
  47. "Failure writing to temporary file");
  48. }
  49. }
  50. close(fd);
  51. return NULL;
  52. }
  53. /* FIXME: We should have one example per function in header. */
  54. static void extract_examples(struct manifest *m,
  55. unsigned int *timeleft,
  56. struct score *score)
  57. {
  58. struct ccan_file *f, *mainh = NULL; /* gcc complains uninitialized */
  59. struct doc_section *d;
  60. bool have_info_example = false, have_header_example = false;
  61. score->total = 2;
  62. list_for_each(get_ccan_file_docs(m->info_file), d, list) {
  63. if (streq(d->type, "example")) {
  64. score->error = add_example(m, m->info_file, d);
  65. if (score->error)
  66. return;
  67. have_info_example = true;
  68. }
  69. }
  70. /* Check main header. */
  71. list_for_each(&m->h_files, f, list) {
  72. if (!strstarts(f->name, m->basename)
  73. || strlen(f->name) != strlen(m->basename) + 2)
  74. continue;
  75. mainh = f;
  76. list_for_each(get_ccan_file_docs(f), d, list) {
  77. if (streq(d->type, "example")) {
  78. score->error = add_example(m, f, d);
  79. if (score->error)
  80. return;
  81. have_header_example = true;
  82. }
  83. }
  84. }
  85. /* We don't fail ccanlint for this. */
  86. score->pass = true;
  87. if (have_info_example && have_header_example) {
  88. score->score = score->total;
  89. return;
  90. }
  91. if (!have_info_example)
  92. score_file_error(score, m->info_file, 0, "No Example: section");
  93. if (!have_header_example)
  94. score_file_error(score, mainh, 0, "No Example: section");
  95. score->score = have_info_example + have_header_example;
  96. }
  97. struct ccanlint examples_exist = {
  98. .key = "examples_exist",
  99. .name = "_info and main header file have Example: sections",
  100. .check = extract_examples,
  101. .needs = "info_exists"
  102. };
  103. REGISTER_TEST(examples_exist);