has_info_documentation.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/doc_extract.h>
  3. #include <tools/tools.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <limits.h>
  9. #include <errno.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <err.h>
  13. #include <ccan/str/str.h>
  14. #include <ccan/talloc/talloc.h>
  15. #include <ccan/noerr/noerr.h>
  16. #include <ccan/grab_file/grab_file.h>
  17. struct info_docs
  18. {
  19. bool summary;
  20. bool description;
  21. bool example;
  22. };
  23. static void *check_has_info_documentation(struct manifest *m,
  24. bool keep,
  25. unsigned int *timeleft)
  26. {
  27. struct list_head *infodocs = get_ccan_file_docs(m->info_file);
  28. struct doc_section *d;
  29. struct info_docs id = { false, false, false };
  30. list_for_each(infodocs, d, list) {
  31. if (!streq(d->function, m->basename))
  32. continue;
  33. if (streq(d->type, "summary"))
  34. id.summary = true;
  35. if (streq(d->type, "description"))
  36. id.description = true;
  37. if (streq(d->type, "example"))
  38. id.example = true;
  39. }
  40. if (id.summary && id.description && id.example)
  41. return NULL;
  42. return talloc_memdup(m, &id, sizeof(id));
  43. }
  44. /* This is defined below. */
  45. extern struct ccanlint has_info_documentation;
  46. static void create_info_template_doc(struct manifest *m, void *check_result)
  47. {
  48. int fd = open("_info.new", O_WRONLY|O_CREAT|O_EXCL, 0666);
  49. FILE *new;
  50. char *oldcontents;
  51. if (fd < 0 || !(new = fdopen(fd, "w")))
  52. err(1, "Creating _info.new to insert documentation");
  53. if (fprintf(new,
  54. "/**\n"
  55. " * %s - [[ONE LINE DESCRIPTION HERE]]\n"
  56. " *\n"
  57. " * Paragraphs why %s exists and where to use it.\n"
  58. " *\n"
  59. " * Followed by an Example: section with a standalone\n"
  60. " * (trivial and usually useless) program\n"
  61. " */\n", m->basename, m->basename) < 0) {
  62. unlink_noerr("_info.new");
  63. err(1, "Writing to _info.new to insert documentation");
  64. }
  65. oldcontents = grab_file(m, "_info", NULL);
  66. if (!oldcontents) {
  67. unlink_noerr("_info.new");
  68. err(1, "Reading _info");
  69. }
  70. if (fprintf(new, "%s", oldcontents) < 0) {
  71. unlink_noerr("_info.new");
  72. err(1, "Appending _info to _info.new");
  73. }
  74. if (fclose(new) != 0) {
  75. unlink_noerr("_info.new");
  76. err(1, "Closing _info.new");
  77. }
  78. if (!move_file("_info.new", "_info")) {
  79. unlink_noerr("_info.new");
  80. err(1, "Renaming _info.new to _info");
  81. }
  82. }
  83. static const char *describe_has_info_documentation(struct manifest *m,
  84. void *check_result)
  85. {
  86. struct info_docs *id = check_result;
  87. char *reason = talloc_strdup(m, "");
  88. if (!id->summary) {
  89. has_info_documentation.handle = create_info_template_doc;
  90. reason = talloc_asprintf_append(reason,
  91. "Your _info file has no module documentation.\n\n"
  92. "CCAN modules use /**-style comments for documentation: the\n"
  93. "overall documentation belongs in the _info metafile.\n");
  94. }
  95. if (!id->description)
  96. reason = talloc_asprintf_append(reason,
  97. "Your _info file has no module description.\n\n"
  98. "The lines after the first summary line in the _info file\n"
  99. "documentation should describe the purpose and use of the\n"
  100. "overall package\n");
  101. if (!id->example)
  102. reason = talloc_asprintf_append(reason,
  103. "Your _info file has no module example.\n\n"
  104. "There should be an Example: section of the _info documentation\n"
  105. "which provides a concise toy program which uses your module\n");
  106. return reason;
  107. }
  108. static unsigned int has_info_documentation_score(struct manifest *m,
  109. void *check_result)
  110. {
  111. struct info_docs *id = check_result;
  112. return id->summary + id->description + id->example;
  113. }
  114. struct ccanlint has_info_documentation = {
  115. .key = "info-documentation",
  116. .name = "Module has documentation in _info",
  117. .total_score = 3,
  118. .score = has_info_documentation_score,
  119. .check = check_has_info_documentation,
  120. .describe = describe_has_info_documentation,
  121. };
  122. REGISTER_TEST(has_info_documentation, NULL);