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 7B79D3842332 for ; Tue, 6 Dec 2022 16:16:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7B79D3842332 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 (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 2B6EfiS3001414; Tue, 6 Dec 2022 16:16:47 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=AoT/F+DI1AbEbTOm6rj1uusX2PtRchpieBMHDvCfcKE=; b=VjUM/m7rBWbFPOV+zBIAA70NIsJfCrTsgQ6tee5lND0yjvWucjsugwrA93CvPZEIOcoI PZ3aJ0ga4EsOy9Gp8Ca8einJP+4y9Ar0Bam4e4arRXk7n6zohDNOrXptdhwhpw6sikvL YEsuosGsyWtHKTN/shMKveE2zcm+trwB5QO+kqzY5Ja4AwQP5FetHCe/uZJmy24kBZvG +xqvOsi0UnLYBZg7NcvdVIinTKTE3XZWCQ8iJGCCuhNIWdFAVc1qk4L7YaMkewzGOfva Nb2V5YkdeZ3LzGCneAXrhPCHrWJe+nxQwZrjg2Ddszo/+HTb9laG9V8sxsYGlGQ+cCE3 Iw== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3m7ydjg84t-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 06 Dec 2022 16:16:45 +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 2B6FTPRb021052; Tue, 6 Dec 2022 16:16:45 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2101.outbound.protection.outlook.com [104.47.58.101]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3m8ua0tteh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 06 Dec 2022 16:16:44 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YrFtpDkG44bb8im4Osqp9ClI1xTkV3X34kw6VTXRymfOxpsC4ankWC5lVURAwLWKixYPB9R72RmbYF6VPPeZONcqbde5PUDMngJBQjeV/qbh4m1qd8mKKturDhII8LlldDIqAI5yFoo2ey3C8zwPC+nMTZGZxD9PivqY+hM8dKzGtIi8jKfk/soTXjENQYXgrOgIy/M2CocXqhiJ5M9tEOYuRx+vjoJ9SeRrDOEuRJ+4mhsGxw+z5tFqR6sBt+RlBnGHnYXe1inDdnlVAZzXYZ2XDDN3tXnPnqrhdGdUoyfQM8oaSvuQa4sDK37Jbw6AyuchfOVzxUT7e/1jLsDqzg== 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=AoT/F+DI1AbEbTOm6rj1uusX2PtRchpieBMHDvCfcKE=; b=BRCxfevgCeC6Sm6Snbf6Pa5NqQFb2B/50lc3b6XxZWMpKIG7kN6GsMqmPa5X7Z6CeKWwjE6kyOMq0fPv6FLWskDZOwvKC4EOJ2MI8JG8DL+D3e2pdTTdJNnjsRv4qXDbPhR/QNMQ12SCHVMuOOPPr7sLXcTzSLVMbXOduS1oA7PhZ1ukwJkPyAgSU6CdkrH+QGP5IPiB2mKhXwNQlsDSBe62lk//kvTeCAgMUpOA764FDU57O14vAMyrXmv2O92LgAnFDNFddhzWLf7f37yxedK2hWPZBF8lzMNZkKCriUWSLJuw1+Y0m1MOJjN96lvA6QqbdwUSIKDMc4qFqaaAlA== 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=AoT/F+DI1AbEbTOm6rj1uusX2PtRchpieBMHDvCfcKE=; b=xmD9ejVpTrIXAasdEY8WttEyOQkFbEh0l0yqD/kAr9LxNqGLqkopWLXn5xOLT2FMG5DXIoviYHu7DQ6w/CGjWaZcos8tD/ERzv4YL49s4aC1PuRtWVBHszA5iQx/pcs4XxmfB9loUajqd0QPsL0cGYWWIjef8K7rhfaYCybd57E= Received: from CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) by CH2PR10MB4197.namprd10.prod.outlook.com (2603:10b6:610:7d::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.14; Tue, 6 Dec 2022 16:16:41 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::9423:79b3:c0dc:1113]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::9423:79b3:c0dc:1113%9]) with mapi id 15.20.5880.014; Tue, 6 Dec 2022 16:16:41 +0000 From: Qing Zhao To: Richard Biener , "joseph@codesourcery.com" CC: gcc Patches Subject: Re: [V2][PATCH 1/1] Add a new warning option -Wstrict-flex-arrays. Thread-Topic: [V2][PATCH 1/1] Add a new warning option -Wstrict-flex-arrays. Thread-Index: AQHZCY3ahBiir6OiuEaeaOTb3/0lRa5hCSSA Date: Tue, 6 Dec 2022 16:16:41 +0000 Message-ID: References: <20221206161438.2396168-1-qing.zhao@oracle.com> <20221206161438.2396168-2-qing.zhao@oracle.com> In-Reply-To: <20221206161438.2396168-2-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_|CH2PR10MB4197:EE_ x-ms-office365-filtering-correlation-id: 87915b1d-f9a4-4eab-e337-08dad7a542d0 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: ZlxtH0vYrlWOoHlaBmuHmAJmgLjvVzb92hGRIC6mONnnbeimKntovGqqAAdIclNselubL64+jtVCZyw0dZjn7Cd4ofF0jVUmvDe/80UZ9USOliowDzaI/ZD3qxMZHraP62+hFgZ0C3EfnDvbleOfAkZwHJWjmUAB33zGVIakJ69IngCWRZG0DeKaR+P5cMwsPViGci1iv4ptsb8LCkSYPo9l1Wpst9zzcfaX532yzJ5IuaCuaJnEnpP8T6YDMgOxmsBBPsAJ7qJQ3Dphyz60t3VOtl4bF/rkooNoFR3pa3LDEckuedHD09V8l0Vlbo34QPwEo+L1U1PLSrqWcfhJDaYiuarIr6hOr9+bkeL7iTNTwUcMAMLBy2JMtF/LIMzNPDQwY7Fo4zBqavsUTVQrur13ZMZ2iOVdnTWh6YldMkydxvWHFEOaIvzDLm1G2YcwLsSCdtRCPXEFIv4kHxBNbv6qvIGyQvendx13Ct0Ypj3yLYbz+GoPS72QN918RDzS9lhTg2IoknFSrsU8QkUdpYZUnPWpKv0n2EAw/gYywKxgs/ybFxYLYYfGi2NEwZibn7gM5hNcG3Noz6VjvJPAIrB82wgPc6iyB+TU0axTFqW30/axE0qYbmgFA9wuY+U647ZZfFgeyVjQb4+6yzUvLngEVzYV0E3os9mlkRuYWdwp7fbd1bDV/F92Bf0WbnAm90WmP33TMCoCCpNI/3kWQyel3tP4mGojw1odhPQrrFfOHjm2AcPJE4jWWKLq5H+R5qPA85ORhVKDT7iLhvdp2Ysd7Fg6IJVQKpT1Usjxia0zjyXvuixQ5VWZ62vBpbcX 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)(136003)(39860400002)(376002)(366004)(396003)(346002)(451199015)(53546011)(186003)(2616005)(6512007)(6506007)(26005)(316002)(84970400001)(8676002)(76116006)(4326008)(478600001)(6486002)(66476007)(91956017)(110136005)(66946007)(64756008)(66556008)(66446008)(71200400001)(30864003)(8936002)(5660300002)(83380400001)(2906002)(41300700001)(38070700005)(122000001)(36756003)(44832011)(33656002)(38100700002)(86362001)(45980500001)(559001)(579004);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?GIgOTU9rDoReIeXS6ox5olH2Qudf32top2i5vQlsIARlDavcyTJaJaC/Wwfq?= =?us-ascii?Q?c86SNqN1+UZkDXYNxJY6YaWYhO2WbncV4MmoPP1jJjr/7IbqLaxKxzySI53R?= =?us-ascii?Q?4YfYOQlDbMjPH+2gauZ0Gf7q4bpfzKFwEHKpmh3F/oT+IWW9ThQZs3Yph2x8?= =?us-ascii?Q?m0Ckyuk8MAofsrNNVG5GqvJJ1Xhd+Smh3YvwRMd09/TTmu8rPQFfYafoa/U/?= =?us-ascii?Q?HrA1sEFfQlSbioHmwDJ2M2B45DogIlK8zjwkgMtWA7LG33KcxMkAWQGURhoP?= =?us-ascii?Q?QjramUNNriR6fip1++3I6G71hHeR8UmGxb7K5pW6rVw31wxWCjwVmm2OhB2a?= =?us-ascii?Q?rR1q2viOS4x22D8ijtrGy09Me6paTbsX6SuWhhM8mNmUvLMNjex2dCtZCjZ4?= =?us-ascii?Q?kL8S73fuv9MKZ6FqwUteASa9PBX97zit17XYommadg2vgqJjXIUiqfG0/PN3?= =?us-ascii?Q?/ophEh3Fo0KipuNMEHb8uxR0/oOaW7EfBlgl9bgjsis30B/x5AA6coutAUBH?= =?us-ascii?Q?3KRtJGDh3walZWCBOM3iaDqKjOtmL00LBE833BixCNeneIYqrtk0gA96Q4to?= =?us-ascii?Q?GysmcHc7pHHdZjtLKseU9mN2PTXWyIVH7ltFwCjpMRUJDrk/zN/lzBm9h8BJ?= =?us-ascii?Q?UNPX5vM5M/Up1wqBXbtvoUSee5JRheHKPxAOyq+X3sLxANfvpAHUACyeKm3+?= =?us-ascii?Q?2hyhrihmL3ofD1fAtg5jmjtUIyNaTzx/6d8rpUk3JRbJyG1VA0OyWRpRIOHD?= =?us-ascii?Q?Abz6hrOtM9r0apL0X5UqrhkSvrr/rEZpkaTf8UOwnXA1tf5mZnH73z+hGlUr?= =?us-ascii?Q?rjVDulsNFo8JHDAus2yEFVZxdB2Qi/oBIWOtg/er5lHpaB+1RTlrLt2RFG8e?= =?us-ascii?Q?RrxrXtRnWYXMxmoqNXtCeNz3MpLGs7oX6DxJ/achgxV6z7I/yh5EdJPagNaj?= =?us-ascii?Q?fMZvk5/d44Ko05FAQ0NSfx5kFC2gAbW9nDvnTDgnTTWpa7o1DdZx4rrYOocH?= =?us-ascii?Q?z1yBVy9yMRkQens5gI1Rm/l4YpgmkSBIGnnb9x75fM0xcx5MH+jgGornPNWI?= =?us-ascii?Q?nwxFUPyMs+6sm0dhyatSfJgKNudbFx9RXGmP43brg0QWQMSBYvTEsDoDSRvo?= =?us-ascii?Q?sY00e1AaVlkLovWCdMDVHhru3VSWV969fTW2yi649SdSffYjyK1czGQXZtGC?= =?us-ascii?Q?sB0VfOyzemvhXRBfoxaXGl4LOcfwdfapfqteNNUnj+YgiIphK+Iw4+m8A0hR?= =?us-ascii?Q?n9RvDutvfGGBNs+iJUmwFjZShVP6M1YSp7LYlhD/NpXBRWOSS16eAzFwcQ9E?= =?us-ascii?Q?dKgXzCc+FNzGr/a4GVqQ4+m/GCXLaSMga/jdq3vBp/MLHhSR0nWdL0xN0c1f?= =?us-ascii?Q?neFwugwICLSqjOVGdc9Ay/DBtuGcgswxgXJxopHhHC1NOozBYzC0h2eorWgn?= =?us-ascii?Q?tnI88mzIQKGe+rSYgIMYs3TIR5EaNDG2rFSXiAgMuojSAk3C5TYPPR4SRZhJ?= =?us-ascii?Q?izs3eikI3fVK/EeWeMCb+RtzcQ4NfRK2UhvVGWizMZd1iXbPB9I/0dW6Ygh/?= =?us-ascii?Q?MLunc6jMlkxfDrREaDSUogQ7Q+CQB8/cbY2GWlNp6tr+1NtT/RfnVSR+O/9/?= =?us-ascii?Q?Xg=3D=3D?= Content-Type: text/plain; charset="us-ascii" Content-ID: <1EBA7A043BDD5143A38CE266CB3FC364@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: IrM64Q0wrG2l9cDua39k+8jmvhVCLybwXLiFfbGr9B9UsFh5sEkM+X31RpvAVbVwLlTbE1oykiK8AVbBAOtWAR8tULsZVm1FlOZZs22sJ5Ii2pfevIQiNuAYM3oqepNDj6jqLcxRfMoUFC7OEjb/LRRGcUwJ772TEoKKgiDRfseNsbv10cXgwl1CstZoH58cpGRk0OZjodhxltNlsg5naPri0aZrE5juDA+vLAD8dYaByidDQLNKIlnB+Nws0Dn04BoYP5Nbwooht5ibD6mq3c7C895aAMmyg5HPyz1qw6YdCwDXroqDoNg5tKV1pg6fJO1NBClPogzw/2+NpRvnHEnZd+a2HAPO0UszcJ7ul01T1TfFD31T08xI0GrSKA3nyb5NMbCeNc7XHg5OFPjWH51sCbaRqBFcgGTbT+4t4ExrAw8h3Spi0vKlJZq5x+8jlN++EaTggzkJ57ZbXzLlW96mvtC/lnnCVUcLHM+OR/xAIRLA5Cj8Tg+H7MrsM9nbn6rV8pKXrdQrUQOQsU7Yk7OFwTreC5Nw3ZPtln9Vi11KmcdJ9hGo7xqrIkPDyv8dbMDWpg3SmS8AHRBpUaps1fmCq4t0nxHd7rgim3nEBn0rdSRwjjyOTLu0y0TDMVV5Eu4d6L5X14LripSaB0Nwv1E4KJMCkKsaTevbzR8UakSvuKKJfdlp4jEP0m3DbzVSW++BHWHZRPvLRCMGqcNbUpAJgihQ0qApkz+TsIHhtJr9dGKAgxh+2E7ks59gfBQXXNMwZBtautkad3I/gF5fPOqnK3fp0XJn2tp/aXnHHhwNAPMCSoxp2Q9LNGR948dP 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: 87915b1d-f9a4-4eab-e337-08dad7a542d0 X-MS-Exchange-CrossTenant-originalarrivaltime: 06 Dec 2022 16:16:41.6439 (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: mC3sP1TAb3z7JK2Kq5uIHWBX6KTGAJFgpgqIpeNzxGYk4b/uDkAOwEMqYBjjk3HAGM1zyi2z8998it7ZHU8hqw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR10MB4197 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.923,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-12-06_10,2022-12-06_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 mlxlogscore=999 mlxscore=0 adultscore=0 phishscore=0 suspectscore=0 malwarescore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2210170000 definitions=main-2212060135 X-Proofpoint-ORIG-GUID: z3-4LVHzr0K3Ow3kMPO3UEmWzTZ3KJld X-Proofpoint-GUID: z3-4LVHzr0K3Ow3kMPO3UEmWzTZ3KJld X-Spam-Status: No, score=-11.3 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: Sorry, Please ignore this email. Qing > On Dec 6, 2022, at 11:14 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, -Warray-bounds is updated: >=20 > A. add the following to clarify the relationship with the LEVEL of > -fstrict-flex-array: >=20 > By default, the trailing array of a structure will be treated as a > flexible array member by '-Warray-bounds' or '-Warray-bounds=3DN' if > it is declared as either a flexible array member per C99 standard > onwards ('[]'), a GCC zero-length array extension ('[0]'), or an > one-element array ('[1]'). As a result, out of bounds subscripts > or offsets into zero-length arrays or one-element arrays are not > warned by default. >=20 > You can add the option '-fstrict-flex-arrays' or > '-fstrict-flex-arrays=3DLEVEL' to control how this option treat > trailing array of a structure as a flexible array member. >=20 > when LEVEL<=3D1, no change to the default behavior. >=20 > when LEVEL=3D2, additional warnings will be issued for out of bounds > subscripts or offsets into one-element arrays; >=20 > when LEVEL=3D3, in addition to LEVEL=3D2, additional warnings will be > issued for out of bounds subscripts or offsets into zero-length > arrays. >=20 > B. change the -Warray-bounds=3D2 to exclude the control on how to tre= at > trailing arrays as flexible array members: >=20 > '-Warray-bounds=3D2' > This warning level also warns about the intermediate results > of pointer arithmetic that may yield out of bounds values. > This warning level may give a larger number of false positives > and is deactivated by default. >=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 > -Warray-bounds by specifying the impact from -fstrict-flex-arrays. > Also update -Warray-bounds=3D2 by eliminating its impact on treating > trailing arrays as flexible array members. > * 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 warning for unsupported combination > of -Wstrict_flex_arrays and -fstrict-flex-array. > * tree-vrp.cc (execute_ranger_vrp): Enable the pass when > warn_strict_flex_array is true. > * tree.cc (array_ref_flexible_size_p): Add one new argument. > (component_ref_sam_type): New function. > (component_ref_size): Control with level of strict-flex-array. > * 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. >=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/Warray-bounds-11.c: Update warnings for -Warray-bounds=3D2. > * 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 | 54 +++- > gcc/gimple-array-bounds.cc | 240 +++++++++++++----- > gcc/opts.cc | 8 + > .../c-c++-common/Wstrict-flex-arrays.c | 9 + > gcc/testsuite/gcc.dg/Warray-bounds-11.c | 2 +- > 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 | 47 ++++ > gcc/testsuite/gcc.dg/Wstrict-flex-arrays-5.c | 49 ++++ > gcc/testsuite/gcc.dg/Wstrict-flex-arrays-6.c | 49 ++++ > gcc/testsuite/gcc.dg/Wstrict-flex-arrays-7.c | 48 ++++ > gcc/testsuite/gcc.dg/Wstrict-flex-arrays-8.c | 48 ++++ > gcc/testsuite/gcc.dg/Wstrict-flex-arrays-9.c | 49 ++++ > gcc/testsuite/gcc.dg/Wstrict-flex-arrays.c | 46 ++++ > gcc/tree-vrp.cc | 2 +- > gcc/tree.cc | 159 +++++++++--- > gcc/tree.h | 12 +- > 21 files changed, 833 insertions(+), 140 deletions(-) > create mode 100644 gcc/testsuite/c-c++-common/Wstrict-flex-arrays.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 0d0ad0a6374..33edeefd285 100644 > --- a/gcc/c-family/c.opt > +++ b/gcc/c-family/c.opt > @@ -976,6 +976,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 4adb89e4aaf..f8cfb1d1090 100644 > --- a/gcc/c/c-decl.cc > +++ b/gcc/c/c-decl.cc > @@ -9040,7 +9040,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; > @@ -9067,26 +9066,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 e312b5cef3d..5f5a6562f97 100644 > --- a/gcc/doc/invoke.texi > +++ b/gcc/doc/invoke.texi > @@ -398,7 +398,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 > @@ -7835,6 +7835,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 > @@ -8040,17 +8065,34 @@ is enabled by @option{-Wall}. It is more effecti= ve when @option{-ftree-vrp} > is active (the default for @option{-O2} and above) but a subset of instan= ces > are issued even without optimization. >=20 > +By default, the trailing array of a structure will be treated as a flexi= ble > +array member by @option{-Warray-bounds} or @option{-Warray-bounds=3D@var= {n}} > +if it is declared as either a flexible array member per C99 standard onw= ards > +(@samp{[]}), a GCC zero-length array extension (@samp{[0]}), or an one-e= lement > +array (@samp{[1]}). As a result, out of bounds subscripts or offsets int= o > +zero-length arrays or one-element arrays are not warned by default. > + > +You can add the option @option{-fstrict-flex-arrays} or > +@option{-fstrict-flex-arrays=3D@var{level}} to control how this > +option treat trailing array of a structure as a flexible array member: > + > +when @var{level}<=3D1, no change to the default behavior. > + > +when @var{level}=3D2, additional warnings will be issued for out of boun= ds > +subscripts or offsets into one-element arrays; > + > +when @var{level}=3D3, in addition to @var{level}=3D2, additional warning= s will be > +issued for out of bounds subscripts or offsets into zero-length arrays. > + > @table @gcctabopt > @item -Warray-bounds=3D1 > This is the default warning level of @option{-Warray-bounds} and is enabl= ed > by @option{-Wall}; higher levels are not, and must be explicitly requeste= d. >=20 > @item -Warray-bounds=3D2 > -This warning level also warns about out of bounds accesses to trailing > -struct members of one-element array types (@pxref{Zero Length}) and abou= t > -the intermediate results of pointer arithmetic that may yield out of bou= nds > -values. This warning level may give a larger number of false positives = and > -is deactivated by default. > +This warning level also warns about the intermediate results of pointer > +arithmetic that may yield out of bounds values. This warning level may > +give a larger number of false positives and is deactivated by default. > @end table >=20 > @item -Warray-compare > diff --git a/gcc/gimple-array-bounds.cc b/gcc/gimple-array-bounds.cc > index eae49ab3910..7cf1c56ac3f 100644 > --- a/gcc/gimple-array-bounds.cc > +++ b/gcc/gimple-array-bounds.cc > @@ -160,38 +160,17 @@ 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. */ > +/* 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. */ >=20 > -bool > -array_bounds_checker::check_array_ref (location_t location, tree ref, > - gimple *stmt, bool ignore_off_by_one) > +static void > +get_up_bounds_for_array_ref (tree ref, tree *decl, > + tree *up_bound, tree *up_bound_p1) > { > - 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 > + || trailing_array (ref, decl)) > { > /* Accesses to trailing arrays via pointers may access storage > beyond the types array bounds. For such arrays, or for flexible > @@ -203,8 +182,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 > { > @@ -217,7 +196,7 @@ 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)) > if (TREE_CODE (refsize) =3D=3D INTEGER_CST) > maxbound =3D refsize; > } > @@ -236,7 +215,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)) > @@ -244,7 +223,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)) > @@ -256,40 +235,49 @@ 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, set OUT_OF_BOUND to true. > + Issue warnings if FOR_ARRAY_BOUND is true. > + return TRUE if warnings are issued. */ >=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, > + bool *out_of_bound) > +{ > + tree low_bound =3D array_ref_low_bound (ref); > tree artype =3D TREE_TYPE (TREE_OPERAND (ref, 0)); >=20 > bool warned =3D false; > + *out_of_bound =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 (); > - } > + *out_of_bound =3D true; > + 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); > } >=20 > if (warned) > @@ -303,24 +291,107 @@ 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); > + { > + *out_of_bound =3D true; > + 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 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); > + { > + *out_of_bound =3D true; > + 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 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); > + { > + *out_of_bound =3D true; > + if (for_array_bound) > + warned =3D warning_at (location, OPT_Warray_bounds, > + "array subscript %E is below array bounds of %qT", > + low_sub, artype); > + } > + 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; > + > + /* 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); > + } > + > + get_up_bounds_for_array_ref (ref, &decl, &up_bound, &up_bound_p1); > + > + 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 (); > + } > + } > + > + 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, warn_array_bounds, > + &out_of_bound); > + >=20 > if (!warned && sam =3D=3D special_array_member::int_0) > warned =3D warning_at (location, OPT_Wzero_length_bounds, > @@ -331,19 +402,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 73fc97756e4..8db53ad6c77 100644 > --- a/gcc/opts.cc > +++ b/gcc/opts.cc > @@ -1411,6 +1411,14 @@ finish_options (struct gcc_options *opts, struct g= cc_options *opts_set, > opts->x_profile_flag =3D 0; > } >=20 > + 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/gcc.dg/Warray-bounds-11.c b/gcc/testsuite/gcc.= dg/Warray-bounds-11.c > index c9fc461942f..3ba2bb67fa0 100644 > --- a/gcc/testsuite/gcc.dg/Warray-bounds-11.c > +++ b/gcc/testsuite/gcc.dg/Warray-bounds-11.c > @@ -78,7 +78,7 @@ void foo(int (*a)[3]) >=20 > h->j[4] =3D 1; // flexible array member > h0->j[4] =3D 1; // zero-sized array extension > - h1->j[4] =3D 1; /* { dg-warning "subscript 4 is above array bound" } */ > + h1->j[4] =3D 1; /* { dg-bogus "subscript 4 is above array bound" } */ > h3->j[4] =3D 1; /* { dg-warning "subscript 4 is above array bound" } */ >=20 > struct h0b* h0b =3D malloc(sizeof(struct h) + 3 * sizeof(int)); > 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..8ea6236b4f7 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-4.c > @@ -0,0 +1,47 @@ > +/* 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; > +} > 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..00f4037d03c > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-5.c > @@ -0,0 +1,49 @@ > +/* 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" } */ > + /*{ 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; > +} > 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..190dd0d89c2 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-6.c > @@ -0,0 +1,49 @@ > +/* 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" } */ > + /*{ dg-warning "array subscript 2 is above array bounds of" "" { t= arget *-*-* } .-1 } */=20 > + trailing_0->c[1] =3D 1; /* { dg-warning "should not be used as a fle= xible array member for level 3" } */ > + /*{ dg-warning "array subscript 1 is outside array bounds of" "" {= target *-*-* } .-1 } */=20 > + 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-7.c b/gcc/testsuite= /gcc.dg/Wstrict-flex-arrays-7.c > new file mode 100644 > index 00000000000..f422705e09f > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-7.c > @@ -0,0 +1,48 @@ > +/* 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-bogus "array subscript 2 is above array bounds of" "" { t= arget *-*-* } .-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; > +} > 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..e9bbd03dd9a > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-8.c > @@ -0,0 +1,48 @@ > +/* 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; > +} > 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..54e8de4b04f > --- /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" } */ > + /*{ dg-warning "array subscript 1 is outside array bounds of" "" = { target *-*-* } .-1 } */ > + 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.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 3846dc1d849..e6c6c5a301d 100644 > --- a/gcc/tree-vrp.cc > +++ b/gcc/tree-vrp.cc > @@ -1087,7 +1087,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 254b2373dcf..b40c95ae8c4 100644 > --- a/gcc/tree.cc > +++ b/gcc/tree.cc > @@ -12719,15 +12719,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) > @@ -12815,7 +12821,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)= . */ > @@ -12837,9 +12846,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))) > @@ -12847,11 +12864,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 > @@ -12913,11 +12935,63 @@ 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 > - array or a trailing one-element array. > + If non-null, set *SAM to the type of special array member. > 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. */ > @@ -12930,7 +13004,7 @@ component_ref_size (tree ref, special_array_membe= r *sam /* =3D NULL */) > 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); > @@ -12951,43 +13025,46 @@ 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) > + /* 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); > + > + switch (strict_flex_array_level) > { > - if (trailing) > - *sam =3D special_array_member::trail_0; > - else > - { > - *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 uni= on > + /* For a reference to a flexible array member of a union > use the size of the union instead of the size of the member. */ > if (TREE_CODE (argtype) =3D=3D UNION_TYPE) > memsize =3D TYPE_SIZE_UNIT (argtype); > diff --git a/gcc/tree.h b/gcc/tree.h > index 4a19de1c94d..23223ca0c87 100644 > --- a/gcc/tree.h > +++ b/gcc/tree.h > @@ -5553,22 +5553,26 @@ 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 > --=20 > 2.31.1 >=20