]> git.ozlabs.org Git - ccan/blobdiff - tools/ccanlint/tests/depends_accurate.c
ccanlint: add testdepends support.
[ccan] / tools / ccanlint / tests / depends_accurate.c
index 3ba19f60a3240e0385dd720b509887bd4e4b493e..0ed1e654cd9324199e045a867b41b4d219593a0f 100644 (file)
@@ -2,6 +2,7 @@
 #include <tools/tools.h>
 #include <ccan/talloc/talloc.h>
 #include <ccan/str/str.h>
+#include <ccan/str_talloc/str_talloc.h>
 #include <ccan/foreach/foreach.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <string.h>
 #include <ctype.h>
 
-static char *strip_spaces(const void *ctx, char *line)
+static bool has_dep(struct manifest *m, bool test_depend, const char *depname)
 {
-       char *p = talloc_strdup(ctx, line);
-       unsigned int i, j;
+       struct manifest *i;
 
-       for (i = 0, j = 0; p[i]; i++) {
-               if (!isspace(p[i]))
-                       p[j++] = p[i];
+       /* We can include ourselves, of course. */
+       if (streq(depname, m->basename))
+               return true;
+
+       list_for_each(&m->deps, i, list) {
+               if (streq(i->basename, depname))
+                       return true;
+       }
+
+       if (test_depend) {
+               list_for_each(&m->test_deps, i, list) {
+                       if (streq(i->basename, depname))
+                               return true;
+               }
        }
-       p[j] = '\0';
-       return p;
+
+       return false;
 }
 
-static bool has_dep(struct manifest *m, const char *depname, bool tap_ok)
+static void check_dep_includes(struct manifest *m, struct score *score,
+                              struct ccan_file *f, bool test_depend)
 {
-       struct ccan_file *f;
+       unsigned int i;
+       char **lines = get_ccan_file_lines(f);
+       struct line_info *li = get_ccan_line_info(f);
 
-       if (tap_ok && streq(depname, "ccan/tap"))
-               return true;
+       for (i = 0; lines[i]; i++) {
+               char *mod;
+               if (!strreg(f, lines[i],
+                           "^[ \t]*#[ \t]*include[ \t]*[<\"]"
+                           "ccan/+([^/]+)/", &mod))
+                       continue;
 
-       /* We can include ourselves, of course. */
-       if (streq(depname + strlen("ccan/"), m->basename))
-               return true;
+               if (has_dep(m, test_depend, mod))
+                       continue;
 
-       list_for_each(&m->dep_dirs, f, list) {
-               if (streq(f->name, depname))
-                       return true;
+               /* FIXME: we can't be sure about
+                * conditional includes, so don't
+                * complain. */
+               if (!li[i].cond) {
+                       score_file_error(score, f, i+1,
+                                        "%s not listed in _info", mod);
+               }
        }
-       return false;
 }
 
 static void check_depends_accurate(struct manifest *m,
-                                  bool keep,
                                   unsigned int *timeleft, struct score *score)
 {
        struct list_head *list;
 
-       foreach_ptr(list, &m->c_files, &m->h_files,
-                   &m->run_tests, &m->api_tests,
+       foreach_ptr(list, &m->c_files, &m->h_files) {
+               struct ccan_file *f;
+
+               list_for_each(list, f, list)
+                       check_dep_includes(m, score, f, false);
+       }
+
+       foreach_ptr(list, &m->run_tests, &m->api_tests,
                    &m->compile_ok_tests, &m->compile_fail_tests,
                    &m->other_test_c_files) {
                struct ccan_file *f;
-               bool tap_ok;
-
-               /* Including ccan/tap is fine for tests. */
-               tap_ok = (list != &m->c_files && list != &m->h_files);
-
-               list_for_each(list, f, list) {
-                       unsigned int i;
-                       char **lines = get_ccan_file_lines(f);
-
-                       for (i = 0; lines[i]; i++) {
-                               char *p;
-                               if (lines[i][strspn(lines[i], " \t")] != '#')
-                                       continue;
-                               p = strip_spaces(f, lines[i]);
-                               if (!strstarts(p, "#include<ccan/")
-                                   && !strstarts(p, "#include\"ccan/"))
-                                       continue;
-                               p += strlen("#include\"");
-                               if (!strchr(strchr(p, '/') + 1, '/'))
-                                       continue;
-                               *strchr(strchr(p, '/') + 1, '/') = '\0';
-                               if (has_dep(m, p, tap_ok))
-                                       continue;
-                               score->error = "Includes a ccan module"
-                                       " not listed in _info";
-                               score_file_error(score, f, i+1, lines[i]);
-                       }
-               }
+
+               list_for_each(list, f, list)
+                       check_dep_includes(m, score, f, true);
        }
 
        if (!score->error) {
-               score->pass = true;
                score->score = score->total;
+               score->pass = true;
        }
 }
 
 struct ccanlint depends_accurate = {
-       .key = "depends-accurate",
-       .name = "Module's CCAN dependencies are the only ccan files #included",
+       .key = "depends_accurate",
+       .name = "Module's CCAN dependencies are the only CCAN files #included",
        .check = check_depends_accurate,
+       .needs = "depends_exist test_depends_exist"
 };
 
-REGISTER_TEST(depends_accurate, &depends_exist, NULL);
+REGISTER_TEST(depends_accurate);