1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/doc_extract.h>
3 #include <tools/tools.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>
25 static void *check_has_info_documentation(struct manifest *m,
26 unsigned int *timeleft)
28 struct list_head *infodocs = get_ccan_file_docs(m->info_file);
29 struct doc_section *d;
30 struct info_docs id = { false, false, false };
32 list_for_each(infodocs, d, list) {
33 if (!streq(d->function, m->basename))
35 if (streq(d->type, "summary"))
37 if (streq(d->type, "description"))
38 id.description = true;
39 if (streq(d->type, "example"))
43 if (id.summary && id.description && id.example)
45 return talloc_memdup(m, &id, sizeof(id));
48 /* This is defined below. */
49 extern struct ccanlint has_info_documentation;
51 static void create_info_template_doc(struct manifest *m, void *check_result)
53 int fd = open("_info.new", O_WRONLY|O_CREAT|O_EXCL, 0666);
57 if (fd < 0 || !(new = fdopen(fd, "w")))
58 err(1, "Creating _info.new to insert documentation");
62 " * %s - [[ONE LINE DESCRIPTION HERE]]\n"
64 " * Paragraphs why %s exists and where to use it.\n"
66 " * Followed by an Example: section with a standalone\n"
67 " * (trivial and usually useless) program\n"
68 " */\n", m->basename, m->basename) < 0) {
69 unlink_noerr("_info.new");
70 err(1, "Writing to _info.new to insert documentation");
73 oldcontents = grab_file(m, "_info", NULL);
75 unlink_noerr("_info.new");
76 err(1, "Reading _info");
78 if (fprintf(new, "%s", oldcontents) < 0) {
79 unlink_noerr("_info.new");
80 err(1, "Appending _info to _info.new");
82 if (fclose(new) != 0) {
83 unlink_noerr("_info.new");
84 err(1, "Closing _info.new");
86 if (!move_file("_info.new", "_info")) {
87 unlink_noerr("_info.new");
88 err(1, "Renaming _info.new to _info");
92 static const char *describe_has_info_documentation(struct manifest *m,
95 struct info_docs *id = check_result;
96 char *reason = talloc_strdup(m, "");
99 has_info_documentation.handle = create_info_template_doc;
100 reason = talloc_asprintf_append(reason,
101 "Your _info file has no module documentation.\n\n"
102 "CCAN modules use /**-style comments for documentation: the\n"
103 "overall documentation belongs in the _info metafile.\n");
105 if (!id->description)
106 reason = talloc_asprintf_append(reason,
107 "Your _info file has no module description.\n\n"
108 "The lines after the first summary line in the _info file\n"
109 "documentation should describe the purpose and use of the\n"
110 "overall package\n");
112 reason = talloc_asprintf_append(reason,
113 "Your _info file has no module example.\n\n"
114 "There should be an Example: section of the _info documentation\n"
115 "which provides a concise toy program which uses your module\n");
119 static unsigned int has_info_documentation_score(struct manifest *m,
122 struct info_docs *id = check_result;
123 return id->summary + id->description + id->example;
126 struct ccanlint has_info_documentation = {
127 .key = "info-documentation",
128 .name = "Module has documentation in _info",
130 .score = has_info_documentation_score,
131 .check = check_has_info_documentation,
132 .describe = describe_has_info_documentation,
135 REGISTER_TEST(has_info_documentation, NULL);