ccanlint: check examples actually mention thing they are demonstrating.
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 17 Jan 2011 05:50:15 +0000 (16:20 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 17 Jan 2011 05:50:15 +0000 (16:20 +1030)
Brad suggested this after finding one such cut & paste in str:

rusty@vivaldi:~/devel/cvs/ccan/ccan/str$ ../../tools/ccanlint/ccanlint
Example: sections demonstrate appropriate function (examples_relevant): FAIL
/home/rusty/devel/cvs/ccan/ccan/str/str.h:64:Example for strcount doesn't mention it
Total score: 37/38

tools/ccanlint/tests/examples_relevant.c [new file with mode: 0644]

diff --git a/tools/ccanlint/tests/examples_relevant.c b/tools/ccanlint/tests/examples_relevant.c
new file mode 100644 (file)
index 0000000..283e78c
--- /dev/null
@@ -0,0 +1,60 @@
+#include <tools/ccanlint/ccanlint.h>
+#include <tools/tools.h>
+#include <ccan/talloc/talloc.h>
+#include <ccan/str/str.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <limits.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <err.h>
+#include <string.h>
+#include <ctype.h>
+
+static void examples_relevant_check(struct manifest *m,
+                                   bool keep,
+                                   unsigned int *timeleft,
+                                   struct score *score)
+{
+       struct ccan_file *f;
+       struct doc_section *d;
+
+       list_for_each(&m->h_files, f, list) {
+               list_for_each(get_ccan_file_docs(f), d, list) {
+                       unsigned int i;
+                       bool found = false;
+
+                       if (!streq(d->type, "example"))
+                               continue;
+
+                       for (i = 0; i < d->num_lines; i++) {
+                               if (strstr(d->lines[i], d->function))
+                                       found = true;
+                       }
+
+                       if (!found) {
+                               score_file_error(score, f, d->srcline+1,
+                                                "Example for %s doesn't"
+                                                " mention it", d->function);
+                       }
+               }
+       }
+
+       if (!score->error) {
+               score->score = score->total;
+               score->pass = true;
+               return;
+       }
+}
+
+struct ccanlint examples_relevant = {
+       .key = "examples_relevant",
+       .name = "Example: sections demonstrate appropriate function",
+       .check = examples_relevant_check,
+       .needs = "examples_exist"
+};
+
+REGISTER_TEST(examples_relevant);