From patchwork Sat Feb 18 20:30:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 9581213 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9F3BA600F6 for ; Sat, 18 Feb 2017 20:31:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 90EF826E97 for ; Sat, 18 Feb 2017 20:31:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 85DB3286F4; Sat, 18 Feb 2017 20:31:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 172AB26E97 for ; Sat, 18 Feb 2017 20:31:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753814AbdBRUbA (ORCPT ); Sat, 18 Feb 2017 15:31:00 -0500 Received: from mail-wm0-f67.google.com ([74.125.82.67]:34648 "EHLO mail-wm0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753461AbdBRUa7 (ORCPT ); Sat, 18 Feb 2017 15:30:59 -0500 Received: by mail-wm0-f67.google.com with SMTP id c85so8052087wmi.1 for ; Sat, 18 Feb 2017 12:30:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=J7iaaI8af6P+tWLmgUld/hMYGmtjkbpGwxkJOCJntM0=; b=S/ZhDyrMcDlW6JSetUblHwDGDkog2RmveWzrdsG8olajYxwTk3lJDoQGK6EoNDgakA KYvSzuV/d6hQBj5h+2sf2/Amlt/IV0YIpPzyf5pG3cbR/kTSSLaewnyYUUtrQbGEoNOw jf+COLkfwZtZ3Fw2VYOhh/9sxTCipUfDg+abYLGlG8JZ7VUqNqheQ51nVR98B7hvxn2u o7OmaSkMRUHMf2K+3YNS2B7d4EDun6Sb33X/LwGE1xkiYD9IHS7OpsHkXdLudAMCRsE1 lufPu+MlDpQTcUdHihNDvxCotZ0yXrqJWwM2QNlhN389eZMTYSLmruDsBP2Bh+pk0DFk cAfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=J7iaaI8af6P+tWLmgUld/hMYGmtjkbpGwxkJOCJntM0=; b=ERrQoHKA4cD/ICN31sKdNTADjotYu26fi3fAiDsSr+AJcEuVwBX9VyeEWovvZASHw5 H4EjX0JWpBk8bfLfixxqMV3U7oLiJbNufQrqP2VuawB4ujSg+i+7h8H27Dy3FyNtK9YY peWjP8px7veEu3mW+O/hW0hXh16EJ+9aPgHtNH3xpo+JTGZjgoXQYom7SzS+v3LlbaaS wq5w6CQyqrDes+uO+ps8bEUs7M6NlibQvm7iVdWBQbroznyReyz8hLloX6BQtigDyP7P kWqbDbov7EBtMWbSFyuK/BlZPGU/vBu63SBRLDxpg+zcsjnAI5+jaEuUUaiaUwWouVX6 C+6A== X-Gm-Message-State: AMke39loqLX9Cz3W+nYw1alVYpkLcX1QYzshUuSkIhIpDPw8xdx9pLB9w2zyP8/tv63Gaw== X-Received: by 10.28.95.87 with SMTP id t84mr10182054wmb.135.1487449857781; Sat, 18 Feb 2017 12:30:57 -0800 (PST) Received: from localhost.localdomain ([2a02:a03f:8c2:3900:48ac:d537:1a61:c3e1]) by smtp.gmail.com with ESMTPSA id a14sm4888854wrc.66.2017.02.18.12.30.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 18 Feb 2017 12:30:57 -0800 (PST) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Christopher Li , Luc Van Oostenryck Subject: [PATCH 4/5] add a method to external_declaration() Date: Sat, 18 Feb 2017 21:30:47 +0100 Message-Id: <20170218203048.22276-5-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170218203048.22276-1-luc.vanoostenryck@gmail.com> References: <20170218203048.22276-1-luc.vanoostenryck@gmail.com> Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP After parsing and validation, the symbols in the declaration are added to the list given in argument, *if* they are not extern symbols. The symbols that are extern are them not added to the list. This is what is needed for usual declarations but ignoring extern symbols make it impossible to emit a diagnostic in less usual situation. This is motivated by the validation of variable declaration inside a for-loop initializer, which valid in C99 but only for variable with local storage. The changes consist in moving the part 'add the symbol to the list if the symbol is not extern' to a separate function which will be called by default. Signed-off-by: Luc Van Oostenryck --- lib.c | 2 +- parse.c | 22 +++++++++++++++------- parse.h | 3 ++- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib.c b/lib.c index d47325243..9badc09b3 100644 --- a/lib.c +++ b/lib.c @@ -1080,7 +1080,7 @@ static struct symbol_list *sparse_tokenstream(struct token *token) // Parse the resulting C code while (!eof_token(token)) - token = external_declaration(token, &translation_unit_used_list); + token = external_declaration(token, NULL, &translation_unit_used_list); return translation_unit_used_list; } diff --git a/parse.c b/parse.c index a4a126720..866186fd2 100644 --- a/parse.c +++ b/parse.c @@ -2230,7 +2230,7 @@ static struct token *parse_for_statement(struct token *token, struct statement * e1 = NULL; /* C99 variable declaration? */ if (lookup_type(token)) { - token = external_declaration(token, &syms); + token = external_declaration(token, NULL, &syms); } else { token = parse_expression(token, &e1); token = expect(token, ';', "in 'for'"); @@ -2457,7 +2457,7 @@ static struct token * statement_list(struct token *token, struct statement_list seen_statement = 0; } stmt = alloc_statement(token->pos, STMT_DECLARATION); - token = external_declaration(token, &stmt->declaration); + token = external_declaration(token, NULL, &stmt->declaration); } else { seen_statement = Wdeclarationafterstatement; token = statement(token, &stmt); @@ -2785,7 +2785,15 @@ static struct token *toplevel_asm_declaration(struct token *token, struct symbol return token; } -struct token *external_declaration(struct token *token, struct symbol_list **list) +static void add_stmt_decl(struct symbol_list **list, struct symbol *decl) +{ + if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) { + add_symbol(list, decl); + fn_local_symbol(decl); + } +} + +struct token *external_declaration(struct token *token, add_decl_t add_decl, struct symbol_list **list) { struct ident *ident = NULL; struct symbol *decl; @@ -2864,6 +2872,9 @@ struct token *external_declaration(struct token *token, struct symbol_list **lis sparse_error(token->pos, "void declaration"); } + if (!add_decl) + add_decl = add_stmt_decl; + for (;;) { if (!is_typedef && match_op(token, '=')) { if (decl->ctype.modifiers & MOD_EXTERN) { @@ -2873,10 +2884,7 @@ struct token *external_declaration(struct token *token, struct symbol_list **lis token = initializer(&decl->initializer, token->next); } if (!is_typedef) { - if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) { - add_symbol(list, decl); - fn_local_symbol(decl); - } + add_decl(list, decl); } check_declaration(decl); if (decl->same_symbol) { diff --git a/parse.h b/parse.h index a2b3e3889..d3199727e 100644 --- a/parse.h +++ b/parse.h @@ -129,7 +129,8 @@ extern int show_statement(struct statement *); extern void show_statement_list(struct statement_list *, const char *); extern int show_expression(struct expression *); -extern struct token *external_declaration(struct token *token, struct symbol_list **list); +typedef void (*add_decl_t)(struct symbol_list **list, struct symbol *decl); +extern struct token *external_declaration(struct token *token, add_decl_t add_decl, struct symbol_list **list); extern struct symbol *ctype_integer(int size, int want_unsigned);