has_examples.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. #include <limits.h>
  10. #include <errno.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <err.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. /* Creates and adds an example file. */
  17. static char *add_example(struct manifest *m, struct ccan_file *source,
  18. bool keep,
  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 = maybe_temp_file(m, ".c", keep, 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 "Failure writing to temporary file";
  47. }
  48. }
  49. close(fd);
  50. return NULL;
  51. }
  52. /* FIXME: We should have one example per function in header. */
  53. struct score {
  54. bool info_example, header_example;
  55. char *error;
  56. };
  57. static void *extract_examples(struct manifest *m,
  58. bool keep,
  59. unsigned int *timeleft)
  60. {
  61. struct ccan_file *f;
  62. struct doc_section *d;
  63. struct score *score = talloc(m, struct score);
  64. score->info_example = score->header_example = false;
  65. score->error = NULL;
  66. list_for_each(get_ccan_file_docs(m->info_file), d, list) {
  67. if (streq(d->type, "example")) {
  68. score->error = add_example(m, m->info_file, keep, d);
  69. if (score->error)
  70. return score;
  71. score->info_example = true;
  72. }
  73. }
  74. /* Check main header. */
  75. list_for_each(&m->h_files, f, list) {
  76. if (!strstarts(f->name, m->basename)
  77. || strlen(f->name) != strlen(m->basename) + 2)
  78. continue;
  79. list_for_each(get_ccan_file_docs(f), d, list) {
  80. if (streq(d->type, "example")) {
  81. score->error = add_example(m, f, keep, d);
  82. if (score->error)
  83. return score;
  84. score->header_example = true;
  85. }
  86. }
  87. }
  88. return score;
  89. }
  90. static unsigned int score_examples(struct manifest *m, void *check_result)
  91. {
  92. struct score *score = check_result;
  93. int total = 0;
  94. if (score->error)
  95. return 0;
  96. total += score->info_example;
  97. total += score->header_example;
  98. return total;
  99. }
  100. static const char *describe_examples(struct manifest *m,
  101. void *check_result)
  102. {
  103. struct score *score = check_result;
  104. char *descrip = NULL;
  105. if (score->error)
  106. return score->error;
  107. if (!score->info_example)
  108. descrip = talloc_asprintf(score,
  109. "Your _info file has no module example.\n\n"
  110. "There should be an Example: section of the _info documentation\n"
  111. "which provides a concise toy program which uses your module\n");
  112. if (!score->header_example)
  113. descrip = talloc_asprintf(score,
  114. "%sMain header file file has no examples\n\n"
  115. "There should be an Example: section for each public function\n"
  116. "demonstrating its use\n", descrip ? descrip : "");
  117. return descrip;
  118. }
  119. struct ccanlint has_examples = {
  120. .key = "has-examples",
  121. .name = "_info and header files have examples",
  122. .score = score_examples,
  123. .check = extract_examples,
  124. .describe = describe_examples,
  125. .total_score = 2,
  126. };
  127. REGISTER_TEST(has_examples, &has_info, NULL);