]> git.ozlabs.org Git - ccan/blobdiff - tools/ccanlint/tests/depends_accurate.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / tools / ccanlint / tests / depends_accurate.c
index 78dad0b13a9c904cacf661a44658948e84ddfc83..26943faa628a6e98fa93ab5badb9e7c67d511c20 100644 (file)
@@ -1,7 +1,7 @@
 #include <tools/ccanlint/ccanlint.h>
 #include <tools/tools.h>
-#include <ccan/talloc/talloc.h>
 #include <ccan/str/str.h>
+#include <ccan/take/take.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, char **deps, bool *used,
+                   const char *depname)
 {
-       char *p = talloc_strdup(ctx, line);
-       unsigned int i, j;
+       unsigned int i;
 
-       for (i = 0, j = 0; p[i]; i++) {
-               if (!isspace(p[i]))
-                       p[j++] = p[i];
+       /* We can include ourselves, of course. */
+       if (strstarts(depname + strlen("ccan/"), m->modname)) {
+               const char *p = depname + strlen("ccan/") + strlen(m->modname);
+               /* And our own tests! */
+               if (streq(p, "/test") || streq(p, ""))
+                       return true;
+               /* But not any submodules; they need an explicit dep */
        }
-       p[j] = '\0';
-       return p;
+
+       for (i = 0; deps[i]; i++) {
+               if (streq(deps[i], depname)) {
+                       used[i] = true;
+                       return true;
+               }
+       }
+       return false;
 }
 
-static bool has_dep(struct manifest *m, const char *depname, bool tap_ok)
+static bool check_dep_includes(struct manifest *m,
+                              char **deps, bool *used,
+                              struct score *score,
+                              struct ccan_file *f)
 {
-       struct ccan_file *f;
+       unsigned int i;
+       char **lines = get_ccan_file_lines(f);
+       struct line_info *li = get_ccan_line_info(f);
+       bool ok = true;
 
-       if (tap_ok && streq(depname, "ccan/tap"))
-               return true;
+       for (i = 0; lines[i]; i++) {
+               char *mod;
+               if (!tal_strreg(f, lines[i],
+                               "^[ \t]*#[ \t]*include[ \t]*[<\"]"
+                               "(ccan/+.+)/+[^/]+\\.[ch][\">]", &mod))
+                       continue;
 
-       /* We can include ourselves, of course. */
-       if (streq(depname + strlen("ccan/"), m->basename))
-               return true;
+               if (has_dep(m, deps, used, 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 (handle common case of idempotent wrap) */
+               if (!li[i].cond || li[i].cond == f->idempotent_cond) {
+                       score_file_error(score, f, i+1,
+                                        "%s not listed in _info", mod);
+                       ok = false;
+               }
        }
-       return false;
+       return ok;
 }
 
-static void *check_depends_accurate(struct manifest *m,
-                                   bool keep,
-                                   unsigned int *timeleft)
+static void check_depends_accurate(struct manifest *m,
+                                  unsigned int *timeleft UNNEEDED,
+                                  struct score *score)
 {
        struct list_head *list;
-       char *report = talloc_strdup(m, "");
+       unsigned int i, core_deps, test_deps;
+       char **deps, **tdeps;
+       bool *used;
+       bool ok = true;
+
+       /* Get the *direct* dependencies. */
+       if (safe_mode) {
+               deps = get_safe_ccan_deps(m, m->dir, "depends", false);
+               tdeps = get_safe_ccan_deps(m, m->dir, "testdepends", false);
+       } else {
+               deps = get_deps(m, m->dir, "depends", false,
+                               get_or_compile_info);
+               tdeps = get_deps(m, m->dir, "testdepends", false,
+                                get_or_compile_info);
+       }
+
+       core_deps = tal_count(deps) - 1;
+       test_deps = tal_count(tdeps) - 1;
 
-       foreach_ptr(list, &m->c_files, &m->h_files,
-                   &m->run_tests, &m->api_tests,
+       used = tal_arrz(m, bool, core_deps + test_deps + 1);
+
+       foreach_ptr(list, &m->c_files, &m->h_files) {
+               struct ccan_file *f;
+
+               list_for_each(list, f, list)
+                       ok &= check_dep_includes(m, deps, used, score, f);
+       }
+
+       for (i = 0; i < core_deps; i++) {
+               if (!used[i] && strstarts(deps[i], "ccan/"))
+                       score_file_error(score, m->info_file, 0,
+                                        "%s is an unused dependency",
+                                        deps[i]);
+       }
+
+       /* Now remove NUL and append test dependencies to deps. */
+       deps = tal_dup_arr(m, char *, take(deps), core_deps, test_deps + 2);
+       memcpy(deps + core_deps, tdeps, sizeof(tdeps[0]) * test_deps);
+       /* ccan/tap is given a free pass. */
+       deps[core_deps + test_deps] = (char *)"ccan/tap";
+       deps[core_deps + test_deps + 1] = NULL;
+
+       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))
-                                       report = talloc_asprintf_append(report,
-                                              "%s:%u:%s\n",
-                                              f->name, i+1, lines[i]);
-                       }
-               }
+
+               list_for_each(list, f, list)
+                       ok &= check_dep_includes(m, deps, used, score, f);
        }
 
-       if (streq(report, "")) {
-               talloc_free(report);
-               report = NULL;
+       for (i = core_deps; i < test_deps; i++) {
+               if (!used[i])
+                       score_file_error(score, m->info_file, 0,
+                                        "%s is an unused test dependency",
+                                        deps[i]);
        }
-       return report;
-}
 
-static const char *describe_depends_accurage(struct manifest *m,
-                                            void *check_result)
-{
-       return talloc_asprintf(check_result, 
-                              "You include ccan modules you don't list as dependencies:\n"
-                              "%s", (char *)check_result);
+       if (!score->error)
+               score->score = score->total;
+
+       /* We don't count unused dependencies as an error (yet!) */
+       score->pass = ok;
 }
 
 struct ccanlint depends_accurate = {
-       .key = "depends-accurate",
-       .name = "Module's CCAN dependencies are the only ccan files #included",
-       .total_score = 1,
+       .key = "depends_accurate",
+       .name = "Module's CCAN dependencies are the only CCAN files #included",
        .check = check_depends_accurate,
-       .describe = describe_depends_accurage,
+       .needs = "depends_exist info_compiles test_depends_exist headers_idempotent"
 };
 
-REGISTER_TEST(depends_accurate, &depends_exist, NULL);
+REGISTER_TEST(depends_accurate);