From patchwork Fri Mar 31 01:44:46 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: 9655387 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 7D09660113 for ; Fri, 31 Mar 2017 01:46:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6F3F528688 for ; Fri, 31 Mar 2017 01:46:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6464428696; Fri, 31 Mar 2017 01:46:43 +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 0752A28688 for ; Fri, 31 Mar 2017 01:46:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935023AbdCaBqn (ORCPT ); Thu, 30 Mar 2017 21:46:43 -0400 Received: from mail-wr0-f195.google.com ([209.85.128.195]:35181 "EHLO mail-wr0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934997AbdCaBql (ORCPT ); Thu, 30 Mar 2017 21:46:41 -0400 Received: by mail-wr0-f195.google.com with SMTP id p52so16621478wrc.2 for ; Thu, 30 Mar 2017 18:46:40 -0700 (PDT) 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=akxudZvh87Go8Lgum8xRf5bSWjeDGRXqP/RtUV+TpMU=; b=AdILChLP3hHco/comg7E5p2FKkAkmNd0GTfDaAi+jF0ettu4dO/UGnC1VSBOckND8u fhbmCoo4L9wyq4/yPhpjEMsTpPln+SFK72RCUeo3Qg1YegacvEGYfRSgqQh7FRRDm5hD vhPWU9hp3Ay9kbSBwnU4kFTOn/EO/noyegzP6UjrOOQAcUFxuEWXaNQ2yXXmekQU+UJI O1AGic6l6QCA8rG1ohmmr5ohD5jb5vT2JhgiluJ/jv4HbWKTXbPSJykB1q3WF4VqdcDB YTToDDf2kCIaV4RO8i01G/WybGrJawfDVNojDctdo8P+3b3eoOeQXJQZ4Yq41Ux5w3Vn FHSA== 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=akxudZvh87Go8Lgum8xRf5bSWjeDGRXqP/RtUV+TpMU=; b=LqMW8Li8sO2+Jso8x+DRsioNaFo745nDAhz90iYmd1gA/+hv7pVopN8sn3jegO9IFL utVeTIYRk+Xxk2AjxVVrrLlWKydoxZ44iqa62SX7j19pm/ORnhOnbvtFJR70DBUZ44D4 G2l2H5Oc0BEamLnEBn/8fRo/iayB/cTHYbANK+wxLwzoEs/PS0QMmhWh9iivsp7P6fqJ 1LhoLQkFQYnEimHBWi4ioxfskrmY2ovn7d+4M6pX0qLiezhox85+G9QxIAQTbDPwcZDK IWtvLQib0iuKUXk1dh3eDbCSldK0A94hegnyHkxnfH4D7do3eNSJpYS72WgtajEhLKRL J3iA== X-Gm-Message-State: AFeK/H3zpRwPo7xDWNgQp6OjfP/s+vR59zeowlnd6c1g+TRncKMCt2Ryc3HULVKJs40WRQ== X-Received: by 10.28.184.87 with SMTP id i84mr378552wmf.129.1490924799390; Thu, 30 Mar 2017 18:46:39 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:88d:1900:cdad:92b8:5279:ae19]) by smtp.gmail.com with ESMTPSA id 189sm917023wmm.31.2017.03.30.18.46.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 30 Mar 2017 18:46:38 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Christopher Li , Nicolai Stange , Luc Van Oostenryck Subject: [PATCH v4 12/25] constexpr: recognize static objects as address constants Date: Fri, 31 Mar 2017 03:44:46 +0200 Message-Id: <20170331014459.9351-13-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.12.0 In-Reply-To: <20170331014459.9351-1-luc.vanoostenryck@gmail.com> References: <20170331014459.9351-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 From: Nicolai Stange Introduce support for recognizing address constants created either - explicitly by referencing a static storage duration object by means of the unary & operator, - implicitly by the use of an expression of array or function type. Initially tag an expression as being an address constant at the primary expression level, i.e. upon encountering a symbol designating an object of static storage duration in primary_expression(). Carry the address constant flag over to the *-preop wrapped expression created by evaluate_symbol_expression(). When dereferencing such a *-preop wrapped expression, make evaluate_addressof() keep any address constant flag for the unwrapped expression. Signed-off-by: Nicolai Stange Signed-off-by: Luc Van Oostenryck --- evaluate.c | 3 ++- expression.c | 8 ++++++++ validation/constexpr-addr-of-static.c | 36 +++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 validation/constexpr-addr-of-static.c diff --git a/evaluate.c b/evaluate.c index 726ec15d3..c111f6d19 100644 --- a/evaluate.c +++ b/evaluate.c @@ -70,9 +70,11 @@ static struct symbol *evaluate_symbol_expression(struct expression *expr) addr->symbol = sym; addr->symbol_name = expr->symbol_name; addr->ctype = &lazy_ptr_ctype; /* Lazy evaluation: we need to do a proper job if somebody does &sym */ + addr->flags = expr->flags; expr->type = EXPR_PREOP; expr->op = '*'; expr->unop = addr; + expr->flags = CEF_NONE; /* The type of a symbol is the symbol itself! */ expr->ctype = sym; @@ -1689,7 +1691,6 @@ static struct symbol *evaluate_addressof(struct expression *expr) } ctype = op->ctype; *expr = *op->unop; - expr->flags = CEF_NONE; if (expr->type == EXPR_SYMBOL) { struct symbol *sym = expr->symbol; diff --git a/expression.c b/expression.c index 7f1eff306..00edd1f99 100644 --- a/expression.c +++ b/expression.c @@ -435,6 +435,14 @@ struct token *primary_expression(struct token *token, struct expression **tree) } expr->symbol_name = token->ident; expr->symbol = sym; + + /* + * A pointer to an lvalue designating a static storage + * duration object is an address constant [6.6(9)]. + */ + if (sym && (sym->ctype.modifiers & (MOD_TOPLEVEL | MOD_STATIC))) + expr->flags = CEF_ADDR; + token = next; break; } diff --git a/validation/constexpr-addr-of-static.c b/validation/constexpr-addr-of-static.c new file mode 100644 index 000000000..a3af99ae7 --- /dev/null +++ b/validation/constexpr-addr-of-static.c @@ -0,0 +1,36 @@ +static int a = 1; +static int b[2] = {1, 1}; +static void c(void) {} + +static int *d = &a; // OK +static int *e = d; // KO +static int *f = b; // OK + +static void (*g)(void) = c; // OK +static void (*h)(void) = &c; // OK + +static int *i = &*&a; // OK +static int *j = &*b; // OK +static int *k = &*d; // KO + + +static void l(void) { + int a = 1; + static int *b = &a; // KO +} + +static void m(void) { + static int a = 1; + static int *b = &a; // OK +} + +/* + * check-name: address of static object constness verification. + * check-command: sparse -Wconstexpr-not-const $file + * + * check-error-start +constexpr-addr-of-static.c:6:17: warning: non-constant initializer for static object +constexpr-addr-of-static.c:14:19: warning: non-constant initializer for static object +constexpr-addr-of-static.c:19:26: warning: non-constant initializer for static object + * check-error-end + */