From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by sourceware.org (Postfix) with ESMTPS id 6142238276C6 for ; Tue, 30 Aug 2022 20:30:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6142238276C6 Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 27UI4ErF002224; Tue, 30 Aug 2022 20:30:52 GMT Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3j7b59ydry-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 30 Aug 2022 20:30:51 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 27UIXRw7038287; Tue, 30 Aug 2022 20:30:50 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2105.outbound.protection.outlook.com [104.47.55.105]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3j79q4bxau-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 30 Aug 2022 20:30:50 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lJe+wz3W+LTVDg7GyDreokMhFIH9jkiLjwIMpJilYlpIhdqFMu4htNETABENsDZ9XSd7lOvMhFssRbmcA4+nRPAuBJXToWWlhUa5PWKsVqbXdlY0lXOebYVBu0BSnK5Qn9rxaLTSWVhlVn9pqsdgh4gemfCKQj66Fgm5/n93Cu04QWGGRnW8a3iO/n80g03vDqXG6R6OuLiRem6yRI7KIPw/DFJ6Z9XND7akxMssaAJnOSieqhWHiKuXaiQxGnLbqzgpu22u9IveC4eTe+/kNWQw+tx390ckcxgVqXcv46mWv0pnfunTjiNUMNmsxyWEZH5WY+FpwHJNI5FrtpglIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=wPhMv4zQVc/zM7hAarJQ1JcQvWvHlGiD9he0Il6FumU=; b=TyFU466rWGmoKWS89dQWX6ayBcX8YrrGRcgmO1Rbmn60iX3zyHWBBI36JMyGGezrQorFOtIwdXDuV1Ak7PDeEMab4qVUokveUsC5uQFUHXEAitYlojeuXIFaK/mPzyGWvDmPco8ynTSZ4McmA7mAQ8MKOr+obM/dGbPyULCiaaCNjMO+HmZMrkEOPcfI9cr5NDGjQZg4GcaAuPY4DOkCF4FACT06fnlklFk7Gtt+g7Qa75gt/n2BcdTtDo2fAbnBndJ5LyMDuZwJfMQ0rQh/t9PdeYKTnYDr9M3IipdoNV5Dzi2WI2o7h954e5lFTJNwYyGmQjFmP98boZLeYYtpzQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none Received: from CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) by BLAPR10MB5265.namprd10.prod.outlook.com (2603:10b6:208:325::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5588.10; Tue, 30 Aug 2022 20:30:44 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::84af:9275:f1d7:de1]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::84af:9275:f1d7:de1%6]) with mapi id 15.20.5566.021; Tue, 30 Aug 2022 20:30:44 +0000 From: Qing Zhao To: "joseph@codesourcery.com" , Nathan Sidwell CC: gcc Patches , richard Biener , jakub Jelinek , martin Sebor , Kees Cook Subject: Fwd: [[GCC13][Patch][V3] 1/2] Add a new option -fstrict-flex-array[=n] and new attribute strict_flex_array Thread-Topic: [[GCC13][Patch][V3] 1/2] Add a new option -fstrict-flex-array[=n] and new attribute strict_flex_array Thread-Index: AQHYskdd57xeUE7/yU2Sg7A2kEHzDg== Date: Tue, 30 Aug 2022 20:30:43 +0000 Message-ID: References: <20220817144042.2931674-1-qing.zhao@oracle.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-mailer: Apple Mail (2.3608.120.23.2.7) x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: a22d4564-62c4-4f08-27ed-08da8ac6837f x-ms-traffictypediagnostic: BLAPR10MB5265:EE_ x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: bQgQX5YDcPqMD5//nnAoPikIhlpnfCtq6ZtgM9q9kjbYlviiIPcBOKE6gXYxgkXSMMxOGmbAHg/jYIKCRMpJeXtgoW9SniylZN8ptVJemUTpELIFOCJEtFyrZ7UrSIbtINkDXbuh6V/nXQbaCG9hMevsSRVwuVhPzSW9v96BZwpgMleiz3OmoMv5R/zHz0NYHi3zlBKM/YOn+dDa0UYAO8Ef1i1u5AAjKncqsOc8SL0vCY5Lek5KW36cTtYMkqGohDKRMncCPng8H9IbrPk+M/i31fR/eC7iiEewWMtOSRWIPjz5ZCdj9nVuPAiMP97DahYudNqOu/lw34cQAIGtneVNCRVSsTJXqNRbsugFuHmO6djnNLlLq3oU1W1iXDoPfUpAz94lTz85Prb2q4ZBrlk/njLmtsVIYj8kp07LMUFr3ZxiSiLMLYZvYtf6/flui9ACwKPwdGaRXjW/CixD6PN74DWuaERw7TQFfPtSsQQ6peiF6W14p1N5C4D2AhjquUqkpJAmESbjLKfJLrqBpe99PG9z51Lwh9AiQEFuwtmAtrNfgr/FL1jlen9eoM5cy4kmvZy9f2fAkcAcKV06cKFf2tNAlv0VZmb9gIlrcmhOlj4Yd+zPMEqBXMghehcGDA1nWF3TCKs1Mytdfh7YLvS1FlXDZQM+3x3uCciaLd2d8tTEVW1PKN6WWghtYqRJl+0fLeVXrSjIWA10YZQGse0JO76zMw2BLleWS33nNJJPZw3VvAYtceQDvjV1gynVZivXdyCW/AGrhnTkx0jw+i+x3RN4pGnkoXag2l6DUJEbBZpMnmt5qvr7FcSPooWajEfqIPS2yEe0QP9630Z4CgbVtt+xOjUY4VJDbOVgG2WJrXbojGiLmEAezx7MXVEZ x-forefront-antispam-report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CH2PR10MB4344.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230016)(396003)(366004)(136003)(39860400002)(376002)(346002)(6506007)(71200400001)(966005)(6512007)(478600001)(41300700001)(6486002)(83380400001)(2616005)(186003)(2906002)(21615005)(44832011)(5660300002)(8936002)(30864003)(54906003)(110136005)(316002)(66556008)(66476007)(4326008)(8676002)(64756008)(66946007)(66446008)(76116006)(91956017)(33656002)(38100700002)(166002)(36756003)(122000001)(86362001)(38070700005)(84970400001)(45980500001)(579004)(559001); DIR:OUT; SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?hcS0S+mPDYYOAucdJDIS2cU65xz5DRMhhEaXzlckCWH4Iusv7pKFvcTdlyo6?= =?us-ascii?Q?NaPvQB60VBBuM4TI8I1wp0RROk6hgkQXrreSFJd+5uCGPPos7VGl24TyzvWi?= =?us-ascii?Q?fcBWZF/2MHcMY1gLddzSy0vG+tOuTRb1ziCvomHiwh0Hofou3PscSavbsGC0?= =?us-ascii?Q?HEgn5F+0qbldMlhAZ4xrke0DPv6GfenrHrMGVMBZ/LKhEPBLEsBbEDqM2ukG?= =?us-ascii?Q?82DCgQXWBQGztEcLQSypEZJc5zdmPRaG17CqgQW+a6TmqX8otXj86twBvGrj?= =?us-ascii?Q?ayfh1PK9rIkO+2epcTYmKuUMH/ay04i4U41hxMXD16TAFLAn5FE5Ku7zUwU2?= =?us-ascii?Q?7I2Flyhip6yZ2cLX8NJShr+Y9saUaa/IXgQJvtzDbCM7/dVymvnbkuOLwX2Z?= =?us-ascii?Q?S5rr9wNwjytDnA0ugNReNEE14EuPtGZJhelcwRhGh0t3Xzvn0dP8rrtbLPM+?= =?us-ascii?Q?Wnn/lcPqz/YIoMW6yNKrXWxwb58maef6j90Vo448TQFolSuD7Dr2vTMxo0Wa?= =?us-ascii?Q?8QTGPApObte3/Bj1jY/quvtS3f7RERiDnmKp/qGktQ2lFMKs8dq0ovSVh2xx?= =?us-ascii?Q?7DY+eIorpO7eF1EdbEmSM+hjxbeJmG7RKJdUNHgFtLEzC/2pKIx9ABcB7Mdo?= =?us-ascii?Q?F9BMndOz6wJYgdT9OA+ngbaespzpWshbwkYkk16ziHqriyg3rpdQE++WM8Zx?= =?us-ascii?Q?DSdoy7niLqJevtesQGUu5p9aKutYQCjzQkvKqTEdi97/26hny6IrOjmXzvo7?= =?us-ascii?Q?qUBrRhUglwIXSmJvN+NaEu5L9PSVyFHcCUEJYROTYo9iKbycMRiAsNhryPuE?= =?us-ascii?Q?H0F83LpIeUBoaItVxZlw1dKvCLgRCYlD/AON2y1aEllfl3eIhUVsKUfR6LnF?= =?us-ascii?Q?EjRp0qu9RS+JOICxhJ2q74ZTcbGrk4YPXWp9c38r7Fv8VA015dLAluTgbE8E?= =?us-ascii?Q?OkaRdw0SMEQceDmk1Maz1bhchclPdA7StzdEpzFbFUbcDSJh8hm+E+W7xJRe?= =?us-ascii?Q?g7XbZIjjuocrj55yGuzaaG+Yafk4K96dIByHJ7RUeykub78P9MYJ/8SeVRMw?= =?us-ascii?Q?GyCPiET6WhZlFIWmfbOa4R5bs4ZzLTwGo9ezhNqqx9cMhy1G/ZYjKI1Zq7qU?= =?us-ascii?Q?jvs9VBd1DjLeShH4nD7QO0tSCpjfOWx6aK9gHoDy4YusafNlmHKbbDhjQsjN?= =?us-ascii?Q?LLX5XOndX/p09dhdtqyJePfwUeyD97N9nLMNkXwNkSWGjnBnhVkb4wHsX2Gn?= =?us-ascii?Q?x2iVD9jZgxhAZtGduXzbGoJbBdebgt9AKObbX8KKvfGDekFMKQjb5OkukNi5?= =?us-ascii?Q?M7EZXr0zTv81ZNPv1cksXVPXRKWF9+bbucLELZuMtGHHnsHfnXRXgT5mpuu7?= =?us-ascii?Q?ofSwYkCNAv/q0ydoWUAsAsNI7aMWXgvrDeEKDn9WdnrkH8F7EtetM5kFwen5?= =?us-ascii?Q?BwBltgbJx8+z/daeLdp2PIqcWlsW0hHqNMtpG00QG0Jza7HFGj0GRt1mv+2Z?= =?us-ascii?Q?yR4G003O+tHyFFyOAga+B2xB7YEjBj31GpGrLCIDGzDDjK21vxbkloJAffAU?= =?us-ascii?Q?t++EGtye+7c7PUyOAXjzabevuA3uC0pRDUe9HcyekIOof6bpAwRsEggqKX4G?= =?us-ascii?Q?B/L7/AugQN6aUbVkD+pYTXI=3D?= MIME-Version: 1.0 X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: a22d4564-62c4-4f08-27ed-08da8ac6837f X-MS-Exchange-CrossTenant-originalarrivaltime: 30 Aug 2022 20:30:44.0027 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: RvF1HdvwdigVwaBaH7iWOh5WBXWhbXdaeqW7ngtaJiHTnUVfjtA1tthNGNa/yh/qDkUgc8nAuoHDiNAy3TaNdw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLAPR10MB5265 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.517,FMLib:17.11.122.1 definitions=2022-08-30_11,2022-08-30_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 suspectscore=0 mlxlogscore=999 adultscore=0 phishscore=0 spamscore=0 bulkscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2207270000 definitions=main-2208300091 X-Proofpoint-ORIG-GUID: 6Jt0SAwPCRILzPzgdoPHfU0E8NNoiNyY X-Proofpoint-GUID: 6Jt0SAwPCRILzPzgdoPHfU0E8NNoiNyY X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, HTML_MESSAGE, KAM_SHORT, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable X-Content-Filtered-By: Mailman/MimeDel 2.1.29 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: , X-List-Received-Date: Tue, 30 Aug 2022 20:31:06 -0000 Hi, Joseph and Nathan, Could you please review the C and C++ FE parts of the patch? https://gcc.gnu.org/pipermail/gcc-patches/2022-August/599901.html The middle-end changes have been approved by Richard already. https://gcc.gnu.org/pipermail/gcc-patches/2022-August/600379.html Thanks. Begin forwarded message: From: Qing Zhao > Subject: [[GCC13][Patch][V3] 1/2] Add a new option -fstrict-flex-array[=3Dn= ] and new attribute strict_flex_array Date: August 17, 2022 at 10:40:41 AM EDT To: gcc-patches@gcc.gnu.org Cc: rguenther@suse.de, jakub@redhat.com, msebor@gmail.com, keescook@chromi= um.org, joseph@codesourcery.com, Qing Zhao > Add the following new option -fstrict-flex-array[=3Dn] and a corresponding attribute strict_flex_array to GCC: '-fstrict-flex-array' Treat the trailing array of a structure as a flexible array member in a stricter way. The positive form is equivalent to '-fstrict-flex-array=3D3', which is the strictest. A trailing array is treated as a flexible array member only when it is declared as a flexible array member per C99 standard onwards. The negative form is equivalent to '-fstrict-flex-array=3D0', which is the least strict. All trailing arrays of structures are treated as flexible array members. '-fstrict-flex-array=3DLEVEL' Treat the trailing array of a structure as a flexible array member in a stricter way. The value of LEVEL controls the level of strictness. The possible values of LEVEL are the same as for the 'strict_flex_array' attribute (*note Variable Attributes::). You can control this behavior for a specific trailing array field of a structure by using the variable attribute 'strict_flex_array' attribute (*note Variable Attributes::). This option is only valid when flexible array member is supported in th= e language. FOR ISO C before C99 and ISO C++, no language support for the= flexible array member at all, this option will be invalid and a warning will be = issued. When -std=3Dgnu89 is specified or C++ with GNU extension, only zero-len= gth array extension and one-size array are supported, as a result, LEVEL=3D3 will= be invalid and a warning will be issued. 'strict_flex_array (LEVEL)' The 'strict_flex_array' attribute should be attached to the trailing array field of a structure. It specifies the level of strictness of treating the trailing array field of a structure as a flexible array member. LEVEL must be an integer betwen 0 to 3. LEVEL=3D0 is the least strict level, all trailing arrays of structures are treated as flexible array members. LEVEL=3D3 is the strictest level, only when the trailing array is declared as a flexible array member per C99 standard onwards ([]), it is treated as a flexible array member. There are two more levels in between 0 and 3, which are provided to support older codes that use GCC zero-length array extension ([0]) or one-size array as flexible array member ([1]): When LEVEL is 1, the trailing array is treated as a flexible array member when it is declared as either [], [0], or [1]; When LEVEL is 2, the trailing array is treated as a flexible array member when it is declared as either [], or [0]. This attribute can be used with or without '-fstrict-flex-array'. When both the attribute and the option present at the same time, the level of the strictness for the specific trailing array field is determined by the attribute. This attribute is only valid when flexible array member is supported in= the language. For ISO C before C99 and ISO C++, no language support for the= flexible array member at all, this attribute will be invalid and a warning is is= sued. When -std=3Dgnu89 is specified or C++ with GNU extension, only zero-len= gth array extension and one-size array are supported, as a result, LEVEL=3D3 will= be invalid and a warning is issued. gcc/c-family/ChangeLog: * c-attribs.cc (handle_strict_flex_arrays_attribute): = New function. (c_common_attribute_table): New item for strict_flex_array. * c-opts.cc (c_common_post_options): Handle the combinati= on of -fstrict-flex-arrays and -std specially. * c.opt: (fstrict-flex-array): New option. (fstrict-flex-array=3D): New option. gcc/c/ChangeLog: * c-decl.cc (flexible_array_member_type_p): New function. (one_element_array_type_p): Likewise. (zero_length_array_type_p): Likewise. (add_flexible_array_elts_to_size): Call new utility routine flexible_array_member_type_p. (is_flexible_array_member_p): New function. (finish_struct): Set the new DECL_NOT_FLEXARRAY flag. gcc/cp/ChangeLog: * module.cc (trees_out::core_bools): Stream out new bit decl_not_flexarray. (trees_in::core_bools): Stream in new bit decl_not_flexarray. gcc/ChangeLog: * doc/extend.texi: Document strict_flex_array attribute. * doc/invoke.texi: Document -fstrict-flex-array[=3Dn] option. * print-tree.cc (print_node): Print new bit decl_not_= flexarray. * tree-core.h (struct tree_decl_common): New bit field decl_not_flexarray. * tree-streamer-in.cc (unpack_ts_decl_common_va= lue_fields): Stream in new bit decl_not_flexarray. * tree-streamer-out.cc (pack_ts_decl_common_va= lue_fields): Stream out new bit decl_not_flexarray. * tree.cc (array_at_struct_end_p): Update it with the new b= it field decl_not_flexarray. * tree.h (DECL_NOT_FLEXARRAY): New flag. gcc/testsuite/ChangeLog: * g++.dg/strict-flex-array-1.C: New test. * g++.dg/strict-flex-array-2.C: New test. * g++.dg/strict-flex-array-3.C: New test. * g++.dg/strict-flex-array-4.C: New test. * gcc.dg/strict-flex-array-1.c: New test. * gcc.dg/strict-flex-array-2.c: New test. * gcc.dg/strict-flex-array-3.c: New test. * gcc.dg/strict-flex-array-4.c: New test. --- gcc/c-family/c-attribs.cc | 94 +++++= ++++++++++ gcc/c-family/c-opts.cc | 41 +++++++ gcc/c-family/c.opt | 7 ++ gcc/c/c-decl.cc | 130 ++++++++= +++++++++++-- gcc/cp/module.cc | 2 + gcc/doc/extend.texi | 33 ++++++ gcc/doc/invoke.texi | 34 +++++- gcc/print-tree.cc | 8 +- gcc/testsuite/g++.dg/strict-flex-array-1.C | 31 +++++ gcc/testsuite/g++.dg/strict-flex-array-2.C | 16 +++ gcc/testsuite/g++.dg/strict-flex-array-3.C | 21 ++++ gcc/testsuite/g++.dg/strict-flex-array-4.C | 9 ++ gcc/testsuite/gcc.dg/strict-flex-array-1.c | 31 +++++ gcc/testsuite/gcc.dg/strict-flex-array-2.c | 15 +++ gcc/testsuite/gcc.dg/strict-flex-array-3.c | 21 ++++ gcc/testsuite/gcc.dg/strict-flex-array-4.c | 10 ++ gcc/tree-core.h | 5 +- gcc/tree-streamer-in.cc | = 1 + gcc/tree-streamer-out.cc | = 1 + gcc/tree.cc | 45 +++++-- gcc/tree.h | 14 ++- 21 files changed, 541 insertions(+), 28 deletions(-) create mode 100644 gcc/testsuite/g++.dg/strict-flex-array-1.C create mode 100644 gcc/testsuite/g++.dg/strict-flex-array-2.C create mode 100644 gcc/testsuite/g++.dg/strict-flex-array-3.C create mode 100644 gcc/testsuite/g++.dg/strict-flex-array-4.C create mode 100644 gcc/testsuite/gcc.dg/strict-flex-array-1.c create mode 100644 gcc/testsuite/gcc.dg/strict-flex-array-2.c create mode 100644 gcc/testsuite/gcc.dg/strict-flex-array-3.c create mode 100644 gcc/testsuite/gcc.dg/strict-flex-array-4.c diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/= c-attribs.cc index e4f1d3542f37..9c9927cefa0d 100644 --- a/gcc/c-family/c-attribs.cc +++ b/gcc/c-family/c-attribs.cc @@ -101,6 +101,8 @@ static tree handle_special_var_sec_attribute (tree *, t= ree, tree, int, bool *); static tree handle_aligned_attribute (tree *, tree, tree, int, bool *); static tree handle_warn_if_not_aligned_attribute (tree *, tree, tree, int, bool *); +static tree handle_strict_flex_arrays_attribute (tree *, tree, tree, + int, bool *); static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ; static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ; static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *)= ; @@ -368,6 +370,8 @@ const struct attribute_spec c_common_attribute_table[] = =3D attr_aligned_exclusions }, { "warn_if_not_aligned", 0, 1, false, false, false, false, handle_warn_if_not_aligned_attribute, NULL }, + { "strict_flex_arrays", 1, 1, false, false, false, false, + handle_strict_flex_arrays_attribute, NULL }, { "weak", 0, 0, true, false, false, false, handle_weak_attribute, NULL }, { "noplt", 0, 0, true, false, false, false, @@ -2505,6 +2509,96 @@ handle_warn_if_not_aligned_attribute (tree *node, tr= ee name, no_add_attrs, true); } +/* Handle a "strict_flex_arrays" attribute; arguments as in + struct attribute_spec.handler. */ + +static tree +handle_strict_flex_arrays_attribute (tree *node, tree name, + tree args, int ARG_UNUSED (flags), + bool *no_add_attrs) +{ + tree decl =3D *node; + tree argval =3D TREE_VALUE (args); + + /* This attribute only applies to field decls of a structure. */ + if (TREE_CODE (decl) !=3D FIELD_DECL) + { + error_at (DECL_SOURCE_LOCATION (decl), + "%qE attribute may not be specified for %q+D", name, decl); + *no_add_attrs =3D true; + } + /* This attribute only applies to field with array type. */ + else if (TREE_CODE (TREE_TYPE (decl)) !=3D ARRAY_TYPE) + { + error_at (DECL_SOURCE_LOCATION (decl), + "%qE attribute may not be specified for a non array field", + name); + *no_add_attrs =3D true; + } + else if (TREE_CODE (argval) !=3D INTEGER_CST) + { + error_at (DECL_SOURCE_LOCATION (decl), + "%qE attribute argument not an integer", name); + *no_add_attrs =3D true; + } + else if (!tree_fits_uhwi_p (argval) || tree_to_uhwi (argval) > 3) + { + error_at (DECL_SOURCE_LOCATION (decl), + "%qE attribute argument %qE is not an integer constant" + " between 0 and 3", name, argval); + *no_add_attrs =3D true; + } + else + { + unsigned int level =3D tree_to_uhwi (argval); + /* check whether the attribute is valid based on language standard. + the attribute is only valid when flexible array member is + supported in the language. Therefore, we should invalid this attribute or + specific level of this attribute for the following situations: + A. When -std=3Dc89 is specified, no language support at all, invalid this + attribute and issue a warning; + B. When -std=3Dgnu89 is specified, only zero-length array extension and + one-size array are supported, level=3D3 will be invalid and a warning + will be issued. + C. C++ without GNU extension, no language support at all, invalid this + attribute and issue a warning; + D. C++ with GNU extension, only zero-length array extension and one-size + array are supported, level=3D3 will be invalid and a warning will be + issued. */ + if (level > 0) + { + if (!c_dialect_cxx () && flag_iso && flag_isoc99 =3D=3D 0) + { + warning (OPT_Wattributes, "%qE attribute ignored since it is " + "not supported with a ISO C before C99", name); + *no_add_attrs =3D true; + } + else if (!c_dialect_cxx () && !flag_iso + && flag_isoc99 =3D=3D 0 && level =3D=3D 3) + { + warning (OPT_Wattributes, "%qE =3D 3 attribute ignored since it is " + "not supported with a GNU extension GNU89", name); + *no_add_attrs =3D true; + } + else if (c_dialect_cxx () && flag_iso) + { + warning (OPT_Wattributes, "%qE attribute ignored since it is " + "not supported with a ISO C++", name); + *no_add_attrs =3D true; + } + else if (c_dialect_cxx () && !flag_iso + && level =3D=3D 3) + { + warning (OPT_Wattributes, "%qE =3D 3 attribute ignored since it is " + "not supported for C++ with GNU extension", name); + *no_add_attrs =3D true; + } + } + } + + return NULL_TREE; +} + /* Handle a "weak" attribute; arguments as in struct attribute_spec.handler. */ diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts= .cc index 4e1463689de3..639eb40f3c86 100644 --- a/gcc/c-family/c-opts.cc +++ b/gcc/c-family/c-opts.cc @@ -870,6 +870,47 @@ c_common_post_options (const char **pfilename) SET_OPTION_IF_UNSET (&global_options, &global_options_set, flag_tree_loop_distribute_patterns, 0); + /* -fstrict-flex-arrays is only valid when flexible array member is + supported in the language. Therefore, we should invalid this option o= r + specific level of this option for the following situations: + A. When -std=3Dc89 is specified, no language support at all, invalid = this + option and issue a warning; + B. When -std=3Dgnu89 is specified, only zero-length array extension a= nd + one-size array are supported, level=3D3 will be invalid and a warning + will be issued. + C. C++ without GNU extension, no language support at all, invalid thi= s + option and issue a warning; + D. C++ with GNU extension, only zero-length array extension and one-s= ize + array are supported, level=3D3 will be invalid and a warning will be + issued. */ + if (flag_strict_flex_arrays > 0) + { + if (!c_dialect_cxx () && flag_iso && flag_isoc99 =3D=3D 0) + { + flag_strict_flex_arrays =3D 0; + warning (0, "%<-fstrict-flex-arrays%> is not supported with a ISO C " + "before C99, ignored"); + } + else if (!c_dialect_cxx () && !flag_iso && flag_isoc99 =3D=3D 0 && f= lag_strict_flex_arrays =3D=3D 3) + { + flag_strict_flex_arrays =3D 0; + warning (0, "%<-fstrict-flex-arrays=3D3%> is not supported with a " + "GNU extension GNU89, ignored"); + } + else if (c_dialect_cxx () && flag_iso) + { + flag_strict_flex_arrays =3D 0; + warning (0, "%<-fstrict-flex-arrays%> is not supported with a ISO " + "C++, ignored"); + } + else if (c_dialect_cxx () && !flag_iso && flag_strict_flex_arrays = =3D=3D 3) + { + flag_strict_flex_arrays =3D 0; + warning (0, "%<-fstrict-flex-arrays=3D3%> is not supported for C++ " + "with GNU extension, ignored"); + } + } + /* -Woverlength-strings is off by default, but is enabled by -Wpedantic. It is never enabled in C++, as the minimum limit is not normative in that standard. */ diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 44e1a60ce246..1e944f8a3055 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -2060,6 +2060,13 @@ fsized-deallocation C++ ObjC++ Var(flag_sized_deallocation) Init(-1) Enable C++14 sized deallocation support. +fstrict-flex-arrays +C C++ Common Alias(fstrict-flex-arrays=3D,3,0) + +fstrict-flex-arrays=3D +C C++ Common Joined RejectNegative UInteger Var(flag_strict_flex_arrays) I= nit(0) IntegerRange(0,3) +-fstrict-flex-arrays=3D Treat the trailing array of a structure = as a flexible array in a stricter way. The default is treating all trailin= g arrays of structures as flexible arrays. + fsquangle C++ ObjC++ WarnRemoved diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index ae8990c138fd..a2e125d4ddd5 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -4999,6 +4999,41 @@ set_array_declarator_inner (struct c_declarator *dec= l, return decl; } +/* Determine whether TYPE is a ISO C99 flexible array memeber type "[]". = */ +static bool +flexible_array_member_type_p (const_tree type) +{ + if (TREE_CODE (type) =3D=3D ARRAY_TYPE + && TYPE_SIZE (type) =3D=3D NULL_TREE + && TYPE_DOMAIN (type) !=3D NULL_TREE + && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) =3D=3D NULL_TREE) + return true; + + return false; +} + +/* Determine whether TYPE is a one-element array type "[1]". */ +static bool +one_element_array_type_p (const_tree type) +{ + if (TREE_CODE (type) !=3D ARRAY_TYPE) + return false; + return integer_zerop (array_type_nelts (type)); +} + +/* Determine whether TYPE is a zero-length array type "[0]". */ +static bool +zero_length_array_type_p (const_tree type) +{ + if (TREE_CODE (type) =3D=3D ARRAY_TYPE) + if (tree type_size =3D TYPE_SIZE_UNIT (type)) + if ((integer_zerop (type_size)) + && TYPE_DOMAIN (type) !=3D NULL_TREE + && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) =3D=3D NULL_TREE) + return true; + return false; +} + /* INIT is a constructor that forms DECL's initializer. If the final element initializes a flexible array field, add the size of that initializer to DECL's size. */ @@ -5013,10 +5048,7 @@ add_flexible_array_elts_to_size (tree decl, tree ini= t) elt =3D CONSTRUCTOR_ELTS (init)->last ().value; type =3D TREE_TYPE (elt); - if (TREE_CODE (type) =3D=3D ARRAY_TYPE - && TYPE_SIZE (type) =3D=3D NULL_TREE - && TYPE_DOMAIN (type) !=3D NULL_TREE - && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) =3D=3D NULL_TREE) + if (flexible_array_member_type_p (type)) { complete_array_type (&type, elt, false); DECL_SIZE (decl) @@ -8720,6 +8752,81 @@ finish_incomplete_vars (tree incomplete_vars, bool t= oplevel) } } + +/* Determine whether the FIELD_DECL X is a flexible array member according= to + the following info: + A. whether the FIELD_DECL X is the last field of the DECL_CONTEXT; + B. whether the FIELD_DECL is an array that is declared as "[]", "[0]", + or "[1]"; + C. flag_strict_flex_arrays; + D. the attribute strict_flex_array that is attached to the field + if presenting. + Return TRUE when it's a flexible array member, FALSE otherwise. */ + +static bool +is_flexible_array_member_p (bool is_last_field, + tree x) +{ + /* if not the last field, return false. */ + if (!is_last_field) + return false; + + /* if not an array field, return false. */ + if (TREE_CODE (TREE_TYPE (x)) !=3D ARRAY_TYPE) + return false; + + bool is_zero_length_array =3D zero_length_array_type_p (TREE_TYPE (x)); + bool is_one_element_array =3D one_element_array_type_p (TREE_TYPE (x)); + bool is_flexible_array =3D flexible_array_member_type_p (TREE_TYPE (x)); + + unsigned int strict_flex_array_level =3D flag_strict_flex_arrays; + + tree attr_strict_flex_array =3D lookup_attribute ("strict_flex_arrays", + DECL_ATTRIBUTES (x)); + /* if there is a strict_flex_array attribute attached to the field, + override the flag_strict_flex_arrays. */ + if (attr_strict_flex_array) + { + /* get the value of the level first from the attribute. */ + unsigned HOST_WIDE_INT attr_strict_flex_array_level =3D 0; + gcc_assert (TREE_VALUE (attr_strict_flex_array) !=3D NULL_TREE); + attr_strict_flex_array =3D TREE_VALUE (attr_strict_flex_array); + gcc_assert (TREE_VALUE (attr_strict_flex_array) !=3D NULL_TREE); + attr_strict_flex_array =3D TREE_VALUE (attr_strict_flex_array); + gcc_assert (tree_fits_uhwi_p (attr_strict_flex_array)); + attr_strict_flex_array_level =3D tree_to_uhwi (attr_strict_flex_arra= y); + + /* the attribute has higher priority than flag_struct_flex_array. *= / + strict_flex_array_level =3D attr_strict_flex_array_level; + } + + switch (strict_flex_array_level) + { + case 0: + /* default, all trailing arrays are flexiable array members. */ + return true; + case 1: + /* Level 1: all "[1]", "[0]", and "[]" are flexiable array members. */ + if (is_one_element_array) + return true; + /* FALLTHROUGH. */ + case 2: + /* Level 2: all "[0]", and "[]" are flexiable array members. */ + if (is_zero_length_array) + return true; + /* FALLTHROUGH. */ + case 3: + /* Level 3: Only "[]" are flexible array members. */ + if (is_flexible_array) + return true; + break; + default: + gcc_unreachable (); + } + return false; +} + + /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T. LOC is the location of the RECORD_TYPE or UNION_TYPE's definition. FIELDLIST is a chain of FIELD_DECL nodes for the fields. @@ -8781,6 +8888,11 @@ finish_struct (location_t loc, tree t, tree fieldlis= t, tree attributes, bool saw_named_field =3D false; for (x =3D fieldlist; x; x =3D DECL_CHAIN (x)) { + /* whether this field is the last field of the structure or union. + for UNION, any field is the last field of it. */ + bool is_last_field =3D (DECL_CHAIN (x) =3D=3D NULL_TREE) + || (TREE_CODE (t) =3D=3D UNION_TYPE); + if (TREE_TYPE (x) =3D=3D error_mark_node) continue; @@ -8819,10 +8931,7 @@ finish_struct (location_t loc, tree t, tree fieldlis= t, tree attributes, DECL_PACKED (x) =3D 1; /* Detect flexible array member in an invalid context. */ - if (TREE_CODE (TREE_TYPE (x)) =3D=3D ARRAY_TYPE - && TYPE_SIZE (TREE_TYPE (x)) =3D=3D NULL_TREE - && TYPE_DOMAIN (TREE_TYPE (x)) !=3D NULL_TREE - && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) =3D=3D NULL_TREE) + if (flexible_array_member_type_p (TREE_TYPE (x))) { if (TREE_CODE (t) =3D=3D UNION_TYPE) { @@ -8830,7 +8939,7 @@ finish_struct (location_t loc, tree t, tree fieldlist= , tree attributes, "flexible array member in union"); TREE_TYPE (x) =3D error_mark_node; } - else if (DECL_CHAIN (x) !=3D NULL_TREE) + else if (!is_last_field) { error_at (DECL_SOURCE_LOCATION (x), "flexible array member not at end of struct"); @@ -8850,6 +8959,9 @@ finish_struct (location_t loc, tree t, tree fieldlist= , tree attributes, pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic, "invalid use of structure with flexible array member"); + /* Set DECL_NOT_FLEXARRAY flag for FIELD_DECL x. */ + DECL_NOT_FLEXARRAY (x) =3D !is_flexible_array_member_p (is_last_fiel= d, x); + if (DECL_NAME (x) || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))) saw_named_field =3D true; diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index f27f4d091e5e..75ee2514f66b 100644 --- a/gcc/cp/module.cc +++ b/gcc/cp/module.cc @@ -5414,6 +5414,7 @@ trees_out::core_bools (tree t) WB (t->decl_common.decl_by_reference_flag); WB (t->decl_common.decl_read_flag); WB (t->decl_common.decl_nonshareable_flag); + WB (t->decl_common.decl_not_flexarray); } if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS)) @@ -5558,6 +5559,7 @@ trees_in::core_bools (tree t) RB (t->decl_common.decl_by_reference_flag); RB (t->decl_common.decl_read_flag); RB (t->decl_common.decl_nonshareable_flag); + RB (t->decl_common.decl_not_flexarray); } if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS)) diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 7fe7f8817cdd..99b43ed3852c 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -7473,6 +7473,39 @@ This warning can be disabled by @option{-Wno-if-not-= aligned}. The @code{warn_if_not_aligned} attribute can also be used for types (@pxref{Common Type Attributes}.) +@cindex @code{strict_flex_arrays} variable attribute +@item strict_flex_arrays (@var{level}) +The @code{strict_flex_arrays} attribute should be attached to the trailing +array field of a structure. It specifies the level of strictness of +treating the trailing array field of a structure as a flexible array +member. @var{level} must be an integer betwen 0 to 3. + +@var{level}=3D0 is the least strict level, all trailing arrays of structur= es +are treated as flexible array members. @var{level}=3D3 is the strictest le= vel, +only when the trailing array is declared as a flexible array member per C9= 9 +standard onwards ([]), it is treated as a flexible array member. + +There are two more levels in between 0 and 3, which are provided to suppor= t +older codes that use GCC zero-length array extension ([0]) or one-size arr= ay +as flexible array member ([1]): +When @var{level} is 1, the trailing array is treated as a flexible array m= ember +when it is declared as either "[]", "[0]", or "[1]"; +When @var{level} is 2, the trailing array is treated as a flexible array m= ember +when it is declared as either "[]", or "[0]". + +This attribute can be used with or without the @option{-fstrict-flex-array= s}. +When both the attribute and the option present at the same time, the level= of +the strictness for the specific trailing array field is determined by the +attribute. + +This attribute is only valid when flexible array member is supported in th= e +language. For ISO C before C99 and ISO C++, no language support for the fl= exible +array member at all, this attribute will be invalid and a warning is issue= d. +When -std=3Dgnu89 is specified or C++ with GNU extension, only zero-length= array +extension and one-size array are supported, as a result, @var{level}=3D3 w= ill be +invalid and a warning is issued. + + @item alloc_size (@var{position}) @itemx alloc_size (@var{position-1}, @var{position-2}) @cindex @code{alloc_size} variable attribute diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 863580b3710a..2a0a3cf3de10 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -207,7 +207,8 @@ in the following sections. -fopenmp -fopenmp-simd @gol -fpermitted-flt-eval-methods=3D@var{standard} @gol -fplan9-extensions -fsigned-bitfields -funsigned-bitfields @gol --fsigned-char -funsigned-char -fsso-struct=3D@var{endianness}} +-fsigned-char -funsigned-char -fstrict-flex-arrays[=3D@var{n}] @gol +-fsso-struct=3D@var{endianness}} @item C++ Language Options @xref{C++ Dialect Options,,Options Controlling C++ Dialect}. @@ -2826,6 +2827,37 @@ The type @code{char} is always a distinct type from = each of @code{signed char} or @code{unsigned char}, even though its behavior is always just like one of those two. +@item -fstrict-flex-arrays +@opindex fstrict-flex-arrays +@opindex fno-strict-flex-arrays +Treat the trailing array of a structure as a flexible array member in a +stricter way. +The positive form is equivalent to @option{-fstrict-flex-arrays=3D3}, whic= h is the +strictest. A trailing array is treated as a flexible array member only wh= en it +is declared as a flexible array member per C99 standard onwards. +The negative form is equivalent to @option{-fstrict-flex-arrays=3D0}, whic= h is the +least strict. All trailing arrays of structures are treated as flexible a= rray +members. + +@item -fstrict-flex-arrays=3D@var{level} +@opindex fstrict-flex-arrays=3D@var{level} +Treat the trailing array of a structure as a flexible array member in a +stricter way. The value of @var{level} controls the level of strictness. + +The possible values of @var{level} are the same as for the +@code{strict_flex_array} attribute (@pxref{Variable Attributes}). + +You can control this behavior for a specific trailing array field of a +structure by using the variable attribute @code{strict_flex_array} attribu= te +(@pxref{Variable Attributes}). + +This option is only valid when flexible array member is supported in the +language. FOR ISO C before C99 and ISO C++, no language support for the fl= exible +array member at all, this option will be invalid and a warning will be iss= ued. +When -std=3Dgnu89 is specified or C++ with GNU extension, only zero-length= array +extension and one-size array are supported, as a result, @var{level}=3D3 w= ill be +invalid and a warning will be issued. + @item -fsso-struct=3D@var{endianness} @opindex fsso-struct Set the default scalar storage order of structures and unions to the diff --git a/gcc/print-tree.cc b/gcc/print-tree.cc index 6d45a4a59669..58a98250cc4f 100644 --- a/gcc/print-tree.cc +++ b/gcc/print-tree.cc @@ -517,8 +517,12 @@ print_node (FILE *file, const char *prefix, tree node,= int indent, fprintf (file, " align:%d warn_if_not_align:%d", DECL_ALIGN (node), DECL_WARN_IF_NOT_ALIGN (node)); if (code =3D=3D FIELD_DECL) - fprintf (file, " offset_align " HOST_WIDE_INT_PRINT_UNSIGNED, - DECL_OFFSET_ALIGN (node)); + { + fprintf (file, " offset_align " HOST_WIDE_INT_PRINT_UNSIGNED, + DECL_OFFSET_ALIGN (node)); + fprintf (file, " decl_not_flexarray: %d", + DECL_NOT_FLEXARRAY (node)); + } if (code =3D=3D FUNCTION_DECL && fndecl_built_in_p (node)) { diff --git a/gcc/testsuite/g++.dg/strict-flex-array-1.C b/gcc/testsuite/g++= .dg/strict-flex-array-1.C new file mode 100644 index 000000000000..47adaf7bac4a --- /dev/null +++ b/gcc/testsuite/g++.dg/strict-flex-array-1.C @@ -0,0 +1,31 @@ +/* testing the correct usage of attribute strict_flex_array. */ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + + +int x __attribute__ ((strict_flex_arrays (1))); /* { dg-error "'strict_fle= x_arrays' attribute may not be specified for 'x'" } */ + +struct trailing { + int a; + int c __attribute ((strict_flex_arrays)); /* { dg-error "wrong number = of arguments specified for 'strict_flex_arrays' attribute" } */ +}; + +struct trailing_1 { + int a; + int b; + int c __attribute ((strict_flex_arrays (2))); /* { dg-error "'strict_f= lex_arrays' attribute may not be specified for a non array field" } */ +}; + +extern int d; + +struct trailing_array_2 { + int a; + int b; + int c[1] __attribute ((strict_flex_arrays (d))); /* { dg-error "'stric= t_flex_arrays' attribute argument not an integer" } */ +}; + +struct trailing_array_3 { + int a; + int b; + int c[0] __attribute ((strict_flex_arrays (5))); /* { dg-error "'stric= t_flex_arrays' attribute argument '5' is not an integer constant between 0 = and 3" } */ +}; diff --git a/gcc/testsuite/g++.dg/strict-flex-array-2.C b/gcc/testsuite/g++= .dg/strict-flex-array-2.C new file mode 100644 index 000000000000..245f8fe0bc73 --- /dev/null +++ b/gcc/testsuite/g++.dg/strict-flex-array-2.C @@ -0,0 +1,16 @@ +/* testing the correct usage of flag -fstrict_flex_array for C++. */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fstrict-flex-arrays -std=3Dc++98" } */ + +struct trailing_array { + int a; + int b; + int c[0] __attribute ((strict_flex_arrays (3))); /* { dg-warning "attr= ibute ignored since it is not supported with a ISO C\\+\\+" } */ +}; + +int foo(int a) +{ + return 0; +} + +/* { dg-warning "is not supported with a ISO C\\+\\+, ignored" "" { targe= t *-*-* } 0 } */ diff --git a/gcc/testsuite/g++.dg/strict-flex-array-3.C b/gcc/testsuite/g++= .dg/strict-flex-array-3.C new file mode 100644 index 000000000000..2a733a5bccf4 --- /dev/null +++ b/gcc/testsuite/g++.dg/strict-flex-array-3.C @@ -0,0 +1,21 @@ +/* testing the correct usage of flag -fstrict_flex_array for C++. */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fstrict-flex-arrays -std=3Dgnu++98" } */ +/* { dg-warning "'-fstrict-flex-arrays=3D3' is not supported for C\\+\\+ w= ith GNU extension, ignored" "" { target *-*-* } 0 } */ + +struct trailing_array { + int a; + int b; + int c[0] __attribute ((strict_flex_arrays (3))); /* { dg-warning "'str= ict_flex_arrays' =3D 3 attribute ignored since it is not supported for C\\+= \\+ with GNU extension" } */ +}; + +struct trailing_array_1 { + int a; + int b; + int c[0] __attribute ((strict_flex_arrays (2))); /* { dg-bogus "'stric= t_flex_arrays' =3D 3 attribute ignored since it is not supported for C\\+\\= + with GNU extension" } */ +}; + +int foo(int a) +{ + return a + 2; +} diff --git a/gcc/testsuite/g++.dg/strict-flex-array-4.C b/gcc/testsuite/g++= .dg/strict-flex-array-4.C new file mode 100644 index 000000000000..804e4cf459ef --- /dev/null +++ b/gcc/testsuite/g++.dg/strict-flex-array-4.C @@ -0,0 +1,9 @@ +/* testing the correct usage of flag -fstrict_flex_array. */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fstrict-flex-arrays=3D2 -std=3Dgnu++98" } */ +/* { dg-bogus "is not supported for C\\+\\+ with GNU extension, ignored" "= " { target *-*-* } 0 } */ + +int foo(int a) +{ + return a + 2; +} diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-1.c b/gcc/testsuite/gcc= .dg/strict-flex-array-1.c new file mode 100644 index 000000000000..47adaf7bac4a --- /dev/null +++ b/gcc/testsuite/gcc.dg/strict-flex-array-1.c @@ -0,0 +1,31 @@ +/* testing the correct usage of attribute strict_flex_array. */ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + + +int x __attribute__ ((strict_flex_arrays (1))); /* { dg-error "'strict_fle= x_arrays' attribute may not be specified for 'x'" } */ + +struct trailing { + int a; + int c __attribute ((strict_flex_arrays)); /* { dg-error "wrong number = of arguments specified for 'strict_flex_arrays' attribute" } */ +}; + +struct trailing_1 { + int a; + int b; + int c __attribute ((strict_flex_arrays (2))); /* { dg-error "'strict_f= lex_arrays' attribute may not be specified for a non array field" } */ +}; + +extern int d; + +struct trailing_array_2 { + int a; + int b; + int c[1] __attribute ((strict_flex_arrays (d))); /* { dg-error "'stric= t_flex_arrays' attribute argument not an integer" } */ +}; + +struct trailing_array_3 { + int a; + int b; + int c[0] __attribute ((strict_flex_arrays (5))); /* { dg-error "'stric= t_flex_arrays' attribute argument '5' is not an integer constant between 0 = and 3" } */ +}; diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-2.c b/gcc/testsuite/gcc= .dg/strict-flex-array-2.c new file mode 100644 index 000000000000..967a240040dc --- /dev/null +++ b/gcc/testsuite/gcc.dg/strict-flex-array-2.c @@ -0,0 +1,15 @@ +/* testing the correct usage of flag -fstrict_flex_array. */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fstrict-flex-arrays -std=3Dc89" } */ +/* { dg-warning "'-fstrict-flex-arrays' is not supported with a ISO C befo= re C99, ignored" "" { target *-*-* } 0 } */ + +struct trailing_array { + int a; + int b; + int c[0] __attribute ((strict_flex_arrays (3))); /* { dg-warning "'str= ict_flex_arrays' attribute ignored since it is not supported with a ISO C b= efore C99" } */ +}; + +int foo(int a) +{ + return a + 2; +} diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-3.c b/gcc/testsuite/gcc= .dg/strict-flex-array-3.c new file mode 100644 index 000000000000..879de7f203c8 --- /dev/null +++ b/gcc/testsuite/gcc.dg/strict-flex-array-3.c @@ -0,0 +1,21 @@ +/* testing the correct usage of flag -fstrict_flex_array. */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fstrict-flex-arrays -std=3Dgnu89" } */ +/* { dg-warning "'-fstrict-flex-arrays=3D3' is not supported with a GNU ex= tension GNU89, ignored" "" { target *-*-* } 0 } */ + +struct trailing_array { + int a; + int b; + int c[0] __attribute ((strict_flex_arrays (3))); /* { dg-warning "'str= ict_flex_arrays' =3D 3 attribute ignored since it is not supported with a G= NU extension GNU89" } */ +}; + +struct trailing_array_1 { + int a; + int b; + int c[0] __attribute ((strict_flex_arrays (2))); /* { dg-bogus "'stric= t_flex_arrays' =3D 3 attribute ignored since it is not supported with a GNU= extension GNU89" } */ +}; + +int foo(int a) +{ + return a + 2; +} diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-4.c b/gcc/testsuite/gcc= .dg/strict-flex-array-4.c new file mode 100644 index 000000000000..ce64c24db301 --- /dev/null +++ b/gcc/testsuite/gcc.dg/strict-flex-array-4.c @@ -0,0 +1,10 @@ +/* testing the correct usage of flag -fstrict_flex_array. */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fstrict-flex-arrays=3D2 -std=3Dgnu89" } */ +/* { dg-bogus "is not supported with a GNU extension GNU89, ignored" "" {= target *-*-* } 0 } */ + + +int foo(int a) +{ + return a + 2; +} diff --git a/gcc/tree-core.h b/gcc/tree-core.h index 86a07c282af2..f822cb539dd0 100644 --- a/gcc/tree-core.h +++ b/gcc/tree-core.h @@ -1813,7 +1813,10 @@ struct GTY(()) tree_decl_common { TYPE_WARN_IF_NOT_ALIGN. */ unsigned int warn_if_not_align : 6; - /* 14 bits unused. */ + /* In FIELD_DECL, this is DECL_NOT_FLEXARRAY. */ + unsigned int decl_not_flexarray : 1; + + /* 13 bits unused. */ /* UID for points-to sets, stable over copying from inlining. */ unsigned int pt_uid; diff --git a/gcc/tree-streamer-in.cc b/gcc/tree= -streamer-in.cc index 196f19c759f2..21e6e8eb1c0a 100644 --- a/gcc/tree-streamer-in.cc +++ b/gcc/tree-streamer-in.cc @@ -261,6 +261,7 @@ unpack_ts_decl_common_value_fields (struct bitpack_d *b= p, tree expr) else SET_DECL_FIELD_ABI_IGNORED (expr, val); expr->decl_common.off_align =3D bp_unpack_value (bp, 8); + DECL_NOT_FLEXARRAY (expr) =3D (unsigned) bp_unpack_value (bp, 1); } else if (VAR_P (expr)) diff --git a/gcc/tree-streamer-out.cc b/gcc/tr= ee-streamer-out.cc index d39dc158a465..68e40dbdb8f2 100644 --- a/gcc/tree-streamer-out.cc +++ b/gcc/tree-streamer-out.cc @@ -229,6 +229,7 @@ pack_ts_decl_common_value_fields (struct bitpack_d *bp,= tree expr) else bp_pack_value (bp, DECL_FIELD_ABI_IGNORED (expr), 1); bp_pack_value (bp, expr->decl_common.off_align, 8); + bp_pack_value (bp, DECL_NOT_FLEXARRAY (expr), 1); } else if (VAR_P (expr)) diff --git a/gcc/tree.cc b/gcc/tree.cc index fed1434d141d..d698e8c9c213 100644 --- a/gcc/tree.cc +++ b/gcc/tree.cc @@ -12678,14 +12678,30 @@ array_ref_up_bound (tree exp) } /* Returns true if REF is an array reference, component reference, - or memory reference to an array at the end of a structure. - If this is the case, the array may be allocated larger - than its upper bound implies. */ + or memory reference to an array whose actual size might be larger + than its upper bound implies, there are multiple cases: + A. a ref to a flexible array member at the end of a structure; + B. a ref to an array with a different type against the original decl; + for example: + short a[16] =3D { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,= 16 }; + (*((char(*)[16])&a[0]))[i+8] + + C. a ref to an array that was passed as a parameter; + for example: + + int test (uint8_t *p, uint32_t t[1][1], int n) { + for (int i =3D 0; i < 4; i++, p++) + t[i][0] =3D ...; + + FIXME, the name of this routine need to be changed to be more accurate.= */ bool array_at_struct_end_p (tree ref) { - tree atype; + /* the TYPE for this array referece. */ + tree atype =3D NULL_TREE; + /* the FIELD_DECL for the array field in the containing structure. */ + tree afield_decl =3D NULL_TREE; if (TREE_CODE (ref) =3D=3D ARRAY_REF || TREE_CODE (ref) =3D=3D ARRAY_RANGE_REF) @@ -12695,7 +12711,10 @@ array_at_struct_end_p (tree ref) } else if (TREE_CODE (ref) =3D=3D COMPONENT_REF && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 1))) =3D=3D ARRAY_TYPE) - atype =3D TREE_TYPE (TREE_OPERAND (ref, 1)); + { + atype =3D TREE_TYPE (TREE_OPERAND (ref, 1)); + afield_decl =3D TREE_OPERAND (ref, 1); + } else if (TREE_CODE (ref) =3D=3D MEM_REF) { tree arg =3D TREE_OPERAND (ref, 0); @@ -12707,6 +12726,7 @@ array_at_struct_end_p (tree ref) if (tree fld =3D last_field (argtype)) { atype =3D TREE_TYPE (fld); + afield_decl =3D fld; if (TREE_CODE (atype) !=3D ARRAY_TYPE) return false; if (VAR_P (arg) && DECL_SIZE (fld)) @@ -12760,13 +12780,16 @@ array_at_struct_end_p (tree ref) ref =3D TREE_OPERAND (ref, 0); } - /* The array now is at struct end. Treat flexible arrays as + gcc_assert (!afield_decl + || (afield_decl && TREE_CODE (afield_decl) =3D=3D FIELD_DECL)); + + /* The array now is at struct end. Treat flexible array member as always subject to extend, even into just padding constrained by an underlying decl. */ if (! TYPE_SIZE (atype) || ! TYPE_DOMAIN (atype) || ! TYPE_MAX_VALUE (TYPE_DOMAIN (atype))) - return true; + return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; /* If the reference is based on a declared entity, the size of the array is constrained by its given domain. (Do not trust commons PR/69368). = */ @@ -12788,9 +12811,9 @@ array_at_struct_end_p (tree ref) if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (atype))) !=3D INTEGER_CST || TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))) !=3D INTEGER_CST || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (atype))) !=3D INTEGER_= CST) - return true; + return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; if (! get_addr_base_and_unit_offset (ref_to_array, &offset)) - return true; + return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; /* If at least one extra element fits it is a flexarray. */ if (known_le ((wi::to_offset (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))) @@ -12798,12 +12821,12 @@ array_at_struct_end_p (tree ref) + 2) * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (atype))), wi::to_offset (DECL_SIZE_UNIT (ref)) - offset)) - return true; + return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; return false; } - return true; + return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; } /* Return a tree representing the offset, in bytes, of the field referenced diff --git a/gcc/tree.h b/gcc/tree.h index e6564aaccb7b..f911c0a46e69 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -2993,6 +2993,12 @@ extern void decl_value_expr_insert (tree, tree); #define DECL_PADDING_P(NODE) \ (FIELD_DECL_CHECK (NODE)->decl_common.decl_flag_3) +/* Used in a FIELD_DECL to indicate whether this field is not a flexible + array member. This is only valid for the last array type field of a + structure. */ +#define DECL_NOT_FLEXARRAY(NODE) \ + (FIELD_DECL_CHECK (NODE)->decl_common.decl_not_flexarray) + /* A numeric unique identifier for a LABEL_DECL. The UID allocation is dense, unique within any one function, and may be used to index arrays. If the value is -1, then no UID has been assigned. */ @@ -5531,10 +5537,10 @@ extern tree component_ref_field_offset (tree); returns null. */ enum struct special_array_member { - none, /* Not a special array member. */ - int_0, /* Interior array member with size zero. */ - trail_0, /* Trailing array member with size zero. */ - trail_1 /* Trailing array member with one element. */ + none, /* Not a special array member. */ + int_0, /* Interior array member with size zero. */ + trail_0, /* Trailing array member with size zero. */ + trail_1 /* Trailing array member with one element. */ }; /* Return the size of the member referenced by the COMPONENT_REF, using -- 2.31.1 From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by sourceware.org (Postfix) with ESMTPS id 6142238276C6 for ; Tue, 30 Aug 2022 20:30:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6142238276C6 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=oracle.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oracle.com Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 27UI4ErF002224; Tue, 30 Aug 2022 20:30:52 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : references : content-type : mime-version; s=corp-2022-7-12; bh=wPhMv4zQVc/zM7hAarJQ1JcQvWvHlGiD9he0Il6FumU=; b=EZ8Lisu9l7PAiPdfAaTAVqQJw9whuQxsdN1q82MHlGzf9f0oAEuf5/mcDF1g2g9ri9qa YaB9QHSnXVdHxMLAtepxu0JSWh8dXrTqaxB1/guegsiXM3pVqCirz4QjCk8wOpRbVYPz T5Usxkd7EflsZ5L2t2KHYiUqzX3LaBKK23o22yWatXkQhJ8pVkGR6jtw6FWoN92UUYEt GWT8rz2uMvu/EjW/+54C5vr2jt5k0uUZsNzCZXjyO/X5KuLyQp9LM+RMB1D1YXx7knmd DDm8+w4OVmiHLHA3j3yOlxZLElM0zvrDOhwdFcDRz8uWyqwT1eKE3o+MKmuetfSd7OX4 fA== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3j7b59ydry-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 30 Aug 2022 20:30:51 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 27UIXRw7038287; Tue, 30 Aug 2022 20:30:50 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2105.outbound.protection.outlook.com [104.47.55.105]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3j79q4bxau-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 30 Aug 2022 20:30:50 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lJe+wz3W+LTVDg7GyDreokMhFIH9jkiLjwIMpJilYlpIhdqFMu4htNETABENsDZ9XSd7lOvMhFssRbmcA4+nRPAuBJXToWWlhUa5PWKsVqbXdlY0lXOebYVBu0BSnK5Qn9rxaLTSWVhlVn9pqsdgh4gemfCKQj66Fgm5/n93Cu04QWGGRnW8a3iO/n80g03vDqXG6R6OuLiRem6yRI7KIPw/DFJ6Z9XND7akxMssaAJnOSieqhWHiKuXaiQxGnLbqzgpu22u9IveC4eTe+/kNWQw+tx390ckcxgVqXcv46mWv0pnfunTjiNUMNmsxyWEZH5WY+FpwHJNI5FrtpglIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=wPhMv4zQVc/zM7hAarJQ1JcQvWvHlGiD9he0Il6FumU=; b=TyFU466rWGmoKWS89dQWX6ayBcX8YrrGRcgmO1Rbmn60iX3zyHWBBI36JMyGGezrQorFOtIwdXDuV1Ak7PDeEMab4qVUokveUsC5uQFUHXEAitYlojeuXIFaK/mPzyGWvDmPco8ynTSZ4McmA7mAQ8MKOr+obM/dGbPyULCiaaCNjMO+HmZMrkEOPcfI9cr5NDGjQZg4GcaAuPY4DOkCF4FACT06fnlklFk7Gtt+g7Qa75gt/n2BcdTtDo2fAbnBndJ5LyMDuZwJfMQ0rQh/t9PdeYKTnYDr9M3IipdoNV5Dzi2WI2o7h954e5lFTJNwYyGmQjFmP98boZLeYYtpzQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=wPhMv4zQVc/zM7hAarJQ1JcQvWvHlGiD9he0Il6FumU=; b=rpBP++uQyK5vZ5nVhJ81tiLrFyWHkvAphxjIiA32cqn/eBqFAnnv/tsehnPLU3jNdcxnFu52a+R5HHz8vc5t0ERScmFHgcouQxD4dzU2SA9PTek61dSZ7Q9VT6T0nW6MO1Tga62pAedvFPk9ggh3XD64Zd6qD5FfKut2LPOx3P8= Received: from CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) by BLAPR10MB5265.namprd10.prod.outlook.com (2603:10b6:208:325::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5588.10; Tue, 30 Aug 2022 20:30:44 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::84af:9275:f1d7:de1]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::84af:9275:f1d7:de1%6]) with mapi id 15.20.5566.021; Tue, 30 Aug 2022 20:30:44 +0000 From: Qing Zhao To: "joseph@codesourcery.com" , Nathan Sidwell CC: gcc Patches , richard Biener , jakub Jelinek , martin Sebor , Kees Cook Subject: Fwd: [[GCC13][Patch][V3] 1/2] Add a new option -fstrict-flex-array[=n] and new attribute strict_flex_array Thread-Topic: [[GCC13][Patch][V3] 1/2] Add a new option -fstrict-flex-array[=n] and new attribute strict_flex_array Thread-Index: AQHYskdd57xeUE7/yU2Sg7A2kEHzDg== Date: Tue, 30 Aug 2022 20:30:43 +0000 Message-ID: References: <20220817144042.2931674-1-qing.zhao@oracle.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-mailer: Apple Mail (2.3608.120.23.2.7) x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: a22d4564-62c4-4f08-27ed-08da8ac6837f x-ms-traffictypediagnostic: BLAPR10MB5265:EE_ x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: bQgQX5YDcPqMD5//nnAoPikIhlpnfCtq6ZtgM9q9kjbYlviiIPcBOKE6gXYxgkXSMMxOGmbAHg/jYIKCRMpJeXtgoW9SniylZN8ptVJemUTpELIFOCJEtFyrZ7UrSIbtINkDXbuh6V/nXQbaCG9hMevsSRVwuVhPzSW9v96BZwpgMleiz3OmoMv5R/zHz0NYHi3zlBKM/YOn+dDa0UYAO8Ef1i1u5AAjKncqsOc8SL0vCY5Lek5KW36cTtYMkqGohDKRMncCPng8H9IbrPk+M/i31fR/eC7iiEewWMtOSRWIPjz5ZCdj9nVuPAiMP97DahYudNqOu/lw34cQAIGtneVNCRVSsTJXqNRbsugFuHmO6djnNLlLq3oU1W1iXDoPfUpAz94lTz85Prb2q4ZBrlk/njLmtsVIYj8kp07LMUFr3ZxiSiLMLYZvYtf6/flui9ACwKPwdGaRXjW/CixD6PN74DWuaERw7TQFfPtSsQQ6peiF6W14p1N5C4D2AhjquUqkpJAmESbjLKfJLrqBpe99PG9z51Lwh9AiQEFuwtmAtrNfgr/FL1jlen9eoM5cy4kmvZy9f2fAkcAcKV06cKFf2tNAlv0VZmb9gIlrcmhOlj4Yd+zPMEqBXMghehcGDA1nWF3TCKs1Mytdfh7YLvS1FlXDZQM+3x3uCciaLd2d8tTEVW1PKN6WWghtYqRJl+0fLeVXrSjIWA10YZQGse0JO76zMw2BLleWS33nNJJPZw3VvAYtceQDvjV1gynVZivXdyCW/AGrhnTkx0jw+i+x3RN4pGnkoXag2l6DUJEbBZpMnmt5qvr7FcSPooWajEfqIPS2yEe0QP9630Z4CgbVtt+xOjUY4VJDbOVgG2WJrXbojGiLmEAezx7MXVEZ x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH2PR10MB4344.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230016)(396003)(366004)(136003)(39860400002)(376002)(346002)(6506007)(71200400001)(966005)(6512007)(478600001)(41300700001)(6486002)(83380400001)(2616005)(186003)(2906002)(21615005)(44832011)(5660300002)(8936002)(30864003)(54906003)(110136005)(316002)(66556008)(66476007)(4326008)(8676002)(64756008)(66946007)(66446008)(76116006)(91956017)(33656002)(38100700002)(166002)(36756003)(122000001)(86362001)(38070700005)(84970400001)(45980500001)(579004)(559001);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?hcS0S+mPDYYOAucdJDIS2cU65xz5DRMhhEaXzlckCWH4Iusv7pKFvcTdlyo6?= =?us-ascii?Q?NaPvQB60VBBuM4TI8I1wp0RROk6hgkQXrreSFJd+5uCGPPos7VGl24TyzvWi?= =?us-ascii?Q?fcBWZF/2MHcMY1gLddzSy0vG+tOuTRb1ziCvomHiwh0Hofou3PscSavbsGC0?= =?us-ascii?Q?HEgn5F+0qbldMlhAZ4xrke0DPv6GfenrHrMGVMBZ/LKhEPBLEsBbEDqM2ukG?= =?us-ascii?Q?82DCgQXWBQGztEcLQSypEZJc5zdmPRaG17CqgQW+a6TmqX8otXj86twBvGrj?= =?us-ascii?Q?ayfh1PK9rIkO+2epcTYmKuUMH/ay04i4U41hxMXD16TAFLAn5FE5Ku7zUwU2?= =?us-ascii?Q?7I2Flyhip6yZ2cLX8NJShr+Y9saUaa/IXgQJvtzDbCM7/dVymvnbkuOLwX2Z?= =?us-ascii?Q?S5rr9wNwjytDnA0ugNReNEE14EuPtGZJhelcwRhGh0t3Xzvn0dP8rrtbLPM+?= =?us-ascii?Q?Wnn/lcPqz/YIoMW6yNKrXWxwb58maef6j90Vo448TQFolSuD7Dr2vTMxo0Wa?= =?us-ascii?Q?8QTGPApObte3/Bj1jY/quvtS3f7RERiDnmKp/qGktQ2lFMKs8dq0ovSVh2xx?= =?us-ascii?Q?7DY+eIorpO7eF1EdbEmSM+hjxbeJmG7RKJdUNHgFtLEzC/2pKIx9ABcB7Mdo?= =?us-ascii?Q?F9BMndOz6wJYgdT9OA+ngbaespzpWshbwkYkk16ziHqriyg3rpdQE++WM8Zx?= =?us-ascii?Q?DSdoy7niLqJevtesQGUu5p9aKutYQCjzQkvKqTEdi97/26hny6IrOjmXzvo7?= =?us-ascii?Q?qUBrRhUglwIXSmJvN+NaEu5L9PSVyFHcCUEJYROTYo9iKbycMRiAsNhryPuE?= =?us-ascii?Q?H0F83LpIeUBoaItVxZlw1dKvCLgRCYlD/AON2y1aEllfl3eIhUVsKUfR6LnF?= =?us-ascii?Q?EjRp0qu9RS+JOICxhJ2q74ZTcbGrk4YPXWp9c38r7Fv8VA015dLAluTgbE8E?= =?us-ascii?Q?OkaRdw0SMEQceDmk1Maz1bhchclPdA7StzdEpzFbFUbcDSJh8hm+E+W7xJRe?= =?us-ascii?Q?g7XbZIjjuocrj55yGuzaaG+Yafk4K96dIByHJ7RUeykub78P9MYJ/8SeVRMw?= =?us-ascii?Q?GyCPiET6WhZlFIWmfbOa4R5bs4ZzLTwGo9ezhNqqx9cMhy1G/ZYjKI1Zq7qU?= =?us-ascii?Q?jvs9VBd1DjLeShH4nD7QO0tSCpjfOWx6aK9gHoDy4YusafNlmHKbbDhjQsjN?= =?us-ascii?Q?LLX5XOndX/p09dhdtqyJePfwUeyD97N9nLMNkXwNkSWGjnBnhVkb4wHsX2Gn?= =?us-ascii?Q?x2iVD9jZgxhAZtGduXzbGoJbBdebgt9AKObbX8KKvfGDekFMKQjb5OkukNi5?= =?us-ascii?Q?M7EZXr0zTv81ZNPv1cksXVPXRKWF9+bbucLELZuMtGHHnsHfnXRXgT5mpuu7?= =?us-ascii?Q?ofSwYkCNAv/q0ydoWUAsAsNI7aMWXgvrDeEKDn9WdnrkH8F7EtetM5kFwen5?= =?us-ascii?Q?BwBltgbJx8+z/daeLdp2PIqcWlsW0hHqNMtpG00QG0Jza7HFGj0GRt1mv+2Z?= =?us-ascii?Q?yR4G003O+tHyFFyOAga+B2xB7YEjBj31GpGrLCIDGzDDjK21vxbkloJAffAU?= =?us-ascii?Q?t++EGtye+7c7PUyOAXjzabevuA3uC0pRDUe9HcyekIOof6bpAwRsEggqKX4G?= =?us-ascii?Q?B/L7/AugQN6aUbVkD+pYTXI=3D?= Content-Type: multipart/alternative; boundary="_000_CE639271B08448C1B476B121E495F370oraclecom_" MIME-Version: 1.0 X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: a22d4564-62c4-4f08-27ed-08da8ac6837f X-MS-Exchange-CrossTenant-originalarrivaltime: 30 Aug 2022 20:30:44.0027 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: RvF1HdvwdigVwaBaH7iWOh5WBXWhbXdaeqW7ngtaJiHTnUVfjtA1tthNGNa/yh/qDkUgc8nAuoHDiNAy3TaNdw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLAPR10MB5265 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.517,FMLib:17.11.122.1 definitions=2022-08-30_11,2022-08-30_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 suspectscore=0 mlxlogscore=999 adultscore=0 phishscore=0 spamscore=0 bulkscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2207270000 definitions=main-2208300091 X-Proofpoint-ORIG-GUID: 6Jt0SAwPCRILzPzgdoPHfU0E8NNoiNyY X-Proofpoint-GUID: 6Jt0SAwPCRILzPzgdoPHfU0E8NNoiNyY X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,HTML_MESSAGE,KAM_SHORT,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Message-ID: <20220830203043.sQNaDKlAr8J4ghAnOtgkRhQA_Sz1yVidHH-jzGJf8JY@z> --_000_CE639271B08448C1B476B121E495F370oraclecom_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Hi, Joseph and Nathan, Could you please review the C and C++ FE parts of the patch? https://gcc.gnu.org/pipermail/gcc-patches/2022-August/599901.html The middle-end changes have been approved by Richard already. https://gcc.gnu.org/pipermail/gcc-patches/2022-August/600379.html Thanks. Begin forwarded message: From: Qing Zhao > Subject: [[GCC13][Patch][V3] 1/2] Add a new option -fstrict-flex-array[=3Dn= ] and new attribute strict_flex_array Date: August 17, 2022 at 10:40:41 AM EDT To: gcc-patches@gcc.gnu.org Cc: rguenther@suse.de, jakub@redhat.com, msebor@gmail.com, keescook@chromi= um.org, joseph@codesourcery.com, Qing Zhao > Add the following new option -fstrict-flex-array[=3Dn] and a corresponding attribute strict_flex_array to GCC: '-fstrict-flex-array' Treat the trailing array of a structure as a flexible array member in a stricter way. The positive form is equivalent to '-fstrict-flex-array=3D3', which is the strictest. A trailing array is treated as a flexible array member only when it is declared as a flexible array member per C99 standard onwards. The negative form is equivalent to '-fstrict-flex-array=3D0', which is the least strict. All trailing arrays of structures are treated as flexible array members. '-fstrict-flex-array=3DLEVEL' Treat the trailing array of a structure as a flexible array member in a stricter way. The value of LEVEL controls the level of strictness. The possible values of LEVEL are the same as for the 'strict_flex_array' attribute (*note Variable Attributes::). You can control this behavior for a specific trailing array field of a structure by using the variable attribute 'strict_flex_array' attribute (*note Variable Attributes::). This option is only valid when flexible array member is supported in the language. FOR ISO C before C99 and ISO C++, no language support for the= flexible array member at all, this option will be invalid and a warning will be = issued. When -std=3Dgnu89 is specified or C++ with GNU extension, only zero-len= gth array extension and one-size array are supported, as a result, LEVEL=3D3 will= be invalid and a warning will be issued. 'strict_flex_array (LEVEL)' The 'strict_flex_array' attribute should be attached to the trailing array field of a structure. It specifies the level of strictness of treating the trailing array field of a structure as a flexible array member. LEVEL must be an integer betwen 0 to 3. LEVEL=3D0 is the least strict level, all trailing arrays of structures are treated as flexible array members. LEVEL=3D3 is the strictest level, only when the trailing array is declared as a flexible array member per C99 standard onwards ([]), it is treated as a flexible array member. There are two more levels in between 0 and 3, which are provided to support older codes that use GCC zero-length array extension ([0]) or one-size array as flexible array member ([1]): When LEVEL is 1, the trailing array is treated as a flexible array member when it is declared as either [], [0], or [1]; When LEVEL is 2, the trailing array is treated as a flexible array member when it is declared as either [], or [0]. This attribute can be used with or without '-fstrict-flex-array'. When both the attribute and the option present at the same time, the level of the strictness for the specific trailing array field is determined by the attribute. This attribute is only valid when flexible array member is supported in= the language. For ISO C before C99 and ISO C++, no language support for the= flexible array member at all, this attribute will be invalid and a warning is is= sued. When -std=3Dgnu89 is specified or C++ with GNU extension, only zero-len= gth array extension and one-size array are supported, as a result, LEVEL=3D3 will= be invalid and a warning is issued. gcc/c-family/ChangeLog: * c-attribs.cc (handle_strict_flex_arrays_attribute): = New function. (c_common_attribute_table): New item for strict_flex_array. * c-opts.cc (c_common_post_options): Handle the combinati= on of -fstrict-flex-arrays and -std specially. * c.opt: (fstrict-flex-array): New option. (fstrict-flex-array=3D): New option. gcc/c/ChangeLog: * c-decl.cc (flexible_array_member_type_p): New function. (one_element_array_type_p): Likewise. (zero_length_array_type_p): Likewise. (add_flexible_array_elts_to_size): Call new utility routine flexible_array_member_type_p. (is_flexible_array_member_p): New function. (finish_struct): Set the new DECL_NOT_FLEXARRAY flag. gcc/cp/ChangeLog: * module.cc (trees_out::core_bools): Stream out new bit decl_not_flexarray. (trees_in::core_bools): Stream in new bit decl_not_flexarray. gcc/ChangeLog: * doc/extend.texi: Document strict_flex_array attribute. * doc/invoke.texi: Document -fstrict-flex-array[=3Dn] option. * print-tree.cc (print_node): Print new bit decl_not_= flexarray. * tree-core.h (struct tree_decl_common): New bit field decl_not_flexarray. * tree-streamer-in.cc (unpack_ts_decl_common_va= lue_fields): Stream in new bit decl_not_flexarray. * tree-streamer-out.cc (pack_ts_decl_common_va= lue_fields): Stream out new bit decl_not_flexarray. * tree.cc (array_at_struct_end_p): Update it with the new b= it field decl_not_flexarray. * tree.h (DECL_NOT_FLEXARRAY): New flag. gcc/testsuite/ChangeLog: * g++.dg/strict-flex-array-1.C: New test. * g++.dg/strict-flex-array-2.C: New test. * g++.dg/strict-flex-array-3.C: New test. * g++.dg/strict-flex-array-4.C: New test. * gcc.dg/strict-flex-array-1.c: New test. * gcc.dg/strict-flex-array-2.c: New test. * gcc.dg/strict-flex-array-3.c: New test. * gcc.dg/strict-flex-array-4.c: New test. --- gcc/c-family/c-attribs.cc | 94 +++++= ++++++++++ gcc/c-family/c-opts.cc | 41 +++++++ gcc/c-family/c.opt | 7 ++ gcc/c/c-decl.cc | 130 ++++++++= +++++++++++-- gcc/cp/module.cc | 2 + gcc/doc/extend.texi | 33 ++++++ gcc/doc/invoke.texi | 34 +++++- gcc/print-tree.cc | 8 +- gcc/testsuite/g++.dg/strict-flex-array-1.C | 31 +++++ gcc/testsuite/g++.dg/strict-flex-array-2.C | 16 +++ gcc/testsuite/g++.dg/strict-flex-array-3.C | 21 ++++ gcc/testsuite/g++.dg/strict-flex-array-4.C | 9 ++ gcc/testsuite/gcc.dg/strict-flex-array-1.c | 31 +++++ gcc/testsuite/gcc.dg/strict-flex-array-2.c | 15 +++ gcc/testsuite/gcc.dg/strict-flex-array-3.c | 21 ++++ gcc/testsuite/gcc.dg/strict-flex-array-4.c | 10 ++ gcc/tree-core.h | 5 +- gcc/tree-streamer-in.cc | = 1 + gcc/tree-streamer-out.cc | = 1 + gcc/tree.cc | 45 +++++-- gcc/tree.h | 14 ++- 21 files changed, 541 insertions(+), 28 deletions(-) create mode 100644 gcc/testsuite/g++.dg/strict-flex-array-1.C create mode 100644 gcc/testsuite/g++.dg/strict-flex-array-2.C create mode 100644 gcc/testsuite/g++.dg/strict-flex-array-3.C create mode 100644 gcc/testsuite/g++.dg/strict-flex-array-4.C create mode 100644 gcc/testsuite/gcc.dg/strict-flex-array-1.c create mode 100644 gcc/testsuite/gcc.dg/strict-flex-array-2.c create mode 100644 gcc/testsuite/gcc.dg/strict-flex-array-3.c create mode 100644 gcc/testsuite/gcc.dg/strict-flex-array-4.c diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/= c-attribs.cc index e4f1d3542f37..9c9927cefa0d 100644 --- a/gcc/c-family/c-attribs.cc +++ b/gcc/c-family/c-attribs.cc @@ -101,6 +101,8 @@ static tree handle_special_var_sec_attribute (tree *, t= ree, tree, int, bool *); static tree handle_aligned_attribute (tree *, tree, tree, int, bool *); static tree handle_warn_if_not_aligned_attribute (tree *, tree, tree, int, bool *); +static tree handle_strict_flex_arrays_attribute (tree *, tree, tree, + int, bool *); static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ; static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ; static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *); @@ -368,6 +370,8 @@ const struct attribute_spec c_common_attribute_table[] = =3D attr_aligned_exclusions }, { "warn_if_not_aligned", 0, 1, false, false, false, false, handle_warn_if_not_aligned_attribute, NULL }, + { "strict_flex_arrays", 1, 1, false, false, false, false, + handle_strict_flex_arrays_attribute, NULL }, { "weak", 0, 0, true, false, false, false, handle_weak_attribute, NULL }, { "noplt", 0, 0, true, false, false, false, @@ -2505,6 +2509,96 @@ handle_warn_if_not_aligned_attribute (tree *node, tr= ee name, no_add_attrs, true); } +/* Handle a "strict_flex_arrays" attribute; arguments as in + struct attribute_spec.handler. */ + +static tree +handle_strict_flex_arrays_attribute (tree *node, tree name, + tree args, int ARG_UNUSED (flags), + bool *no_add_attrs) +{ + tree decl =3D *node; + tree argval =3D TREE_VALUE (args); + + /* This attribute only applies to field decls of a structure. */ + if (TREE_CODE (decl) !=3D FIELD_DECL) + { + error_at (DECL_SOURCE_LOCATION (decl), + "%qE attribute may not be specified for %q+D", name, decl); + *no_add_attrs =3D true; + } + /* This attribute only applies to field with array type. */ + else if (TREE_CODE (TREE_TYPE (decl)) !=3D ARRAY_TYPE) + { + error_at (DECL_SOURCE_LOCATION (decl), + "%qE attribute may not be specified for a non array field", + name); + *no_add_attrs =3D true; + } + else if (TREE_CODE (argval) !=3D INTEGER_CST) + { + error_at (DECL_SOURCE_LOCATION (decl), + "%qE attribute argument not an integer", name); + *no_add_attrs =3D true; + } + else if (!tree_fits_uhwi_p (argval) || tree_to_uhwi (argval) > 3) + { + error_at (DECL_SOURCE_LOCATION (decl), + "%qE attribute argument %qE is not an integer constant" + " between 0 and 3", name, argval); + *no_add_attrs =3D true; + } + else + { + unsigned int level =3D tree_to_uhwi (argval); + /* check whether the attribute is valid based on language standard. + the attribute is only valid when flexible array member is + supported in the language. Therefore, we should invalid this attribute or + specific level of this attribute for the following situations: + A. When -std=3Dc89 is specified, no language support at all, invalid this + attribute and issue a warning; + B. When -std=3Dgnu89 is specified, only zero-length array extension and + one-size array are supported, level=3D3 will be invalid and a warning + will be issued. + C. C++ without GNU extension, no language support at all, invalid this + attribute and issue a warning; + D. C++ with GNU extension, only zero-length array extension and one-size + array are supported, level=3D3 will be invalid and a warning will be + issued. */ + if (level > 0) + { + if (!c_dialect_cxx () && flag_iso && flag_isoc99 =3D=3D 0) + { + warning (OPT_Wattributes, "%qE attribute ignored since it is " + "not supported with a ISO C before C99", name); + *no_add_attrs =3D true; + } + else if (!c_dialect_cxx () && !flag_iso + && flag_isoc99 =3D=3D 0 && level =3D=3D 3) + { + warning (OPT_Wattributes, "%qE =3D 3 attribute ignored since it is " + "not supported with a GNU extension GNU89", name); + *no_add_attrs =3D true; + } + else if (c_dialect_cxx () && flag_iso) + { + warning (OPT_Wattributes, "%qE attribute ignored since it is " + "not supported with a ISO C++", name); + *no_add_attrs =3D true; + } + else if (c_dialect_cxx () && !flag_iso + && level =3D=3D 3) + { + warning (OPT_Wattributes, "%qE =3D 3 attribute ignored since it is " + "not supported for C++ with GNU extension", name); + *no_add_attrs =3D true; + } + } + } + + return NULL_TREE; +} + /* Handle a "weak" attribute; arguments as in struct attribute_spec.handler. */ diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts= .cc index 4e1463689de3..639eb40f3c86 100644 --- a/gcc/c-family/c-opts.cc +++ b/gcc/c-family/c-opts.cc @@ -870,6 +870,47 @@ c_common_post_options (const char **pfilename) SET_OPTION_IF_UNSET (&global_options, &global_options_set, flag_tree_loop_distribute_patterns, 0); + /* -fstrict-flex-arrays is only valid when flexible array member is + supported in the language. Therefore, we should invalid this option or + specific level of this option for the following situations: + A. When -std=3Dc89 is specified, no language support at all, invalid = this + option and issue a warning; + B. When -std=3Dgnu89 is specified, only zero-length array extension a= nd + one-size array are supported, level=3D3 will be invalid and a warning + will be issued. + C. C++ without GNU extension, no language support at all, invalid this + option and issue a warning; + D. C++ with GNU extension, only zero-length array extension and one-s= ize + array are supported, level=3D3 will be invalid and a warning will be + issued. */ + if (flag_strict_flex_arrays > 0) + { + if (!c_dialect_cxx () && flag_iso && flag_isoc99 =3D=3D 0) + { + flag_strict_flex_arrays =3D 0; + warning (0, "%<-fstrict-flex-arrays%> is not supported with a ISO C " + "before C99, ignored"); + } + else if (!c_dialect_cxx () && !flag_iso && flag_isoc99 =3D=3D 0 && f= lag_strict_flex_arrays =3D=3D 3) + { + flag_strict_flex_arrays =3D 0; + warning (0, "%<-fstrict-flex-arrays=3D3%> is not supported with a " + "GNU extension GNU89, ignored"); + } + else if (c_dialect_cxx () && flag_iso) + { + flag_strict_flex_arrays =3D 0; + warning (0, "%<-fstrict-flex-arrays%> is not supported with a ISO " + "C++, ignored"); + } + else if (c_dialect_cxx () && !flag_iso && flag_strict_flex_arrays = =3D=3D 3) + { + flag_strict_flex_arrays =3D 0; + warning (0, "%<-fstrict-flex-arrays=3D3%> is not supported for C++ " + "with GNU extension, ignored"); + } + } + /* -Woverlength-strings is off by default, but is enabled by -Wpedantic. It is never enabled in C++, as the minimum limit is not normative in that standard. */ diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 44e1a60ce246..1e944f8a3055 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -2060,6 +2060,13 @@ fsized-deallocation C++ ObjC++ Var(flag_sized_deallocation) Init(-1) Enable C++14 sized deallocation support. +fstrict-flex-arrays +C C++ Common Alias(fstrict-flex-arrays=3D,3,0) + +fstrict-flex-arrays=3D +C C++ Common Joined RejectNegative UInteger Var(flag_strict_flex_arrays) I= nit(0) IntegerRange(0,3) +-fstrict-flex-arrays=3D Treat the trailing array of a structure = as a flexible array in a stricter way. The default is treating all trailin= g arrays of structures as flexible arrays. + fsquangle C++ ObjC++ WarnRemoved diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index ae8990c138fd..a2e125d4ddd5 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -4999,6 +4999,41 @@ set_array_declarator_inner (struct c_declarator *dec= l, return decl; } +/* Determine whether TYPE is a ISO C99 flexible array memeber type "[]". = */ +static bool +flexible_array_member_type_p (const_tree type) +{ + if (TREE_CODE (type) =3D=3D ARRAY_TYPE + && TYPE_SIZE (type) =3D=3D NULL_TREE + && TYPE_DOMAIN (type) !=3D NULL_TREE + && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) =3D=3D NULL_TREE) + return true; + + return false; +} + +/* Determine whether TYPE is a one-element array type "[1]". */ +static bool +one_element_array_type_p (const_tree type) +{ + if (TREE_CODE (type) !=3D ARRAY_TYPE) + return false; + return integer_zerop (array_type_nelts (type)); +} + +/* Determine whether TYPE is a zero-length array type "[0]". */ +static bool +zero_length_array_type_p (const_tree type) +{ + if (TREE_CODE (type) =3D=3D ARRAY_TYPE) + if (tree type_size =3D TYPE_SIZE_UNIT (type)) + if ((integer_zerop (type_size)) + && TYPE_DOMAIN (type) !=3D NULL_TREE + && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) =3D=3D NULL_TREE) + return true; + return false; +} + /* INIT is a constructor that forms DECL's initializer. If the final element initializes a flexible array field, add the size of that initializer to DECL's size. */ @@ -5013,10 +5048,7 @@ add_flexible_array_elts_to_size (tree decl, tree ini= t) elt =3D CONSTRUCTOR_ELTS (init)->last ().value; type =3D TREE_TYPE (elt); - if (TREE_CODE (type) =3D=3D ARRAY_TYPE - && TYPE_SIZE (type) =3D=3D NULL_TREE - && TYPE_DOMAIN (type) !=3D NULL_TREE - && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) =3D=3D NULL_TREE) + if (flexible_array_member_type_p (type)) { complete_array_type (&type, elt, false); DECL_SIZE (decl) @@ -8720,6 +8752,81 @@ finish_incomplete_vars (tree incomplete_vars, bool t= oplevel) } } + +/* Determine whether the FIELD_DECL X is a flexible array member according= to + the following info: + A. whether the FIELD_DECL X is the last field of the DECL_CONTEXT; + B. whether the FIELD_DECL is an array that is declared as "[]", "[0]", + or "[1]"; + C. flag_strict_flex_arrays; + D. the attribute strict_flex_array that is attached to the field + if presenting. + Return TRUE when it's a flexible array member, FALSE otherwise. */ + +static bool +is_flexible_array_member_p (bool is_last_field, + tree x) +{ + /* if not the last field, return false. */ + if (!is_last_field) + return false; + + /* if not an array field, return false. */ + if (TREE_CODE (TREE_TYPE (x)) !=3D ARRAY_TYPE) + return false; + + bool is_zero_length_array =3D zero_length_array_type_p (TREE_TYPE (x)); + bool is_one_element_array =3D one_element_array_type_p (TREE_TYPE (x)); + bool is_flexible_array =3D flexible_array_member_type_p (TREE_TYPE (x)); + + unsigned int strict_flex_array_level =3D flag_strict_flex_arrays; + + tree attr_strict_flex_array =3D lookup_attribute ("strict_flex_arrays", + DECL_ATTRIBUTES (x)); + /* if there is a strict_flex_array attribute attached to the field, + override the flag_strict_flex_arrays. */ + if (attr_strict_flex_array) + { + /* get the value of the level first from the attribute. */ + unsigned HOST_WIDE_INT attr_strict_flex_array_level =3D 0; + gcc_assert (TREE_VALUE (attr_strict_flex_array) !=3D NULL_TREE); + attr_strict_flex_array =3D TREE_VALUE (attr_strict_flex_array); + gcc_assert (TREE_VALUE (attr_strict_flex_array) !=3D NULL_TREE); + attr_strict_flex_array =3D TREE_VALUE (attr_strict_flex_array); + gcc_assert (tree_fits_uhwi_p (attr_strict_flex_array)); + attr_strict_flex_array_level =3D tree_to_uhwi (attr_strict_flex_arra= y); + + /* the attribute has higher priority than flag_struct_flex_array. */ + strict_flex_array_level =3D attr_strict_flex_array_level; + } + + switch (strict_flex_array_level) + { + case 0: + /* default, all trailing arrays are flexiable array members. */ + return true; + case 1: + /* Level 1: all "[1]", "[0]", and "[]" are flexiable array members. */ + if (is_one_element_array) + return true; + /* FALLTHROUGH. */ + case 2: + /* Level 2: all "[0]", and "[]" are flexiable array members. */ + if (is_zero_length_array) + return true; + /* FALLTHROUGH. */ + case 3: + /* Level 3: Only "[]" are flexible array members. */ + if (is_flexible_array) + return true; + break; + default: + gcc_unreachable (); + } + return false; +} + + /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T. LOC is the location of the RECORD_TYPE or UNION_TYPE's definition. FIELDLIST is a chain of FIELD_DECL nodes for the fields. @@ -8781,6 +8888,11 @@ finish_struct (location_t loc, tree t, tree fieldlis= t, tree attributes, bool saw_named_field =3D false; for (x =3D fieldlist; x; x =3D DECL_CHAIN (x)) { + /* whether this field is the last field of the structure or union. + for UNION, any field is the last field of it. */ + bool is_last_field =3D (DECL_CHAIN (x) =3D=3D NULL_TREE) + || (TREE_CODE (t) =3D=3D UNION_TYPE); + if (TREE_TYPE (x) =3D=3D error_mark_node) continue; @@ -8819,10 +8931,7 @@ finish_struct (location_t loc, tree t, tree fieldlis= t, tree attributes, DECL_PACKED (x) =3D 1; /* Detect flexible array member in an invalid context. */ - if (TREE_CODE (TREE_TYPE (x)) =3D=3D ARRAY_TYPE - && TYPE_SIZE (TREE_TYPE (x)) =3D=3D NULL_TREE - && TYPE_DOMAIN (TREE_TYPE (x)) !=3D NULL_TREE - && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) =3D=3D NULL_TREE) + if (flexible_array_member_type_p (TREE_TYPE (x))) { if (TREE_CODE (t) =3D=3D UNION_TYPE) { @@ -8830,7 +8939,7 @@ finish_struct (location_t loc, tree t, tree fieldlist= , tree attributes, "flexible array member in union"); TREE_TYPE (x) =3D error_mark_node; } - else if (DECL_CHAIN (x) !=3D NULL_TREE) + else if (!is_last_field) { error_at (DECL_SOURCE_LOCATION (x), "flexible array member not at end of struct"); @@ -8850,6 +8959,9 @@ finish_struct (location_t loc, tree t, tree fieldlist= , tree attributes, pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic, "invalid use of structure with flexible array member"); + /* Set DECL_NOT_FLEXARRAY flag for FIELD_DECL x. */ + DECL_NOT_FLEXARRAY (x) =3D !is_flexible_array_member_p (is_last_fiel= d, x); + if (DECL_NAME (x) || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))) saw_named_field =3D true; diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index f27f4d091e5e..75ee2514f66b 100644 --- a/gcc/cp/module.cc +++ b/gcc/cp/module.cc @@ -5414,6 +5414,7 @@ trees_out::core_bools (tree t) WB (t->decl_common.decl_by_reference_flag); WB (t->decl_common.decl_read_flag); WB (t->decl_common.decl_nonshareable_flag); + WB (t->decl_common.decl_not_flexarray); } if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS)) @@ -5558,6 +5559,7 @@ trees_in::core_bools (tree t) RB (t->decl_common.decl_by_reference_flag); RB (t->decl_common.decl_read_flag); RB (t->decl_common.decl_nonshareable_flag); + RB (t->decl_common.decl_not_flexarray); } if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS)) diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 7fe7f8817cdd..99b43ed3852c 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -7473,6 +7473,39 @@ This warning can be disabled by @option{-Wno-if-not-= aligned}. The @code{warn_if_not_aligned} attribute can also be used for types (@pxref{Common Type Attributes}.) +@cindex @code{strict_flex_arrays} variable attribute +@item strict_flex_arrays (@var{level}) +The @code{strict_flex_arrays} attribute should be attached to the trailing +array field of a structure. It specifies the level of strictness of +treating the trailing array field of a structure as a flexible array +member. @var{level} must be an integer betwen 0 to 3. + +@var{level}=3D0 is the least strict level, all trailing arrays of structur= es +are treated as flexible array members. @var{level}=3D3 is the strictest le= vel, +only when the trailing array is declared as a flexible array member per C99 +standard onwards ([]), it is treated as a flexible array member. + +There are two more levels in between 0 and 3, which are provided to support +older codes that use GCC zero-length array extension ([0]) or one-size arr= ay +as flexible array member ([1]): +When @var{level} is 1, the trailing array is treated as a flexible array m= ember +when it is declared as either "[]", "[0]", or "[1]"; +When @var{level} is 2, the trailing array is treated as a flexible array m= ember +when it is declared as either "[]", or "[0]". + +This attribute can be used with or without the @option{-fstrict-flex-array= s}. +When both the attribute and the option present at the same time, the level= of +the strictness for the specific trailing array field is determined by the +attribute. + +This attribute is only valid when flexible array member is supported in the +language. For ISO C before C99 and ISO C++, no language support for the fl= exible +array member at all, this attribute will be invalid and a warning is issue= d. +When -std=3Dgnu89 is specified or C++ with GNU extension, only zero-length= array +extension and one-size array are supported, as a result, @var{level}=3D3 w= ill be +invalid and a warning is issued. + + @item alloc_size (@var{position}) @itemx alloc_size (@var{position-1}, @var{position-2}) @cindex @code{alloc_size} variable attribute diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 863580b3710a..2a0a3cf3de10 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -207,7 +207,8 @@ in the following sections. -fopenmp -fopenmp-simd @gol -fpermitted-flt-eval-methods=3D@var{standard} @gol -fplan9-extensions -fsigned-bitfields -funsigned-bitfields @gol --fsigned-char -funsigned-char -fsso-struct=3D@var{endianness}} +-fsigned-char -funsigned-char -fstrict-flex-arrays[=3D@var{n}] @gol +-fsso-struct=3D@var{endianness}} @item C++ Language Options @xref{C++ Dialect Options,,Options Controlling C++ Dialect}. @@ -2826,6 +2827,37 @@ The type @code{char} is always a distinct type from = each of @code{signed char} or @code{unsigned char}, even though its behavior is always just like one of those two. +@item -fstrict-flex-arrays +@opindex fstrict-flex-arrays +@opindex fno-strict-flex-arrays +Treat the trailing array of a structure as a flexible array member in a +stricter way. +The positive form is equivalent to @option{-fstrict-flex-arrays=3D3}, whic= h is the +strictest. A trailing array is treated as a flexible array member only wh= en it +is declared as a flexible array member per C99 standard onwards. +The negative form is equivalent to @option{-fstrict-flex-arrays=3D0}, whic= h is the +least strict. All trailing arrays of structures are treated as flexible a= rray +members. + +@item -fstrict-flex-arrays=3D@var{level} +@opindex fstrict-flex-arrays=3D@var{level} +Treat the trailing array of a structure as a flexible array member in a +stricter way. The value of @var{level} controls the level of strictness. + +The possible values of @var{level} are the same as for the +@code{strict_flex_array} attribute (@pxref{Variable Attributes}). + +You can control this behavior for a specific trailing array field of a +structure by using the variable attribute @code{strict_flex_array} attribu= te +(@pxref{Variable Attributes}). + +This option is only valid when flexible array member is supported in the +language. FOR ISO C before C99 and ISO C++, no language support for the fl= exible +array member at all, this option will be invalid and a warning will be iss= ued. +When -std=3Dgnu89 is specified or C++ with GNU extension, only zero-length= array +extension and one-size array are supported, as a result, @var{level}=3D3 w= ill be +invalid and a warning will be issued. + @item -fsso-struct=3D@var{endianness} @opindex fsso-struct Set the default scalar storage order of structures and unions to the diff --git a/gcc/print-tree.cc b/gcc/print-tree.cc index 6d45a4a59669..58a98250cc4f 100644 --- a/gcc/print-tree.cc +++ b/gcc/print-tree.cc @@ -517,8 +517,12 @@ print_node (FILE *file, const char *prefix, tree node,= int indent, fprintf (file, " align:%d warn_if_not_align:%d", DECL_ALIGN (node), DECL_WARN_IF_NOT_ALIGN (node)); if (code =3D=3D FIELD_DECL) - fprintf (file, " offset_align " HOST_WIDE_INT_PRINT_UNSIGNED, - DECL_OFFSET_ALIGN (node)); + { + fprintf (file, " offset_align " HOST_WIDE_INT_PRINT_UNSIGNED, + DECL_OFFSET_ALIGN (node)); + fprintf (file, " decl_not_flexarray: %d", + DECL_NOT_FLEXARRAY (node)); + } if (code =3D=3D FUNCTION_DECL && fndecl_built_in_p (node)) { diff --git a/gcc/testsuite/g++.dg/strict-flex-array-1.C b/gcc/testsuite/g++= .dg/strict-flex-array-1.C new file mode 100644 index 000000000000..47adaf7bac4a --- /dev/null +++ b/gcc/testsuite/g++.dg/strict-flex-array-1.C @@ -0,0 +1,31 @@ +/* testing the correct usage of attribute strict_flex_array. */ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + + +int x __attribute__ ((strict_flex_arrays (1))); /* { dg-error "'strict_fle= x_arrays' attribute may not be specified for 'x'" } */ + +struct trailing { + int a; + int c __attribute ((strict_flex_arrays)); /* { dg-error "wrong number = of arguments specified for 'strict_flex_arrays' attribute" } */ +}; + +struct trailing_1 { + int a; + int b; + int c __attribute ((strict_flex_arrays (2))); /* { dg-error "'strict_f= lex_arrays' attribute may not be specified for a non array field" } */ +}; + +extern int d; + +struct trailing_array_2 { + int a; + int b; + int c[1] __attribute ((strict_flex_arrays (d))); /* { dg-error "'stric= t_flex_arrays' attribute argument not an integer" } */ +}; + +struct trailing_array_3 { + int a; + int b; + int c[0] __attribute ((strict_flex_arrays (5))); /* { dg-error "'stric= t_flex_arrays' attribute argument '5' is not an integer constant between 0 = and 3" } */ +}; diff --git a/gcc/testsuite/g++.dg/strict-flex-array-2.C b/gcc/testsuite/g++= .dg/strict-flex-array-2.C new file mode 100644 index 000000000000..245f8fe0bc73 --- /dev/null +++ b/gcc/testsuite/g++.dg/strict-flex-array-2.C @@ -0,0 +1,16 @@ +/* testing the correct usage of flag -fstrict_flex_array for C++. */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fstrict-flex-arrays -std=3Dc++98" } */ + +struct trailing_array { + int a; + int b; + int c[0] __attribute ((strict_flex_arrays (3))); /* { dg-warning "attr= ibute ignored since it is not supported with a ISO C\\+\\+" } */ +}; + +int foo(int a) +{ + return 0; +} + +/* { dg-warning "is not supported with a ISO C\\+\\+, ignored" "" { targe= t *-*-* } 0 } */ diff --git a/gcc/testsuite/g++.dg/strict-flex-array-3.C b/gcc/testsuite/g++= .dg/strict-flex-array-3.C new file mode 100644 index 000000000000..2a733a5bccf4 --- /dev/null +++ b/gcc/testsuite/g++.dg/strict-flex-array-3.C @@ -0,0 +1,21 @@ +/* testing the correct usage of flag -fstrict_flex_array for C++. */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fstrict-flex-arrays -std=3Dgnu++98" } */ +/* { dg-warning "'-fstrict-flex-arrays=3D3' is not supported for C\\+\\+ w= ith GNU extension, ignored" "" { target *-*-* } 0 } */ + +struct trailing_array { + int a; + int b; + int c[0] __attribute ((strict_flex_arrays (3))); /* { dg-warning "'str= ict_flex_arrays' =3D 3 attribute ignored since it is not supported for C\\+= \\+ with GNU extension" } */ +}; + +struct trailing_array_1 { + int a; + int b; + int c[0] __attribute ((strict_flex_arrays (2))); /* { dg-bogus "'stric= t_flex_arrays' =3D 3 attribute ignored since it is not supported for C\\+\\= + with GNU extension" } */ +}; + +int foo(int a) +{ + return a + 2; +} diff --git a/gcc/testsuite/g++.dg/strict-flex-array-4.C b/gcc/testsuite/g++= .dg/strict-flex-array-4.C new file mode 100644 index 000000000000..804e4cf459ef --- /dev/null +++ b/gcc/testsuite/g++.dg/strict-flex-array-4.C @@ -0,0 +1,9 @@ +/* testing the correct usage of flag -fstrict_flex_array. */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fstrict-flex-arrays=3D2 -std=3Dgnu++98" } */ +/* { dg-bogus "is not supported for C\\+\\+ with GNU extension, ignored" "= " { target *-*-* } 0 } */ + +int foo(int a) +{ + return a + 2; +} diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-1.c b/gcc/testsuite/gcc= .dg/strict-flex-array-1.c new file mode 100644 index 000000000000..47adaf7bac4a --- /dev/null +++ b/gcc/testsuite/gcc.dg/strict-flex-array-1.c @@ -0,0 +1,31 @@ +/* testing the correct usage of attribute strict_flex_array. */ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + + +int x __attribute__ ((strict_flex_arrays (1))); /* { dg-error "'strict_fle= x_arrays' attribute may not be specified for 'x'" } */ + +struct trailing { + int a; + int c __attribute ((strict_flex_arrays)); /* { dg-error "wrong number = of arguments specified for 'strict_flex_arrays' attribute" } */ +}; + +struct trailing_1 { + int a; + int b; + int c __attribute ((strict_flex_arrays (2))); /* { dg-error "'strict_f= lex_arrays' attribute may not be specified for a non array field" } */ +}; + +extern int d; + +struct trailing_array_2 { + int a; + int b; + int c[1] __attribute ((strict_flex_arrays (d))); /* { dg-error "'stric= t_flex_arrays' attribute argument not an integer" } */ +}; + +struct trailing_array_3 { + int a; + int b; + int c[0] __attribute ((strict_flex_arrays (5))); /* { dg-error "'stric= t_flex_arrays' attribute argument '5' is not an integer constant between 0 = and 3" } */ +}; diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-2.c b/gcc/testsuite/gcc= .dg/strict-flex-array-2.c new file mode 100644 index 000000000000..967a240040dc --- /dev/null +++ b/gcc/testsuite/gcc.dg/strict-flex-array-2.c @@ -0,0 +1,15 @@ +/* testing the correct usage of flag -fstrict_flex_array. */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fstrict-flex-arrays -std=3Dc89" } */ +/* { dg-warning "'-fstrict-flex-arrays' is not supported with a ISO C befo= re C99, ignored" "" { target *-*-* } 0 } */ + +struct trailing_array { + int a; + int b; + int c[0] __attribute ((strict_flex_arrays (3))); /* { dg-warning "'str= ict_flex_arrays' attribute ignored since it is not supported with a ISO C b= efore C99" } */ +}; + +int foo(int a) +{ + return a + 2; +} diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-3.c b/gcc/testsuite/gcc= .dg/strict-flex-array-3.c new file mode 100644 index 000000000000..879de7f203c8 --- /dev/null +++ b/gcc/testsuite/gcc.dg/strict-flex-array-3.c @@ -0,0 +1,21 @@ +/* testing the correct usage of flag -fstrict_flex_array. */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fstrict-flex-arrays -std=3Dgnu89" } */ +/* { dg-warning "'-fstrict-flex-arrays=3D3' is not supported with a GNU ex= tension GNU89, ignored" "" { target *-*-* } 0 } */ + +struct trailing_array { + int a; + int b; + int c[0] __attribute ((strict_flex_arrays (3))); /* { dg-warning "'str= ict_flex_arrays' =3D 3 attribute ignored since it is not supported with a G= NU extension GNU89" } */ +}; + +struct trailing_array_1 { + int a; + int b; + int c[0] __attribute ((strict_flex_arrays (2))); /* { dg-bogus "'stric= t_flex_arrays' =3D 3 attribute ignored since it is not supported with a GNU= extension GNU89" } */ +}; + +int foo(int a) +{ + return a + 2; +} diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-4.c b/gcc/testsuite/gcc= .dg/strict-flex-array-4.c new file mode 100644 index 000000000000..ce64c24db301 --- /dev/null +++ b/gcc/testsuite/gcc.dg/strict-flex-array-4.c @@ -0,0 +1,10 @@ +/* testing the correct usage of flag -fstrict_flex_array. */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fstrict-flex-arrays=3D2 -std=3Dgnu89" } */ +/* { dg-bogus "is not supported with a GNU extension GNU89, ignored" "" {= target *-*-* } 0 } */ + + +int foo(int a) +{ + return a + 2; +} diff --git a/gcc/tree-core.h b/gcc/tree-core.h index 86a07c282af2..f822cb539dd0 100644 --- a/gcc/tree-core.h +++ b/gcc/tree-core.h @@ -1813,7 +1813,10 @@ struct GTY(()) tree_decl_common { TYPE_WARN_IF_NOT_ALIGN. */ unsigned int warn_if_not_align : 6; - /* 14 bits unused. */ + /* In FIELD_DECL, this is DECL_NOT_FLEXARRAY. */ + unsigned int decl_not_flexarray : 1; + + /* 13 bits unused. */ /* UID for points-to sets, stable over copying from inlining. */ unsigned int pt_uid; diff --git a/gcc/tree-streamer-in.cc b/gcc/tree= -streamer-in.cc index 196f19c759f2..21e6e8eb1c0a 100644 --- a/gcc/tree-streamer-in.cc +++ b/gcc/tree-streamer-in.cc @@ -261,6 +261,7 @@ unpack_ts_decl_common_value_fields (struct bitpack_d *b= p, tree expr) else SET_DECL_FIELD_ABI_IGNORED (expr, val); expr->decl_common.off_align =3D bp_unpack_value (bp, 8); + DECL_NOT_FLEXARRAY (expr) =3D (unsigned) bp_unpack_value (bp, 1); } else if (VAR_P (expr)) diff --git a/gcc/tree-streamer-out.cc b/gcc/tr= ee-streamer-out.cc index d39dc158a465..68e40dbdb8f2 100644 --- a/gcc/tree-streamer-out.cc +++ b/gcc/tree-streamer-out.cc @@ -229,6 +229,7 @@ pack_ts_decl_common_value_fields (struct bitpack_d *bp,= tree expr) else bp_pack_value (bp, DECL_FIELD_ABI_IGNORED (expr), 1); bp_pack_value (bp, expr->decl_common.off_align, 8); + bp_pack_value (bp, DECL_NOT_FLEXARRAY (expr), 1); } else if (VAR_P (expr)) diff --git a/gcc/tree.cc b/gcc/tree.cc index fed1434d141d..d698e8c9c213 100644 --- a/gcc/tree.cc +++ b/gcc/tree.cc @@ -12678,14 +12678,30 @@ array_ref_up_bound (tree exp) } /* Returns true if REF is an array reference, component reference, - or memory reference to an array at the end of a structure. - If this is the case, the array may be allocated larger - than its upper bound implies. */ + or memory reference to an array whose actual size might be larger + than its upper bound implies, there are multiple cases: + A. a ref to a flexible array member at the end of a structure; + B. a ref to an array with a different type against the original decl; + for example: + short a[16] =3D { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,= 16 }; + (*((char(*)[16])&a[0]))[i+8] + + C. a ref to an array that was passed as a parameter; + for example: + + int test (uint8_t *p, uint32_t t[1][1], int n) { + for (int i =3D 0; i < 4; i++, p++) + t[i][0] =3D ...; + + FIXME, the name of this routine need to be changed to be more accurate.= */ bool array_at_struct_end_p (tree ref) { - tree atype; + /* the TYPE for this array referece. */ + tree atype =3D NULL_TREE; + /* the FIELD_DECL for the array field in the containing structure. */ + tree afield_decl =3D NULL_TREE; if (TREE_CODE (ref) =3D=3D ARRAY_REF || TREE_CODE (ref) =3D=3D ARRAY_RANGE_REF) @@ -12695,7 +12711,10 @@ array_at_struct_end_p (tree ref) } else if (TREE_CODE (ref) =3D=3D COMPONENT_REF && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 1))) =3D=3D ARRAY_TYPE) - atype =3D TREE_TYPE (TREE_OPERAND (ref, 1)); + { + atype =3D TREE_TYPE (TREE_OPERAND (ref, 1)); + afield_decl =3D TREE_OPERAND (ref, 1); + } else if (TREE_CODE (ref) =3D=3D MEM_REF) { tree arg =3D TREE_OPERAND (ref, 0); @@ -12707,6 +12726,7 @@ array_at_struct_end_p (tree ref) if (tree fld =3D last_field (argtype)) { atype =3D TREE_TYPE (fld); + afield_decl =3D fld; if (TREE_CODE (atype) !=3D ARRAY_TYPE) return false; if (VAR_P (arg) && DECL_SIZE (fld)) @@ -12760,13 +12780,16 @@ array_at_struct_end_p (tree ref) ref =3D TREE_OPERAND (ref, 0); } - /* The array now is at struct end. Treat flexible arrays as + gcc_assert (!afield_decl + || (afield_decl && TREE_CODE (afield_decl) =3D=3D FIELD_DECL)); + + /* The array now is at struct end. Treat flexible array member as always subject to extend, even into just padding constrained by an underlying decl. */ if (! TYPE_SIZE (atype) || ! TYPE_DOMAIN (atype) || ! TYPE_MAX_VALUE (TYPE_DOMAIN (atype))) - return true; + return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; /* If the reference is based on a declared entity, the size of the array is constrained by its given domain. (Do not trust commons PR/69368). = */ @@ -12788,9 +12811,9 @@ array_at_struct_end_p (tree ref) if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (atype))) !=3D INTEGER_CST || TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))) !=3D INTEGER_CST || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (atype))) !=3D INTEGER_= CST) - return true; + return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; if (! get_addr_base_and_unit_offset (ref_to_array, &offset)) - return true; + return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; /* If at least one extra element fits it is a flexarray. */ if (known_le ((wi::to_offset (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))) @@ -12798,12 +12821,12 @@ array_at_struct_end_p (tree ref) + 2) * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (atype))), wi::to_offset (DECL_SIZE_UNIT (ref)) - offset)) - return true; + return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; return false; } - return true; + return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; } /* Return a tree representing the offset, in bytes, of the field referenced diff --git a/gcc/tree.h b/gcc/tree.h index e6564aaccb7b..f911c0a46e69 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -2993,6 +2993,12 @@ extern void decl_value_expr_insert (tree, tree); #define DECL_PADDING_P(NODE) \ (FIELD_DECL_CHECK (NODE)->decl_common.decl_flag_3) +/* Used in a FIELD_DECL to indicate whether this field is not a flexible + array member. This is only valid for the last array type field of a + structure. */ +#define DECL_NOT_FLEXARRAY(NODE) \ + (FIELD_DECL_CHECK (NODE)->decl_common.decl_not_flexarray) + /* A numeric unique identifier for a LABEL_DECL. The UID allocation is dense, unique within any one function, and may be used to index arrays. If the value is -1, then no UID has been assigned. */ @@ -5531,10 +5537,10 @@ extern tree component_ref_field_offset (tree); returns null. */ enum struct special_array_member { - none, /* Not a special array member. */ - int_0, /* Interior array member with size zero. */ - trail_0, /* Trailing array member with size zero. */ - trail_1 /* Trailing array member with one element. */ + none, /* Not a special array member. */ + int_0, /* Interior array member with size zero. */ + trail_0, /* Trailing array member with size zero. */ + trail_1 /* Trailing array member with one element. */ }; /* Return the size of the member referenced by the COMPONENT_REF, using -- 2.31.1 --_000_CE639271B08448C1B476B121E495F370oraclecom_--