From patchwork Fri Feb 24 18:35:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1747648 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=A/kGuyj6; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4PNdr46zLLz2461 for ; Sat, 25 Feb 2023 05:35:48 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CC6A33850223 for ; Fri, 24 Feb 2023 18:35:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CC6A33850223 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677263746; bh=gCGs666XNX/nMV/ViTHXcP61+mxf2U3kod4R88Gm9FM=; 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=A/kGuyj6cvik6X4SNhiV0C9ZaXHIScWqIJojvAWTjXPUDBRKyEZe9nbMvuZnnwfWg Onp5pp2VlYo7RjnScV3nGZ08yaXbWgpfzl4fwF8AToM82jKP1wuNfPWf4KO+jlKaA1 JnGHFc+gsyfTq9O/AoyVQImKh/+r65maAbayY6Zo= 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 8B69D3857C45 for ; Fri, 24 Feb 2023 18:35:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8B69D3857C45 Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 31OHhwvC019998; Fri, 24 Feb 2023 18:35:21 GMT Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3ntp9twxrs-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 24 Feb 2023 18:35:21 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 31OHQ39b030233; Fri, 24 Feb 2023 18:35:20 GMT Received: from nam02-bn1-obe.outbound.protection.outlook.com (mail-bn1nam02lp2047.outbound.protection.outlook.com [104.47.51.47]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3ntn49umj4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 24 Feb 2023 18:35:20 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZjKcajF7ZcTitCv1ORlm2z1b0nh/za0vTYQ7oQxDDeaY2FbWnGdUswsHdHPTw+gFYoHOZ1ZYYsK8wxA+uA865JHlgfyTblWiMiwetFCEHMiD4qPRu9snIad/6mKYgJzZvVtDASQgovq46YaK/R41OAMrIIOY2iygcbxt21O+g6Y2v1orx/9UwSMMqxU+GEbyZhhYshfD7ApoI7zURg3in60BrDmZdyhyRfEYzimGBvmTmne7FTJN89RcfKeU4ZAfrmKd8ygP2zS60x7IgtCWakqKY0pwHNh/XumYLph1cqfeMxKqOznpapfeQjgs4VvsJCMXr0K0lNQu1UDlEslxYw== 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=gCGs666XNX/nMV/ViTHXcP61+mxf2U3kod4R88Gm9FM=; b=CPzwNnRAliE6ZUHgwayjelV2RQGsgw/ehPO3cJ4oEL2Q2W/Ircmls2phSIuWv7oB4ID8sGVsorylyaM1WgL6hWtth2fXuNv+QLBwtMpOhPTyd6gOUdUIE8EcqR2Vu+ZFvgdt+6UhdktbKoq43Z1iN04rGE8/JdR4hKaJm5UKR3e38nJBvhUvsDoA9v+KAIvfJkuBoV++cLL9nufBO3ADPPSfGKhXAXua3j8d5Cia5N454PrJORf0fA5Xzsy62YZ8664oylg6stU6iUaFmFgkQBhwJzplNaFes6VzdYyDv+DziiBxzrxK8px6LeBjwGdy3WQU9QHJEo1YiYQIaJTx+w== 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 PH7PR10MB5856.namprd10.prod.outlook.com (2603:10b6:510:131::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.7; Fri, 24 Feb 2023 18:35:17 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7030:ed2e:20d0:cd5b]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7030:ed2e:20d0:cd5b%7]) with mapi id 15.20.6156.007; Fri, 24 Feb 2023 18:35:17 +0000 To: joseph@codesourcery.com, rguenther@suse.de Cc: siddhesh@gotplt.org, keescook@chromium.org, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [v4][PATCH 1/2] Handle component_ref to a structre/union field including C99 FAM [PR101832] Date: Fri, 24 Feb 2023 18:35:04 +0000 Message-Id: <20230224183505.4112295-2-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230224183505.4112295-1-qing.zhao@oracle.com> References: <20230224183505.4112295-1-qing.zhao@oracle.com> X-ClientProxiedBy: AM4P190CA0009.EURP190.PROD.OUTLOOK.COM (2603:10a6:200:56::19) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|PH7PR10MB5856:EE_ X-MS-Office365-Filtering-Correlation-Id: 3dba05db-0339-4cc7-e03c-08db1695e01e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bA1nzlz/XTjIO5gYlXM5w78xJfXc5eaAgK14OZvB2XXCH/KpEVgmGBsMj3BmoE0vVdR6S769zb4Xdq/fD+lIWN7kEFbvMCcUqM+zfpEfwiUXapmL/NtwjLdeh3mD74stzpD/3SVPnfhJB4XDEd5cfGjH1mN1ETidV8ZW2PBUxKsypigBsC3l5izIqf8uwUdbwXtdh/3JXSjo2hjfRyLkGQ4+F4vQjGOXnZnS4qMRjxOwZGDIOaoPhPPADnNrgb8vFcIshsBJc57KO4lfZITdQwdsa6Wc+pjiWQkU52i1xQ8qNRv3O9JmtpofpMpQWLP7wHC9q3SPFid2VDMTRUkl15AlWSQ2/Pi0N0MJt0X0z91OyAjFlNjNWuBI2k8J7D5mRdRYv6lws6RQ+rAR3r9oXcRBlQrlgbKBSMGO0QxQeQ7c1MLpqmxdfI72EE9AG/0tFmn8zl74sdFbYFIX/CT9QLJjeycNuw2+dFEIIbpk5PT/WOP/gtdXcy5om7odszSiSsEYhKQMvyCEUxRFwdAN7OeCeXE5iuviKLUSoiEMiLKk7mUoMMbux6zCe3TL3jGeMBHB6R7LI6NvtacIqQPwsxVbP65NaSJ2NBG3C7s1Yx3O1LtpFHDGMdVuGmdl3RRF 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:(13230025)(396003)(39860400002)(366004)(376002)(346002)(136003)(451199018)(36756003)(26005)(6666004)(6506007)(6512007)(1076003)(186003)(38100700002)(83380400001)(2616005)(107886003)(5660300002)(41300700001)(8676002)(66946007)(4326008)(66476007)(66556008)(8936002)(2906002)(30864003)(44832011)(478600001)(316002)(6486002)(86362001)(84970400001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Gi78L1/GgCodf+EF8J9JdVDCM1VbCe4LNKcu/0CKZnlg86PH1nTs+q5d1y+25aNQ20nSWriPBVqg5NM5zzwhBpGeWtQCle8v6vuLwm9v7+kknCjRqdz2kDW1IRPs7fkBD/6jk17e+JWAhv1iQC/tqkH7fVOpkBpetGc4CGZLdNIA64mfU8K1xchJu6UK0h1ezUKoNDdF+plyiaXhdOivJMzqh+nuguIRIX86KmEkGuhDf7EIdYsNC333BsG7kD5DvzSEYfc517iMyVoKCQ1i6yYIy9arIb0+eHvRPGIH2tQ4jPQAINtHfcHKur99nzYTvS78k6AEyGRjXYA4j04zJtBdqEZyLlaenr1nJv1UiDuYL05tVV1WDcdKeQB1/iaNt/8ujr4XlfKNVDsMowCpMcyQTic5J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racle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3dba05db-0339-4cc7-e03c-08db1695e01e X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Feb 2023 18:35:17.1458 (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: eYAWlHl741YQ9wcjsHZsqX9PraDRJZ4orqRL4J9ynkw4P3jX/lwUOdcopJSpeMpA9P6TZwpHCUB2CJMA+75laA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR10MB5856 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.930,Hydra:6.0.562,FMLib:17.11.170.22 definitions=2023-02-24_14,2023-02-24_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 bulkscore=0 phishscore=0 suspectscore=0 malwarescore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2302240146 X-Proofpoint-GUID: 1XucgJf4EeIOWjk76QdLBL8HSDH7ZBy8 X-Proofpoint-ORIG-GUID: 1XucgJf4EeIOWjk76QdLBL8HSDH7ZBy8 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_STOCKGEN, 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" GCC extension accepts the case when a struct with a C99 flexible array member is embedded into another struct or union (possibly recursively). __builtin_object_size should treat such struct as flexible size. gcc/c/ChangeLog: PR tree-optimization/101832 * c-decl.cc (finish_struct): Set TYPE_INCLUDE_FLEXARRAY for struct/union type. gcc/cp/ChangeLog: PR tree-optimization/101832 * module.cc (trees_out::core_bools): Stream out new bit type_include_flexarray. (trees_in::core_bools): Stream in new bit type_include_flexarray. gcc/ChangeLog: PR tree-optimization/101832 * print-tree.cc (print_node): Print new bit type_include_flexarray. * tree-core.h (struct tree_type_common): New bit type_include_flexarray. * tree-object-size.cc (addr_object_size): Handle structure/union type when it has flexible size. * tree-streamer-in.cc (unpack_ts_type_common_value_fields): Stream in new bit type_include_flexarray. * tree-streamer-out.cc (pack_ts_type_common_value_fields): Stream out new bit type_include_flexarray. * tree.h (TYPE_INCLUDE_FLEXARRAY): New macro TYPE_INCLUDE_FLEXARRAY. gcc/testsuite/ChangeLog: PR tree-optimization/101832 * gcc.dg/builtin-object-size-pr101832.c: New test. --- gcc/c/c-decl.cc | 12 ++ gcc/cp/module.cc | 2 + gcc/print-tree.cc | 5 + .../gcc.dg/builtin-object-size-pr101832.c | 134 ++++++++++++++++++ gcc/tree-core.h | 4 +- gcc/tree-object-size.cc | 79 +++++++---- gcc/tree-streamer-in.cc | 1 + gcc/tree-streamer-out.cc | 1 + gcc/tree.h | 6 + 9 files changed, 215 insertions(+), 29 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/builtin-object-size-pr101832.c diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 08078eadeb8..f589a2f5192 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -9284,6 +9284,18 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, /* Set DECL_NOT_FLEXARRAY flag for FIELD_DECL x. */ DECL_NOT_FLEXARRAY (x) = !is_flexible_array_member_p (is_last_field, x); + /* Set TYPE_INCLUDE_FLEXARRAY for the context of x, t + * when x is an array. */ + if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE) + TYPE_INCLUDE_FLEXARRAY (t) = flexible_array_member_type_p (TREE_TYPE (x)) ; + /* Recursively set TYPE_INCLUDE_FLEXARRAY for the context of x, t + when x is the last field. */ + else if ((TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE + || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE) + && TYPE_INCLUDE_FLEXARRAY (TREE_TYPE (x)) + && is_last_field) + TYPE_INCLUDE_FLEXARRAY (t) = true; + if (DECL_NAME (x) || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))) saw_named_field = true; diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index ac2fe66b080..c750361b704 100644 --- a/gcc/cp/module.cc +++ b/gcc/cp/module.cc @@ -5371,6 +5371,7 @@ trees_out::core_bools (tree t) WB (t->type_common.lang_flag_5); WB (t->type_common.lang_flag_6); WB (t->type_common.typeless_storage); + WB (t->type_common.type_include_flexarray); } if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON)) @@ -5551,6 +5552,7 @@ trees_in::core_bools (tree t) RB (t->type_common.lang_flag_5); RB (t->type_common.lang_flag_6); RB (t->type_common.typeless_storage); + RB (t->type_common.type_include_flexarray); } if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON)) diff --git a/gcc/print-tree.cc b/gcc/print-tree.cc index 1f3afcbbc86..efacdb7686f 100644 --- a/gcc/print-tree.cc +++ b/gcc/print-tree.cc @@ -631,6 +631,11 @@ print_node (FILE *file, const char *prefix, tree node, int indent, && TYPE_CXX_ODR_P (node)) fputs (" cxx-odr-p", file); + if ((code == RECORD_TYPE + || code == UNION_TYPE) + && TYPE_INCLUDE_FLEXARRAY (node)) + fputs (" include-flexarray", file); + /* The transparent-union flag is used for different things in different nodes. */ if ((code == UNION_TYPE || code == RECORD_TYPE) diff --git a/gcc/testsuite/gcc.dg/builtin-object-size-pr101832.c b/gcc/testsuite/gcc.dg/builtin-object-size-pr101832.c new file mode 100644 index 00000000000..60078e11634 --- /dev/null +++ b/gcc/testsuite/gcc.dg/builtin-object-size-pr101832.c @@ -0,0 +1,134 @@ +/* PR 101832: + GCC extension accepts the case when a struct with a C99 flexible array + member is embedded into another struct (possibly recursively). + __builtin_object_size will treat such struct as flexible size. + However, when a structure with non-C99 flexible array member, i.e, trailing + [0], [1], or [4], is embedded into anther struct, the stucture will not + be treated as flexible size. */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +#include "builtin-object-size-common.h" + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + __builtin_printf ("ok: %s == %zd\n", #p, p); \ + else {\ + __builtin_printf ("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ + FAIL (); \ + } \ +} while (0); + + +struct A { + int n; + char data[]; +}; + +struct B { + int m; + struct A a; +}; + +struct C { + int q; + struct B b; +}; + +struct A0 { + int n; + char data[0]; +}; + +struct B0 { + int m; + struct A0 a; +}; + +struct C0 { + int q; + struct B0 b; +}; + +struct A1 { + int n; + char data[1]; +}; + +struct B1 { + int m; + struct A1 a; +}; + +struct C1 { + int q; + struct B1 b; +}; + +struct An { + int n; + char data[8]; +}; + +struct Bn { + int m; + struct An a; +}; + +struct Cn { + int q; + struct Bn b; +}; + +volatile void *magic1, *magic2; + +int main (int argc, char *argv[]) +{ + struct B *outer; + struct C *outest; + + /* Make sure optimization can't find some other object size. */ + outer = (void *)magic1; + outest = (void *)magic2; + + expect (__builtin_object_size (&outer->a, 1), -1); + expect (__builtin_object_size (&outest->b, 1), -1); + expect (__builtin_object_size (&outest->b.a, 1), -1); + + struct B0 *outer0; + struct C0 *outest0; + + /* Make sure optimization can't find some other object size. */ + outer0 = (void *)magic1; + outest0 = (void *)magic2; + + expect (__builtin_object_size (&outer0->a, 1), sizeof (outer0->a)); + expect (__builtin_object_size (&outest0->b, 1), sizeof (outest0->b)); + expect (__builtin_object_size (&outest0->b.a, 1), sizeof (outest0->b.a)); + + struct B1 *outer1; + struct C1 *outest1; + + /* Make sure optimization can't find some other object size. */ + outer1 = (void *)magic1; + outest1 = (void *)magic2; + + expect (__builtin_object_size (&outer1->a, 1), sizeof (outer1->a)); + expect (__builtin_object_size (&outest1->b, 1), sizeof (outest1->b)); + expect (__builtin_object_size (&outest1->b.a, 1), sizeof (outest1->b.a)); + + struct Bn *outern; + struct Cn *outestn; + + /* Make sure optimization can't find some other object size. */ + outern = (void *)magic1; + outestn = (void *)magic2; + + expect (__builtin_object_size (&outern->a, 1), sizeof (outern->a)); + expect (__builtin_object_size (&outestn->b, 1), sizeof (outestn->b)); + expect (__builtin_object_size (&outestn->b.a, 1), sizeof (outestn->b.a)); + + DONE (); + return 0; +} diff --git a/gcc/tree-core.h b/gcc/tree-core.h index acd8deea34e..705d5702b9c 100644 --- a/gcc/tree-core.h +++ b/gcc/tree-core.h @@ -1718,7 +1718,9 @@ struct GTY(()) tree_type_common { unsigned empty_flag : 1; unsigned indivisible_p : 1; unsigned no_named_args_stdarg_p : 1; - unsigned spare : 15; + /* TYPE_INCLUDE_FLEXARRAY flag for RECORD_TYPE and UNION_TYPE. */ + unsigned int type_include_flexarray : 1; + unsigned spare : 14; alias_set_type alias_set; tree pointer_to; diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index 9a936a91983..22b3c72ea6e 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -633,45 +633,68 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, v = NULL_TREE; break; case COMPONENT_REF: - if (TREE_CODE (TREE_TYPE (v)) != ARRAY_TYPE) + /* When the ref is not to an array, a record or a union, it + will not have flexible size, compute the object size + directly. */ + if ((TREE_CODE (TREE_TYPE (v)) != ARRAY_TYPE) + && (TREE_CODE (TREE_TYPE (v)) != RECORD_TYPE) + && (TREE_CODE (TREE_TYPE (v)) != UNION_TYPE)) { v = NULL_TREE; break; } - is_flexible_array_mem_ref = array_ref_flexible_size_p (v); - while (v != pt_var && TREE_CODE (v) == COMPONENT_REF) - if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) - != UNION_TYPE - && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) - != QUAL_UNION_TYPE) - break; - else - v = TREE_OPERAND (v, 0); - if (TREE_CODE (v) == COMPONENT_REF - && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) - == RECORD_TYPE) + if (TREE_CODE (TREE_TYPE (v)) == RECORD_TYPE + || TREE_CODE (TREE_TYPE (v)) == UNION_TYPE) + /* if the record or union does not include a flexible array + recursively, compute the object size directly. */ { - /* compute object size only if v is not a - flexible array member. */ - if (!is_flexible_array_mem_ref) + if (!TYPE_INCLUDE_FLEXARRAY (TREE_TYPE (v))) { v = NULL_TREE; break; } - v = TREE_OPERAND (v, 0); + else + v = TREE_OPERAND (v, 0); } - while (v != pt_var && TREE_CODE (v) == COMPONENT_REF) - if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) - != UNION_TYPE - && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) - != QUAL_UNION_TYPE) - break; - else - v = TREE_OPERAND (v, 0); - if (v != pt_var) - v = NULL_TREE; else - v = pt_var; + { + /* Now the ref is to an array type. */ + is_flexible_array_mem_ref + = array_ref_flexible_size_p (v); + while (v != pt_var && TREE_CODE (v) == COMPONENT_REF) + if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) + != UNION_TYPE + && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) + != QUAL_UNION_TYPE) + break; + else + v = TREE_OPERAND (v, 0); + if (TREE_CODE (v) == COMPONENT_REF + && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) + == RECORD_TYPE) + { + /* compute object size only if v is not a + flexible array member. */ + if (!is_flexible_array_mem_ref) + { + v = NULL_TREE; + break; + } + v = TREE_OPERAND (v, 0); + } + while (v != pt_var && TREE_CODE (v) == COMPONENT_REF) + if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) + != UNION_TYPE + && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) + != QUAL_UNION_TYPE) + break; + else + v = TREE_OPERAND (v, 0); + if (v != pt_var) + v = NULL_TREE; + else + v = pt_var; + } break; default: v = pt_var; diff --git a/gcc/tree-streamer-in.cc b/gcc/tree-streamer-in.cc index d4dc30f048f..c19ede0631d 100644 --- a/gcc/tree-streamer-in.cc +++ b/gcc/tree-streamer-in.cc @@ -390,6 +390,7 @@ unpack_ts_type_common_value_fields (struct bitpack_d *bp, tree expr) TYPE_TRANSPARENT_AGGR (expr) = (unsigned) bp_unpack_value (bp, 1); TYPE_FINAL_P (expr) = (unsigned) bp_unpack_value (bp, 1); TYPE_CXX_ODR_P (expr) = (unsigned) bp_unpack_value (bp, 1); + TYPE_INCLUDE_FLEXARRAY (expr) = (unsigned) bp_unpack_value (bp, 1); } else if (TREE_CODE (expr) == ARRAY_TYPE) TYPE_NONALIASED_COMPONENT (expr) = (unsigned) bp_unpack_value (bp, 1); diff --git a/gcc/tree-streamer-out.cc b/gcc/tree-streamer-out.cc index d107229da5c..73e4b4e547c 100644 --- a/gcc/tree-streamer-out.cc +++ b/gcc/tree-streamer-out.cc @@ -357,6 +357,7 @@ pack_ts_type_common_value_fields (struct bitpack_d *bp, tree expr) bp_pack_value (bp, flag_wpa && TYPE_CANONICAL (expr) ? TYPE_CXX_ODR_P (TYPE_CANONICAL (expr)) : TYPE_CXX_ODR_P (expr), 1); + bp_pack_value (bp, TYPE_INCLUDE_FLEXARRAY (expr), 1); } else if (TREE_CODE (expr) == ARRAY_TYPE) bp_pack_value (bp, TYPE_NONALIASED_COMPONENT (expr), 1); diff --git a/gcc/tree.h b/gcc/tree.h index 92ac0e6a214..ab1cdc3dc85 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -778,6 +778,12 @@ extern void omp_clause_range_check_failed (const_tree, const char *, int, #define TYPE_NO_NAMED_ARGS_STDARG_P(NODE) \ (TYPE_CHECK (NODE)->type_common.no_named_args_stdarg_p) +/* True if this RECORD_TYPE or UNION_TYPE includes a flexible array member + at the last field recursively. */ +#define TYPE_INCLUDE_FLEXARRAY(NODE) \ + (TYPE_CHECK (NODE)->type_common.type_include_flexarray) + + /* In an IDENTIFIER_NODE, this means that assemble_name was called with this string as an argument. */ #define TREE_SYMBOL_REFERENCED(NODE) \