X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fcdump%2Fcdump.c;h=f080b968b5732593dbed861d241614878c5191bf;hb=cc3db07ecc15f3e1e556387d82318697956e8e0c;hp=20cdb4eb1f6edafa613d5e7589bfb6c3a77e7e2b;hpb=158691ae36fe1da78ec54dbcc0006f603398dae2;p=ccan diff --git a/ccan/cdump/cdump.c b/ccan/cdump/cdump.c index 20cdb4eb..f080b968 100644 --- a/ccan/cdump/cdump.c +++ b/ccan/cdump/cdump.c @@ -16,6 +16,16 @@ static void add_token(struct token **toks, const char *p, size_t len) (*toks)[n].len = len; } +static size_t to_eol(const char *p) +{ + size_t len = strcspn(p, "\n"); + + /* And any \ continuations. */ + while (p[len] && p[len-1] == '\\') + len += strcspn(p+len+1, "\n") + 1; + return len; +} + /* Simplified tokenizer: comments and preproc directives removed, identifiers are a token, others are single char tokens. */ static struct token *tokenize(const void *ctx, const char *code) @@ -27,10 +37,10 @@ static struct token *tokenize(const void *ctx, const char *code) for (i = 0; code[i]; i += len) { if (code[i] == '#' && start_of_line) { /* Preprocessor line. */ - len = strcspn(code+i, "\n"); + len = to_eol(code + i); } else if (code[i] == '/' && code[i+1] == '/') { /* One line comment. */ - len = strcspn(code+i, "\n"); + len = to_eol(code + i); if (tok_start != -1U) { add_token(&toks, code+tok_start, i - tok_start); tok_start = -1U; @@ -84,25 +94,31 @@ struct parse_state { char *complaints; }; -static bool tok_is(const struct token **toks, const char *target) +static const struct token *tok_peek(const struct token **toks) { - return (*toks)->p && (*toks)->len == strlen(target) - && memcmp((*toks)->p, target, (*toks)->len) == 0; + /* Ignore removed tokens (eg. comments) */ + while (toks[0]->len == 0) { + if (!toks[0]->p) + return NULL; + (*toks)++; + } + return toks[0]; } -static const struct token *tok_peek(const struct token **toks) +static bool tok_is(const struct token **toks, const char *target) { - if (toks[0]->p) - return toks[0]; - return NULL; + const struct token *t = tok_peek(toks); + return (t && t->len == strlen(target) + && memcmp(t->p, target, t->len) == 0); } static const struct token *tok_take(const struct token **toks) { - if (!toks[0]->p) - return NULL; + const struct token *t = tok_peek(toks); + if (t) + (*toks)++; - return (*toks)++; + return t; } static const struct token *tok_take_if(const struct token **toks, @@ -133,8 +149,27 @@ static char *string_of_toks(const tal_t *ctx, const struct token *first, const struct token *until) { - const struct token *end = until - 1; - return tal_strndup(ctx, first->p, end->p - first->p + end->len); + char *str, *p; + + /* Careful to skip erased tokens (eg. comments) */ + str = p = tal_arr(ctx, char, until->p - first->p + 1); + while (first != until) { + const struct token *next = first + 1; + + if (first->len) { + memcpy(p, first->p, first->len); + p += first->len; + /* Insert space if they weren't adjacent, unless last */ + if (next != until) { + if (first->p + first->len != next->p) + *(p++) = ' '; + } + } + first = next; + } + *p = '\0'; + + return str; } static char *tok_take_until(const tal_t *ctx, @@ -240,13 +275,40 @@ static void tok_take_unknown_statement(struct parse_state *ps) tok_take_if(&ps->toks, ";"); } +static bool tok_take_expr(struct parse_state *ps, const char *term) +{ + while (!tok_is(&ps->toks, term)) { + if (tok_take_if(&ps->toks, "(")) { + if (!tok_take_expr(ps, ")")) + return false; + } else if (tok_take_if(&ps->toks, "[")) { + if (!tok_take_expr(ps, "]")) + return false; + } else if (!tok_take(&ps->toks)) + return false; + } + return tok_take(&ps->toks); +} + +static char *tok_take_expr_str(const tal_t *ctx, + struct parse_state *ps, + const char *term) +{ + const struct token *start = tok_peek(&ps->toks); + + if (!tok_take_expr(ps, term)) + return NULL; + + return string_of_toks(ctx, start, ps->toks - 1); +} + /* [ ... */ static bool tok_take_array(struct parse_state *ps, struct cdump_type **type) { /* This will be some arbitrary expression! */ struct cdump_type *arr = get_type(ps->defs, CDUMP_ARRAY, NULL); - arr->u.arr.size = tok_take_until(arr, &ps->toks, "]"); + arr->u.arr.size = tok_take_expr_str(arr, ps, "]"); if (!arr->u.arr.size) { complain(ps, "Could not find closing array size ]"); return false; @@ -255,8 +317,6 @@ static bool tok_take_array(struct parse_state *ps, struct cdump_type **type) arr->u.arr.type = *type; *type = arr; - /* Swallow ] */ - tok_take(&ps->toks); return true; } @@ -311,6 +371,47 @@ static bool tok_take_type(struct parse_state *ps, struct cdump_type **type) return true; } +/* CDUMP */ +static bool tok_maybe_take_cdump_note(const tal_t *ctx, + struct parse_state *ps, const char **note) +{ + *note = NULL; + if (tok_take_if(&ps->toks, "CDUMP")) { + if (!tok_take_if(&ps->toks, "(")) { + complain(ps, "Expected ( after CDUMP"); + return false; + } + *note = tok_take_expr_str(ctx, ps, ")"); + if (!*note) { + complain(ps, "Expected ) after CDUMP("); + return false; + } + } + return true; +} + +/* __attribute__((...)) */ +static bool tok_ignore_attribute(struct parse_state *ps) +{ + if (!tok_take_if(&ps->toks, "__attribute__")) + return true; + + if (!tok_take_if(&ps->toks, "(") || !tok_take_if(&ps->toks, "(")) { + complain(ps, "Expected (( after __attribute__"); + return false; + } + + if (!tok_take_expr(ps, ")")) { + complain(ps, "Expected expression after __attribute__(("); + return false; + } + if (!tok_take_if(&ps->toks, ")")) { + complain(ps, "Expected )) __attribute__(("); + return false; + } + return true; +} + /* struct|union ... */ static bool tok_take_conglom(struct parse_state *ps, enum cdump_type_kind conglom_kind) @@ -333,6 +434,12 @@ static bool tok_take_conglom(struct parse_state *ps, return false; } + if (!tok_maybe_take_cdump_note(e, ps, &e->note)) + return false; + + if (!tok_ignore_attribute(ps)) + return false; + if (!tok_take_if(&ps->toks, "{")) { complain(ps, "Expected { for struct/union"); return false; @@ -344,6 +451,9 @@ static bool tok_take_conglom(struct parse_state *ps, const struct token *quals; unsigned int num_quals = 0; + if (!tok_ignore_attribute(ps)) + return false; + /* Anything can have these prepended. */ quals = ps->toks; while (tok_take_if(&ps->toks, "const") @@ -373,6 +483,9 @@ static bool tok_take_conglom(struct parse_state *ps, while (tok_take_if(&ps->toks, "*")) m->type = ptr_of(ps, m->type); + if (!tok_ignore_attribute(ps)) + return false; + m->name = tok_take_ident(e, &ps->toks); if (!m->name) { complain(ps, "Expected name for member"); @@ -384,6 +497,14 @@ static bool tok_take_conglom(struct parse_state *ps, if (!tok_take_array(ps, &m->type)) return false; } + + /* CDUMP() */ + if (!tok_maybe_take_cdump_note(e->u.members, + ps, &m->note)) + return false; + + if (!tok_ignore_attribute(ps)) + return false; } while (tok_take_if(&ps->toks, ",")); if (!tok_take_if(&ps->toks, ";")) { @@ -392,10 +513,19 @@ static bool tok_take_conglom(struct parse_state *ps, } } - if (tok_take_if(&ps->toks, "}") && tok_take_if(&ps->toks, ";")) - return true; - complain(ps, "Expected }; at end of struct/union"); - return false; + if (!tok_take_if(&ps->toks, "}")) { + complain(ps, "Expected } at end of struct/union"); + return false; + } + + if (!tok_ignore_attribute(ps)) + return false; + + if (!tok_take_if(&ps->toks, ";")) { + complain(ps, "Expected ; at end of struct/union"); + return false; + } + return true; } /* enum ... */ @@ -419,6 +549,13 @@ static bool tok_take_enum(struct parse_state *ps) return false; } + /* CDUMP() */ + if (!tok_maybe_take_cdump_note(e, ps, &e->note)) + return false; + + if (!tok_ignore_attribute(ps)) + return false; + if (!tok_take_if(&ps->toks, "{")) { complain(ps, "Expected { after enum name"); return false; @@ -428,6 +565,10 @@ static bool tok_take_enum(struct parse_state *ps) do { struct cdump_enum_val *v; + /* GCC extension: comma and end of enum */ + if (tok_is(&ps->toks, "}")) + break; + tal_resize(&e->u.enum_vals, n+1); v = &e->u.enum_vals[n++]; @@ -436,6 +577,11 @@ static bool tok_take_enum(struct parse_state *ps) complain(ps, "Expected enum value name"); return false; } + + /* CDUMP() */ + if (!tok_maybe_take_cdump_note(e->u.enum_vals, ps, &v->note)) + return false; + if (tok_take_if(&ps->toks, "=")) { v->value = tok_take_until(e, &ps->toks, ",}"); if (!v->value) { @@ -446,11 +592,19 @@ static bool tok_take_enum(struct parse_state *ps) v->value = NULL; } while (tok_take_if(&ps->toks, ",")); - if (tok_take_if(&ps->toks, "}") && tok_take_if(&ps->toks, ";")) - return true; + if (!tok_take_if(&ps->toks, "}")) { + complain(ps, "Expected } at end of enum"); + return false; + } - complain(ps, "Expected }; at end of enum"); - return false; + if (!tok_ignore_attribute(ps)) + return false; + + if (!tok_take_if(&ps->toks, ";")) { + complain(ps, "Expected ; at end of enum"); + return false; + } + return true; } static bool gather_undefines(const char *name, @@ -508,6 +662,8 @@ struct cdump_definitions *cdump_extract(const tal_t *ctx, const char *code, toks = ps.toks = tokenize(ps.defs, code); while (tok_peek(&ps.toks)) { + if (!tok_ignore_attribute(&ps)) + goto fail; if (tok_take_if(&ps.toks, "struct")) { if (!tok_take_conglom(&ps, CDUMP_STRUCT)) goto fail;