From patchwork Fri Mar 31 01:44:55 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: 9655405 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 E7D9460113 for ; Fri, 31 Mar 2017 01:46:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D9FF228688 for ; Fri, 31 Mar 2017 01:46:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CEC7828698; Fri, 31 Mar 2017 01:46:52 +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 7442A28688 for ; Fri, 31 Mar 2017 01:46:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934872AbdCaBqv (ORCPT ); Thu, 30 Mar 2017 21:46:51 -0400 Received: from mail-wr0-f196.google.com ([209.85.128.196]:34287 "EHLO mail-wr0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934699AbdCaBqu (ORCPT ); Thu, 30 Mar 2017 21:46:50 -0400 Received: by mail-wr0-f196.google.com with SMTP id w43so16583460wrb.1 for ; Thu, 30 Mar 2017 18:46:48 -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=Ec8805EkTgf21J3d794x3zEMD8w4+sAmtfIKubwueNY=; b=kq1CuA5OA0/bC85TK4NfPtMf5TGrOWZZqlYno2yCPb9geOF8k/Uh+biWjrwr41AwhL b7SiESHID2gzqeUdl0c1kZJ4vVlcdfDPrzt+/T6RZFTe60Uia5rDAeBcOlROM06y9aqF 63XlGHdXtQV+2AchhMQX3YT/EVsGMcg7Ji2ubm/MvK1y5/vId1coBiJMsAZR6E8Ccl8t Zw/IhxCVpkTH5OId2Xcw7tht19JPFKq3y8PONL2PxTGnHOK1NAnqXUnyHZaELiKO/5qx 9l0OCq+X8RS09Tsft7/G2j6xMIBZgxEc3iMh6fN9mO3RnqhudmHfFAiL6QZDpocn7k7s MJXg== 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=Ec8805EkTgf21J3d794x3zEMD8w4+sAmtfIKubwueNY=; b=b7wEn67rQRBNYrLOs5zA9ujxLx6jpWtsuikOx0c9xws2erIjuocxoP41wI6xG3H/0v lvHLu3pbpdevQ1WzANRKge9Eo3Fulv/LOD4OTJkNAn5VzauFON2aur3NDNSwaNGv9NeU ybbPt+8PVHaUPFZ5g+6DBBJuKxdlFAt6WPESxDyr3Q92gFQ/km00gKDRljm6IgkngHEN CGswuqxWzhbvYUm3zUZH0oM+TL3IL8R6GVxxVSrKjQyddspXVRaSjt4IBj2XEzoAkJO2 +s0PKt/Z5NTxskKsU7CTDH5sJNDt9VE+78Noyhc9IpMMgJXxWnqWMED3oq/1id+Vg5Ji ++Xw== X-Gm-Message-State: AFeK/H3fixl/OR9nLkRBbthG4D3dzKrlCfjDTEA+QH+7ZXeYoQ0mXzxh5aoPVM3LJs3WiQ== X-Received: by 10.28.73.197 with SMTP id w188mr838448wma.46.1490924807853; Thu, 30 Mar 2017 18:46:47 -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.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 30 Mar 2017 18:46:47 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Christopher Li , Nicolai Stange , Luc Van Oostenryck Subject: [PATCH v4 21/25] constexpr: support compound literals as address constants Date: Fri, 31 Mar 2017 03:44:55 +0200 Message-Id: <20170331014459.9351-22-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 Toplevel compound literals have got static storage duration [6.5.2.5(6)]. This implies that 1. their addresses are address constants [6.6(9)] and 2. their initializers must contain constant expressions only [6.5.2.5(3), 6.7.8(4)] . Flag the anonymous symbol created at expression parsing time as having static storage duration if the compound literal occurs at top level scope. Flag the whole expression as being an address constant at evaluation time if its corresponding anonymous symbol had been previously marked as having static storage duration. Signed-off-by: Nicolai Stange Signed-off-by: Luc Van Oostenryck --- evaluate.c | 2 ++ expression.c | 2 ++ validation/constexpr-compound-literal.c | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 validation/constexpr-compound-literal.c diff --git a/evaluate.c b/evaluate.c index 022c7b5f3..1ae370c7b 100644 --- a/evaluate.c +++ b/evaluate.c @@ -2841,6 +2841,8 @@ static struct symbol *evaluate_cast(struct expression *expr) addr->ctype = &lazy_ptr_ctype; /* Lazy eval */ addr->symbol = sym; + if (sym->ctype.modifiers & MOD_TOPLEVEL) + addr->flags |= CEF_ADDR; expr->type = EXPR_PREOP; expr->op = '*'; diff --git a/expression.c b/expression.c index 3f1ea2141..e4b770fc0 100644 --- a/expression.c +++ b/expression.c @@ -711,6 +711,8 @@ static struct token *cast_expression(struct token *token, struct expression **tr cast->cast_type = sym; token = expect(token, ')', "at end of cast operator"); if (match_op(token, '{')) { + if (toplevel(block_scope)) + sym->ctype.modifiers |= MOD_TOPLEVEL; if (is_force) warning(sym->pos, "[force] in compound literal"); diff --git a/validation/constexpr-compound-literal.c b/validation/constexpr-compound-literal.c new file mode 100644 index 000000000..d7f21ad7a --- /dev/null +++ b/validation/constexpr-compound-literal.c @@ -0,0 +1,19 @@ +static int *a = &(int){ 1 }; // OK +static int *b = &(int){ *a }; // KO + +static void foo(void) +{ + int *b = &(int){ 1 }; // OK + int *c = &(int){ *a }; // OK + static int *d = &(int){ 1 }; // KO +} + +/* + * check-name: compound literal address constness verification + * check-command: sparse -Wconstexpr-not-const $file + * + * check-error-start +constexpr-compound-literal.c:2:25: warning: non-constant initializer for static object +constexpr-compound-literal.c:8:27: warning: non-constant initializer for static object + * check-error-end + */