diff options
author | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2020-07-19 18:23:46 +0200 |
---|---|---|
committer | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2020-07-23 23:25:35 +0200 |
commit | 06ce9117090be44b6b1b5b34c074f6605f1d9a7d (patch) | |
tree | 2ce74370d4540a03cff482ab901625a2b8f289e1 | |
parent | 279d8171c795e4e2af205b00631d4a5b8f880910 (diff) | |
download | sparse-06ce9117090be44b6b1b5b34c074f6605f1d9a7d.tar.gz |
simplify & fix parsing of array declarators
Any type qualifier is valid inside an abstract-array-declarator
but currently only 'restrict' is accepted. Also the parsing of
this is somehow more complex than needed and done by comparing
the identifiers instead of being driven by the keyword table.
So, simplify & fix the parsing of these declarators by:
1) using the keyword type KW_QUALIFIER to identify all type
qualifier at once.
2) add a new keyword type just for 'static'
3) folding the helper abstract_array_static_declarator() into
the main function: abstract_array_declarator().
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
-rw-r--r-- | parse.c | 28 | ||||
-rw-r--r-- | symbol.h | 2 | ||||
-rw-r--r-- | validation/abstract-array-declarator-quals.c | 1 |
3 files changed, 11 insertions, 20 deletions
@@ -171,7 +171,7 @@ static struct symbol_op register_op = { }; static struct symbol_op static_op = { - .type = KW_MODIFIER, + .type = KW_MODIFIER|KW_STATIC, .declarator = static_specifier, }; @@ -1721,28 +1721,20 @@ static struct token *declaration_specifiers(struct token *token, struct decl_sta return token; } -static struct token *abstract_array_static_declarator(struct token *token, int *has_static) -{ - while (token->ident == &static_ident) { - if (*has_static) - sparse_error(token->pos, "duplicate array static declarator"); - - *has_static = 1; - token = token->next; - } - return token; - -} - static struct token *abstract_array_declarator(struct token *token, struct symbol *sym) { struct expression *expr = NULL; int has_static = 0; - token = abstract_array_static_declarator(token, &has_static); - - if (match_idents(token, &restrict_ident, &__restrict_ident, &__restrict___ident, NULL)) - token = abstract_array_static_declarator(token->next, &has_static); + while (token_type(token) == TOKEN_IDENT) { + struct symbol *sym = lookup_keyword(token->ident, NS_TYPEDEF); + if (!sym || !(sym->op->type & (KW_STATIC|KW_QUALIFIER))) + break; + if (has_static && (sym->op->type & KW_STATIC)) + sparse_error(token->pos, "duplicate array static declarator"); + has_static |= (sym->op->type & KW_STATIC); + token = token->next; + } token = assignment_expression(token, &expr); sym->array_size = expr; return token; @@ -81,7 +81,7 @@ enum keyword { KW_BUILTIN = 1 << 4, KW_ASM = 1 << 5, KW_MODE = 1 << 6, - // KW UNUSED = 1 << 7, + KW_STATIC = 1 << 7, // KW UNUSED = 1 << 8, KW_EXACT = 1 << 9, }; diff --git a/validation/abstract-array-declarator-quals.c b/validation/abstract-array-declarator-quals.c index 85a35a2a..e69df902 100644 --- a/validation/abstract-array-declarator-quals.c +++ b/validation/abstract-array-declarator-quals.c @@ -18,5 +18,4 @@ void ok7(int a[const volatile restrict static N]); /* * check-name: abstract-array-declarator-quals - * check-known-to-fail */ |