diff options
author | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2020-05-13 22:32:04 +0200 |
---|---|---|
committer | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2020-05-21 17:27:46 +0200 |
commit | 384008074a8d8881fefaa4fdda330120385d1259 (patch) | |
tree | b7987270b948da703c344bd9918a7c4e5dc435f4 | |
parent | d89355c8ec5761eecc18a46c48f254feee8c9234 (diff) | |
download | sparse-384008074a8d8881fefaa4fdda330120385d1259.tar.gz |
bad-label: respect attribute((unused))
Currently, attributes on labels were simply ignored. This was fine
since nothing was done wth them anyway.
But now that Sparse can give a warning for unused labels it would
be nice to also support the attribute 'unused' not to issues the
warning when not desired.
So, add a small helper around handle_attributes() and use this
instead of skipping the attributes.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
-rw-r--r-- | parse.c | 11 | ||||
-rw-r--r-- | scope.c | 2 | ||||
-rw-r--r-- | symbol.h | 1 | ||||
-rw-r--r-- | validation/label-unused.c | 6 |
4 files changed, 19 insertions, 1 deletions
@@ -2554,6 +2554,15 @@ static struct token *parse_range_statement(struct token *token, struct statement return expect(token, ';', "after range statement"); } +static struct token *handle_label_attributes(struct token *token, struct symbol *label) +{ + struct decl_state ctx = { }; + + token = handle_attributes(token, &ctx, KW_ATTRIBUTE); + label->label_modifiers = ctx.ctype.modifiers; + return token; +} + static struct token *statement(struct token *token, struct statement **tree) { struct statement *stmt = alloc_statement(token->pos, STMT_NONE); @@ -2566,7 +2575,7 @@ static struct token *statement(struct token *token, struct statement **tree) if (match_op(token->next, ':')) { struct symbol *s = label_symbol(token, 0); - token = skip_attributes(token->next->next); + token = handle_label_attributes(token->next->next, s); if (s->stmt) { sparse_error(stmt->pos, "label '%s' redefined", show_ident(s->ident)); // skip the label to avoid multiple definitions @@ -155,6 +155,8 @@ void end_label_scope(void) FOR_EACH_PTR(label_scope->symbols, sym) { if (!sym->stmt || sym->used) continue; + if (sym->label_modifiers & MOD_UNUSED) + continue; warning(sym->pos, "unused label '%s'", show_ident(sym->ident)); } END_FOR_EACH_PTR(sym); @@ -170,6 +170,7 @@ struct symbol { struct /* NS_LABEL */ { struct scope *label_scope; struct position label_pos; + unsigned long label_modifiers; }; struct /* NS_SYMBOL */ { unsigned long offset; diff --git a/validation/label-unused.c b/validation/label-unused.c index a654ef77..e3f255e1 100644 --- a/validation/label-unused.c +++ b/validation/label-unused.c @@ -13,6 +13,12 @@ l: }); } +static void baz(void) +{ +l: __attribute__((unused)); + return; +} + /* * check-name: label-unused * |