X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=tools%2Fccanlint%2Ffile_analysis.c;h=25e36743c51906206f1d99179b083f26a7084adf;hp=f11b76b3b3f9c969e275de05784659caa0d4ee29;hb=979071e8587d4819a7f78613b68d29e222e5db63;hpb=aabf300e324f7da5134d7ad45afba11225045c24 diff --git a/tools/ccanlint/file_analysis.c b/tools/ccanlint/file_analysis.c index f11b76b3..25e36743 100644 --- a/tools/ccanlint/file_analysis.c +++ b/tools/ccanlint/file_analysis.c @@ -1,9 +1,7 @@ #include "config.h" #include "ccanlint.h" -#include #include -#include -#include +#include #include #include #include @@ -38,10 +36,11 @@ struct list_head *get_ccan_file_docs(struct ccan_file *f) * @in_comment: are we already within a comment (from prev line). * @unterminated: are we still in a comment for next line. */ -static char *remove_comments(const char *line, bool in_comment, +static char *remove_comments(const tal_t *ctx, + const char *line, bool in_comment, bool *unterminated) { - char *p, *ret = talloc_array(line, char, strlen(line) + 1); + char *p, *ret = tal_arr(ctx, char, strlen(line) + 1); p = ret; for (;;) { @@ -121,7 +120,7 @@ static bool parse_hash_if(struct pp_conditions *cond, const char **line) /* FIXME: We just chain them, ignoring operators. */ if (get_token(line, "||") || get_token(line, "&&")) { - struct pp_conditions *sub = talloc(cond, struct pp_conditions); + struct pp_conditions *sub = tal(cond, struct pp_conditions); sub->parent = cond->parent; sub->type = PP_COND_IFDEF; @@ -137,10 +136,10 @@ static struct pp_conditions *analyze_directive(struct ccan_file *f, const char *line, struct pp_conditions *parent) { - struct pp_conditions *cond = talloc(f, struct pp_conditions); + struct pp_conditions *cond = tal(f, struct pp_conditions); bool unused; - line = remove_comments(line, false, &unused); + line = remove_comments(f, line, false, &unused); cond->parent = parent; cond->type = PP_COND_IFDEF; @@ -187,7 +186,7 @@ static struct pp_conditions *analyze_directive(struct ccan_file *f, cond->inverse = !cond->inverse; return cond; } else if (get_token(&line, "endif")) { - talloc_free(cond); + tal_free(cond); /* Malformed? */ if (!parent) return NULL; @@ -195,7 +194,7 @@ static struct pp_conditions *analyze_directive(struct ccan_file *f, return parent->parent; } else { /* Not a conditional. */ - talloc_free(cond); + tal_free(cond); return parent; } @@ -219,9 +218,10 @@ struct line_info *get_ccan_line_info(struct ccan_file *f) return f->line_info; get_ccan_file_lines(f); - f->line_info = talloc_array(f->lines, struct line_info, f->num_lines); + f->line_info = tal_arr(f->lines, struct line_info, + tal_count(f->lines)-1); - for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) { + for (i = 0; f->lines[i]; continued = continues(f->lines[i++])) { char *p; bool still_doc_line; @@ -233,7 +233,7 @@ struct line_info *get_ccan_line_info(struct ccan_file *f) /* Same as last line. */ f->line_info[i].type = f->line_info[i-1].type; /* Update in_comment. */ - remove_comments(f->lines[i], in_comment, &in_comment); + remove_comments(f, f->lines[i], in_comment, &in_comment); continue; } @@ -248,7 +248,7 @@ struct line_info *get_ccan_line_info(struct ccan_file *f) still_doc_line = (in_comment && f->line_info[i-1].type == DOC_LINE); - p = remove_comments(f->lines[i], in_comment, &in_comment); + p = remove_comments(f, f->lines[i], in_comment, &in_comment); if (is_empty(p)) { if (strstarts(f->lines[i], "/**") || still_doc_line) f->line_info[i].type = DOC_LINE; @@ -256,7 +256,7 @@ struct line_info *get_ccan_line_info(struct ccan_file *f) f->line_info[i].type = COMMENT_LINE; } else f->line_info[i].type = CODE_LINE; - talloc_free(p); + tal_free(p); } return f->line_info; } @@ -330,7 +330,7 @@ static enum line_compiled get_pp(struct pp_conditions *cond, static void add_symbol(struct list_head *head, const char *symbol, const unsigned int *value) { - struct symbol *sym = talloc(head, struct symbol); + struct symbol *sym = tal(head, struct symbol); sym->name = symbol; sym->value = value; list_add(head, &sym->list); @@ -345,7 +345,7 @@ enum line_compiled get_ccan_line_pp(struct pp_conditions *cond, struct list_head *head; va_list ap; - head = talloc(NULL, struct list_head); + head = tal(NULL, struct list_head); list_head_init(head); va_start(ap, value); @@ -356,45 +356,69 @@ enum line_compiled get_ccan_line_pp(struct pp_conditions *cond, add_symbol(head, symbol, value); } ret = get_pp(cond, head); - talloc_free(head); + tal_free(head); + va_end(ap); return ret; } +static void score_error_vfmt(struct score *score, const char *source, + const char *errorfmt, va_list ap) +{ + + if (!score->error) + score->error = tal_strdup(score, ""); + + if (verbose < 2 && strcount(score->error, "\n") > 5) { + if (!strends(score->error, + "... more (use -vv to see them all)\n")) { + score->error = tal_strcat(score, + take(score->error), + "... more (use -vv to see" + " them all)\n"); + } + return; + } + + tal_append_fmt(&score->error, "%s:", source); + tal_append_vfmt(&score->error, errorfmt, ap); + score->error = tal_strcat(score, take(score->error), "\n"); +} + + + +void score_error(struct score *score, const char *source, + const char *errorfmt, ...) +{ + va_list ap; + + va_start(ap, errorfmt); + score_error_vfmt(score, source, errorfmt, ap); + va_end(ap); +} + void score_file_error(struct score *score, struct ccan_file *f, unsigned line, const char *errorfmt, ...) { va_list ap; + char *source; - struct file_error *fe = talloc(score, struct file_error); + struct file_error *fe = tal(score, struct file_error); fe->file = f; fe->line = line; list_add_tail(&score->per_file_errors, &fe->list); - if (!score->error) - score->error = talloc_strdup(score, ""); - - if (verbose < 2 && strcount(score->error, "\n") > 5) - return; - if (line) - score->error = talloc_asprintf_append(score->error, - "%s:%u:", - f->fullname, line); + source = tal_fmt(score, "%s:%u", f->fullname, line); else - score->error = talloc_asprintf_append(score->error, - "%s:", f->fullname); + source = tal_fmt(score, "%s", f->fullname); va_start(ap, errorfmt); - score->error = talloc_vasprintf_append(score->error, errorfmt, ap); + score_error_vfmt(score, source, errorfmt, ap); va_end(ap); - score->error = talloc_append_string(score->error, "\n"); - - if (verbose < 2 && strcount(score->error, "\n") > 5) - score->error = talloc_append_string(score->error, - "... more (use -vv to see them all)\n"); } -char *get_or_compile_info(const void *ctx, const char *dir) + +char *get_or_compile_info(const void *ctx UNNEEDED, const char *dir) { struct manifest *m = get_manifest(NULL, dir);