has_examples.c 3.5 KB

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