From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by sourceware.org (Postfix) with ESMTPS id B271038983A4; Tue, 15 Nov 2022 15:41:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B271038983A4 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 (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 2AFFRi2N005432; Tue, 15 Nov 2022 15:41:13 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : references : in-reply-to : content-type : content-id : content-transfer-encoding : mime-version; s=corp-2022-7-12; bh=GtRFwT3cAyFIUUfgUmgNUzMLhHhedNdolCScS0y7jdQ=; b=ouOysWBTv9k2FTTNHb4No/3OyXmiyuPIE1lRzjwlULvvT7mxS3tbxmqEs/C5axF0S8c2 Q90FLiqgAMHQRq967EqPOPjJt3c0n8ZOr6CsJGCepTZFTZdNPsaaiGPgJSjyu+iWLxZF 0q9NQr4bzq/d0XOiDq3AtzGsesBjrhdLXQWQ9UEMqf9/83NwZg5EQa1fLIM/Z8AJ4go0 kbqH43P0tuw7TdPkU3EzUAbG4yVXn4SeiNUB5kCK5yKtoxKVdGqQ7cq9t7BuQloxrs4G ts6xyLcuakeejfvr8aHKMj+fsY8QrLQ8/fhgDV5nYI+iVtN3oc60dO9Mm9YVIgNF2q1Q 5A== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3kv3htssc9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 15 Nov 2022 15:41:12 +0000 Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 2AFFFEcd004926; Tue, 15 Nov 2022 15:41:12 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2108.outbound.protection.outlook.com [104.47.58.108]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3ku3k6qthp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 15 Nov 2022 15:41:11 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BUJZbvlMycJtHD67yoqvMyXu9Qm5VWjZ0UfHZ2l8uvAREWaRxyhO6FqAEyBQ0TD+vs3hlPnNXlULSzjsJh6YR3Bqvq6aU2dpdSMWAcuA2h7TVSBe1OQ7xD1yiR2R6pFcPBZNfDwnAIbUE5rX834cgFI6nTx9Fw8Z/2Ooh4qzFwtRkkXLJwBH0E1BZsyNivaUkj/qmSeoje0QTWBlYskYo9H46dTB7vtZAGEOOGyM5WtFp8na3xpsiamJb4oIZt2Qhixjs7gSuTY6AD0xcAYGd6qopbWCM58ywWru4GVWzPIhwQqPeboVQiixOcNGLkwZ+lN1DjfVh4hZ1O1oN05xTQ== 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=GtRFwT3cAyFIUUfgUmgNUzMLhHhedNdolCScS0y7jdQ=; b=MnvkqWGP8BDTswPIcwMjoIhmE11eOY+cNo6JQ+UkLhBz6myuJ5C7d7LU3UlpI9GdSYvSZDI0crebx9JzU2T4heNO32EKgAxRHz7ZK3CU/HQJ0rMaXFEQHsksy7xw4HEl6zL6djnCFOj0jxNX/f0ZOW0vicN8oKXGdOCOzTcPm1AISBkYICtM8/7gVJhA5IqoC7qkCmxYk09Vn9OeJfX5ccCodTJj/Eo1+IR3ejnPZv3J9B+4LYQwChRhEYAOcF7uFDSxe/GIV6Cj5Vun3imXEHB7jhaniPt8Mu8g0EsF+MkkCFHgqKH22g/EtLeT/dZbx2Pyn7fQqW6NueDYXqXAuA== 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=GtRFwT3cAyFIUUfgUmgNUzMLhHhedNdolCScS0y7jdQ=; b=yPA5azaKSMhHWU2NChNKVSCGZw8kG4S+iijc0OPNzYba7ICISoCQZ076ik9rphKv5UpgxuvBBiDia39N51wvRcujZI5/R11AbFhK7lL286Tf94OYSlwxT2isxT1cDEv7opEPiIuGoBh5MG77wjlAaJNcDSj+9BESXACHlQ7QJ8Y= Received: from CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) by BL3PR10MB6233.namprd10.prod.outlook.com (2603:10b6:208:38c::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5813.18; Tue, 15 Nov 2022 15:41:09 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::9423:79b3:c0dc:1113]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::9423:79b3:c0dc:1113%8]) with mapi id 15.20.5813.018; Tue, 15 Nov 2022 15:41:08 +0000 From: Qing Zhao To: Richard Biener , "joseph@codesourcery.com" CC: gcc Patches , Kees Cook , "siddhesh@gcc.gnu.org" Subject: Re: [PATCH 2/2] Add a new warning option -Wstrict-flex-arrays. Thread-Topic: [PATCH 2/2] Add a new warning option -Wstrict-flex-arrays. Thread-Index: AQHY84GSjiE+CH4eVEq4fZ1E1BjODa5AKleA Date: Tue, 15 Nov 2022 15:41:08 +0000 Message-ID: <5451C61C-5BF9-4B41-8144-242F78B97A39@oracle.com> References: <20221108145113.955321-1-qing.zhao@oracle.com> <20221108145113.955321-3-qing.zhao@oracle.com> In-Reply-To: <20221108145113.955321-3-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.3696.120.41.1.1) x-ms-publictraffictype: Email x-ms-traffictypediagnostic: CH2PR10MB4344:EE_|BL3PR10MB6233:EE_ x-ms-office365-filtering-correlation-id: b4d1cbd4-6ae6-4950-1085-08dac71fd0e4 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: lfZX0LdtyoGYeboZbHar0+a1sbQ9A8RzP6gBI3oJ3WziGfD59E/hf2sx2bQ5vKbCHfgOYrr3A9A1IpfC52PeoSrnBXKb4q+YnSCfhHtu2i5BrpzvBaE+GWsnmoREattYhpZIn88XCkuz6vmG2itB+wg4CinUCtgqnQbLl+jh6kEFg0LWhkVwIeDCNJb7P2HJshEn26n4rw31peO4+cd4gkr9wYA8LkVdxPmgX3dg2Tjij9y9tUij98jLEshuVZAayAV5lHnCEr1l5+rCLGt59+5noV4+rPB3jq+6R0KVMRie9OzdpS6D3HdlXoxNXBUvlsQLrTavF6a+rdt6KWZEeMFAUHAe28OD8A1MMBM1uKTLDDVdcMpfbKjT7u9JBoqaZ1my4lxRMJbhDZE/9WjaJow05fqhZqNPAWkHRhsNXNCw8mcD523JE5PJBz3LBZU19f6axoTTKOkcTY/a9nd8RTDnqr9hydg+lBWVaCpvrnFCK0yZRTuQgfhvQuimDZ6C87txW6YyG585o2v6QU5PQ8yPE0htS1hbbDCXKtuq2scHG/Fw/myQfRScCpRt4GsEurKPoGjGYzSMKRbYQIyiTS9Rn1fRhoe7iJonrq1GxEjjf84ikRiYMaSCVHWRFGZHGsVo8tHfq6hrltBMpeibm8wuWSQA15DA4LLbKSdfc2NoBj6Y0pQgkrO0vi9m80m/Gx4juEdIyA/LE1mLtoyX7RQC64wo/0pxq0iVJcMRvjfUsaqu1IQBWSv+qNV+OnWaTGwGvykdZr5uDu0MGWYKCwXLmiBRT38H02RihUCTP1vr/CEyBXqrVK3S+9AvI4h8auTja7SdcWlgFrlhluISiFF7nCR5QMouZflffF3kZOg= 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:(13230022)(39860400002)(376002)(366004)(136003)(346002)(396003)(451199015)(33656002)(84970400001)(86362001)(38070700005)(36756003)(83380400001)(5660300002)(44832011)(2906002)(30864003)(53546011)(2616005)(186003)(6506007)(26005)(6512007)(38100700002)(122000001)(76116006)(91956017)(66946007)(66556008)(66476007)(8936002)(71200400001)(110136005)(54906003)(66446008)(64756008)(8676002)(4326008)(6486002)(41300700001)(316002)(478600001)(45980500001)(559001)(579004);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?mh1RJG80/EClif7R86QcuESRIIlwHHRRI4s9IFjqh+8c+I1Ag02rlqLROJse?= =?us-ascii?Q?qYBY6MJxrEyIFDwHhKh77LWaOrOjV4fyXywzBJbO7+z+Q1Xae+S/WtTh9IiB?= =?us-ascii?Q?T6iTW1OSn9eS9nNHBAVxLJc1CwuaMVyI7VAkr07NNmMP5cV9Ycfb1WMsjWT0?= =?us-ascii?Q?52RF6DibQ1mxLMXb56m4FmsySMsjmhn+/yoEQZh8pl8Si5eLSc3Si7tmeJM9?= =?us-ascii?Q?nGzrm+evTN1vFoVyLOOVPbSeTEG/BFy8jJXXOcPpUG5BwFHER/7HoUQwBrRm?= =?us-ascii?Q?w8NUo7+1LRutl/nPoyBtj/QKdKaCR6IBZDWaGv1ARXRL/bEkCPFww81WE1U1?= =?us-ascii?Q?uZn0xLeYqlrmsLJ0avWzCD2HNNu20wtwXc5IKb5OTb8wGcYq/1mz0CNUSkX0?= =?us-ascii?Q?d9b1iOFHU/9umCsz+dQmsjmCZlyQlm+ONpQl9Ltljuo1ND5FPm4Yif62gVgp?= =?us-ascii?Q?ZNRON7/tD0bQey8q+ScDV9RSA1gvRweCPItutdLKcMgNL+osn8yzF2zQ+gQu?= =?us-ascii?Q?dqOzEOUN4ZzZiie9JSHgmqFljN23pL65zwbyX7sSrsyC+txGeKSSxhSCN409?= =?us-ascii?Q?8EF7jDYp1mQvZqqVDAI/cCvnIokdEnzOuFcyzdqfeY3maPNr546/ww43tRYN?= =?us-ascii?Q?yLegLkfdGhf8oZ1k5CBdhYlSczGE0Qtkpw/gWoj70qLAX49CeC6QZVfbfqON?= =?us-ascii?Q?edPiqcw0P/Of7+5NLtqALp1fAGnTMWK35F03nAA6ZvD8jJGJgtcA3ba0EyKY?= =?us-ascii?Q?KPe+H+7x7oshoWF9s42JsSgGHZDqpE07SUEiD3vJFDD4awanzPJw9T3o7mmM?= =?us-ascii?Q?bH7x8Ws7wXH47Q46YHxddbquzAAwMsTwTlyVg5FnHk7aN2IDG7vZ1We53u0Y?= =?us-ascii?Q?FL8OeiE3f5hxDe15fqUKFL6W22QYaG5PQSlJkVWOv/w9CiLbvfDWsO+Uwp2T?= =?us-ascii?Q?f+nEq2MCGvUGMfQFyOQD+wnUGWTS5zfXZgZ+2alrHgNpuvB+LUTTTjYb4WAd?= =?us-ascii?Q?TOlgW+1dVI0kX9xtsy7gJUL48cLMo4M+31cafPGvHKsEgkQ6IEwEtDDBxHCa?= =?us-ascii?Q?SHoLkPubnqroqfz1WxhoBaHt/Zx6gEB0aVMlIXypdQDHi9wMTNNoGXYe3boP?= =?us-ascii?Q?oRAuY5VFq1XIp5JnkL9wmVY3qAfHoFqi3gkoNmaEJjsG9jYZ54TP+BSZkSZ9?= =?us-ascii?Q?eibLL4S+J06+9M5ReF4T6e4gNm3xoshi8EzuZJzDtXVRjMBMgesLugjiOBfC?= =?us-ascii?Q?O1tdYk8gOAtiN4bHGmb6TZuTxvJ+lW9Y+yLKhdoVInoJfnJqGTOvrh50bh4/?= =?us-ascii?Q?E/q2wI44fW+XMPTLa5RBPEardXcpOEX1pRmG1DVM+3/cGQPt8h56ecs6vRef?= =?us-ascii?Q?ZiXBPOXph+RDujYUkwdGjQM6b2KPruotB9ipaViZKy1j77VoUetOj7tnhmH5?= =?us-ascii?Q?9k7hYHa7Gv8rrO8BlYSThxB5OQ78Mz6Zc2vX8Hyf5TeHrHDeQZ96tVmn+Jy/?= =?us-ascii?Q?sgb1LtBB9gWp9+kkf1kl9IY84ZkS/IqPb8JQHwsbHmGaN6MJkR9srGk9gqjJ?= =?us-ascii?Q?ofNEnrBCZuTjxmdlRfleGYwXf0m3wZHPI5cy3DLm?= Content-Type: text/plain; charset="us-ascii" Content-ID: <3119CC905790DA4B92F22DB1CB458728@namprd10.prod.outlook.com> Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: U4OtGOH2x4jwE3+OM+BklUJgfOV6uO619cZmuJ6BTPahYNPMB71MjMgvuEhg2Ob8gXlscH2KdO4T/FSnI8mVnUnwz5MeKeiWmIy/Jte6OBDrkus6C4RRnIPXp8nyf/tA0K0YT6zeVpaz8ziMFxq2Dx1P3/myUzh2Qek6cG2DV9Gc6mRfTifEhVwB7iDWDzq4yKPsNiCQujsfvTebCdv0KUwq1AApimLFg16f41A8swsgQur+YVDULkffi6O1+yt67fGOtGLogHW5jIkEx0KczYHoNWuGUYzoPXKxV/af9rKZ70n51alblqTjYONlsltorEpff+WCyy4wwigGLN1C3+O9rCI+wqHRo0lwOgmLwWLq+YHx2L1e2YNVozGlezpuXCmnuVNtouHtzXVoaeh+iAwdLThkNnVGyURwxaeTiOy5mmfhNqYTzgYj+GQ6s8X+NHv6Oye4As4UKX6vTQDLAIgHMshDeADGFfMHHTDMuBy4Mb4mJzjrCEnKuv70HJ4PHvqLMJltjMgXloHRmniV27Y4tno5kcX3B0WRpALoOeObpSWZmcdO5V75rlbrSewqTA5aVGaOkFyz5NxfV2ezuOde3eBn6Q3GWC23F/cSJ+UKyOyI2l8poEuQQkivox4TuPGPuqUW5ygNfnkvK+d6QF+9DDdksxod8sQtJrMoT0FT5zO8Jc0nhvnFIJdfOhJpbsDEsAeT77mK/eyD3YvpWF7wwbGNpUOuDlCtVXqte9jGv7YgmQmbTh/oTvxNcmRRLyvlauIHI48xWf38eBTQyd5bFGlv7+nvA9WcVeZp25Yzf5X3KTkMJ9AWaQFKZJodIzNF1nazPbuk8MCF/aYhIj2SlWoOPsy9q4sbfTGLI38A2IYfdFMpVB0+fa9UGum4 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: b4d1cbd4-6ae6-4950-1085-08dac71fd0e4 X-MS-Exchange-CrossTenant-originalarrivaltime: 15 Nov 2022 15:41:08.8243 (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: 2cr39/Y5kgw7SjueOXRuI0U0Ag4cQ3+ch3Px4bcusoiQZ/somz/3uI6KA8yps+qZmoHpLV5/Y/qMFw2lZXPAAg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR10MB6233 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-11-15_08,2022-11-15_03,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxscore=0 adultscore=0 phishscore=0 malwarescore=0 suspectscore=0 mlxlogscore=999 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2210170000 definitions=main-2211150105 X-Proofpoint-GUID: ByRR9LmZc5wDKYi2NepNhhUDdJ5qxCNS X-Proofpoint-ORIG-GUID: ByRR9LmZc5wDKYi2NepNhhUDdJ5qxCNS 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,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Ping on this patch. thanks. Qing > On Nov 8, 2022, at 9:51 AM, Qing Zhao wrote: >=20 > '-Wstrict-flex-arrays' > Warn about inproper usages of flexible array members according to > the LEVEL of the 'strict_flex_array (LEVEL)' attribute attached to > the trailing array field of a structure if it's available, > otherwise according to the LEVEL of the option > '-fstrict-flex-arrays=3DLEVEL'. >=20 > This option is effective only when LEVEL is bigger than 0. > Otherwise, it will be ignored with a warning. >=20 > when LEVEL=3D1, warnings will be issued for a trailing array > reference of a structure that have 2 or more elements if the > trailing array is referenced as a flexible array member. >=20 > when LEVEL=3D2, in addition to LEVEL=3D1, additional warnings will be > issued for a trailing one-element array reference of a structure if > the array is referenced as a flexible array member. >=20 > when LEVEL=3D3, in addition to LEVEL=3D2, additional warnings will be > issued for a trailing zero-length array reference of a structure if > the array is referenced as a flexible array member. >=20 > At the same time, keep -Warray-bounds=3D[1|2] warnings unchanged from > -fstrict-flex-arrays. >=20 > gcc/ChangeLog: >=20 > * attribs.cc (strict_flex_array_level_of): New function. > * attribs.h (strict_flex_array_level_of): Prototype for new function. > * doc/invoke.texi: Document -Wstrict-flex-arrays option. Update > -fstrict-flex-arrays[=3Dn] options. > * gimple-array-bounds.cc (array_bounds_checker::check_array_ref): > Issue warnings for -Wstrict-flex-arrays. > (get_up_bounds_for_array_ref): New function. > (check_out_of_bounds_and_warn): New function. > * opts.cc (finish_options): Issue warnings for unsupported combination > of -Warray-bounds and -fstrict-flex-arrays, -Wstrict_flex_arrays and > -fstrict-flex-array. > * tree-vrp.cc (execute_vrp): Enable the pass when > warn_strict_flex_array is true. > (execute_ranger_vrp): Likewise. > * tree.cc (array_ref_flexible_size_p): Add one new argument. > (component_ref_sam_type): New function. > (component_ref_size): Add one new argument, > * tree.h (array_ref_flexible_size_p): Update prototype. > (enum struct special_array_member): Add two new enum values. > (component_ref_sam_type): New prototype. > (component_ref_size): Update prototype. >=20 > gcc/c-family/ChangeLog: >=20 > * c.opt (Wstrict-flex-arrays): New option. >=20 > gcc/c/ChangeLog: >=20 > * c-decl.cc (is_flexible_array_member_p): Call new function > strict_flex_array_level_of. >=20 > gcc/testsuite/ChangeLog: >=20 > * c-c++-common/Wstrict-flex-arrays.c: New test. > * c-c++-common/Wstrict-flex-arrays_2.c: New test. > * gcc.dg/Wstrict-flex-arrays-2.c: New test. > * gcc.dg/Wstrict-flex-arrays-3.c: New test. > * gcc.dg/Wstrict-flex-arrays-4.c: New test. > * gcc.dg/Wstrict-flex-arrays-5.c: New test. > * gcc.dg/Wstrict-flex-arrays-6.c: New test. > * gcc.dg/Wstrict-flex-arrays-7.c: New test. > * gcc.dg/Wstrict-flex-arrays-8.c: New test. > * gcc.dg/Wstrict-flex-arrays-9.c: New test. > * gcc.dg/Wstrict-flex-arrays.c: New test. > --- > gcc/attribs.cc | 30 ++ > gcc/attribs.h | 2 + > gcc/c-family/c.opt | 5 + > gcc/c/c-decl.cc | 22 +- > gcc/doc/invoke.texi | 33 ++- > gcc/gimple-array-bounds.cc | 264 +++++++++++++----- > gcc/opts.cc | 15 + > .../c-c++-common/Wstrict-flex-arrays.c | 9 + > .../c-c++-common/Wstrict-flex-arrays_2.c | 9 + > gcc/testsuite/gcc.dg/Wstrict-flex-arrays-2.c | 46 +++ > gcc/testsuite/gcc.dg/Wstrict-flex-arrays-3.c | 46 +++ > gcc/testsuite/gcc.dg/Wstrict-flex-arrays-4.c | 49 ++++ > gcc/testsuite/gcc.dg/Wstrict-flex-arrays-5.c | 48 ++++ > gcc/testsuite/gcc.dg/Wstrict-flex-arrays-6.c | 48 ++++ > gcc/testsuite/gcc.dg/Wstrict-flex-arrays-7.c | 50 ++++ > gcc/testsuite/gcc.dg/Wstrict-flex-arrays-8.c | 49 ++++ > gcc/testsuite/gcc.dg/Wstrict-flex-arrays-9.c | 49 ++++ > gcc/testsuite/gcc.dg/Wstrict-flex-arrays.c | 46 +++ > gcc/tree-vrp.cc | 6 +- > gcc/tree.cc | 165 ++++++++--- > gcc/tree.h | 15 +- > 21 files changed, 870 insertions(+), 136 deletions(-) > create mode 100644 gcc/testsuite/c-c++-common/Wstrict-flex-arrays.c > create mode 100644 gcc/testsuite/c-c++-common/Wstrict-flex-arrays_2.c > create mode 100644 gcc/testsuite/gcc.dg/Wstrict-flex-arrays-2.c > create mode 100644 gcc/testsuite/gcc.dg/Wstrict-flex-arrays-3.c > create mode 100644 gcc/testsuite/gcc.dg/Wstrict-flex-arrays-4.c > create mode 100644 gcc/testsuite/gcc.dg/Wstrict-flex-arrays-5.c > create mode 100644 gcc/testsuite/gcc.dg/Wstrict-flex-arrays-6.c > create mode 100644 gcc/testsuite/gcc.dg/Wstrict-flex-arrays-7.c > create mode 100644 gcc/testsuite/gcc.dg/Wstrict-flex-arrays-8.c > create mode 100644 gcc/testsuite/gcc.dg/Wstrict-flex-arrays-9.c > create mode 100644 gcc/testsuite/gcc.dg/Wstrict-flex-arrays.c >=20 > diff --git a/gcc/attribs.cc b/gcc/attribs.cc > index 27dea748561..095def4d6b0 100644 > --- a/gcc/attribs.cc > +++ b/gcc/attribs.cc > @@ -2456,6 +2456,36 @@ init_attr_rdwr_indices (rdwr_map *rwm, tree attrs) > } > } >=20 > +/* Get the LEVEL of the strict_flex_array for the ARRAY_FIELD based on t= he > + values of attribute strict_flex_array and the flag_strict_flex_arrays= . */ > +unsigned int > +strict_flex_array_level_of (tree array_field) > +{ > + gcc_assert (TREE_CODE (array_field) =3D=3D FIELD_DECL); > + unsigned int strict_flex_array_level =3D flag_strict_flex_arrays; > + > + tree attr_strict_flex_array > + =3D lookup_attribute ("strict_flex_array", DECL_ATTRIBUTES (array_fi= eld)); > + /* 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_ar= ray); > + > + /* The attribute has higher priority than flag_struct_flex_array. = */ > + strict_flex_array_level =3D attr_strict_flex_array_level; > + } > + return strict_flex_array_level; > +} > + > + > /* Return the access specification for a function parameter PARM > or null if the current function has no such specification. */ >=20 > diff --git a/gcc/attribs.h b/gcc/attribs.h > index 1dc16e4bc4e..742811e6fda 100644 > --- a/gcc/attribs.h > +++ b/gcc/attribs.h > @@ -398,4 +398,6 @@ extern void init_attr_rdwr_indices (rdwr_map *, tree)= ; > extern attr_access *get_parm_access (rdwr_map &, tree, > tree =3D current_function_decl); >=20 > +extern unsigned int strict_flex_array_level_of (tree); > + > #endif // GCC_ATTRIBS_H > diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt > index 01d480759ae..bc33710a649 100644 > --- a/gcc/c-family/c.opt > +++ b/gcc/c-family/c.opt > @@ -968,6 +968,11 @@ Wstringop-truncation > C ObjC C++ LTO ObjC++ Var(warn_stringop_truncation) Warning Init (1) Lang= EnabledBy(C ObjC C++ LTO ObjC++, Wall) > Warn about truncation in string manipulation functions like strncat and s= trncpy. >=20 > +Wstrict-flex-arrays > +C C++ Var(warn_strict_flex_arrays) Warning > +Warn about inproper usages of flexible array members > +according to the level of -fstrict-flex-arrays. > + > Wsuggest-attribute=3Dformat > C ObjC C++ ObjC++ Var(warn_suggest_attribute_format) Warning > Warn about functions which might be candidates for format attributes. > diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc > index 4746e310d2d..c9cb46daacc 100644 > --- a/gcc/c/c-decl.cc > +++ b/gcc/c/c-decl.cc > @@ -8828,7 +8828,6 @@ finish_incomplete_vars (tree incomplete_vars, bool = toplevel) > } > } >=20 > - > /* Determine whether the FIELD_DECL X is a flexible array member accordin= g to > the following info: > A. whether the FIELD_DECL X is the last field of the DECL_CONTEXT; > @@ -8855,26 +8854,7 @@ is_flexible_array_member_p (bool is_last_field, > 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))= ; >=20 > - unsigned int strict_flex_array_level =3D flag_strict_flex_arrays; > - > - tree attr_strict_flex_array =3D lookup_attribute ("strict_flex_array", > - 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_ar= ray); > - > - /* The attribute has higher priority than flag_struct_flex_array. = */ > - strict_flex_array_level =3D attr_strict_flex_array_level; > - } > + unsigned int strict_flex_array_level =3D strict_flex_array_level_of (x= ); >=20 > switch (strict_flex_array_level) > { > diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi > index 64f77e8367a..a35e6bf27e0 100644 > --- a/gcc/doc/invoke.texi > +++ b/gcc/doc/invoke.texi > @@ -397,7 +397,7 @@ Objective-C and Objective-C++ Dialects}. > -Wstrict-aliasing=3Dn -Wstrict-overflow -Wstrict-overflow=3D@var{n} @go= l > -Wstring-compare @gol > -Wno-stringop-overflow -Wno-stringop-overread @gol > --Wno-stringop-truncation @gol > +-Wno-stringop-truncation -Wstrict-flex-arrays @gol > -Wsuggest-attribute=3D@r{[}pure@r{|}const@r{|}noreturn@r{|}format@r{|}mal= loc@r{]} @gol > -Wswitch -Wno-switch-bool -Wswitch-default -Wswitch-enum @gol > -Wno-switch-outside-range -Wno-switch-unreachable -Wsync-nand @gol > @@ -2848,12 +2848,18 @@ The negative form is equivalent to @option{-fstri= ct-flex-arrays=3D0}, which is the > least strict. All trailing arrays of structures are treated as flexible = array > members. >=20 > +This option is not compatible with @option{-Warray-bounds} due to histor= ical > +reason. The behavior of @option{-Warray-bounds} is not changed by this = option. > + > @item -fstrict-flex-arrays=3D@var{level} > @opindex fstrict-flex-arrays=3D@var{level} > Control when to treat the trailing array of a structure as a flexible arr= ay > member for the purpose of accessing the elements of such an array. The v= alue > of @var{level} controls the level of strictness. >=20 > +This option is not compatible with @option{-Warray-bounds} due to histor= ical > +reason. The behavior of @option{-Warray-bounds} is not changed by this = option. > + > The possible values of @var{level} are the same as for the > @code{strict_flex_array} attribute (@pxref{Variable Attributes}). >=20 > @@ -7662,6 +7668,31 @@ however, are not suitable arguments to functions t= hat expect > such arrays GCC issues warnings unless it can prove that the use is > safe. @xref{Common Variable Attributes}. >=20 > +@item -Wstrict-flex-arrays > +@opindex Wstrict-flex-arrays > +@opindex Wno-strict-flex-arrays > +Warn about inproper usages of flexible array members > +according to the @var{level} of the @code{strict_flex_array (@var{level}= )} > +attribute attached to the trailing array field of a structure if it's > +available, otherwise according to the @var{level} of the option > +@option{-fstrict-flex-arrays=3D@var{level}}. > + > +This option is effective only when @var{level} is bigger than 0. Otherw= ise, > +it will be ignored with a warning. > + > +when @var{level}=3D1, warnings will be issued for a trailing array refer= ence > +of a structure that have 2 or more elements if the trailing array is ref= erenced > +as a flexible array member. > + > +when @var{level}=3D2, in addition to @var{level}=3D1, additional warning= s will be > +issued for a trailing one-element array reference of a structure > +if the array is referenced as a flexible array member. > + > +when @var{level}=3D3, in addition to @var{level}=3D2, additional warning= s will be > +issued for a trailing zero-length array reference of a structure > +if the array is referenced as a flexible array member. > + > + > @item -Wsuggest-attribute=3D@r{[}pure@r{|}const@r{|}noreturn@r{|}format@r= {|}cold@r{|}malloc@r{]} > @opindex Wsuggest-attribute=3D > @opindex Wno-suggest-attribute=3D > diff --git a/gcc/gimple-array-bounds.cc b/gcc/gimple-array-bounds.cc > index fbf448e045d..7f790c5a19a 100644 > --- a/gcc/gimple-array-bounds.cc > +++ b/gcc/gimple-array-bounds.cc > @@ -170,38 +170,20 @@ trailing_array (tree arg, tree *pref) > return array_ref_flexible_size_p (arg); > } >=20 > -/* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible > - arrays and "struct" hacks. If VRP can determine that the array > - subscript is a constant, check if it is outside valid range. If > - the array subscript is a RANGE, warn if it is non-overlapping with > - valid range. IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside > - a ADDR_EXPR. Return true if a warning has been issued or if > - no-warning is set. */ > - > -bool > -array_bounds_checker::check_array_ref (location_t location, tree ref, > - gimple *stmt, bool ignore_off_by_one) > +/* Acquire the upper bound and upper bound plus one for the array > + reference REF and record them into UP_BOUND and UP_BOUND_P1. > + Set *DECL to the decl or expresssion REF refers to. > + FOR_ARRAY_BOUND is true when this is for array bound checking. */ > + > +static void > +get_up_bounds_for_array_ref (tree ref, tree *decl, > + tree *up_bound, tree *up_bound_p1, > + bool for_array_bound) > { > - if (warning_suppressed_p (ref, OPT_Warray_bounds)) > - /* Return true to have the caller prevent warnings for enclosing > - refs. */ > - return true; > - > - tree low_sub =3D TREE_OPERAND (ref, 1); > - tree up_sub =3D low_sub; > - tree up_bound =3D array_ref_up_bound (ref); > - > - /* Referenced decl if one can be determined. */ > - tree decl =3D NULL_TREE; > - > - /* Set for accesses to interior zero-length arrays. */ > - special_array_member sam{ }; > - > - tree up_bound_p1; > - > - if (!up_bound > - || TREE_CODE (up_bound) !=3D INTEGER_CST > - || (warn_array_bounds < 2 && trailing_array (ref, &decl))) > + if (!(*up_bound) > + || TREE_CODE (*up_bound) !=3D INTEGER_CST > + || ((for_array_bound ? (warn_array_bounds < 2) : warn_strict_flex_= arrays) > + && trailing_array (ref, decl))) > { > /* Accesses to trailing arrays via pointers may access storage > beyond the types array bounds. For such arrays, or for flexible > @@ -213,8 +195,8 @@ array_bounds_checker::check_array_ref (location_t loc= ation, tree ref, > if (TREE_CODE (eltsize) !=3D INTEGER_CST > || integer_zerop (eltsize)) > { > - up_bound =3D NULL_TREE; > - up_bound_p1 =3D NULL_TREE; > + *up_bound =3D NULL_TREE; > + *up_bound_p1 =3D NULL_TREE; > } > else > { > @@ -227,7 +209,8 @@ array_bounds_checker::check_array_ref (location_t loc= ation, tree ref, > { > /* Try to determine the size of the trailing array from > its initializer (if it has one). */ > - if (tree refsize =3D component_ref_size (arg, &sam)) > + if (tree refsize > + =3D component_ref_size (arg, NULL, for_array_bound)) > if (TREE_CODE (refsize) =3D=3D INTEGER_CST) > maxbound =3D refsize; > } > @@ -246,7 +229,7 @@ array_bounds_checker::check_array_ref (location_t loc= ation, tree ref, > { > /* Try to determine the size from a pointer to > an array if BASE is one. */ > - if (tree size =3D get_ref_size (base, &decl)) > + if (tree size =3D get_ref_size (base, decl)) > maxbound =3D size; > } > else if (!compref && DECL_P (base)) > @@ -254,7 +237,7 @@ array_bounds_checker::check_array_ref (location_t loc= ation, tree ref, > if (TREE_CODE (basesize) =3D=3D INTEGER_CST) > { > maxbound =3D basesize; > - decl =3D base; > + *decl =3D base; > } >=20 > if (known_gt (off, 0)) > @@ -266,40 +249,47 @@ array_bounds_checker::check_array_ref (location_t l= ocation, tree ref, > else > maxbound =3D fold_convert (sizetype, maxbound); >=20 > - up_bound_p1 =3D int_const_binop (TRUNC_DIV_EXPR, maxbound, eltsize); > + *up_bound_p1 =3D int_const_binop (TRUNC_DIV_EXPR, maxbound, eltsize); >=20 > - if (up_bound_p1 !=3D NULL_TREE) > - up_bound =3D int_const_binop (MINUS_EXPR, up_bound_p1, > + if (*up_bound_p1 !=3D NULL_TREE) > + *up_bound =3D int_const_binop (MINUS_EXPR, *up_bound_p1, > build_int_cst (ptrdiff_type_node, 1)); > else > - up_bound =3D NULL_TREE; > + *up_bound =3D NULL_TREE; > } > } > else > - up_bound_p1 =3D int_const_binop (PLUS_EXPR, up_bound, > - build_int_cst (TREE_TYPE (up_bound), 1)); > + *up_bound_p1 =3D int_const_binop (PLUS_EXPR, *up_bound, > + build_int_cst (TREE_TYPE (*up_bound), 1)); > + return; > +} >=20 > - tree low_bound =3D array_ref_low_bound (ref); > +/* Given the LOW_SUB_ORG, LOW_SUB and UP_SUB, and the computed UP_BOUND > + and UP_BOUND_P1, check whether the array reference REF is out of boun= d. > + When out of bounds, issue warnings if FOR_ARRAY_BOUND is true. > + otherwise, return true without issue warnings. */ >=20 > +static bool > +check_out_of_bounds_and_warn (location_t location, tree ref, > + tree low_sub_org, tree low_sub, tree up_sub, > + tree up_bound, tree up_bound_p1, > + const value_range *vr, > + bool ignore_off_by_one, bool for_array_bound) > +{ > + tree low_bound =3D array_ref_low_bound (ref); > tree artype =3D TREE_TYPE (TREE_OPERAND (ref, 0)); >=20 > bool warned =3D false; >=20 > /* Empty array. */ > if (up_bound && tree_int_cst_equal (low_bound, up_bound_p1)) > - warned =3D warning_at (location, OPT_Warray_bounds, > - "array subscript %E is outside array bounds of %qT", > - low_sub, artype); > - > - const value_range *vr =3D NULL; > - if (TREE_CODE (low_sub) =3D=3D SSA_NAME) > { > - vr =3D get_value_range (low_sub, stmt); > - if (!vr->undefined_p () && !vr->varying_p ()) > - { > - low_sub =3D vr->kind () =3D=3D VR_RANGE ? vr->max () : vr->min (); > - up_sub =3D vr->kind () =3D=3D VR_RANGE ? vr->min () : vr->max (); > - } > + if (for_array_bound) > + warned =3D warning_at (location, OPT_Warray_bounds, > + "array subscript %E is outside array" > + " bounds of %qT", low_sub_org, artype); > + else > + warned =3D true; > } >=20 > if (warned) > @@ -313,24 +303,127 @@ array_bounds_checker::check_array_ref (location_t = location, tree ref, > : tree_int_cst_le (up_bound, up_sub)) > && TREE_CODE (low_sub) =3D=3D INTEGER_CST > && tree_int_cst_le (low_sub, low_bound)) > - warned =3D warning_at (location, OPT_Warray_bounds, > - "array subscript [%E, %E] is outside " > - "array bounds of %qT", > - low_sub, up_sub, artype); > + { > + if (for_array_bound) > + warned =3D warning_at (location, OPT_Warray_bounds, > + "array subscript [%E, %E] is outside " > + "array bounds of %qT", > + low_sub, up_sub, artype); > + else > + warned =3D true; > + } > } > else if (up_bound > && TREE_CODE (up_sub) =3D=3D INTEGER_CST > && (ignore_off_by_one > ? !tree_int_cst_le (up_sub, up_bound_p1) > : !tree_int_cst_le (up_sub, up_bound))) > - warned =3D warning_at (location, OPT_Warray_bounds, > - "array subscript %E is above array bounds of %qT", > - up_sub, artype); > + { > + if (for_array_bound) > + warned =3D warning_at (location, OPT_Warray_bounds, > + "array subscript %E is above array bounds of %qT", > + up_sub, artype); > + else > + warned =3D true; > + } > else if (TREE_CODE (low_sub) =3D=3D INTEGER_CST > && tree_int_cst_lt (low_sub, low_bound)) > - warned =3D warning_at (location, OPT_Warray_bounds, > - "array subscript %E is below array bounds of %qT", > - low_sub, artype); > + { > + if (for_array_bound) > + warned =3D warning_at (location, OPT_Warray_bounds, > + "array subscript %E is below array bounds of %qT", > + low_sub, artype); > + else > + warned =3D true; > + } > + return warned; > +} > + > +/* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible > + arrays and "struct" hacks. If VRP can determine that the array > + subscript is a constant, check if it is outside valid range. If > + the array subscript is a RANGE, warn if it is non-overlapping with > + valid range. IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside > + a ADDR_EXPR. Return true if a warning has been issued or if > + no-warning is set. */ > + > +bool > +array_bounds_checker::check_array_ref (location_t location, tree ref, > + gimple *stmt, bool ignore_off_by_one) > +{ > + if (warning_suppressed_p (ref, OPT_Warray_bounds)) > + /* Return true to have the caller prevent warnings for enclosing > + refs. */ > + return true; > + > + /* Upper bound and Upper bound plus one for -Warray-bounds. */ > + tree up_bound =3D array_ref_up_bound (ref); > + tree up_bound_p1 =3D NULL_TREE; > + > + /* Upper bound and upper bound plus one for -Wstrict-flex-array. */ > + tree up_bound_strict =3D up_bound; > + tree up_bound_p1_strict =3D NULL_TREE; > + > + /* Referenced decl if one can be determined. */ > + tree decl =3D NULL_TREE; > + > + /* Set to the type of the special array member for a COMPONENT_REF. *= / > + special_array_member sam{ }; > + > + tree arg =3D TREE_OPERAND (ref, 0); > + const bool compref =3D TREE_CODE (arg) =3D=3D COMPONENT_REF; > + > + unsigned int strict_flex_array_level =3D flag_strict_flex_arrays; > + > + if (compref) > + { > + /* Try to determine special array member type for this COMPONENT_R= EF. */ > + sam =3D component_ref_sam_type (arg); > + /* Get the level of strict_flex_array for this array field. */ > + tree afield_decl =3D TREE_OPERAND (arg, 1); > + strict_flex_array_level =3D strict_flex_array_level_of (afield_dec= l); > + } > + > + if (warn_array_bounds) > + get_up_bounds_for_array_ref (ref, &decl, &up_bound, &up_bound_p1, > + true); > + if (warn_strict_flex_arrays) > + get_up_bounds_for_array_ref (ref, &decl, &up_bound_strict, > + &up_bound_p1_strict, false); > + > + > + > + bool warned =3D false; > + bool out_of_bound =3D false; > + > + tree artype =3D TREE_TYPE (TREE_OPERAND (ref, 0)); > + tree low_sub_org =3D TREE_OPERAND (ref, 1); > + tree up_sub =3D low_sub_org; > + tree low_sub =3D low_sub_org; > + > + const value_range *vr =3D NULL; > + if (TREE_CODE (low_sub_org) =3D=3D SSA_NAME) > + { > + vr =3D get_value_range (low_sub_org, stmt); > + if (!vr->undefined_p () && !vr->varying_p ()) > + { > + low_sub =3D vr->kind () =3D=3D VR_RANGE ? vr->max () : vr->min (); > + up_sub =3D vr->kind () =3D=3D VR_RANGE ? vr->min () : vr->max (); > + } > + } > + > + if (warn_array_bounds) > + warned =3D check_out_of_bounds_and_warn (location, ref, > + low_sub_org, low_sub, up_sub, > + up_bound, up_bound_p1, vr, > + ignore_off_by_one, true); > + > + if (warn_strict_flex_arrays) > + out_of_bound =3D check_out_of_bounds_and_warn (location, ref, > + low_sub_org, low_sub, up_sub, > + up_bound_strict, > + up_bound_p1_strict, vr, > + ignore_off_by_one, false); >=20 > if (!warned && sam =3D=3D special_array_member::int_0) > warned =3D warning_at (location, OPT_Wzero_length_bounds, > @@ -341,19 +434,56 @@ array_bounds_checker::check_array_ref (location_t l= ocation, tree ref, > "of an interior zero-length array %qT")), > low_sub, artype); >=20 > - if (warned) > + if (warned || out_of_bound) > { > - if (dump_file && (dump_flags & TDF_DETAILS)) > + if (warned && dump_file && (dump_flags & TDF_DETAILS)) > { > fprintf (dump_file, "Array bound warning for "); > dump_generic_expr (MSG_NOTE, TDF_SLIM, ref); > fprintf (dump_file, "\n"); > } >=20 > + /* issue warnings for -Wstrict-flex-arrays according to the level = of > + flag_strict_flex_arrays. */ > + if (out_of_bound && warn_strict_flex_arrays) > + switch (strict_flex_array_level) > + { > + case 3: > + /* Issue additional warnings for trailing arrays [0]. */ > + if (sam =3D=3D special_array_member::trail_0) > + warned =3D warning_at (location, OPT_Wstrict_flex_arrays, > + "trailing array %qT should not be used as " > + "a flexible array member for level 3", > + artype); > + /* FALLTHROUGH. */ > + case 2: > + /* Issue additional warnings for trailing arrays [1]. */ > + if (sam =3D=3D special_array_member::trail_1) > + warned =3D warning_at (location, OPT_Wstrict_flex_arrays, > + "trailing array %qT should not be used as " > + "a flexible array member for level 2 and " > + "above", artype); > + /* FALLTHROUGH. */ > + case 1: > + /* Issue warnings for trailing arrays [n]. */ > + if (sam =3D=3D special_array_member::trail_n) > + warned =3D warning_at (location, OPT_Wstrict_flex_arrays, > + "trailing array %qT should not be used as " > + "a flexible array member for level 1 and " > + "above", artype); > + break; > + case 0: > + /* Do nothing. */ > + break; > + default: > + gcc_unreachable (); > + } > + > /* Avoid more warnings when checking more significant subscripts > of the same expression. */ > ref =3D TREE_OPERAND (ref, 0); > suppress_warning (ref, OPT_Warray_bounds); > + suppress_warning (ref, OPT_Wstrict_flex_arrays); >=20 > if (decl) > ref =3D decl; > diff --git a/gcc/opts.cc b/gcc/opts.cc > index ae079fcd20e..ca162aa781c 100644 > --- a/gcc/opts.cc > +++ b/gcc/opts.cc > @@ -1409,6 +1409,21 @@ finish_options (struct gcc_options *opts, struct g= cc_options *opts_set, > opts->x_profile_flag =3D 0; > } >=20 > + if (opts->x_warn_array_bounds) > + if (opts->x_flag_strict_flex_arrays) > + { > + warning_at (UNKNOWN_LOCATION, 0, > + "%<-Warray-bounds%> is not impacted by " > + "%<-fstrict-flex-arrays%>"); > + } > + if (opts->x_warn_strict_flex_arrays) > + if (opts->x_flag_strict_flex_arrays =3D=3D 0) > + { > + opts->x_warn_strict_flex_arrays =3D 0; > + warning_at (UNKNOWN_LOCATION, 0, > + "%<-Wstrict-flex-arrays%> is ignored when" > + " %<-fstrict-flex-arrays%> does not present"); > + } >=20 > diagnose_options (opts, opts_set, loc); > } > diff --git a/gcc/testsuite/c-c++-common/Wstrict-flex-arrays.c b/gcc/tests= uite/c-c++-common/Wstrict-flex-arrays.c > new file mode 100644 > index 00000000000..72b4b7c6406 > --- /dev/null > +++ b/gcc/testsuite/c-c++-common/Wstrict-flex-arrays.c > @@ -0,0 +1,9 @@ > +/* Test the usage of option -Wstrict-flex-arrays. */ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -Wstrict-flex-arrays" } */ > + > +int main(int argc, char *argv[]) > +{ > + return 0; > +} > +/* { dg-warning "is ignored when \'-fstrict-flex-arrays\' does not prese= nt" "" { target *-*-* } 0 } */ > diff --git a/gcc/testsuite/c-c++-common/Wstrict-flex-arrays_2.c b/gcc/tes= tsuite/c-c++-common/Wstrict-flex-arrays_2.c > new file mode 100644 > index 00000000000..af9ec922dea > --- /dev/null > +++ b/gcc/testsuite/c-c++-common/Wstrict-flex-arrays_2.c > @@ -0,0 +1,9 @@ > +/* Test the usage of option -Wstrict-flex-arrays. */ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -Warray-bounds -fstrict-flex-arrays" } */ > + > +int main(int argc, char *argv[]) > +{ > + return 0; > +} > +/* { dg-warning "\'-Warray-bounds\' is not impacted by \'-fstrict-flex-a= rrays\'" "" { target *-*-* } 0 } */ > diff --git a/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-2.c b/gcc/testsuite= /gcc.dg/Wstrict-flex-arrays-2.c > new file mode 100644 > index 00000000000..59d5a5fcb23 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-2.c > @@ -0,0 +1,46 @@ > +/* Test -Wstrict-flex-arrays. */ > +/* { dg-do run } */ > +/* { dg-options "-O2 -Wstrict-flex-arrays -fstrict-flex-arrays=3D2" } */ > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4];=20 > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1];=20 > +}; > + > +struct trailing_array_3 { > + int a; > + int b; > + int c[0]; > +}; > +struct trailing_array_4 { > + int a; > + int b; > + int c[]; > +}; > + > +void __attribute__((__noinline__)) stuff( > + struct trailing_array_1 *normal, > + struct trailing_array_2 *trailing_1, > + struct trailing_array_3 *trailing_0, > + struct trailing_array_4 *trailing_flex) > +{ > + normal->c[5] =3D 5; /*{ dg-warning "should not be used as a flexibl= e array member for level 1 and above" } */ > + trailing_1->c[2] =3D 2; /* { dg-warning "should not be used as a fle= xible array member for level 2 and above" } */ > + trailing_0->c[1] =3D 1; /* { dg-bogus "should not be used as a flexi= ble array member for level 2 and above" } */ > + trailing_flex->c[10] =3D 10; /* { dg-bogus "should not be used as a = flexible array member for level 2 and above" } */ > + > +} > + > +int main(int argc, char *argv[]) > +{ > + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)arg= v[0]); > + > + return 0; > +} > diff --git a/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-3.c b/gcc/testsuite= /gcc.dg/Wstrict-flex-arrays-3.c > new file mode 100644 > index 00000000000..2f66151e927 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-3.c > @@ -0,0 +1,46 @@ > +/* Test -Wstrict-flex-arrays. */ > +/* { dg-do run } */ > +/* { dg-options "-O2 -Wstrict-flex-arrays -fstrict-flex-arrays=3D3" } */ > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4];=20 > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1];=20 > +}; > + > +struct trailing_array_3 { > + int a; > + int b; > + int c[0]; > +}; > +struct trailing_array_4 { > + int a; > + int b; > + int c[]; > +}; > + > +void __attribute__((__noinline__)) stuff( > + struct trailing_array_1 *normal, > + struct trailing_array_2 *trailing_1, > + struct trailing_array_3 *trailing_0, > + struct trailing_array_4 *trailing_flex) > +{ > + normal->c[5] =3D 5; /*{ dg-warning "should not be used as a flexibl= e array member for level 1 and above" } */ > + trailing_1->c[2] =3D 2; /* { dg-warning "should not be used as a fle= xible array member for level 2 and above" } */ > + trailing_0->c[1] =3D 1; /* { dg-warning "should not be used as a fle= xible array member for level 3" } */ > + trailing_flex->c[10] =3D 10; /* { dg-bogus "should not be used as a = flexible array member for level 3" } */ > + > +} > + > +int main(int argc, char *argv[]) > +{ > + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)arg= v[0]); > + > + return 0; > +} > diff --git a/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-4.c b/gcc/testsuite= /gcc.dg/Wstrict-flex-arrays-4.c > new file mode 100644 > index 00000000000..6a4576568ac > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-4.c > @@ -0,0 +1,49 @@ > +/* Test -Wstrict-flex-arrays + -Warray-bounds. */ > +/* { dg-do run } */ > +/* { dg-options "-O2 -Wstrict-flex-arrays -fstrict-flex-arrays=3D1 -Warr= ay-bounds" } */ > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4];=20 > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1];=20 > +}; > + > +struct trailing_array_3 { > + int a; > + int b; > + int c[0]; > +}; > +struct trailing_array_4 { > + int a; > + int b; > + int c[]; > +}; > + > +void __attribute__((__noinline__)) stuff( > + struct trailing_array_1 *normal, > + struct trailing_array_2 *trailing_1, > + struct trailing_array_3 *trailing_0, > + struct trailing_array_4 *trailing_flex) > +{ > + normal->c[5] =3D 5; /*{ dg-warning "should not be used as a flexibl= e array member for level 1 and above" } */ > + /*{ dg-warning "array subscript 5 is above array bounds of" "" {= target *-*-* } .-1 } */ > + trailing_1->c[2] =3D 2; /* { dg-bogus "should not be used as a flexi= ble array member for level 1 and above" } */ > + trailing_0->c[1] =3D 1; /* { dg-bogus "should not be used as a flexi= ble array member for level 1 and above" } */ > + trailing_flex->c[10] =3D 10; /* { dg-bogus "should not be used as a = flexible array member for level 1 and above" } */ > + > +} > + > +int main(int argc, char *argv[]) > +{ > + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)arg= v[0]); > + > + return 0; > +} > + > +/* { dg-warning "\'-Warray-bounds\' is not impacted by \'-fstrict-flex-a= rrays\'" "" { target *-*-* } 0 } */ > diff --git a/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-5.c b/gcc/testsuite= /gcc.dg/Wstrict-flex-arrays-5.c > new file mode 100644 > index 00000000000..e550fbd24e1 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-5.c > @@ -0,0 +1,48 @@ > +/* Test -Wstrict-flex-arrays + -Warray-bounds. */ > +/* { dg-do run } */ > +/* { dg-options "-O2 -Wstrict-flex-arrays -fstrict-flex-arrays=3D2 -Warr= ay-bounds" } */ > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4];=20 > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1];=20 > +}; > + > +struct trailing_array_3 { > + int a; > + int b; > + int c[0]; > +}; > +struct trailing_array_4 { > + int a; > + int b; > + int c[]; > +}; > + > +void __attribute__((__noinline__)) stuff( > + struct trailing_array_1 *normal, > + struct trailing_array_2 *trailing_1, > + struct trailing_array_3 *trailing_0, > + struct trailing_array_4 *trailing_flex) > +{ > + normal->c[5] =3D 5; /*{ dg-warning "should not be used as a flexibl= e array member for level 1 and above" } */ > + /*{ dg-warning "array subscript 5 is above array bounds of" "" {= target *-*-* } .-1 } */ > + trailing_1->c[2] =3D 2; /* { dg-warning "should not be used as a fle= xible array member for level 2 and above" } */ > + trailing_0->c[1] =3D 1; /* { dg-bogus "should not be used as a flexi= ble array member for level 2 and above" } */ > + trailing_flex->c[10] =3D 10; /* { dg-bogus "should not be used as a = flexible array member for level 2 and above" } */ > + > +} > + > +int main(int argc, char *argv[]) > +{ > + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)arg= v[0]); > + > + return 0; > +} > +/* { dg-warning "\'-Warray-bounds\' is not impacted by \'-fstrict-flex-a= rrays\'" "" { target *-*-* } 0 } */ > diff --git a/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-6.c b/gcc/testsuite= /gcc.dg/Wstrict-flex-arrays-6.c > new file mode 100644 > index 00000000000..d5b61136b3a > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-6.c > @@ -0,0 +1,48 @@ > +/* Test -Wstrict-flex-arrays. */ > +/* { dg-do run } */ > +/* { dg-options "-O2 -Wstrict-flex-arrays -fstrict-flex-arrays=3D3 -Warr= ay-bounds" } */ > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4];=20 > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1];=20 > +}; > + > +struct trailing_array_3 { > + int a; > + int b; > + int c[0]; > +}; > +struct trailing_array_4 { > + int a; > + int b; > + int c[]; > +}; > + > +void __attribute__((__noinline__)) stuff( > + struct trailing_array_1 *normal, > + struct trailing_array_2 *trailing_1, > + struct trailing_array_3 *trailing_0, > + struct trailing_array_4 *trailing_flex) > +{ > + normal->c[5] =3D 5; /*{ dg-warning "should not be used as a flexibl= e array member for level 1 and above" } */ > + /*{ dg-warning "array subscript 5 is above array bounds of" "" { t= arget *-*-* } .-1 } */=20 > + trailing_1->c[2] =3D 2; /* { dg-warning "should not be used as a fle= xible array member for level 2 and above" } */ > + trailing_0->c[1] =3D 1; /* { dg-warning "should not be used as a fle= xible array member for level 3" } */ > + trailing_flex->c[10] =3D 10; /* { dg-bogus "should not be used as a = flexible array member for level 3" } */ > + > +} > + > +int main(int argc, char *argv[]) > +{ > + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)arg= v[0]); > + > + return 0; > +} > +/* { dg-warning "\'-Warray-bounds\' is not impacted by \'-fstrict-flex-a= rrays\'" "" { target *-*-* } 0 } */ > diff --git a/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-7.c b/gcc/testsuite= /gcc.dg/Wstrict-flex-arrays-7.c > new file mode 100644 > index 00000000000..eb82a60c261 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-7.c > @@ -0,0 +1,50 @@ > +/* Test -Wstrict-flex-arrays + -Warray-bounds=3D2. */ > +/* { dg-do run } */ > +/* { dg-options "-O2 -Wstrict-flex-arrays -fstrict-flex-arrays=3D1 -Warr= ay-bounds=3D2" } */ > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4];=20 > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1];=20 > +}; > + > +struct trailing_array_3 { > + int a; > + int b; > + int c[0]; > +}; > +struct trailing_array_4 { > + int a; > + int b; > + int c[]; > +}; > + > +void __attribute__((__noinline__)) stuff( > + struct trailing_array_1 *normal, > + struct trailing_array_2 *trailing_1, > + struct trailing_array_3 *trailing_0, > + struct trailing_array_4 *trailing_flex) > +{ > + normal->c[5] =3D 5; /*{ dg-warning "should not be used as a flexibl= e array member for level 1 and above" } */ > + /*{ dg-warning "array subscript 5 is above array bounds of" "" {= target *-*-* } .-1 } */ > + trailing_1->c[2] =3D 2; /* { dg-bogus "should not be used as a flexi= ble array member for level 1 and above" } */ > + /*{ dg-warning "array subscript 2 is above array bounds of" "" {= target *-*-* } .-1 } */ > + trailing_0->c[1] =3D 1; /* { dg-bogus "should not be used as a flexi= ble array member for level 1 and above" } */ > + trailing_flex->c[10] =3D 10; /* { dg-bogus "should not be used as a = flexible array member for level 1 and above" } */ > + > +} > + > +int main(int argc, char *argv[]) > +{ > + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)arg= v[0]); > + > + return 0; > +} > + > +/* { dg-warning "\'-Warray-bounds\' is not impacted by \'-fstrict-flex-a= rrays\'" "" { target *-*-* } 0 } */ > diff --git a/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-8.c b/gcc/testsuite= /gcc.dg/Wstrict-flex-arrays-8.c > new file mode 100644 > index 00000000000..f48a50e04f5 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-8.c > @@ -0,0 +1,49 @@ > +/* Test -Wstrict-flex-arrays + -Warray-bounds=3D2. */ > +/* { dg-do run } */ > +/* { dg-options "-O2 -Wstrict-flex-arrays -fstrict-flex-arrays=3D2 -Warr= ay-bounds=3D2" } */ > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4];=20 > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1];=20 > +}; > + > +struct trailing_array_3 { > + int a; > + int b; > + int c[0]; > +}; > +struct trailing_array_4 { > + int a; > + int b; > + int c[]; > +}; > + > +void __attribute__((__noinline__)) stuff( > + struct trailing_array_1 *normal, > + struct trailing_array_2 *trailing_1, > + struct trailing_array_3 *trailing_0, > + struct trailing_array_4 *trailing_flex) > +{ > + normal->c[5] =3D 5; /*{ dg-warning "should not be used as a flexibl= e array member for level 1 and above" } */ > + /*{ dg-warning "array subscript 5 is above array bounds of" "" {= target *-*-* } .-1 } */ > + trailing_1->c[2] =3D 2; /* { dg-warning "should not be used as a fle= xible array member for level 2 and above" } */ > + /*{ dg-warning "array subscript 2 is above array bounds of" "" = { target *-*-* } .-1 } */ > + trailing_0->c[1] =3D 1; /* { dg-bogus "should not be used as a flexi= ble array member for level 2 and above" } */ > + trailing_flex->c[10] =3D 10; /* { dg-bogus "should not be used as a = flexible array member for level 2 and above" } */ > + > +} > + > +int main(int argc, char *argv[]) > +{ > + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)arg= v[0]); > + > + return 0; > +} > +/* { dg-warning "\'-Warray-bounds\' is not impacted by \'-fstrict-flex-a= rrays\'" "" { target *-*-* } 0 } */ > diff --git a/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-9.c b/gcc/testsuite= /gcc.dg/Wstrict-flex-arrays-9.c > new file mode 100644 > index 00000000000..9f2877ef4e1 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-9.c > @@ -0,0 +1,49 @@ > +/* Test -Wstrict-flex-arrays + -Warray-bounds=3D2. */ > +/* { dg-do run } */ > +/* { dg-options "-O2 -Wstrict-flex-arrays -fstrict-flex-arrays=3D3 -Warr= ay-bounds=3D2" } */ > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4];=20 > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1];=20 > +}; > + > +struct trailing_array_3 { > + int a; > + int b; > + int c[0]; > +}; > +struct trailing_array_4 { > + int a; > + int b; > + int c[]; > +}; > + > +void __attribute__((__noinline__)) stuff( > + struct trailing_array_1 *normal, > + struct trailing_array_2 *trailing_1, > + struct trailing_array_3 *trailing_0, > + struct trailing_array_4 *trailing_flex) > +{ > + normal->c[5] =3D 5; /*{ dg-warning "should not be used as a flexibl= e array member for level 1 and above" } */ > + /*{ dg-warning "array subscript 5 is above array bounds of" "" { t= arget *-*-* } .-1 } */=20 > + trailing_1->c[2] =3D 2; /* { dg-warning "should not be used as a fle= xible array member for level 2 and above" } */ > + /*{ dg-warning "array subscript 2 is above array bounds of" "" { = target *-*-* } .-1 } */ > + trailing_0->c[1] =3D 1; /* { dg-warning "should not be used as a fle= xible array member for level 3" } */ > + trailing_flex->c[10] =3D 10; /* { dg-bogus "should not be used as a = flexible array member for level 3" } */ > + > +} > + > +int main(int argc, char *argv[]) > +{ > + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)arg= v[0]); > + > + return 0; > +} > +/* { dg-warning "\'-Warray-bounds\' is not impacted by \'-fstrict-flex-a= rrays\'" "" { target *-*-* } 0 } */ > diff --git a/gcc/testsuite/gcc.dg/Wstrict-flex-arrays.c b/gcc/testsuite/g= cc.dg/Wstrict-flex-arrays.c > new file mode 100644 > index 00000000000..43a9098f138 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/Wstrict-flex-arrays.c > @@ -0,0 +1,46 @@ > +/* Test -Wstrict-flex-arrays. */ > +/* { dg-do run } */ > +/* { dg-options "-O2 -Wstrict-flex-arrays -fstrict-flex-arrays=3D1" } */ > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4];=20 > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1];=20 > +}; > + > +struct trailing_array_3 { > + int a; > + int b; > + int c[0]; > +}; > +struct trailing_array_4 { > + int a; > + int b; > + int c[]; > +}; > + > +void __attribute__((__noinline__)) stuff( > + struct trailing_array_1 *normal, > + struct trailing_array_2 *trailing_1, > + struct trailing_array_3 *trailing_0, > + struct trailing_array_4 *trailing_flex) > +{ > + normal->c[5] =3D 5; /*{ dg-warning "should not be used as a flexibl= e array member for level 1 and above" } */ > + trailing_1->c[2] =3D 2; /* { dg-bogus "should not be used as a flexi= ble array member for level 1 and above" } */ > + trailing_0->c[1] =3D 1; /* { dg-bogus "should not be used as a flexi= ble array member for level 1 and above" } */ > + trailing_flex->c[10] =3D 10; /* { dg-bogus "should not be used as a = flexible array member for level 1 and above" } */ > + > +} > + > +int main(int argc, char *argv[]) > +{ > + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)arg= v[0]); > + > + return 0; > +} > diff --git a/gcc/tree-vrp.cc b/gcc/tree-vrp.cc > index e5a292bb875..1f1b56c1a52 100644 > --- a/gcc/tree-vrp.cc > +++ b/gcc/tree-vrp.cc > @@ -4229,12 +4229,12 @@ execute_vrp (struct function *fun, bool warn_arra= y_bounds_p) > the flag on every edge now, rather than in > check_array_bounds_dom_walker's ctor; vrp_folder may clear > it from some edges. */ > - if (warn_array_bounds && warn_array_bounds_p) > + if ((warn_array_bounds || warn_strict_flex_arrays) && warn_array_bound= s_p) > set_all_edges_as_executable (fun); >=20 > folder.substitute_and_fold (); >=20 > - if (warn_array_bounds && warn_array_bounds_p) > + if ((warn_array_bounds || warn_strict_flex_arrays) && warn_array_bound= s_p) > { > array_bounds_checker array_checker (fun, &vrp_vr_values); > array_checker.check (); > @@ -4353,7 +4353,7 @@ execute_ranger_vrp (struct function *fun, bool warn= _array_bounds_p) > if (dump_file && (dump_flags & TDF_DETAILS)) > ranger->dump (dump_file); >=20 > - if (warn_array_bounds && warn_array_bounds_p) > + if ((warn_array_bounds || warn_strict_flex_arrays) && warn_array_bound= s_p) > { > // Set all edges as executable, except those ranger says aren't. > int non_exec_flag =3D ranger->non_executable_edge_flag; > diff --git a/gcc/tree.cc b/gcc/tree.cc > index d2b0b34a725..0f5f7151b6b 100644 > --- a/gcc/tree.cc > +++ b/gcc/tree.cc > @@ -12726,15 +12726,21 @@ array_ref_up_bound (tree exp) > 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 ...; > + > + If non-null, set IS_TRAILING_ARRAY to true if the ref is the above ca= se A. > */ >=20 > bool > -array_ref_flexible_size_p (tree ref) > +array_ref_flexible_size_p (tree ref, bool *is_trailing_array /* =3D NULL= */) > { > - /* the TYPE for this array referece. */ > + /* The TYPE for this array referece. */ > tree atype =3D NULL_TREE; > - /* the FIELD_DECL for the array field in the containing structure. */ > + /* The FIELD_DECL for the array field in the containing structure. */ > tree afield_decl =3D NULL_TREE; > + /* Whether this array is the trailing array of a structure. */ > + bool is_trailing_array_tmp =3D false; > + if (!is_trailing_array) > + is_trailing_array =3D &is_trailing_array_tmp; >=20 > if (TREE_CODE (ref) =3D=3D ARRAY_REF > || TREE_CODE (ref) =3D=3D ARRAY_RANGE_REF) > @@ -12822,7 +12828,10 @@ array_ref_flexible_size_p (tree ref) > if (! TYPE_SIZE (atype) > || ! TYPE_DOMAIN (atype) > || ! TYPE_MAX_VALUE (TYPE_DOMAIN (atype))) > - return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; > + { > + *is_trailing_array =3D afield_decl && TREE_CODE (afield_decl) =3D= =3D FIELD_DECL; > + return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; > + } >=20 > /* If the reference is based on a declared entity, the size of the arra= y > is constrained by its given domain. (Do not trust commons PR/69368)= . */ > @@ -12844,9 +12853,17 @@ array_ref_flexible_size_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 INTEGE= R_CST) > - return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; > + { > + *is_trailing_array > + =3D afield_decl && TREE_CODE (afield_decl) =3D=3D FIELD_DECL; > + return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; > + } > if (! get_addr_base_and_unit_offset (ref_to_array, &offset)) > - return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; > + { > + *is_trailing_array > + =3D afield_decl && TREE_CODE (afield_decl) =3D=3D FIELD_DECL; > + return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; > + } >=20 > /* If at least one extra element fits it is a flexarray. */ > if (known_le ((wi::to_offset (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))) > @@ -12854,11 +12871,16 @@ array_ref_flexible_size_p (tree ref) > + 2) > * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (atype))), > wi::to_offset (DECL_SIZE_UNIT (ref)) - offset)) > - return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; > + { > + *is_trailing_array > + =3D afield_decl && TREE_CODE (afield_decl) =3D=3D FIELD_DECL; > + return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; > + } >=20 > return false; > } >=20 > + *is_trailing_array =3D afield_decl && TREE_CODE (afield_decl) =3D=3D F= IELD_DECL; > return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; > } >=20 > @@ -12920,24 +12942,81 @@ get_initializer_for (tree init, tree decl) > return NULL_TREE; > } >=20 > +/* Determines the special array member type for the array reference REF.= */ > +special_array_member > +component_ref_sam_type (tree ref) > +{ > + special_array_member sam_type =3D special_array_member::none; > + > + tree member =3D TREE_OPERAND (ref, 1); > + tree memsize =3D DECL_SIZE_UNIT (member); > + if (memsize) > + { > + tree memtype =3D TREE_TYPE (member); > + if (TREE_CODE (memtype) !=3D ARRAY_TYPE) > + return sam_type; > + > + bool trailing =3D false; > + (void)array_ref_flexible_size_p (ref, &trailing); > + bool zero_length =3D integer_zerop (memsize); > + if (!trailing && !zero_length) > + /* MEMBER is an interior array with > + more than one element. */ > + return special_array_member::int_n; > + > + if (zero_length) > + { > + if (trailing) > + return special_array_member::trail_0; > + else > + return special_array_member::int_0; > + } > + > + if (!zero_length) > + if (tree dom =3D TYPE_DOMAIN (memtype)) > + if (tree min =3D TYPE_MIN_VALUE (dom)) > + if (tree max =3D TYPE_MAX_VALUE (dom)) > + if (TREE_CODE (min) =3D=3D INTEGER_CST > + && TREE_CODE (max) =3D=3D INTEGER_CST) > + { > + offset_int minidx =3D wi::to_offset (min); > + offset_int maxidx =3D wi::to_offset (max); > + offset_int neltsm1 =3D maxidx - minidx; > + if (neltsm1 > 0) > + /* MEMBER is a trailing array with more than > + one elements. */ > + return special_array_member::trail_n; > + > + if (neltsm1 =3D=3D 0) > + return special_array_member::trail_1; > + } > + } > + > + return sam_type; > +} > + > /* Determines the size of the member referenced by the COMPONENT_REF > REF, using its initializer expression if necessary in order to > determine the size of an initialized flexible array member. > - If non-null, set *ARK when REF refers to an interior zero-length > + If non-null, set *SAM when REF refers to an interior zero-length > array or a trailing one-element array. > Returns the size as sizetype (which might be zero for an object > with an uninitialized flexible array member) or null if the size > - cannot be determined. */ > + cannot be determined. > + when FOR_ARRAY_BOUND_CHECK is true, this routine is called for > + -Warray-bounds check only, due to historical reason, the LEVEL > + of -Warray-bounds=3DLEVEL is not controled by -fstrict-flex-arrays. = */ >=20 > tree > -component_ref_size (tree ref, special_array_member *sam /* =3D NULL */) > +component_ref_size (tree ref, special_array_member *sam /* =3D NULL */, > + bool for_array_bound_check /* =3D FALSE */) > { > gcc_assert (TREE_CODE (ref) =3D=3D COMPONENT_REF); >=20 > special_array_member sambuf; > if (!sam) > sam =3D &sambuf; > - *sam =3D special_array_member::none; > + *sam =3D component_ref_sam_type (ref); >=20 > /* The object/argument referenced by the COMPONENT_REF and its type. *= / > tree arg =3D TREE_OPERAND (ref, 0); > @@ -12958,41 +13037,49 @@ component_ref_size (tree ref, special_array_mem= ber *sam /* =3D NULL */) > return (tree_int_cst_equal (memsize, TYPE_SIZE_UNIT (memtype)) > ? memsize : NULL_TREE); >=20 > - bool trailing =3D array_ref_flexible_size_p (ref); > - bool zero_length =3D integer_zerop (memsize); > - if (!trailing && !zero_length) > - /* MEMBER is either an interior array or is an array with > - more than one element. */ > + /* 2-or-more elements arrays are treated as normal arrays by defau= lt. */ > + if (*sam =3D=3D special_array_member::int_n > + || *sam =3D=3D special_array_member::trail_n) > return memsize; >=20 > - if (zero_length) > + /* For -Warray-bounds=3DLEVEL check, historically we have to treat > + trail_0 and trail_1 as flexible arrays by default when LEVEL=3D1. > + for other cases, flag_strict_flex_arrays will control how to treat > + the trailing arrays as flexiable array members. */ > + > + tree afield_decl =3D TREE_OPERAND (ref, 1); > + unsigned int strict_flex_array_level > + =3D strict_flex_array_level_of (afield_decl); > + > + if (!for_array_bound_check) > { > - if (trailing) > - *sam =3D special_array_member::trail_0; > - else > + switch (strict_flex_array_level) > { > - *sam =3D special_array_member::int_0; > - memsize =3D NULL_TREE; > + case 3: > + /* Treaing 0-length trailing arrays as normal array. */ > + if (*sam =3D=3D special_array_member::trail_0) > + return size_zero_node; > + /* FALLTHROUGH. */ > + case 2: > + /* Treating 1-element trailing arrays as normal array. */ > + if (*sam =3D=3D special_array_member::trail_1) > + return memsize; > + /* FALLTHROUGH. */ > + case 1: > + /* Treating 2-or-more elements trailing arrays as normal > + array. */ > + if (*sam =3D=3D special_array_member::trail_n) > + return memsize; > + /* FALLTHROUGH. */ > + case 0: > + break; > + default: > + gcc_unreachable (); > } > } >=20 > - if (!zero_length) > - if (tree dom =3D TYPE_DOMAIN (memtype)) > - if (tree min =3D TYPE_MIN_VALUE (dom)) > - if (tree max =3D TYPE_MAX_VALUE (dom)) > - if (TREE_CODE (min) =3D=3D INTEGER_CST > - && TREE_CODE (max) =3D=3D INTEGER_CST) > - { > - offset_int minidx =3D wi::to_offset (min); > - offset_int maxidx =3D wi::to_offset (max); > - offset_int neltsm1 =3D maxidx - minidx; > - if (neltsm1 > 0) > - /* MEMBER is an array with more than one element. */ > - return memsize; > - > - if (neltsm1 =3D=3D 0) > - *sam =3D special_array_member::trail_1; > - } > + if (*sam =3D=3D special_array_member::int_0) > + memsize =3D NULL_TREE; >=20 > /* For a reference to a zero- or one-element array member of a unio= n > use the size of the union instead of the size of the member. */ > diff --git a/gcc/tree.h b/gcc/tree.h > index 0fcdd6b06d0..f81a1f6efa1 100644 > --- a/gcc/tree.h > +++ b/gcc/tree.h > @@ -5551,28 +5551,33 @@ extern tree array_ref_low_bound (tree); > /* Returns true if REF is an array reference, a component reference, > or a memory reference to an array whose actual size might be larger > than its upper bound implies. */ > -extern bool array_ref_flexible_size_p (tree); > +extern bool array_ref_flexible_size_p (tree, bool * =3D NULL); >=20 > /* Return a tree representing the offset, in bytes, of the field referenc= ed > by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. *= / > extern tree component_ref_field_offset (tree); >=20 > -/* Describes a "special" array member due to which component_ref_size > - returns null. */ > +/* Describes a "special" array member for a COMPONENT_REF. */ > 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. */ > + trail_1, /* Trailing array member with one element. */ > + trail_n, /* Trailing array member with two or more elements. */ > + int_n /* Interior array member with one or more elements. */ > }; >=20 > +/* Determines the special array member type for a COMPONENT_REF. */ > +extern special_array_member component_ref_sam_type (tree); > + > /* Return the size of the member referenced by the COMPONENT_REF, using > its initializer expression if necessary in order to determine the size > of an initialized flexible array member. The size might be zero for > an object with an uninitialized flexible array member or null if it > cannot be determined. */ > -extern tree component_ref_size (tree, special_array_member * =3D NULL); > +extern tree component_ref_size (tree, special_array_member * =3D NULL, > + bool =3D false); >=20 > extern int tree_map_base_eq (const void *, const void *); > extern unsigned int tree_map_base_hash (const void *); > --=20 > 2.31.1 >=20