aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2020-07-16 18:26:21 +0200
committerLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2020-07-25 23:19:12 +0200
commit49fc08a0378a482e1d7db8ddc508bed11a3787ae (patch)
treeaa2806381003a5588c7197ffb96b5df319be1bb1
parent3ba7f29e393c28861e15e113ed3c9698bd00ef6f (diff)
downloadsparse-49fc08a0378a482e1d7db8ddc508bed11a3787ae.tar.gz
attribute: factorize matching of '__attribute__'
Matching the keyword '__attribute__' (or '__attribute') needs several tests and this matching is needed to handle attributes or to skip them. So, create an helper, match_attribute(), and use it in the loops to handle and to skip attributes. Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
-rw-r--r--parse.c35
1 files changed, 16 insertions, 19 deletions
diff --git a/parse.c b/parse.c
index 14b1746d..681d98e4 100644
--- a/parse.c
+++ b/parse.c
@@ -1761,6 +1761,20 @@ static struct token *handle_asm_name(struct token *token, struct decl_state *ctx
return token;
}
+///
+// test if @token is '__attribute__' (or one of its variant)
+static bool match_attribute(struct token *token)
+{
+ struct symbol *sym;
+
+ if (token_type(token) != TOKEN_IDENT)
+ return false;
+ sym = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
+ if (!sym || sym->type != SYM_KEYWORD)
+ return false;
+ return sym->op->type & KW_ATTRIBUTE;
+}
+
static struct token *skip_attribute(struct token *token)
{
token = token->next;
@@ -1782,15 +1796,7 @@ static struct token *skip_attribute(struct token *token)
static struct token *skip_attributes(struct token *token)
{
- struct symbol *keyword;
- for (;;) {
- if (token_type(token) != TOKEN_IDENT)
- break;
- keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
- if (!keyword || keyword->type != SYM_KEYWORD)
- break;
- if (!(keyword->op->type & KW_ATTRIBUTE))
- break;
+ while (match_attribute(token)) {
token = expect(token->next, '(', "after attribute");
token = expect(token, '(', "after attribute");
while (token_type(token) == TOKEN_IDENT) {
@@ -1807,17 +1813,8 @@ static struct token *skip_attributes(struct token *token)
static struct token *handle_attributes(struct token *token, struct decl_state *ctx)
{
- struct symbol *keyword;
- for (;;) {
- if (token_type(token) != TOKEN_IDENT)
- break;
- keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
- if (!keyword || keyword->type != SYM_KEYWORD)
- break;
- if (!(keyword->op->type & KW_ATTRIBUTE))
- break;
+ while (match_attribute(token))
token = attribute_specifier(token->next, ctx);
- }
return token;
}