has_info_documentation.c 3.6 KB

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