From patchwork Thu Jan 12 23:04:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 63127 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 55EA838543B8 for ; Thu, 12 Jan 2023 23:04:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 55EA838543B8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1673564692; bh=fpXsh3zJYIrk0InjMhXPiP5+eKJILCSvKjPf902AKJ0=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=ZKJGbW1d6o48sYi/eaGE7cl91XeUjYmTVJOdVgrY1JG32JFTl0q0fK6pZB2iDPaXu uYi9Dx+6gmZp1uMKBlDyqpczWhbDUijtbPZFmEGU9ikLNij0+YsA7mWgJeI0jn0vRT mPVIaIEhDZvl0YXXYyzYB8tNdSamhwpOSeRpsfzA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by sourceware.org (Postfix) with ESMTPS id 27EC73858C74 for ; Thu, 12 Jan 2023 23:04:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 27EC73858C74 Received: from pps.filterd (m0333520.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 30CJXpxa031569; Thu, 12 Jan 2023 23:04:16 GMT Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3my0sckk4x-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 12 Jan 2023 23:04:16 +0000 Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 30CMWunP021780; Thu, 12 Jan 2023 23:04:15 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2108.outbound.protection.outlook.com [104.47.55.108]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3n1k4qxgpf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 12 Jan 2023 23:04:15 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=iPl8AHuykLuEHU2amB5ZZnozIHA6dcdn6NEwHYhvBLzW6pSFUhKmmD8+TuaA1M9f28h20UjZaRhKZgeWi2c7R3FMpja4BgONiAjWIdJ04nFTOvgFuM35x5DdX+feNB2PsN8c59Z+muiZ4Eej+Wtv+vlVg4fa2tfupLlMjQUF0xKTU/DD6WvvVYk3uonGwkOT3LZH6NcmXqvabiNrgzW11Ex0UHfMmAWE3DtREveibZr/qwTmRbzDmQ3YLXnjRo1+MPQZUT6TL3TcBNl8QxpN7wrGJThMehLvaBnfC4FCnaBGAjoY+C5M0Y2BLpeQKMNws9m9SyV+ZuZ6ABQU8b0l+Q== 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=fpXsh3zJYIrk0InjMhXPiP5+eKJILCSvKjPf902AKJ0=; b=jecnEIj4qlsNKanD6C3gTnUF5bxt+mp62On1P7Opl+BolDbLUUB5HbpZzzwr1dfTcBhz+4uq9vaRSovYDzz52Yreqhxbh5+zS+FjSW549g/pVPBW9y3TKeWHrUBbNJiWFRLRcrxH+mjtetAyCrspyQF9tUIas9DKJJJ+KZILCymqzsH9khtF9lsi9Bt4TEH7QMXRt4oax9/O6Rj8DlQTXIedO22GjGF7gx0De3KuO2SeGVvuzNoaS9+ChHc8oYESAYVxU0W+pxXJOSE1HNSQ5U0L8lJkOmtpwZ4Aoyyi2gFMe3fO5qMdnzavrW+UZx36DkjTl1j/cA4v8wjzNOs9Kg== 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 PH0PR10MB5793.namprd10.prod.outlook.com (2603:10b6:510:fa::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.11; Thu, 12 Jan 2023 23:04:12 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::c96c:cb50:dea2:2cd2]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::c96c:cb50:dea2:2cd2%4]) with mapi id 15.20.6002.013; Thu, 12 Jan 2023 23:04:12 +0000 To: rguenther@suse.de Cc: gcc-patches@gcc.gnu.org, Qing Zhao Subject: [PATCH 1/1] Replace flag_strict_flex_arrays with DECL_NOT_FLEXARRAY in middle-end Date: Thu, 12 Jan 2023 23:04:06 +0000 Message-Id: <20230112230406.2023047-2-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230112230406.2023047-1-qing.zhao@oracle.com> References: <20230112230406.2023047-1-qing.zhao@oracle.com> X-ClientProxiedBy: BL0PR1501CA0033.namprd15.prod.outlook.com (2603:10b6:207:17::46) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|PH0PR10MB5793:EE_ X-MS-Office365-Filtering-Correlation-Id: 8ec47dd6-73a9-48fa-0111-08daf4f151fd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: wOk7sm/qtWrPzB+4ITbFIcPpiypT1Q3LuIM1hfa+3bLwNIc06AArGMV2BIuxu13+NAOJwAJx+t9NxDQbjUJ20qWIIJpaCXyic2ue7yXHC3P6zbC3ngnxlqvGnFbRAnwWBWUPedH/fMvVSkMoGetWONEy46HMf3FFjHNv5Xpqv8GmgF2KYc3kPMhFzKDA9pop0Tt0nbz4rh6K0uGlYCHucY9JNKugFgPSE4c8LKkVwtdjjbV6QvsVF3+u+NXsXD9pAIGCzMbHoztcOZXaCPTmAMVbrIX8hUMYDcatFUqQrXsY2431AkSYKSCVbYSgiWbnZkIM/paJZpCZWP3bCcloxcMNJMw4hwOf5VzGnKsW5NoTUoe+Tpl7lyzo/eiUwZd62oVFRBuF67+FjOWZKbF+vT/X1cquUs0XLR8nnVP+siq24aZCOp7xnzR2vRqABLZY7Z2cPg8i8l+Ihun4bqJlGHRiskw2DVRRF7b9xLaHjqMAmijiI1th865KQ7NvMv2ACVf4Rwqx1I9QQyHxbLQFOm1idAjRHjdOFzZEDutIG7qGdyyyLq15GEU2es4l5j2ql/dX6MP2c/cfpas2/K3U01abGRgXv2iCfl1pNrVJ0+vcy1w2XSEJblyF/Qf3asAKe9udEclgxmxPBenjsw4OacEVjczN87B5ekDvU8JemF1PV3xuSMY2zk4znnDqGuICfA/RHvQFg3ZlCixMZ7Adtk/vaertOz1qQRGErFTlQ6/BA3/6SOYjjOaFFG/K8jwe 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)(396003)(366004)(376002)(39860400002)(136003)(346002)(451199015)(36756003)(84970400001)(66946007)(8936002)(4326008)(86362001)(44832011)(8676002)(66476007)(5660300002)(30864003)(6916009)(6486002)(38100700002)(2906002)(83380400001)(478600001)(316002)(1076003)(41300700001)(66556008)(107886003)(6512007)(26005)(2616005)(6666004)(6506007)(186003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: d4hlTPNDXPopLqvgRyDrbH5tP4bqDPO3KCu8R8WI6/rsU1BGbeIZboE3b+cgNlJTQFfVrFol6J2Fir+5wZAOq3OsCazr1lIiKSq05PmaG1EbKlQ4nohV02eA3SOqbDh8Sxt2nqujR7pxPZ/b/qHmWh9gSt0IaqW6FyHmzA23di3aH2zWmd20X3lBstZ8Y7/KAAJzzxCRn6GTHVdRh7t7EQzDJwWqDq/tnPlykCNi7R3wGFkgBLHFl0fALJLjZ5IRwiKulS0Ol8fZY6TytgG4CACJ/myxeAc4ud918P8PIWh63czya4+nZQ8Qbz6aKf3NDT6w93og8s39sjdkh9M8FuPPZZ7PUpInvtCqAaUSuXP5p+LFrp4zDk+fUjrkfmrCOigW5Q9CD2UUo/dM3stLhQXjtcirdKk4fRHXWbcQCIydo2lMLJO8gvtgSlDqbtp4WJF5yrkX+4wuBx1oV1nM7lPQ9CPaPp+YjZAnbA3aI9gN9S+1wbqYa/hmiFKWj+5dDsfLGFRMtw/K+anq9I2EzUWW4EiH3nFYpVBX2lrS1oquBJg67BUDo7lsfqn57l4FVN5e9hFfRPxz9XGh+tPq+vXKy3IIpKi2JFgov3y5lxaB6FY44Wm4N7dgYQd2O6y2HNERdKuKQKFUv2SMCVgaLthk9FxJuI1eHxBEYX/k9gX5+B1uCWKQQTi0C7s8726CJ09VFN8Eo+hPyRxdf4FF4eVZzOWYVRlafap2piMZk4Pqoc00FKG/4lHXloiq7f73XduNrGnrHead1lz49N5ywoepsDQkhN0p6Z7Mb6Ly1QM/ecKCntqzXw8aCST0Q935fFTjGWshf+j6g8nlKFfFvJpRkhf9bG6z7vSikv1eGTLNQK+BVcaA4FfNiaYFdjAhBxGq+E0CAZqk0wvuL4+OZvwanJbUMKzDSXneuItvRv1C/kiCGc0vXCXT+m2YxeX5S/mfty25Km+2/nmJRyhY1Hz9t+t7cS651T3cT2X4aqm3YcM2NeieLem8lzYAMkuz4v/lbVEoLemDWezBV9/ioOjowL/DhYjugj/LhWTdEm8O0qrtYHzYTl4VWJvRRKjdibkozTahxBUBAuIPAtelmQ5qB5Pne8JdDTXd2vKx9tdABd4xaoJt/l+a8tz6w/ek01LidJx0umjm6l4fPKzo29iUP0rX24o/SRUxUoIV35uF5OrRZ3Uw9C87lF0uNoP1ZHQzOOnA83TDn4VWbXgNI+HgNE2qQTYZRHRV78m8sLmlr7xwYRNcpUQjuiqsGG/v/CUHKN7OdKcyQxevrUi7jwieYeb07LuiDofbwVDpyAZbPRIIumcbhhujtaTlRZvhTw6ijQpPYGRQpK3n9AAXti8sWjg8+N0L/Kt4yicknUvVmbgNp2+aUbUN3k01aj+eSLeuAy6ZFJ/jIvVEOAtwp41sEy+u5kLhlN7ry0y2AgDdlFiI6v+7h2C8DA0dOfOk43ZIlKWmd1UBJFroNW/yI/tJkgb40WoNwAT2NbuC29AQjUUqBXLew9QH0c/+YCbQcfBAOZqUkRFJSq2acqJjZmdr9K86+qJ1gDd9OZpexYhVIGW0r9+4rB78BhWLEh+1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: JbxKKAh1oVgsGrru8o1EeMzRfP6RHUGgz7V8wO3kzkdwqzynOw3JOh/xW9kInfycC2DWDkn5w5l15ueklHiUSI7pNTYZNDdP41c3rH37Go2Kjp41qQkycuP7IWkanobqcFyqv7WJLG0syeVDAdCOCxB5vpjtiouA6xjRGJ7pUZXgwYcQFcvQJndX6DGd3eXWrfcJErM5mI6x1AmoF1m47gxLkha2fuVW6REmaCGs9aigIeDvqVsmNcu5xNn3iaOGvwiUpsD6eZZ1Ur3AXlwdAQat7QaiB8IG2bFRoXWhM4NEqfM06IdZCjhN/nih1gEw8VjvHjwkmQ+z3YxipENRE10wY8s3qbh0UcgUGQc0Yi0A9sLaZtz3khx0IiRE0KHjO/W8b0xcLvc+Qg0fDgkFaoNHxbQ5+sQFXJkYMwNEmDdGe5O6BCo058saAiFVXkht/aa2Vop3diQYfL9zaULfDTsy6bm7OvLWKXMMijId8AfwBjrAgXXfvqhO9BDqTu/CrB6uB9EAfuI0hLsVtC0u3WvsSL2Z55/778qrPL4MCssHZV8as1Q3tj362Fcqx37IICuc9pKsUeMv4ILtubVzmamHBjALN/GeSENCXEnWhBjXiAbRqJX0o4sXVnECsebvKwAOidQfcSsRZ7EVpcCSgEbmdGMJ/R7ezTILSg9OUn53DIfRsTQbIWQ0mMZ5hBIM4gehMZR6vKBqGsxwACS/4bij68rQUtzJYOvqRAvd5OG3JUZ5XWAr+tCfQBMz3T0RYTdFYW9sI+58SEZKjJfxmw== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8ec47dd6-73a9-48fa-0111-08daf4f151fd X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jan 2023 23:04:12.7114 (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: pDRSbYfDoZoZkYM5vDnlkf3bFt1bPfti3Sv4q1JB3kONujVvo4UgwwSMlnSTRA9dxxcjhf+jM+WfDa9BjjR/zQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR10MB5793 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.923,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2023-01-12_12,2023-01-12_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 phishscore=0 suspectscore=0 malwarescore=0 mlxscore=0 bulkscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2301120166 X-Proofpoint-GUID: qZVglvTyLweeIbKxR3ubXqhJJLrlQV9d X-Proofpoint-ORIG-GUID: qZVglvTyLweeIbKxR3ubXqhJJLrlQV9d X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Qing Zhao via Gcc-patches From: Qing Zhao Reply-To: Qing Zhao Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" We should not directly check flag_strict_flex_arrays in the middle end. Instead, check DECL_NOT_FLEXARRAY(array_field_decl) which is set by C/C++ FEs according to -fstrict-flex-arrays and the corresponding attribute attached to the array_field. As a result, We will lose the LEVEL information of -fstrict-flex-arrays in the middle end. -Wstrict-flex-arrays will not be able to issue such information. update the testing cases accordingly. gcc/ChangeLog: * attribs.cc (strict_flex_array_level_of): Move this function to ... * attribs.h (strict_flex_array_level_of): Remove the declaration. * gimple-array-bounds.cc (array_bounds_checker::check_array_ref): replace the referece to strict_flex_array_level_of with DECL_NOT_FLEXARRAY. * tree.cc (component_ref_size): Likewise. gcc/c/ChangeLog: * c-decl.cc (strict_flex_array_level_of): ... here. gcc/testsuite/ChangeLog: * gcc.dg/Warray-bounds-flex-arrays-1.c: Delete the level information from the message issued by -Wstrict-flex-arrays. * gcc.dg/Warray-bounds-flex-arrays-2.c: Likewise. * gcc.dg/Warray-bounds-flex-arrays-3.c: Likewise. * gcc.dg/Warray-bounds-flex-arrays-4.c: Likewise. * gcc.dg/Warray-bounds-flex-arrays-5.c: Likewise. * gcc.dg/Warray-bounds-flex-arrays-6.c: Likewise. * gcc.dg/Wstrict-flex-arrays-2.c: Likewise. * gcc.dg/Wstrict-flex-arrays-3.c: Likewise. * gcc.dg/Wstrict-flex-arrays.c: Likewise. --- gcc/attribs.cc | 30 ------------ gcc/attribs.h | 2 - gcc/c/c-decl.cc | 28 +++++++++++ gcc/gimple-array-bounds.cc | 46 ++++--------------- .../gcc.dg/Warray-bounds-flex-arrays-1.c | 2 +- .../gcc.dg/Warray-bounds-flex-arrays-2.c | 4 +- .../gcc.dg/Warray-bounds-flex-arrays-3.c | 6 +-- .../gcc.dg/Warray-bounds-flex-arrays-4.c | 2 +- .../gcc.dg/Warray-bounds-flex-arrays-5.c | 4 +- .../gcc.dg/Warray-bounds-flex-arrays-6.c | 6 +-- gcc/testsuite/gcc.dg/Wstrict-flex-arrays-2.c | 8 ++-- gcc/testsuite/gcc.dg/Wstrict-flex-arrays-3.c | 8 ++-- gcc/testsuite/gcc.dg/Wstrict-flex-arrays.c | 8 ++-- gcc/tree.cc | 37 +++------------ 14 files changed, 68 insertions(+), 123 deletions(-) diff --git a/gcc/attribs.cc b/gcc/attribs.cc index 49cb299a3c1..7db730cf831 100644 --- a/gcc/attribs.cc +++ b/gcc/attribs.cc @@ -2456,36 +2456,6 @@ 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 e7540f71d6a..140e70b64e0 100644 --- a/gcc/attribs.h +++ b/gcc/attribs.h @@ -398,6 +398,4 @@ 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/c-decl.cc b/gcc/c/c-decl.cc index d76ffb3380d..38647c100c1 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -9047,6 +9047,34 @@ finish_incomplete_vars (tree incomplete_vars, bool toplevel) } } +/* 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. */ +static 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; +} /* Determine whether the FIELD_DECL X is a flexible array member according to the following info: diff --git a/gcc/gimple-array-bounds.cc b/gcc/gimple-array-bounds.cc index 116200a317f..66fd46e9b6c 100644 --- a/gcc/gimple-array-bounds.cc +++ b/gcc/gimple-array-bounds.cc @@ -350,18 +350,14 @@ array_bounds_checker::check_array_ref (location_t location, tree ref, /* Set to the type of the special array member for a COMPONENT_REF. */ special_array_member sam{ }; - + tree afield_decl = NULL_TREE; 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) + if (TREE_CODE (arg) == COMPONENT_REF) { /* 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); + afield_decl = TREE_OPERAND (arg, 1); } get_up_bounds_for_array_ref (ref, &decl, &up_bound, &up_bound_p1); @@ -412,39 +408,15 @@ array_bounds_checker::check_array_ref (location_t location, tree ref, /* 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) + if ((out_of_bound && warn_strict_flex_arrays) + && (((sam == special_array_member::trail_0) + || (sam == special_array_member::trail_1) + || (sam == special_array_member::trail_n)) + && DECL_NOT_FLEXARRAY (afield_decl))) warned = warning_at (location, OPT_Wstrict_flex_arrays, "trailing array %qT should not be used as " - "a flexible array member for level 3", + "a flexible array member", 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. */ diff --git a/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-1.c b/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-1.c index 65c9fec43af..fd2aacf6452 100644 --- a/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-1.c +++ b/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-1.c @@ -32,7 +32,7 @@ void __attribute__((__noinline__)) stuff( struct trailing_array_4 *trailing_flex) { normal->c[5] = 5; /*{ dg-warning "array subscript 5 is above array bounds of" } */ - /*{ dg-warning "should not be used as a flexible array member for level 1 and above" "" { target *-*-* } .-1 } */ + /*{ dg-warning "should not be used as a flexible array member" "" { target *-*-* } .-1 } */ trailing_1->c[2] = 2; /* { dg-bogus "array subscript " } */ trailing_0->c[1] = 1; /* { dg-bogus "array subscript " } */ trailing_flex->c[10] = 10; /* { dg-bogus "array subscript " } */ diff --git a/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-2.c b/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-2.c index 2b5a895c598..55b7535424b 100644 --- a/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-2.c +++ b/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-2.c @@ -32,9 +32,9 @@ void __attribute__((__noinline__)) stuff( struct trailing_array_4 *trailing_flex) { normal->c[5] = 5; /*{ dg-warning "array subscript 5 is above array bounds of" } */ - /*{ dg-warning "should not be used as a flexible array member for level 1 and above" "" { target *-*-* } .-1 } */ + /*{ dg-warning "should not be used as a flexible array member" "" { target *-*-* } .-1 } */ trailing_1->c[2] = 2; /* { dg-warning "array subscript 2 is above array bounds of" } */ - /* { dg-warning "should not be used as a flexible array member for level 2 and above" "" { target *-*-* } .-1 } */ + /* { dg-warning "should not be used as a flexible array member" "" { target *-*-* } .-1 } */ trailing_0->c[1] = 1; /* { dg-bogus "array subscript " } */ trailing_flex->c[10] = 10; /* { dg-bogus "array subscript " } */ diff --git a/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-3.c b/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-3.c index 25b903f2615..abdf8f15c78 100644 --- a/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-3.c +++ b/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-3.c @@ -32,11 +32,11 @@ void __attribute__((__noinline__)) stuff( struct trailing_array_4 *trailing_flex) { normal->c[5] = 5; /*{ dg-warning "array subscript 5 is above array bounds of" } */ - /*{ dg-warning "should not be used as a flexible array member for level 1 and above" "" { target *-*-* } .-1 } */ + /*{ dg-warning "should not be used as a flexible array member" "" { target *-*-* } .-1 } */ trailing_1->c[2] = 2; /*{ dg-warning "array subscript 2 is above array bounds of" } */ - /* { dg-warning "should not be used as a flexible array member for level 2 and above" "" { target *-*-* } .-1 } */ + /* { dg-warning "should not be used as a flexible array member" "" { target *-*-* } .-1 } */ trailing_0->c[1] = 1; /*{ dg-warning "array subscript 1 is outside array bounds of" } */ - /* { dg-warning "should not be used as a flexible array member for level 3" "" { target *-*-* } .-1 } */ + /* { dg-warning "should not be used as a flexible array member" "" { target *-*-* } .-1 } */ trailing_flex->c[10] = 10; /* { dg-bogus "array subscript" } */ } diff --git a/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-4.c b/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-4.c index 5fc500a19ca..82b38ff1ad1 100644 --- a/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-4.c +++ b/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-4.c @@ -32,7 +32,7 @@ void __attribute__((__noinline__)) stuff( struct trailing_array_4 *trailing_flex) { normal->c[5] = 5; /*{ dg-warning "array subscript 5 is above array bounds of" } */ - /*{ dg-warning "should not be used as a flexible array member for level 1 and above" "" { target *-*-* } .-1 } */ + /*{ dg-warning "should not be used as a flexible array member" "" { target *-*-* } .-1 } */ trailing_1->c[2] = 2; /* { dg-bogus "array subscript " } */ trailing_0->c[1] = 1; /* { dg-bogus "array subscript " } */ trailing_flex->c[10] = 10; /* { dg-bogus "array subscript " } */ diff --git a/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-5.c b/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-5.c index 30bb4ca8832..dca02288be7 100644 --- a/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-5.c +++ b/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-5.c @@ -32,9 +32,9 @@ void __attribute__((__noinline__)) stuff( struct trailing_array_4 *trailing_flex) { normal->c[5] = 5; /*{ dg-warning "array subscript 5 is above array bounds of" } */ - /*{ dg-warning "should not be used as a flexible array member for level 1 and above" "" { target *-*-* } .-1 } */ + /*{ dg-warning "should not be used as a flexible array member" "" { target *-*-* } .-1 } */ trailing_1->c[2] = 2; /*{ dg-warning "array subscript 2 is above array bounds of" } */ - /* { dg-warning "should not be used as a flexible array member for level 2 and above" "" { target *-*-* } .-1 } */ + /* { dg-warning "should not be used as a flexible array member" "" { target *-*-* } .-1 } */ trailing_0->c[1] = 1; /* { dg-bogus "array subscript " } */ trailing_flex->c[10] = 10; /* { dg-bogus "array subscript " } */ diff --git a/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-6.c b/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-6.c index e847a44516e..fc9f73b4535 100644 --- a/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-6.c +++ b/gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-6.c @@ -32,11 +32,11 @@ void __attribute__((__noinline__)) stuff( struct trailing_array_4 *trailing_flex) { normal->c[5] = 5; /*{ dg-warning "array subscript 5 is above array bounds of" } */ - /*{ dg-warning "should not be used as a flexible array member for level 1 and above" "" { target *-*-* } .-1 } */ + /*{ dg-warning "should not be used as a flexible array member" "" { target *-*-* } .-1 } */ trailing_1->c[2] = 2; /*{ dg-warning "array subscript 2 is above array bounds of" } */ - /* { dg-warning "should not be used as a flexible array member for level 2 and above" "" { target *-*-* } .-1 } */ + /* { dg-warning "should not be used as a flexible array member" "" { target *-*-* } .-1 } */ trailing_0->c[1] = 1; /*{ dg-warning "array subscript 1 is outside array bounds of" } */ - /* { dg-warning "should not be used as a flexible array member for level 3" "" { target *-*-* } .-1 } */ + /* { dg-warning "should not be used as a flexible array member" "" { target *-*-* } .-1 } */ trailing_flex->c[10] = 10; /* { dg-bogus "array subscript " } */ } diff --git a/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-2.c b/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-2.c index 2e241f96208..befc604f19b 100644 --- a/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-2.c +++ b/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-2.c @@ -31,9 +31,9 @@ void __attribute__((__noinline__)) stuff( 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" } */ + normal->c[5] = 5; /*{ dg-warning "should not be used as a flexible array member" } */ + trailing_1->c[2] = 2; /* { dg-warning "should not be used as a flexible array member" } */ + trailing_0->c[1] = 1; /* { dg-bogus "should not be used as a flexible array member" } */ + trailing_flex->c[10] = 10; /* { dg-bogus "should not be used as a flexible array member" } */ } diff --git a/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-3.c b/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-3.c index 97eb65ba0a9..cc37a092c3e 100644 --- a/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-3.c +++ b/gcc/testsuite/gcc.dg/Wstrict-flex-arrays-3.c @@ -31,9 +31,9 @@ void __attribute__((__noinline__)) stuff( 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" } */ + normal->c[5] = 5; /*{ dg-warning "should not be used as a flexible array member" } */ + trailing_1->c[2] = 2; /* { dg-warning "should not be used as a flexible array member" } */ + trailing_0->c[1] = 1; /* { dg-warning "should not be used as a flexible array member" } */ + trailing_flex->c[10] = 10; /* { dg-bogus "should not be used as a flexible array member" } */ } diff --git a/gcc/testsuite/gcc.dg/Wstrict-flex-arrays.c b/gcc/testsuite/gcc.dg/Wstrict-flex-arrays.c index 110fdc72778..735b23a1dc6 100644 --- a/gcc/testsuite/gcc.dg/Wstrict-flex-arrays.c +++ b/gcc/testsuite/gcc.dg/Wstrict-flex-arrays.c @@ -31,9 +31,9 @@ void __attribute__((__noinline__)) stuff( 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" } */ + normal->c[5] = 5; /*{ dg-warning "should not be used as a flexible array member" } */ + trailing_1->c[2] = 2; /* { dg-bogus "should not be used as a flexible array member" } */ + trailing_0->c[1] = 1; /* { dg-bogus "should not be used as a flexible array member" } */ + trailing_flex->c[10] = 10; /* { dg-bogus "should not be used as a flexible array member" } */ } diff --git a/gcc/tree.cc b/gcc/tree.cc index 78b64ee98b3..7473912a065 100644 --- a/gcc/tree.cc +++ b/gcc/tree.cc @@ -13074,38 +13074,15 @@ component_ref_size (tree ref, special_array_member *sam /* = NULL */) || *sam == special_array_member::trail_n) return memsize; - /* 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) - { - 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 (); - } + gcc_assert (TREE_CODE (afield_decl) == FIELD_DECL); + /* if the trailing array is a not a flexible array member, treat it as + a normal array. */ + if (DECL_NOT_FLEXARRAY (afield_decl) + && *sam != special_array_member::int_0) + return memsize; - if (*sam == special_array_member::int_0) + if (*sam == special_array_member::int_0) memsize = NULL_TREE; /* For a reference to a flexible array member of a union