]> git.ozlabs.org Git - ccan/blobdiff - tools/ccanlint/tests/headers_idempotent.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / tools / ccanlint / tests / headers_idempotent.c
index 59d240e339f1a60477dd12814a47eb833e5db08d..79e5c367d3a35d0dc4c433605a83512e17dee9c8 100644 (file)
@@ -1,6 +1,5 @@
 #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 <string.h>
 #include <ctype.h>
 
-static const char explain[] 
-= "Headers usually start with the C preprocessor lines to prevent multiple\n"
-  "inclusions.  These look like the following:\n"
-  "#ifndef CCAN_<MODNAME>_H\n"
-  "#define CCAN_<MODNAME>_H\n"
-  "...\n"
-  "#endif /* CCAN_<MODNAME>_H */\n";
-
 static void fix_name(char *name)
 {
        unsigned int i;
@@ -46,26 +37,26 @@ static void handle_idem(struct manifest *m, struct score *score)
                /* Main header gets CCAN_FOO_H, others CCAN_FOO_XXX_H */
                if (strstarts(e->file->name, m->basename)
                    || strlen(e->file->name) == strlen(m->basename) + 2)
-                       name = talloc_asprintf(score, "CCAN_%s_H", m->basename);
+                       name = tal_fmt(score, "CCAN_%s_H", m->modname);
                else
-                       name = talloc_asprintf(score, "CCAN_%s_%s",
-                                              m->basename, e->file->name);
+                       name = tal_fmt(score, "CCAN_%s_%s",
+                                      m->modname, e->file->name);
                fix_name(name);
 
-               q = talloc_asprintf(score,
+               q = tal_fmt(score,
                            "Should I wrap %s in #ifndef/#define %s for you?",
                            e->file->name, name);
                if (!ask(q))
                        continue;
 
-               tmpname = maybe_temp_file(score, ".h", false, e->file->name);
+               tmpname = temp_file(score, ".h", e->file->name);
                out = fopen(tmpname, "w");
                if (!out)
                        err(1, "Opening %s", tmpname);
                if (fprintf(out, "#ifndef %s\n#define %s\n", name, name) < 0)
                        err(1, "Writing %s", tmpname);
 
-               for (i = 0; i < e->file->num_lines; i++)
+               for (i = 0; e->file->lines[i]; i++)
                        if (fprintf(out, "%s\n", e->file->lines[i]) < 0)
                                err(1, "Writing %s", tmpname);
 
@@ -87,11 +78,11 @@ static void check_idem(struct ccan_file *f, struct score *score)
        const char *line, *sym;
 
        line_info = get_ccan_line_info(f);
-       if (f->num_lines < 3)
+       if (tal_count(f->lines) < 4)
                /* FIXME: We assume small headers probably uninteresting. */
                return;
 
-       for (i = 0; i < f->num_lines; i++) {
+       for (i = 0; f->lines[i]; i++) {
                if (line_info[i].type == DOC_LINE
                    || line_info[i].type == COMMENT_LINE)
                        continue;
@@ -105,11 +96,11 @@ static void check_idem(struct ccan_file *f, struct score *score)
        }
 
        /* No code at all?  Don't complain. */
-       if (i == f->num_lines)
+       if (!f->lines[i])
                return;
 
        first_preproc_line = i;
-       for (i = first_preproc_line+1; i < f->num_lines; i++) {
+       for (i = first_preproc_line+1; f->lines[i]; i++) {
                if (line_info[i].type == DOC_LINE
                    || line_info[i].type == COMMENT_LINE)
                        continue;
@@ -123,12 +114,13 @@ static void check_idem(struct ccan_file *f, struct score *score)
        }
 
        /* No code at all?  Weird. */
-       if (i == f->num_lines)
+       if (!f->lines[i])
                return;
 
-       /* We expect a condition on this line. */
+       /* We expect a condition around this line. */
        if (!line_info[i].cond) {
-               score_file_error(score, f, i+1, "Expected #ifndef");
+               score_file_error(score, f, first_preproc_line+1,
+                                "Expected #ifndef");
                return;
        }
 
@@ -137,7 +129,8 @@ static void check_idem(struct ccan_file *f, struct score *score)
        /* We expect the condition to be ! IFDEF <symbol>. */
        if (line_info[i].cond->type != PP_COND_IFDEF
            || !line_info[i].cond->inverse) {
-               score_file_error(score, f, i+1, "Expected #ifndef");
+               score_file_error(score, f, first_preproc_line+1,
+                                "Expected #ifndef");
                return;
        }
 
@@ -158,8 +151,11 @@ static void check_idem(struct ccan_file *f, struct score *score)
                return;
        }
 
+       /* Record this for use in depends_accurate */
+       f->idempotent_cond = line_info[i].cond;
+
        /* Rest of code should all be covered by that conditional. */
-       for (i++; i < f->num_lines; i++) {
+       for (i++; f->lines[i]; i++) {
                unsigned int val = 0;
                if (line_info[i].type == DOC_LINE
                    || line_info[i].type == COMMENT_LINE)
@@ -174,16 +170,18 @@ static void check_idem(struct ccan_file *f, struct score *score)
 }
 
 static void check_idempotent(struct manifest *m,
-                            bool keep,
-                            unsigned int *timeleft, struct score *score)
+                            unsigned int *timeleft UNNEEDED,
+                            struct score *score)
 {
        struct ccan_file *f;
 
+       /* We don't fail ccanlint for this. */
+       score->pass = true;
+
        list_for_each(&m->h_files, f, list) {
                check_idem(f, score);
        }
        if (!score->error) {
-               score->pass = true;
                score->score = score->total;
        }
 }
@@ -193,7 +191,7 @@ struct ccanlint headers_idempotent = {
        .name = "Module headers are #ifndef/#define wrapped",
        .check = check_idempotent,
        .handle = handle_idem,
-       .needs = ""
+       .needs = "info_exists main_header_exists"
 };
 
 REGISTER_TEST(headers_idempotent);