Message ID | 20221206161438.2396168-1-qing.zhao@oracle.com |
---|---|
Headers |
Return-Path: <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2912843wrr; Tue, 6 Dec 2022 08:15:33 -0800 (PST) X-Google-Smtp-Source: AA0mqf7jntDxTOIPfIPvtACQnDC96R38O+6cJy31tQHwzaU8tp/YDZ+bVYMFtEG9AjfT59nPJf+y X-Received: by 2002:a17:906:a107:b0:77f:9082:73c7 with SMTP id t7-20020a170906a10700b0077f908273c7mr56473329ejy.517.1670343333409; Tue, 06 Dec 2022 08:15:33 -0800 (PST) Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id lt27-20020a170906fa9b00b007ae127c6c7asi2565843ejb.176.2022.12.06.08.15.33 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Dec 2022 08:15:33 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Btal7VfD; arc=fail (signature failed); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E5EDA384699A for <ouuuleilei@gmail.com>; Tue, 6 Dec 2022 16:15:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E5EDA384699A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670343331; bh=KMjOGGYxUUqTwnWZh+iyZL+DoOLwe/vLWfTjEPHOOcQ=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Btal7VfDfMAGDA8SilyuTRRG8PVuUbkixtGJ69N/KnWFMBGXMoWKDzJsTjGg9/K99 iEFDm97CVjMSwg8Cv/LCoBLsv0F6E0QVLOXg5cRK7xKy3H2+0ud+vgn7c7GzzL/TM8 GGDQuXf5EALei5kz+cIC+SAtPpFvy1Nejmevjdus= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by sourceware.org (Postfix) with ESMTPS id EBBB438469A0; Tue, 6 Dec 2022 16:14:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org EBBB438469A0 Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 2B6EfW2I030399; Tue, 6 Dec 2022 16:14:45 GMT Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3m7ycf800k-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 06 Dec 2022 16:14:44 +0000 Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 2B6Ftxrl033980; Tue, 6 Dec 2022 16:14:43 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2108.outbound.protection.outlook.com [104.47.55.108]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3m8u93tc3g-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 06 Dec 2022 16:14:43 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gh1BLtxvkAxvR5mpEswqkuTXhzRt1Vvck3X4yPyAJhdW33OHT/8XuZ4E7OxjNdoaE/OFT1B2argT44HiaiyGE4fddXOYKXALxTXTtFVluyz2G+CNfpghn6zYfgiO8YtwqfRIMqXQ2ghdja+BD/hg8qp/rR1eCaPGo9F86cXOsO3jmp4KIPLkatswhGyh3Qgtx3LNquNWnBbk5bIjLKz0hyBSgRzbr7Zo8YhYgXj2Hi+3yOL6IeD95pc8IzXUm2FttAbGrdlrqrxv/KMcmIL7VP9cCjoBv/JHWFYo8UxG4BWeaAfWTy/aovocx+M7KVmib+RAnoJSw+DYqOMF5cPfbA== 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=KMjOGGYxUUqTwnWZh+iyZL+DoOLwe/vLWfTjEPHOOcQ=; b=B+5tnHHWYD7behrhGJkVro+TlezttHjQtlP/VCY1zO2bMVopQ4Cs3RPDng5Rqg6R9rdHSYeT9i4K0oxaNs27C37CR1USBV5IZgPQVWObvtHj5zhJE6X2+5yrk8+PpAoar84IeFUWgSGjPrpIJTXGknqynr4lLsA1CvZ9PuBQbTis+T5zarz2fQjsWz0WhnRIkmHwtCUkBXy1cHSsK18y/ZQyuUtf4DeuctP7NX7lgghJw+HWO4vyOa5g8rbe4nK9DAQVVN+DEDydBMPzNnnNB81pz5WMOLqExZEqVxfrIZcSqwInEAOQtah5Whb98IL3mBGGHKH1vpHdZnZWkkZJqg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none Received: from CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) by SN7PR10MB6666.namprd10.prod.outlook.com (2603:10b6:806:298::11) 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:14: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:14:41 +0000 To: rguenther@suse.de, joseph@codesourcery.com Cc: gcc-patches@gcc.gnu.org, keescook@chromium.org, siddhesh@gcc.gnu.org, Qing Zhao <qing.zhao@oracle.com> Subject: [V3][PATCH 0/2]Update -Warray-bounds with -fstrict-flex-arrays Date: Tue, 6 Dec 2022 16:14:35 +0000 Message-Id: <20221206161438.2396168-1-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: BL1PR13CA0430.namprd13.prod.outlook.com (2603:10b6:208:2c3::15) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|SN7PR10MB6666:EE_ X-MS-Office365-Filtering-Correlation-Id: d8241d0a-6465-44b3-688a-08dad7a4fb10 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JYpBKmbJNQod+E+jCzfC1FBZE2o/EZAWtGLaXjsRUwpkKSC+JpX26ajRDTArmV0baMeo5VDTW6kYr+Uql95XPKrKwEMe5Hb5h4QxRBjynas7v5qqauNW7mic0D919pGtxD2AYDacV5BJ4bcQ5NOdhWhRNb4hiliC8q7Ay3GeqKB3gNlAncSfylclfJ2PR7I54glZE97TH21h8y6tbt8ONUM3XQFMZXTWqiVQ5SYG6lKO060WeJWxEZa1KEW0RFYvwp4Mgnaj0Hvzcql6nHkvDYScJhesEqeklYu4ROhlOPZUwDKy5PvGUCgBE1m4Yk+mQOgsPZ4m/xp191e3BQ/cgSj+GjvXu3BQ16Vv9llRY7bkWXqC2gDZph7dbhmqzAWqqUQQFThiCsRJcpjwM2BMrnzt4RhW83A3zMVgToPTspA5jOoE0nrjoD02nFXGhCHoc0b3500GjYgfT8dfa85B2nBjgrDgAlheAxH/Yoqfr8HPvz5KtwPdB6BZa3UKDkm3RJ59n80K503BXiZwcfZK5fMb4SKpkuSfZF85Jw70q08AW+nDANmYRmcfH21Uc0votUtlT4bQSZeOwjqjKUeXZGBDFFnmI35IuvWsf6aY20W+vm3BKh4l2yYWBU8G7dEoLN5GOzrKqR7bhGq7k0Owcw== 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)(376002)(346002)(136003)(396003)(39860400002)(366004)(451199015)(8676002)(66556008)(66946007)(66476007)(44832011)(15650500001)(5660300002)(41300700001)(8936002)(2906002)(4744005)(316002)(4326008)(36756003)(38100700002)(86362001)(478600001)(83380400001)(186003)(1076003)(2616005)(107886003)(6666004)(6486002)(6506007)(6512007)(26005); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: gxoWboq3IW5PNMbuOmD2ykmZQauwPAaKKljXopcy+etIqKUdgUzD9wJk3YdeKHg5Ewf/jd8S+uIP5/KRB+Fa+03dpmibej18aE3G+pcB6PHpO/eI4T+rcV698dqF7ekHMkrQK5dUm3RVgG4goSDyQn08jupOA3kZb9DT69HxfPdnV/1B3dchd1qLVRzWE5OTwuOx0gIB6PF5MLp/sBmkeidlMT1mxxew4pPxwFhrczcZkqxrjF+LRyRO21D7u8ZfHD/FUkYuy/JsOnMlxr6goAXou7fZZ5SAcMS2wLybL1JG0yaWskiapyjekWVAFmnQ5TdUdr3YrYB/hXYNLpAbREgqTrPudtrBVqBiOx0v+tmjpf9eKZvAITdGyDuEyhoQMoMV8g66ELjQcxCkVZ2UPY7PZkfbz+jNM3P4N2f7b41pZzYjqFX5gTWwlaOVi+Oiz7hegy1iZVZEyeLIqVARYoVekYoLJg+yyUefihC8uB8C0akeSlb5n+100cp81WKp/SBjTuZn20b0jvng41LczSrTHCQXizZIU68ZBR3E8muatHkdaYjF/3yXGv+frWwjAGp+Jv7hMaHi+AqcfVplxl2JQ83mV3jQE4uOFzXLJwRJAsf+g4SDsvwMDs5RkAjA0Nz6LM2PZqIf7p3kociFN+bAeEp9B5ENwfcZNCRkXxCZeKxEvQ4SVsdXMm6vYDEOI4jfZFfQH6dvmgL3LgksOwErB384oH5uZLGnQrgPldUAnRH5d7D4lzwpr2NApVhL62Iy9t7TPuR49w3s72W5FfQxmXPfVEHJQBAccolM/gtQXcVLdIQsj0Fh2IdMqQ59/sOdNBrZb90DdRGi6gFpRv5bWNlYGQQ33xnGKf67gycfJKenrI/aNiQxaqVhQ8sLO4QxR1R0INdY85vUYgzPjxiTVixwa9T/X+x0NnwYCp4y/B8DhmYjMoTq5RbjbnhOHGYuUmWJ+qUmPAeAXWrqEUFcROLi+LFjCm6MxrAHIJbqotZN+PcyJJJFGKkKiSN/0VHzOOeUKdi/cRA5t2lM/AoIBtFVsADPGiOYsWvhRz2BQX04eQ2zTw0rdbI15L3/RmVamD7TRf5CKO0NehQoiq7ktgysNJBm/taFIRImeRzBOso4iVPtUtLMkQxbnRDSs9MIZHzJK6qWt0U75hj8zKqOeip8k87WCa9yLKU320mLFnBaW/j2mcI+O0od9eTje0LZ10FpnCNVCfOyL6oiTo0Az9oH/W8RG4LiSDEa7f+NKVte3qja3ORiDQIfe0iLXnY9Tasw/jgp9t6NpY8AjMKwyxNIyV5/wjBL6fZUFQDqm8P1ea+nFcAOIEAV+eDvcpCS2YjXc98uuxrfEDa+EIxcs0aCBVGXGVUx8agBv8qt28MWFyAEHUyUIkzMqMxX+/yeOlLBW/jMic+PfOf7JJELZbSbpMIa5/rqBSzkHuFFdnQgAujESn0cCC6UU62qqMB8KsN6TyPJ4cHI8S8cVVx9r/1sy+WfNF1tDlrOG+pfzqhSNmS2qC0C3fYXDqiNPxLYe09Mk3AVCpDmJKRR2KpmfqHfagbclcJC2QY3kcwI8tWofStexvA7LGiGsiW3 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: 9RnZOeaA49qvG//ddD/HlNmALW75VoAtr/VDoo8uWjPK44u1/ik2N9EMyfdMXCjosTvKdhM3VxVqvUOzXd4PMWmweokPGnhQ97iCx2vJYhWJIO4mFkfxHt5CV2LQnzRrJ/EJjwdnzVd2Ecjc/U5Xo0La3+KBT9MmwyIaXEYkhqriODKJsWdJ1s2/9JB1vuMMX3URTAff5xR+DX1Og4if7K37j3UoD1J1T7k07pBxjUJuyUW+B1LvQBC1Nr3yLY7wWlAF89KQ+OvtQ6wMjk/4xspst7cKug31WpG1MjegRVg+L1srH7keY7y8QQj/bTNr9X7PyGEtrhTa3tOyDJbIt+lDlyjfj7ScK3tvVKNnKnlZFY7awi/J0fMOFFGPHw2iJ+x7/jNAh3C/Hs3RrI6qL/scSjA5n6L4QFhAX7Xw+LB2yFManrRtbSGfxC43H/caA4zaxMI7xqGWqr1NXQdgEQOyt64jTWVgqXFSpmP5NosoVCQLWkLP/x0xlm1KJ5fj2bzJrk3JaDTQEA9x4hr/FPvbSaZs5IewrIl944vTf11xF+2VH/vOGkWyWESkxAlFWnaO8G3wG4ELwvkO/UBo6iMaxoG9zZRHGMcA5iGeF9JQTOGnZG0i9HUO9WU636lLepBJW9MyQM0RkDcajdCjUOXDakARL8wYGbeXcdky6gvoEnkDP3Qn2Z0ddqwMM8NMrszgHehPYEWePE7Oj7CiTrQwZcscJndwcgVm9ZBILeYy39KF4kw7rcQ9ABkUOeQMsp0zeVQMrW6lH+nwnBQ++g1WOtMkzJn64s3cx8QUVLGDW4Y+LxhZ4GrPCkawcx3zo/hkinunARp2KScuwEVL4C3H0SLvdu0j8H4cX8MU2tG7r96ES54pRFGuCE/PF9Yt X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: d8241d0a-6465-44b3-688a-08dad7a4fb10 X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Dec 2022 16:14:41.4829 (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: O2vx9FO7Hlt6jXoFO69KsauNwVyta5L/8qsBitQ17nazwUaNZnHYlO6BatKmDGV9X+0h8u5PX1QWtq13eS+TcQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR10MB6666 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 adultscore=0 bulkscore=0 mlxscore=0 mlxlogscore=875 spamscore=0 phishscore=0 suspectscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2210170000 definitions=main-2212060135 X-Proofpoint-ORIG-GUID: 9YHiacn0mLvos5snBn7u3attXSWEluTq X-Proofpoint-GUID: 9YHiacn0mLvos5snBn7u3attXSWEluTq X-Spam-Status: No, score=-5.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Qing Zhao via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Qing Zhao <qing.zhao@oracle.com> Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751481931306515413?= X-GMAIL-MSGID: =?utf-8?q?1751481931306515413?= |
Series |
Update -Warray-bounds with -fstrict-flex-arrays
|
|
Message
Qing Zhao
Dec. 6, 2022, 4:14 p.m. UTC
Hi, this is the 3rd version of the patch. Per Richard's request, I split the patch into two seperate patches: 1. Update -Warray-bounds with -fstrict-flex-arrays. 2. Add a new warning option -Wstrict-flex-arrays. I have bootstrapped and regression tested on both X86 and aarch64 without any issue. Okay for committing? thanks. Qing
Comments
Sorry, Please ignore this email. Qing > On Dec 6, 2022, at 11:14 AM, Qing Zhao <qing.zhao@oracle.com> wrote: > > '-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=LEVEL'. > > This option is effective only when LEVEL is bigger than 0. > Otherwise, it will be ignored with a warning. > > when LEVEL=1, 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. > > when LEVEL=2, in addition to LEVEL=1, 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. > > when LEVEL=3, in addition to LEVEL=2, 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. > > At the same time, -Warray-bounds is updated: > > A. add the following to clarify the relationship with the LEVEL of > -fstrict-flex-array: > > By default, the trailing array of a structure will be treated as a > flexible array member by '-Warray-bounds' or '-Warray-bounds=N' 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. > > You can add the option '-fstrict-flex-arrays' or > '-fstrict-flex-arrays=LEVEL' to control how this option treat > trailing array of a structure as a flexible array member. > > when LEVEL<=1, no change to the default behavior. > > when LEVEL=2, additional warnings will be issued for out of bounds > subscripts or offsets into one-element arrays; > > when LEVEL=3, in addition to LEVEL=2, additional warnings will be > issued for out of bounds subscripts or offsets into zero-length > arrays. > > B. change the -Warray-bounds=2 to exclude the control on how to treat > trailing arrays as flexible array members: > > '-Warray-bounds=2' > 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. > > gcc/ChangeLog: > > * 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=2 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. > > gcc/c-family/ChangeLog: > > * c.opt (Wstrict-flex-arrays): New option. > > gcc/c/ChangeLog: > > * c-decl.cc (is_flexible_array_member_p): Call new function > strict_flex_array_level_of. > > gcc/testsuite/ChangeLog: > > * 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=2. > * 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 > > 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) > } > } > > +/* Get the LEVEL of the strict_flex_array for the ARRAY_FIELD based on the > + 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) == FIELD_DECL); > + unsigned int strict_flex_array_level = flag_strict_flex_arrays; > + > + tree attr_strict_flex_array > + = lookup_attribute ("strict_flex_array", DECL_ATTRIBUTES (array_field)); > + /* 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 = 0; > + gcc_assert (TREE_VALUE (attr_strict_flex_array) != NULL_TREE); > + attr_strict_flex_array = TREE_VALUE (attr_strict_flex_array); > + gcc_assert (TREE_VALUE (attr_strict_flex_array) != NULL_TREE); > + attr_strict_flex_array = TREE_VALUE (attr_strict_flex_array); > + gcc_assert (tree_fits_uhwi_p (attr_strict_flex_array)); > + attr_strict_flex_array_level = tree_to_uhwi (attr_strict_flex_array); > + > + /* The attribute has higher priority than flag_struct_flex_array. */ > + strict_flex_array_level = 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. */ > > 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 = current_function_decl); > > +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) LangEnabledBy(C ObjC C++ LTO ObjC++, Wall) > Warn about truncation in string manipulation functions like strncat and strncpy. > > +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=format > 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) > } > } > > - > /* Determine whether the FIELD_DECL X is a flexible array member according to > the following info: > A. whether the FIELD_DECL X is the last field of the DECL_CONTEXT; > @@ -9067,26 +9066,7 @@ is_flexible_array_member_p (bool is_last_field, > bool is_one_element_array = one_element_array_type_p (TREE_TYPE (x)); > bool is_flexible_array = flexible_array_member_type_p (TREE_TYPE (x)); > > - unsigned int strict_flex_array_level = flag_strict_flex_arrays; > - > - tree attr_strict_flex_array = 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 = 0; > - gcc_assert (TREE_VALUE (attr_strict_flex_array) != NULL_TREE); > - attr_strict_flex_array = TREE_VALUE (attr_strict_flex_array); > - gcc_assert (TREE_VALUE (attr_strict_flex_array) != NULL_TREE); > - attr_strict_flex_array = TREE_VALUE (attr_strict_flex_array); > - gcc_assert (tree_fits_uhwi_p (attr_strict_flex_array)); > - attr_strict_flex_array_level = tree_to_uhwi (attr_strict_flex_array); > - > - /* The attribute has higher priority than flag_struct_flex_array. */ > - strict_flex_array_level = attr_strict_flex_array_level; > - } > + unsigned int strict_flex_array_level = strict_flex_array_level_of (x); > > 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=n -Wstrict-overflow -Wstrict-overflow=@var{n} @gol > -Wstring-compare @gol > -Wno-stringop-overflow -Wno-stringop-overread @gol > --Wno-stringop-truncation @gol > +-Wno-stringop-truncation -Wstrict-flex-arrays @gol > -Wsuggest-attribute=@r{[}pure@r{|}const@r{|}noreturn@r{|}format@r{|}malloc@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 that expect > such arrays GCC issues warnings unless it can prove that the use is > safe. @xref{Common Variable Attributes}. > > +@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=@var{level}}. > + > +This option is effective only when @var{level} is bigger than 0. Otherwise, > +it will be ignored with a warning. > + > +when @var{level}=1, 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. > + > +when @var{level}=2, in addition to @var{level}=1, 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. > + > +when @var{level}=3, in addition to @var{level}=2, 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. > + > + > @item -Wsuggest-attribute=@r{[}pure@r{|}const@r{|}noreturn@r{|}format@r{|}cold@r{|}malloc@r{]} > @opindex Wsuggest-attribute= > @opindex Wno-suggest-attribute= > @@ -8040,17 +8065,34 @@ is enabled by @option{-Wall}. It is more effective when @option{-ftree-vrp} > is active (the default for @option{-O2} and above) but a subset of instances > are issued even without optimization. > > +By default, the trailing array of a structure will be treated as a flexible > +array member by @option{-Warray-bounds} or @option{-Warray-bounds=@var{n}} > +if it is declared as either a flexible array member per C99 standard onwards > +(@samp{[]}), a GCC zero-length array extension (@samp{[0]}), or an one-element > +array (@samp{[1]}). As a result, out of bounds subscripts or offsets into > +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=@var{level}} to control how this > +option treat trailing array of a structure as a flexible array member: > + > +when @var{level}<=1, no change to the default behavior. > + > +when @var{level}=2, additional warnings will be issued for out of bounds > +subscripts or offsets into one-element arrays; > + > +when @var{level}=3, in addition to @var{level}=2, additional warnings will be > +issued for out of bounds subscripts or offsets into zero-length arrays. > + > @table @gcctabopt > @item -Warray-bounds=1 > This is the default warning level of @option{-Warray-bounds} and is enabled > by @option{-Wall}; higher levels are not, and must be explicitly requested. > > @item -Warray-bounds=2 > -This warning level also warns about out of bounds accesses to trailing > -struct members of one-element array types (@pxref{Zero Length}) and 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. > +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 > > @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); > } > > -/* 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. */ > > -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 = TREE_OPERAND (ref, 1); > - tree up_sub = low_sub; > - tree up_bound = array_ref_up_bound (ref); > - > - /* Referenced decl if one can be determined. */ > - tree decl = 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) != INTEGER_CST > - || (warn_array_bounds < 2 && trailing_array (ref, &decl))) > + if (!(*up_bound) > + || TREE_CODE (*up_bound) != 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 location, tree ref, > if (TREE_CODE (eltsize) != INTEGER_CST > || integer_zerop (eltsize)) > { > - up_bound = NULL_TREE; > - up_bound_p1 = NULL_TREE; > + *up_bound = NULL_TREE; > + *up_bound_p1 = NULL_TREE; > } > else > { > @@ -217,7 +196,7 @@ array_bounds_checker::check_array_ref (location_t location, tree ref, > { > /* Try to determine the size of the trailing array from > its initializer (if it has one). */ > - if (tree refsize = component_ref_size (arg, &sam)) > + if (tree refsize = component_ref_size (arg)) > if (TREE_CODE (refsize) == INTEGER_CST) > maxbound = refsize; > } > @@ -236,7 +215,7 @@ array_bounds_checker::check_array_ref (location_t location, tree ref, > { > /* Try to determine the size from a pointer to > an array if BASE is one. */ > - if (tree size = get_ref_size (base, &decl)) > + if (tree size = get_ref_size (base, decl)) > maxbound = size; > } > else if (!compref && DECL_P (base)) > @@ -244,7 +223,7 @@ array_bounds_checker::check_array_ref (location_t location, tree ref, > if (TREE_CODE (basesize) == INTEGER_CST) > { > maxbound = basesize; > - decl = base; > + *decl = base; > } > > if (known_gt (off, 0)) > @@ -256,40 +235,49 @@ array_bounds_checker::check_array_ref (location_t location, tree ref, > else > maxbound = fold_convert (sizetype, maxbound); > > - up_bound_p1 = int_const_binop (TRUNC_DIV_EXPR, maxbound, eltsize); > + *up_bound_p1 = int_const_binop (TRUNC_DIV_EXPR, maxbound, eltsize); > > - if (up_bound_p1 != NULL_TREE) > - up_bound = int_const_binop (MINUS_EXPR, up_bound_p1, > + if (*up_bound_p1 != NULL_TREE) > + *up_bound = int_const_binop (MINUS_EXPR, *up_bound_p1, > build_int_cst (ptrdiff_type_node, 1)); > else > - up_bound = NULL_TREE; > + *up_bound = NULL_TREE; > } > } > else > - up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound, > - build_int_cst (TREE_TYPE (up_bound), 1)); > + *up_bound_p1 = int_const_binop (PLUS_EXPR, *up_bound, > + build_int_cst (TREE_TYPE (*up_bound), 1)); > + return; > +} > > - tree low_bound = 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 bound. > + When out of bounds, set OUT_OF_BOUND to true. > + Issue warnings if FOR_ARRAY_BOUND is true. > + return TRUE if warnings are issued. */ > > +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 = array_ref_low_bound (ref); > tree artype = TREE_TYPE (TREE_OPERAND (ref, 0)); > > bool warned = false; > + *out_of_bound = false; > > /* Empty array. */ > if (up_bound && tree_int_cst_equal (low_bound, up_bound_p1)) > - warned = warning_at (location, OPT_Warray_bounds, > - "array subscript %E is outside array bounds of %qT", > - low_sub, artype); > - > - const value_range *vr = NULL; > - if (TREE_CODE (low_sub) == SSA_NAME) > { > - vr = get_value_range (low_sub, stmt); > - if (!vr->undefined_p () && !vr->varying_p ()) > - { > - low_sub = vr->kind () == VR_RANGE ? vr->max () : vr->min (); > - up_sub = vr->kind () == VR_RANGE ? vr->min () : vr->max (); > - } > + *out_of_bound = true; > + if (for_array_bound) > + warned = warning_at (location, OPT_Warray_bounds, > + "array subscript %E is outside array" > + " bounds of %qT", low_sub_org, artype); > } > > 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) == INTEGER_CST > && tree_int_cst_le (low_sub, low_bound)) > - warned = warning_at (location, OPT_Warray_bounds, > - "array subscript [%E, %E] is outside " > - "array bounds of %qT", > - low_sub, up_sub, artype); > + { > + *out_of_bound = true; > + if (for_array_bound) > + warned = 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) == INTEGER_CST > && (ignore_off_by_one > ? !tree_int_cst_le (up_sub, up_bound_p1) > : !tree_int_cst_le (up_sub, up_bound))) > - warned = warning_at (location, OPT_Warray_bounds, > - "array subscript %E is above array bounds of %qT", > - up_sub, artype); > + { > + *out_of_bound = true; > + if (for_array_bound) > + warned = warning_at (location, OPT_Warray_bounds, > + "array subscript %E is above array bounds of %qT", > + up_sub, artype); > + } > else if (TREE_CODE (low_sub) == INTEGER_CST > && tree_int_cst_lt (low_sub, low_bound)) > - warned = warning_at (location, OPT_Warray_bounds, > - "array subscript %E is below array bounds of %qT", > - low_sub, artype); > + { > + *out_of_bound = true; > + if (for_array_bound) > + warned = 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 = array_ref_up_bound (ref); > + tree up_bound_p1 = NULL_TREE; > + > + /* Referenced decl if one can be determined. */ > + tree decl = NULL_TREE; > + > + /* Set to the type of the special array member for a COMPONENT_REF. */ > + special_array_member sam{ }; > + > + tree arg = TREE_OPERAND (ref, 0); > + const bool compref = TREE_CODE (arg) == COMPONENT_REF; > + > + unsigned int strict_flex_array_level = flag_strict_flex_arrays; > + > + if (compref) > + { > + /* Try to determine special array member type for this COMPONENT_REF. */ > + sam = component_ref_sam_type (arg); > + /* Get the level of strict_flex_array for this array field. */ > + tree afield_decl = TREE_OPERAND (arg, 1); > + strict_flex_array_level = strict_flex_array_level_of (afield_decl); > + } > + > + get_up_bounds_for_array_ref (ref, &decl, &up_bound, &up_bound_p1); > + > + bool warned = false; > + bool out_of_bound = false; > + > + tree artype = TREE_TYPE (TREE_OPERAND (ref, 0)); > + tree low_sub_org = TREE_OPERAND (ref, 1); > + tree up_sub = low_sub_org; > + tree low_sub = low_sub_org; > + > + const value_range *vr = NULL; > + if (TREE_CODE (low_sub_org) == SSA_NAME) > + { > + vr = get_value_range (low_sub_org, stmt); > + if (!vr->undefined_p () && !vr->varying_p ()) > + { > + low_sub = vr->kind () == VR_RANGE ? vr->max () : vr->min (); > + up_sub = vr->kind () == VR_RANGE ? vr->min () : vr->max (); > + } > + } > + > + warned = 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); > + > > if (!warned && sam == special_array_member::int_0) > warned = warning_at (location, OPT_Wzero_length_bounds, > @@ -331,19 +402,56 @@ array_bounds_checker::check_array_ref (location_t location, tree ref, > "of an interior zero-length array %qT")), > low_sub, artype); > > - 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"); > } > > + /* 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 == special_array_member::trail_0) > + warned = 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 == special_array_member::trail_1) > + warned = 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 == special_array_member::trail_n) > + warned = 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 = TREE_OPERAND (ref, 0); > suppress_warning (ref, OPT_Warray_bounds); > + suppress_warning (ref, OPT_Wstrict_flex_arrays); > > if (decl) > ref = 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 gcc_options *opts_set, > opts->x_profile_flag = 0; > } > > + if (opts->x_warn_strict_flex_arrays) > + if (opts->x_flag_strict_flex_arrays == 0) > + { > + opts->x_warn_strict_flex_arrays = 0; > + warning_at (UNKNOWN_LOCATION, 0, > + "%<-Wstrict-flex-arrays%> is ignored when" > + " %<-fstrict-flex-arrays%> does not present"); > + } > > diagnose_options (opts, opts_set, loc); > } > diff --git a/gcc/testsuite/c-c++-common/Wstrict-flex-arrays.c b/gcc/testsuite/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 present" "" { 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]) > > h->j[4] = 1; // flexible array member > h0->j[4] = 1; // zero-sized array extension > - h1->j[4] = 1; /* { dg-warning "subscript 4 is above array bound" } */ > + h1->j[4] = 1; /* { dg-bogus "subscript 4 is above array bound" } */ > h3->j[4] = 1; /* { dg-warning "subscript 4 is above array bound" } */ > > struct h0b* h0b = 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=2" } */ > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4]; > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1]; > +}; > + > +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] = 5; /*{ dg-warning "should not be used as a flexible array member for level 1 and above" } */ > + trailing_1->c[2] = 2; /* { dg-warning "should not be used as a flexible array member for level 2 and above" } */ > + trailing_0->c[1] = 1; /* { dg-bogus "should not be used as a flexible array member for level 2 and above" } */ > + trailing_flex->c[10] = 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 *)argv[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=3" } */ > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4]; > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1]; > +}; > + > +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] = 5; /*{ dg-warning "should not be used as a flexible array member for level 1 and above" } */ > + trailing_1->c[2] = 2; /* { dg-warning "should not be used as a flexible array member for level 2 and above" } */ > + trailing_0->c[1] = 1; /* { dg-warning "should not be used as a flexible array member for level 3" } */ > + trailing_flex->c[10] = 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 *)argv[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=1 -Warray-bounds" } */ > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4]; > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1]; > +}; > + > +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] = 5; /*{ dg-warning "should not be used as a flexible array member for level 1 and above" } */ > + /*{ dg-warning "array subscript 5 is above array bounds of" "" { target *-*-* } .-1 } */ > + trailing_1->c[2] = 2; /* { dg-bogus "should not be used as a flexible array member for level 1 and above" } */ > + trailing_0->c[1] = 1; /* { dg-bogus "should not be used as a flexible array member for level 1 and above" } */ > + trailing_flex->c[10] = 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 *)argv[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=2 -Warray-bounds" } */ > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4]; > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1]; > +}; > + > +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] = 5; /*{ dg-warning "should not be used as a flexible array member for level 1 and above" } */ > + /*{ dg-warning "array subscript 5 is above array bounds of" "" { target *-*-* } .-1 } */ > + trailing_1->c[2] = 2; /* { dg-warning "should not be used as a flexible array member for level 2 and above" } */ > + /*{ dg-warning "array subscript 2 is above array bounds of" "" { target *-*-* } .-1 } */ > + > + trailing_0->c[1] = 1; /* { dg-bogus "should not be used as a flexible array member for level 2 and above" } */ > + trailing_flex->c[10] = 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 *)argv[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=3 -Warray-bounds" } */ > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4]; > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1]; > +}; > + > +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] = 5; /*{ dg-warning "should not be used as a flexible array member for level 1 and above" } */ > + /*{ dg-warning "array subscript 5 is above array bounds of" "" { target *-*-* } .-1 } */ > + trailing_1->c[2] = 2; /* { dg-warning "should not be used as a flexible array member for level 2 and above" } */ > + /*{ dg-warning "array subscript 2 is above array bounds of" "" { target *-*-* } .-1 } */ > + trailing_0->c[1] = 1; /* { dg-warning "should not be used as a flexible array member for level 3" } */ > + /*{ dg-warning "array subscript 1 is outside array bounds of" "" { target *-*-* } .-1 } */ > + trailing_flex->c[10] = 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 *)argv[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=2. */ > +/* { dg-do run } */ > +/* { dg-options "-O2 -Wstrict-flex-arrays -fstrict-flex-arrays=1 -Warray-bounds=2" } */ > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4]; > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1]; > +}; > + > +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] = 5; /*{ dg-warning "should not be used as a flexible array member for level 1 and above" } */ > + /*{ dg-warning "array subscript 5 is above array bounds of" "" { target *-*-* } .-1 } */ > + trailing_1->c[2] = 2; /* { dg-bogus "should not be used as a flexible array member for level 1 and above" } */ > + /*{ dg-bogus "array subscript 2 is above array bounds of" "" { target *-*-* } .-1 } */ > + trailing_0->c[1] = 1; /* { dg-bogus "should not be used as a flexible array member for level 1 and above" } */ > + trailing_flex->c[10] = 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 *)argv[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=2. */ > +/* { dg-do run } */ > +/* { dg-options "-O2 -Wstrict-flex-arrays -fstrict-flex-arrays=2 -Warray-bounds=2" } */ > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4]; > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1]; > +}; > + > +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] = 5; /*{ dg-warning "should not be used as a flexible array member for level 1 and above" } */ > + /*{ dg-warning "array subscript 5 is above array bounds of" "" { target *-*-* } .-1 } */ > + trailing_1->c[2] = 2; /* { dg-warning "should not be used as a flexible array member for level 2 and above" } */ > + /*{ dg-warning "array subscript 2 is above array bounds of" "" { target *-*-* } .-1 } */ > + trailing_0->c[1] = 1; /* { dg-bogus "should not be used as a flexible array member for level 2 and above" } */ > + trailing_flex->c[10] = 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 *)argv[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=2. */ > +/* { dg-do run } */ > +/* { dg-options "-O2 -Wstrict-flex-arrays -fstrict-flex-arrays=3 -Warray-bounds=2" } */ > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4]; > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1]; > +}; > + > +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] = 5; /*{ dg-warning "should not be used as a flexible array member for level 1 and above" } */ > + /*{ dg-warning "array subscript 5 is above array bounds of" "" { target *-*-* } .-1 } */ > + trailing_1->c[2] = 2; /* { dg-warning "should not be used as a flexible array member for level 2 and above" } */ > + /*{ dg-warning "array subscript 2 is above array bounds of" "" { target *-*-* } .-1 } */ > + trailing_0->c[1] = 1; /* { dg-warning "should not be used as a flexible array member for level 3" } */ > + /*{ dg-warning "array subscript 1 is outside array bounds of" "" { target *-*-* } .-1 } */ > + trailing_flex->c[10] = 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 *)argv[0]); > + > + return 0; > +} > diff --git a/gcc/testsuite/gcc.dg/Wstrict-flex-arrays.c b/gcc/testsuite/gcc.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=1" } */ > + > +struct trailing_array_1 { > + int a; > + int b; > + int c[4]; > +}; > + > +struct trailing_array_2 { > + int a; > + int b; > + int c[1]; > +}; > + > +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] = 5; /*{ dg-warning "should not be used as a flexible array member for level 1 and above" } */ > + trailing_1->c[2] = 2; /* { dg-bogus "should not be used as a flexible array member for level 1 and above" } */ > + trailing_0->c[1] = 1; /* { dg-bogus "should not be used as a flexible array member for level 1 and above" } */ > + trailing_flex->c[10] = 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 *)argv[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); > > - if (warn_array_bounds && warn_array_bounds_p) > + if ((warn_array_bounds || warn_strict_flex_arrays) && warn_array_bounds_p) > { > // Set all edges as executable, except those ranger says aren't. > int non_exec_flag = 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 = 0; i < 4; i++, p++) > t[i][0] = ...; > + > + If non-null, set IS_TRAILING_ARRAY to true if the ref is the above case A. > */ > > bool > -array_ref_flexible_size_p (tree ref) > +array_ref_flexible_size_p (tree ref, bool *is_trailing_array /* = NULL */) > { > - /* the TYPE for this array referece. */ > + /* The TYPE for this array referece. */ > tree atype = 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 = NULL_TREE; > + /* Whether this array is the trailing array of a structure. */ > + bool is_trailing_array_tmp = false; > + if (!is_trailing_array) > + is_trailing_array = &is_trailing_array_tmp; > > if (TREE_CODE (ref) == ARRAY_REF > || TREE_CODE (ref) == 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 = afield_decl && TREE_CODE (afield_decl) == FIELD_DECL; > + return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; > + } > > /* If the reference is based on a declared entity, the size of the array > is constrained by its given domain. (Do not trust commons PR/69368). */ > @@ -12837,9 +12846,17 @@ array_ref_flexible_size_p (tree ref) > if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (atype))) != INTEGER_CST > || TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))) != INTEGER_CST > || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (atype))) != INTEGER_CST) > - return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; > + { > + *is_trailing_array > + = afield_decl && TREE_CODE (afield_decl) == 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 > + = afield_decl && TREE_CODE (afield_decl) == FIELD_DECL; > + return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; > + } > > /* If at least one extra element fits it is a flexarray. */ > if (known_le ((wi::to_offset (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))) > @@ -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 > + = afield_decl && TREE_CODE (afield_decl) == FIELD_DECL; > + return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; > + } > > return false; > } > > + *is_trailing_array = afield_decl && TREE_CODE (afield_decl) == FIELD_DECL; > return afield_decl ? !DECL_NOT_FLEXARRAY (afield_decl) : true; > } > > @@ -12913,11 +12935,63 @@ get_initializer_for (tree init, tree decl) > return NULL_TREE; > } > > +/* 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 = special_array_member::none; > + > + tree member = TREE_OPERAND (ref, 1); > + tree memsize = DECL_SIZE_UNIT (member); > + if (memsize) > + { > + tree memtype = TREE_TYPE (member); > + if (TREE_CODE (memtype) != ARRAY_TYPE) > + return sam_type; > + > + bool trailing = false; > + (void)array_ref_flexible_size_p (ref, &trailing); > + bool zero_length = 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 = TYPE_DOMAIN (memtype)) > + if (tree min = TYPE_MIN_VALUE (dom)) > + if (tree max = TYPE_MAX_VALUE (dom)) > + if (TREE_CODE (min) == INTEGER_CST > + && TREE_CODE (max) == INTEGER_CST) > + { > + offset_int minidx = wi::to_offset (min); > + offset_int maxidx = wi::to_offset (max); > + offset_int neltsm1 = maxidx - minidx; > + if (neltsm1 > 0) > + /* MEMBER is a trailing array with more than > + one elements. */ > + return special_array_member::trail_n; > + > + if (neltsm1 == 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_member *sam /* = NULL */) > special_array_member sambuf; > if (!sam) > sam = &sambuf; > - *sam = special_array_member::none; > + *sam = component_ref_sam_type (ref); > > /* The object/argument referenced by the COMPONENT_REF and its type. */ > tree arg = TREE_OPERAND (ref, 0); > @@ -12951,43 +13025,46 @@ component_ref_size (tree ref, special_array_member *sam /* = NULL */) > return (tree_int_cst_equal (memsize, TYPE_SIZE_UNIT (memtype)) > ? memsize : NULL_TREE); > > - bool trailing = array_ref_flexible_size_p (ref); > - bool zero_length = 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 default. */ > + if (*sam == special_array_member::int_n > + || *sam == special_array_member::trail_n) > return memsize; > > - if (zero_length) > + /* flag_strict_flex_arrays will control how to treat > + the trailing arrays as flexiable array members. */ > + > + tree afield_decl = TREE_OPERAND (ref, 1); > + unsigned int strict_flex_array_level > + = strict_flex_array_level_of (afield_decl); > + > + switch (strict_flex_array_level) > { > - if (trailing) > - *sam = special_array_member::trail_0; > - else > - { > - *sam = special_array_member::int_0; > - memsize = NULL_TREE; > - } > + case 3: > + /* Treaing 0-length trailing arrays as normal array. */ > + if (*sam == special_array_member::trail_0) > + return size_zero_node; > + /* FALLTHROUGH. */ > + case 2: > + /* Treating 1-element trailing arrays as normal array. */ > + if (*sam == special_array_member::trail_1) > + return memsize; > + /* FALLTHROUGH. */ > + case 1: > + /* Treating 2-or-more elements trailing arrays as normal > + array. */ > + if (*sam == special_array_member::trail_n) > + return memsize; > + /* FALLTHROUGH. */ > + case 0: > + break; > + default: > + gcc_unreachable (); > } > > - if (!zero_length) > - if (tree dom = TYPE_DOMAIN (memtype)) > - if (tree min = TYPE_MIN_VALUE (dom)) > - if (tree max = TYPE_MAX_VALUE (dom)) > - if (TREE_CODE (min) == INTEGER_CST > - && TREE_CODE (max) == INTEGER_CST) > - { > - offset_int minidx = wi::to_offset (min); > - offset_int maxidx = wi::to_offset (max); > - offset_int neltsm1 = maxidx - minidx; > - if (neltsm1 > 0) > - /* MEMBER is an array with more than one element. */ > - return memsize; > - > - if (neltsm1 == 0) > - *sam = special_array_member::trail_1; > - } > + if (*sam == special_array_member::int_0) > + memsize = NULL_TREE; > > - /* For a reference to a zero- or one-element array member of a union > + /* 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) == UNION_TYPE) > memsize = 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 * = NULL); > > /* Return a tree representing the offset, in bytes, of the field referenced > by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */ > extern tree component_ref_field_offset (tree); > > -/* 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. */ > }; > > +/* 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 > -- > 2.31.1 >