has_examples.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 = maybe_temp_file(m, ".c", keep,
  26. talloc_asprintf(m, "%s/example-%s-%s.c",
  27. talloc_dirname(m,
  28. source->fullname),
  29. source->name,
  30. example->function));
  31. f = new_ccan_file(m, talloc_dirname(m, name), talloc_basename(m, name));
  32. talloc_steal(f, name);
  33. list_add_tail(&m->examples, &f->list);
  34. fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
  35. if (fd < 0)
  36. return talloc_asprintf(m, "Creating temporary file %s: %s",
  37. f->fullname, strerror(errno));
  38. for (i = 0; i < example->num_lines; i++) {
  39. if (write(fd, example->lines[i], strlen(example->lines[i]))
  40. != strlen(example->lines[i])
  41. || write(fd, "\n", 1) != 1) {
  42. close(fd);
  43. return "Failure writing to temporary file";
  44. }
  45. }
  46. close(fd);
  47. return NULL;
  48. }
  49. /* FIXME: We should have one example per function in header. */
  50. struct score {
  51. bool info_example, header_example;
  52. char *error;
  53. };
  54. static void *extract_examples(struct manifest *m,
  55. bool keep,
  56. unsigned int *timeleft)
  57. {
  58. struct ccan_file *f;
  59. struct doc_section *d;
  60. struct score *score = talloc(m, struct score);
  61. score->info_example = score->header_example = false;
  62. score->error = NULL;
  63. list_for_each(get_ccan_file_docs(m->info_file), d, list) {
  64. if (streq(d->type, "example")) {
  65. score->error = add_example(m, m->info_file, keep, d);
  66. if (score->error)
  67. return score;
  68. score->info_example = true;
  69. }
  70. }
  71. /* Check main header. */
  72. list_for_each(&m->h_files, f, list) {
  73. if (!strstarts(f->name, m->basename)
  74. || strlen(f->name) != strlen(m->basename) + 2)
  75. continue;
  76. list_for_each(get_ccan_file_docs(f), d, list) {
  77. if (streq(d->type, "example")) {
  78. score->error = add_example(m, f, keep, d);
  79. if (score->error)
  80. return score;
  81. score->header_example = true;
  82. }
  83. }
  84. }
  85. return score;
  86. }
  87. static unsigned int score_examples(struct manifest *m, void *check_result)
  88. {
  89. struct score *score = check_result;
  90. int total = 0;
  91. if (score->error)
  92. return 0;
  93. total += score->info_example;
  94. total += score->header_example;
  95. return total;
  96. }
  97. static const char *describe_examples(struct manifest *m,
  98. void *check_result)
  99. {
  100. struct score *score = check_result;
  101. char *descrip = NULL;
  102. if (score->error)
  103. return score->error;
  104. if (!score->info_example)
  105. descrip = talloc_asprintf(score,
  106. "Your _info file has no module example.\n\n"
  107. "There should be an Example: section of the _info documentation\n"
  108. "which provides a concise toy program which uses your module\n");
  109. if (!score->header_example)
  110. descrip = talloc_asprintf(score,
  111. "%sMain header file file has no examples\n\n"
  112. "There should be an Example: section for each public function\n"
  113. "demonstrating its use\n", descrip ? descrip : "");
  114. return descrip;
  115. }
  116. struct ccanlint has_examples = {
  117. .key = "has-examples",
  118. .name = "_info and header files have examples",
  119. .score = score_examples,
  120. .check = extract_examples,
  121. .describe = describe_examples,
  122. .total_score = 2,
  123. };
  124. REGISTER_TEST(has_examples, &has_info, NULL);