1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/doc_extract.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>
24 static void *check_has_info_documentation(struct manifest *m)
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 };
30 list_for_each(infodocs, d, list) {
31 if (!streq(d->function, m->basename))
33 if (streq(d->type, "summary"))
35 if (streq(d->type, "description"))
36 id.description = true;
37 if (streq(d->type, "example"))
41 if (id.summary && id.description && id.example)
43 return talloc_memdup(m, &id, sizeof(id));
46 /* This is defined below. */
47 extern struct ccanlint has_info_documentation;
49 static void create_info_template_doc(struct manifest *m, void *check_result)
51 int fd = open("_info.new", O_WRONLY|O_CREAT|O_EXCL, 0666);
55 if (fd < 0 || !(new = fdopen(fd, "w")))
56 err(1, "Creating _info.new to insert documentation");
60 " * %s - [[ONE LINE DESCRIPTION HERE]]\n"
62 " * Paragraphs why %s exists and where to use it.\n"
64 " * Followed by an Example: section with a standalone\n"
65 " * (trivial and usually useless) program\n"
66 " */\n", m->basename, m->basename) < 0) {
67 unlink_noerr("_info.new");
68 err(1, "Writing to _info.new to insert documentation");
71 oldcontents = grab_file(m, "_info", NULL);
73 unlink_noerr("_info.new");
74 err(1, "Reading _info");
76 if (fprintf(new, "%s", oldcontents) < 0) {
77 unlink_noerr("_info.new");
78 err(1, "Appending _info to _info.new");
80 if (fclose(new) != 0) {
81 unlink_noerr("_info.new");
82 err(1, "Closing _info.new");
84 if (rename("_info.new", "_info") != 0) {
85 unlink_noerr("_info.new");
86 err(1, "Renaming _info.new to _info");
90 static const char *describe_has_info_documentation(struct manifest *m,
93 struct info_docs *id = check_result;
94 char *reason = talloc_strdup(m, "");
97 has_info_documentation.handle = create_info_template_doc;
98 reason = talloc_asprintf_append(reason,
99 "Your _info file has no module documentation.\n\n"
100 "CCAN modules use /**-style comments for documentation: the\n"
101 "overall documentation belongs in the _info metafile.\n");
103 if (!id->description)
104 reason = talloc_asprintf_append(reason,
105 "Your _info file has no module description.\n\n"
106 "The lines after the first summary line in the _info file\n"
107 "documentation should describe the purpose and use of the\n"
108 "overall package\n");
110 reason = talloc_asprintf_append(reason,
111 "Your _info file has no module example.\n\n"
112 "There should be an Example: section of the _info documentation\n"
113 "which provides a concise toy program which uses your module\n");
117 static unsigned int has_info_documentation_score(struct manifest *m,
120 struct info_docs *id = check_result;
121 return id->summary + id->description + id->example;
124 struct ccanlint has_info_documentation = {
125 .name = "Documentation in _info file",
127 .score = has_info_documentation_score,
128 .check = check_has_info_documentation,
129 .describe = describe_has_info_documentation,
132 REGISTER_TEST(has_info_documentation, NULL);