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 struct list_head *infodocs = get_ccan_file_docs(m->info_file);
28 struct doc_section *d;
29 struct info_docs id = { false, false, false };
31 list_for_each(infodocs, d, list) {
32 if (!streq(d->function, m->basename))
34 if (streq(d->type, "summary"))
36 if (streq(d->type, "description"))
37 id.description = true;
38 if (streq(d->type, "example"))
42 if (id.summary && id.description && id.example)
44 return talloc_memdup(m, &id, sizeof(id));
47 /* This is defined below. */
48 extern struct ccanlint has_info_documentation;
50 static void create_info_template_doc(struct manifest *m, void *check_result)
52 int fd = open("_info.new", O_WRONLY|O_CREAT|O_EXCL, 0666);
56 if (fd < 0 || !(new = fdopen(fd, "w")))
57 err(1, "Creating _info.new to insert documentation");
61 " * %s - [[ONE LINE DESCRIPTION HERE]]\n"
63 " * Paragraphs why %s exists and where to use it.\n"
65 " * Followed by an Example: section with a standalone\n"
66 " * (trivial and usually useless) program\n"
67 " */\n", m->basename, m->basename) < 0) {
68 unlink_noerr("_info.new");
69 err(1, "Writing to _info.new to insert documentation");
72 oldcontents = grab_file(m, "_info", NULL);
74 unlink_noerr("_info.new");
75 err(1, "Reading _info");
77 if (fprintf(new, "%s", oldcontents) < 0) {
78 unlink_noerr("_info.new");
79 err(1, "Appending _info to _info.new");
81 if (fclose(new) != 0) {
82 unlink_noerr("_info.new");
83 err(1, "Closing _info.new");
85 if (!move_file("_info.new", "_info")) {
86 unlink_noerr("_info.new");
87 err(1, "Renaming _info.new to _info");
91 static const char *describe_has_info_documentation(struct manifest *m,
94 struct info_docs *id = check_result;
95 char *reason = talloc_strdup(m, "");
98 has_info_documentation.handle = create_info_template_doc;
99 reason = talloc_asprintf_append(reason,
100 "Your _info file has no module documentation.\n\n"
101 "CCAN modules use /**-style comments for documentation: the\n"
102 "overall documentation belongs in the _info metafile.\n");
104 if (!id->description)
105 reason = talloc_asprintf_append(reason,
106 "Your _info file has no module description.\n\n"
107 "The lines after the first summary line in the _info file\n"
108 "documentation should describe the purpose and use of the\n"
109 "overall package\n");
111 reason = talloc_asprintf_append(reason,
112 "Your _info file has no module example.\n\n"
113 "There should be an Example: section of the _info documentation\n"
114 "which provides a concise toy program which uses your module\n");
118 static unsigned int has_info_documentation_score(struct manifest *m,
121 struct info_docs *id = check_result;
122 return id->summary + id->description + id->example;
125 struct ccanlint has_info_documentation = {
126 .name = "Documentation in _info file",
128 .score = has_info_documentation_score,
129 .check = check_has_info_documentation,
130 .describe = describe_has_info_documentation,
133 REGISTER_TEST(has_info_documentation, NULL);