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