has_info_documentation.c 3.7 KB

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