From 384008074a8d8881fefaa4fdda330120385d1259 Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Wed, 13 May 2020 22:32:04 +0200 Subject: 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 --- parse.c | 11 ++++++++++- scope.c | 2 ++ symbol.h | 1 + validation/label-unused.c | 6 ++++++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/parse.c b/parse.c index a8e4a02e..3cd84a3c 100644 --- a/parse.c +++ b/parse.c @@ -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 diff --git a/scope.c b/scope.c index 3a0d784f..fb587c47 100644 --- a/scope.c +++ b/scope.c @@ -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); diff --git a/symbol.h b/symbol.h index 2293d06d..6f904795 100644 --- a/symbol.h +++ b/symbol.h @@ -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 * -- cgit 1.2.3-korg