From patchwork Mon Jan 9 21:57:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 41184 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2401407wrt; Mon, 9 Jan 2023 13:58:12 -0800 (PST) X-Google-Smtp-Source: AMrXdXuei6TntGTBtW3zXEvL6+vUJgOKbhIUsus/PED48a4dTz4/WASyygzBCBie/HWl5NiAP6DF X-Received: by 2002:a05:6402:294d:b0:48e:c0c3:7974 with SMTP id ed13-20020a056402294d00b0048ec0c37974mr22009660edb.12.1673301492842; Mon, 09 Jan 2023 13:58:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673301492; cv=none; d=google.com; s=arc-20160816; b=KNEmKNRgzohBMIKZfqnU3JOfrCa/7wBxTKTAhZoVee/HNHnINEJZFdNb3xmJkFc00F YXGZm3354m5b5Br8/k5X3q3gUIBOejkuNgAo+FjJdzPB/xwc1Vm7R4yRYINeuKMXkQ/k yRxdwsjNbda27F/Gg4Z3VMlk5kTq+7wIkrF7T0drTqCdnAd7gU8Fm+9G7bJdmi8pdNed IKr4kEYdFAgs92Z5JiysIaL1QKuXdHF52QR5bMIc0KG0osC4QXFb5iTS29QkvMD6TT6q 7GOHNicvNjTqD2lEsAJHGZ0ck/BZtr09h1nYQlX0hqguc4t+h84vSdavyp10AtO1G7Ox h7Lg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mime-version:message-id:subject :to:from:date:ironport-sdr:dmarc-filter:delivered-to; bh=VhhYn9EMAUteuwDLizyK6Gy4TusHLC4xw7IyrxL4IGE=; b=yKab45BLbRHhcuh4S2EpssqeKVyOYx7S183EF9UaG/V4C2fcPUw7HqPSQSHlgoPq4d PxrZ+rHKn/IPaiuFeN8O0hGLntRlu4z0NMDbWieVbAvlm+STDzK/5rXJBfDjvy/fqpJe FM3p9rOJa5DXF9s5/eEue72XG68LyXtkVy5Q+AzmZfHt+PCjsvnk03ogcXCuLmYTVsy5 a2k5YrSwY4ow8a4uw8bVLyz6R7t7TXe6wa3fa8Blo/IaOfOhz558FJ93FX+uSaQnpSge 7cXTCfbi+CWhgKmQBVcDHKB9/sl3uvaYRs7SpVSPBlYhnl6udsgoNLiqpl1juoGEp7pa VXBQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id z11-20020a056402274b00b00482071c30e3si12601566edd.39.2023.01.09.13.58.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Jan 2023 13:58:12 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 29FF438618B2 for ; Mon, 9 Jan 2023 21:57:47 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from esa2.mentor.iphmx.com (esa2.mentor.iphmx.com [68.232.141.98]) by sourceware.org (Postfix) with ESMTPS id B6D4F385B50A for ; Mon, 9 Jan 2023 21:57:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B6D4F385B50A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mentor.com X-IronPort-AV: E=Sophos;i="5.96,313,1665475200"; d="scan'208";a="93642781" Received: from orw-gwy-01-in.mentorg.com ([192.94.38.165]) by esa2.mentor.iphmx.com with ESMTP; 09 Jan 2023 13:57:21 -0800 IronPort-SDR: 73ci8jNYvV/jKY0sk2H1YgJVhhuJxQbYfDoIxwtoDwbmg2osrn9N26Ct2+Lhtl5zvJig3Dm+Gi PuEjIkM9LGWonCMVlKjb8bFbeNEVBQtqivvmz3iQCB7jLEmdKoHdLvDb1YZ6W8gS2NnTKdzHKS +VBtYVjpWnFyaDs7XW2oYs8MjzCcAmvOojiL9tKVajyN2q5LrJE+EOwVXs2iNjlvrrGEoAUUof /47hcKEzK7CC8BMxK/r09wkyuQv+nOIUjLQUqknmUG4+ALYGYws9WcF0Qoy1aNYdu95oxXX0m0 9e8= Date: Mon, 9 Jan 2023 21:57:16 +0000 From: Joseph Myers To: Subject: [committed] c: Check for modifiable static compound literals in inline definitions Message-ID: <572c6924-616-997f-c3c1-684f3bf37b59@codesourcery.com> MIME-Version: 1.0 X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: svr-ies-mbx-13.mgc.mentorg.com (139.181.222.13) To svr-ies-mbx-10.mgc.mentorg.com (139.181.222.10) X-Spam-Status: No, score=-3115.0 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754583786230169948?= X-GMAIL-MSGID: =?utf-8?q?1754583786230169948?= The C rule against modifiable objects with static storage duration in inline definitions should apply to compound literals (using the C2x feature of storage-class specifiers for compound literals) just as to variables. Add a call to record_inline_static for compound literals to make sure this case is detected. Bootstrapped with no regressions for x86_64-pc-linux-gnu. gcc/c/ * c-decl.cc (build_compound_literal): Call record_inline_static. gcc/testsuite/ * gcc.dg/c2x-complit-8.c: New test. diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index e47ca6718b3..d76ffb3380d 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -6260,6 +6260,13 @@ build_compound_literal (location_t loc, tree type, tree init, bool non_const, DECL_USER_ALIGN (decl) = 1; } store_init_value (loc, decl, init, NULL_TREE); + if (current_scope != file_scope + && TREE_STATIC (decl) + && !TREE_READONLY (decl) + && DECL_DECLARED_INLINE_P (current_function_decl) + && DECL_EXTERNAL (current_function_decl)) + record_inline_static (input_location, current_function_decl, + decl, csi_modifiable); if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type)) { diff --git a/gcc/testsuite/gcc.dg/c2x-complit-8.c b/gcc/testsuite/gcc.dg/c2x-complit-8.c new file mode 100644 index 00000000000..fb614ab7802 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c2x-complit-8.c @@ -0,0 +1,70 @@ +/* Test C2x storage class specifiers in compound literals: inline function + constraints. */ +/* { dg-do compile } */ +/* { dg-options "-std=c2x -pedantic-errors" } */ + +inline void +f1 () +{ + (static int) { 123 }; /* { dg-error "static but declared in inline function 'f1' which is not static" } */ + (static thread_local int) { 456 } ; /* { dg-error "static but declared in inline function 'f1' which is not static" } */ + (int) { 789 }; + (register int) { 1234 }; +} + +inline void +f1e () +{ + (static int) { 123 }; + (static thread_local int) { 456 } ; +} + +static inline void +f1s () +{ + (static int) { 123 }; + (static thread_local int) { 456 } ; +} + +inline void +f2 () +{ + (static const int) { 123 }; + (static thread_local const int) { 456 }; +} + +inline void +f2e () +{ + (static const int) { 123 }; + (static thread_local const int) { 456 }; +} + +static inline void +f2s () +{ + (static const int) { 123 }; + (static thread_local const int) { 456 }; +} + +inline void +f3 () +{ + (static constexpr int) { 123 }; +} + +inline void +f3e () +{ + (static constexpr int) { 123 }; +} + +static inline void +f3s () +{ + (static constexpr int) { 123 }; +} + +extern void f1e (); +extern void f2e (); +extern void f3e ();