From patchwork Thu Mar 16 21:47:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 66483 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 131B5385084B for ; Thu, 16 Mar 2023 21:48:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 131B5385084B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1679003294; bh=JY6ESzb1HV1FBzVcPBYqtKVELhmI52WhJDEOmVK5TQs=; 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=kywWLFO9tp5x9Bdv1Ys6qjn42VNvrAZvkkFARUW5stbvUcigSA7OIOpuV3VBuf/lA wUyhj9WjajIG7e4i7PLCsEqlJ91b24TrOYB20V6IA7jVtIPN19KmaeCkX0CybY0lvL eH8IsyutW0POOnFJBWjUvEBlUr/bsP+I016RKAAg= 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 2DE8A38582A4 for ; Thu, 16 Mar 2023 21:47:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2DE8A38582A4 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 32GIi9np008573; Thu, 16 Mar 2023 21:47:41 GMT Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3pbs262bt6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 16 Mar 2023 21:47:40 +0000 Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 32GKDnAw023437; Thu, 16 Mar 2023 21:47:40 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2169.outbound.protection.outlook.com [104.47.58.169]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3pbqkywv7t-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 16 Mar 2023 21:47:40 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ky8w9Z6dJlkRFgCadFxMlNW2+00GbgiIrdtPa76LzzfBClRb+X1J3ku3edCUCIhnumpAUaQ8HHibzQ+nbCtehoVVrcRIZfzowlMPtztvMnSi7ccIQDU/BmH81EFxswVzBtKllj0VJpkTgHlyKzVtK8vjLldf29ek+yb47YYK+b4vAuWXed5kCKIqUz37Tph0N8X2UDizXOdF+AinhvzBZUHK3oUD1FDnqJrDcSZMftgBGX0YthzK49m6lGLWlBqvWzdgsW6ugDu+fxdmri3WHm4wChrSFhWxMAwxSy4vuMzhOj2v3LIlKfHpqGSMQv34EUJ8C6g01mouYhLEzE48vg== 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=JY6ESzb1HV1FBzVcPBYqtKVELhmI52WhJDEOmVK5TQs=; b=QAHGMQxVT/+f4sXMCFL/gwbn9kO1zQNsu+Js6HU5HAgTzP1QiKg/LfcrTdPoExc8/peyqVpMY1RQI0qxFptYo5f0b8I3kf7q6CQ0bbJC6Mi8ztLZInndmmS4dADji+ao90wcNBr0x0liYEraw/oHvLDl7fwPwEGp3jc7jePb4EZfIBHCwrW+IcnBDbsd1IC+PdKjS0XabBHAZa62a5G0O0/0WQ38owtccOZoWDeSbaUJfLWdkeuKtMWooBIkDMZr5zCrVQ8muLwt6qnnLhUhRRxsgPMFA9ah46wx4qQHpDEjbxAh3iLksHZDBAUbi1fcWnwlsOs1g4BqCkSwt8GlLg== 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 BLAPR10MB5282.namprd10.prod.outlook.com (2603:10b6:208:30e::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.33; Thu, 16 Mar 2023 21:47:38 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7030:ed2e:20d0:cd5b]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7030:ed2e:20d0:cd5b%8]) with mapi id 15.20.6178.029; Thu, 16 Mar 2023 21:47:38 +0000 To: joseph@codesourcery.com, jakub@redhat.com, sandra@codesourcery.com Cc: rguenther@suse.de, siddhesh@gotplt.org, keescook@chromium.org, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [V5][PATCH 1/2] Handle component_ref to a structre/union field including flexible array member [PR101832] Date: Thu, 16 Mar 2023 21:47:14 +0000 Message-Id: <20230316214715.604671-2-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230316214715.604671-1-qing.zhao@oracle.com> References: <20230316214715.604671-1-qing.zhao@oracle.com> X-ClientProxiedBy: SJ0PR05CA0117.namprd05.prod.outlook.com (2603:10b6:a03:334::32) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|BLAPR10MB5282:EE_ X-MS-Office365-Filtering-Correlation-Id: c6fe7d41-e6f6-4546-a052-08db26680f8a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 7yjb61iCzYMYcGRgHc+L9mkPrOKQDHdNaVPB0GvpE7c78o7HG/TrhhW+W/n1elUpXmvVX/1fEd/dqI6+KZNSvejt73MEgZPmnf8avReq125FYEYk2dBR1y9Fy2aTeOpdX27c/vVO4AKdRfudPln0AESPCnSLxAPYMBjoeNnUshVDctf5frxAiIgIJu+mphUxxNKaqYlbkSR0SUUaxFfVvXbmwo87eyC0uQLq2MGnpspXeiJ88GCWs2kkfdBkLzsRgT9DrTj3ZYNZ8lnppIyBS0ewkhwMOExNyXOmqBW+K0LxSIIW2QAqBgwyUeo2P4uicZ4rUEyvz9FYivIygzEcZQetLCZ3lSMjeWD1w/KaaFBQ9JiSRdTgtraXVI1f1NU8y6R+UdEVSBX7f+hi9i0LfmXFUXipctgbCNtRSj3iGSkuGQWFt4S5rsPMC1cASW1ov/VE9uCef3oYqIyn6+90BaJB6uVoBhV5Jqeg77jSECA8aD1AzogP05weWBIcmVZeAb/Vx68MiRzTot93n4ktaYAXARp5ZqhDKSeaOHiIRubATiHK1l76MvpH360ChsHKqA3eZdJ4S6CCJ6+wJz5CaJWpLPTzqO4StKtJYZfZc1bmg601KmvmG8FYpeliCxuB 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)(39860400002)(136003)(376002)(366004)(396003)(346002)(451199018)(84970400001)(8936002)(41300700001)(478600001)(66476007)(66556008)(4326008)(8676002)(66946007)(36756003)(86362001)(38100700002)(1076003)(26005)(6506007)(6512007)(186003)(6486002)(107886003)(6666004)(5660300002)(44832011)(30864003)(2906002)(316002)(83380400001)(2616005); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: jlRjWO8BFh4TyxMWhDTA/blwPl9LrPi+ui8rWE0QebUFEqVFfj3cRzapCiEQFWnRmTq/KNaZO7+MrZ8pMS3z7pdBIDiMk5k+xTJwtrNmtRIEqjORBpI9zBAm0IkXUsjYsmtjaShl1aWEDXWkHtHWfD98OpOWHa+qDGn9TxwFIx83jzbZpwbyqz1uHTG9GG4ERxyST1Mo9wgQmH9sfpx03NlHIVVozAo6p0bNJdPcYUgwDvgJ7xNenuxLe4NIkVMI6IfeFkoNhT5gM0LIB0IwKlOPD19p3rOYuZodwE/kMpXtHh9JxwKWWcfDx8peh5/NoicGCmFAZ/t2eWxUuG5B3YalBAmlCLcJt4Mq5qojVi9F7TQwYDhPgUptFccca/EIVo1vEPX2akY3bTlpHk0EUtNXYewjsWlFxNa6Icd8tt6mqCeQG6qNzGyawxNvGQ/d8l2ioDbtvxIUZQ5nMFaMLNiEHqwdRrs0kw6/8kbxIEqAalshfZi6B3VQA+z5iRhrQbsUbr4GzOr+kxgBXs1ztHVRaep7FOgyqq2y382u0o5IpfT4ZuCt/+DAkL3E0Tsxsbtven4YS006WT/ZAkvy8fL6uQgB+zLTqh/i3o/VuTY0I6o2CAlZdfgHuVa+jFGli/U0TuviOT1Cs6DOTBMd2QM8D11biwPkCNMUSQCzjsGsGgCUbXKG7L76xNSU8GhZWOHaD0sXbEP2MoORi00Dz71Ct+N82l5oObL5VITZDMmukPBSmWqiN9rgBLJaB4kUReY9o831lYguFyQ5MNRltn3mAFKFLkm4X/kbYH/rBLow0e/Vz+Azr+N+GquAVfir1lanHsWUehEyd4a8hl1ZO1r0iYrKUwex6emm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A8UBgUx91C6hdyymHAdpRRHpEaeGfGULgxApi2S9LECNJvdyG7c00zobuIg209fuZLVDsUV+qq+GDmbnhjQQmVQdFS0y3MycmngSiNDF/6jicLsuz+LC6DjKylv77oDCuK+VZif7hZxjUADUZN4lOvZcLgikAPWISNcQRWXOn92qWJVtO6ToQMiF1Ub472ru4ICiZAFqnxypmeoJOthOxkmGAOK+Yfe4oevy4OWFMbqo/tU5CS+s+vjPt3yxrhlute0CTcpoihIPkl8UvzhI+Yn+hAtDfiAS9JZOBHBFtBhBuWrxCOgwcgFlIKHri4K6tH7HF9GcRrCW0siMOcYbncrCNvczOEZfzLSi0QN7vYhLeb+G6hHzYVh3UHHQsetpOo2Ljg9PU4xIPGmxtuDLRoLakSzes7SOurlFk1kyYfXeg7/CfubBvE1ij1UVromMtPxPg058uwReAU3c8DhNthZFa8yXFnnAQbpFTLq8I/zRugz5jIxBwbVKVf2+QKr0LrJapQ0vECMlWhdseasF15k6WF/xHDyZn+rUpxJ3wvaj6n3KBYnzABJCPOhTVwuH+MNG++sfg+e/TIJOJOc1dlsLIMcb4Gnff/Yvfr+/vzmnCctO6LXU8kHlsFebvlVZAcTPdlAPFjfSdBlSl2VqBl0QwLAQfjC2Zw4+n4= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: c6fe7d41-e6f6-4546-a052-08db26680f8a X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Mar 2023 21:47:38.3973 (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: fbegpeTrcx9rX6d45zjefgZ8czfEK3jbUlT83AS46q75FSEoPsm+EvxzjCVtxsfHCysUTYOritsng2KS0QqSiQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLAPR10MB5282 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-16_14,2023-03-16_02,2023-02-09_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 malwarescore=0 bulkscore=0 suspectscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2303150002 definitions=main-2303160162 X-Proofpoint-ORIG-GUID: FROt8d0uq0QJOnE99LF5A9z3Bn5XYdW3 X-Proofpoint-GUID: FROt8d0uq0QJOnE99LF5A9z3Bn5XYdW3 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" GCC extension accepts the case when a struct with a flexible array member is embedded into another struct or union (possibly recursively). __builtin_object_size should treat such struct as flexible size per -fstrict-flex-arrays. gcc/c/ChangeLog: PR tree-optimization/101832 * c-decl.cc (finish_struct): Set TYPE_INCLUDE_FLEXARRAY for struct/union type. gcc/lto/ChangeLog: PR tree-optimization/101832 * lto-common.cc (compare_tree_sccs_1): Compare bit TYPE_NO_NAMED_ARGS_STDARG_P or TYPE_INCLUDE_FLEXARRAY properly for its corresponding type. gcc/ChangeLog: PR tree-optimization/101832 * print-tree.cc (print_node): Print new bit type_include_flexarray. * tree-core.h (struct tree_type_common): Use bit no_named_args_stdarg_p as type_include_flexarray for RECORD_TYPE or UNION_TYPE. * 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 bit no_named_args_stdarg_p properly for its corresponding type. * tree-streamer-out.cc (pack_ts_type_common_value_fields): Stream out bit no_named_args_stdarg_p properly for its corresponding type. * 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 | 11 ++ gcc/lto/lto-common.cc | 5 +- gcc/print-tree.cc | 5 + .../gcc.dg/builtin-object-size-pr101832.c | 134 ++++++++++++++++++ gcc/tree-core.h | 2 + gcc/tree-object-size.cc | 23 ++- gcc/tree-streamer-in.cc | 5 +- gcc/tree-streamer-out.cc | 5 +- gcc/tree.h | 7 +- 9 files changed, 192 insertions(+), 5 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 e537d33f398..14c54809b9d 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -9258,6 +9258,17 @@ 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 and is the last field. */ + if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE) + TYPE_INCLUDE_FLEXARRAY (t) + = is_last_field && flexible_array_member_type_p (TREE_TYPE (x)); + /* Recursively set TYPE_INCLUDE_FLEXARRAY for the context of x, t + when x is an union or record and is the last field. */ + else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))) + TYPE_INCLUDE_FLEXARRAY (t) + = is_last_field && TYPE_INCLUDE_FLEXARRAY (TREE_TYPE (x)); + if (DECL_NAME (x) || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))) saw_named_field = true; diff --git a/gcc/lto/lto-common.cc b/gcc/lto/lto-common.cc index 882dd8971a4..9dde7118266 100644 --- a/gcc/lto/lto-common.cc +++ b/gcc/lto/lto-common.cc @@ -1275,7 +1275,10 @@ compare_tree_sccs_1 (tree t1, tree t2, tree **map) if (AGGREGATE_TYPE_P (t1)) compare_values (TYPE_TYPELESS_STORAGE); compare_values (TYPE_EMPTY_P); - compare_values (TYPE_NO_NAMED_ARGS_STDARG_P); + if (FUNC_OR_METHOD_TYPE_P (t1)) + compare_values (TYPE_NO_NAMED_ARGS_STDARG_P); + if (RECORD_OR_UNION_TYPE_P (t1)) + compare_values (TYPE_INCLUDE_FLEXARRAY); compare_values (TYPE_PACKED); compare_values (TYPE_RESTRICT); compare_values (TYPE_USER_ALIGN); 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 fd2be57b78c..1d94c9a8ce2 100644 --- a/gcc/tree-core.h +++ b/gcc/tree-core.h @@ -1717,6 +1717,8 @@ struct GTY(()) tree_type_common { unsigned typeless_storage : 1; unsigned empty_flag : 1; unsigned indivisible_p : 1; + /* TYPE_NO_NAMED_ARGS_STDARG_P for a stdarg function. Or TYPE_INCLUDE_FLEXARRAY + for RECORD_TYPE and UNION_TYPE. */ unsigned no_named_args_stdarg_p : 1; unsigned spare : 15; diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index 9a936a91983..1619d144ecd 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -633,11 +633,32 @@ 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 aggregate type, i.e, an array, + a record or a union, it will not have flexible size, + compute the object size directly. */ + if (!AGGREGATE_TYPE_P (TREE_TYPE (v))) { v = NULL_TREE; break; } + /* if the ref is to a record or union type, but the type + does not include a flexible array recursively, compute + the object size directly. */ + if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (v))) + { + if (!TYPE_INCLUDE_FLEXARRAY (TREE_TYPE (v))) + { + v = NULL_TREE; + break; + } + else + { + v = TREE_OPERAND (v, 0); + break; + } + } + /* Now the ref is to an array type. */ + gcc_assert (TREE_CODE (TREE_TYPE (v)) == 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))) diff --git a/gcc/tree-streamer-in.cc b/gcc/tree-streamer-in.cc index d4dc30f048f..56add06a70a 100644 --- a/gcc/tree-streamer-in.cc +++ b/gcc/tree-streamer-in.cc @@ -398,7 +398,10 @@ unpack_ts_type_common_value_fields (struct bitpack_d *bp, tree expr) if (AGGREGATE_TYPE_P (expr)) TYPE_TYPELESS_STORAGE (expr) = (unsigned) bp_unpack_value (bp, 1); TYPE_EMPTY_P (expr) = (unsigned) bp_unpack_value (bp, 1); - TYPE_NO_NAMED_ARGS_STDARG_P (expr) = (unsigned) bp_unpack_value (bp, 1); + if (FUNC_OR_METHOD_TYPE_P (expr)) + TYPE_NO_NAMED_ARGS_STDARG_P (expr) = (unsigned) bp_unpack_value (bp, 1); + if (RECORD_OR_UNION_TYPE_P (expr)) + TYPE_INCLUDE_FLEXARRAY (expr) = (unsigned) bp_unpack_value (bp, 1); TYPE_PRECISION (expr) = bp_unpack_var_len_unsigned (bp); SET_TYPE_ALIGN (expr, bp_unpack_var_len_unsigned (bp)); #ifdef ACCEL_COMPILER diff --git a/gcc/tree-streamer-out.cc b/gcc/tree-streamer-out.cc index d107229da5c..64f6c8bbabb 100644 --- a/gcc/tree-streamer-out.cc +++ b/gcc/tree-streamer-out.cc @@ -365,7 +365,10 @@ pack_ts_type_common_value_fields (struct bitpack_d *bp, tree expr) if (AGGREGATE_TYPE_P (expr)) bp_pack_value (bp, TYPE_TYPELESS_STORAGE (expr), 1); bp_pack_value (bp, TYPE_EMPTY_P (expr), 1); - bp_pack_value (bp, TYPE_NO_NAMED_ARGS_STDARG_P (expr), 1); + if (FUNC_OR_METHOD_TYPE_P (expr)) + bp_pack_value (bp, TYPE_NO_NAMED_ARGS_STDARG_P (expr), 1); + if (RECORD_OR_UNION_TYPE_P (expr)) + bp_pack_value (bp, TYPE_INCLUDE_FLEXARRAY(expr), 1); bp_pack_var_len_unsigned (bp, TYPE_PRECISION (expr)); bp_pack_var_len_unsigned (bp, TYPE_ALIGN (expr)); } diff --git a/gcc/tree.h b/gcc/tree.h index 91375f9652f..18c9f416758 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -786,7 +786,12 @@ extern void omp_clause_range_check_failed (const_tree, const char *, int, (...) prototype, where arguments can be accessed with va_start and va_arg), as opposed to an unprototyped function. */ #define TYPE_NO_NAMED_ARGS_STDARG_P(NODE) \ - (TYPE_CHECK (NODE)->type_common.no_named_args_stdarg_p) + (FUNC_OR_METHOD_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) \ + (RECORD_OR_UNION_CHECK (NODE)->type_common.no_named_args_stdarg_p) /* In an IDENTIFIER_NODE, this means that assemble_name was called with this string as an argument. */ From patchwork Thu Mar 16 21:47:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 66484 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 084D03850867 for ; Thu, 16 Mar 2023 21:49:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 084D03850867 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1679003354; bh=DSsoVXYRqMOERy1Z8kIAk97XFipJ1pYRiTePwxpacak=; 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=lm+H0KnQM8VT4u3aTJzuWeT7GGwqIR5rHXsZq8xfHeeait3z/cae1mvd/WlgI41iH veXJNi1vi1/HeEhV1my4Gnw15PY1Zd3hyssoaRLKaSb1dRM9XoRm6plBCc1hw/lZNV kPeGmZxIlA+ldghxNN8fkTUNSw1u05lOV/LokgPQ= 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 90C863857352 for ; Thu, 16 Mar 2023 21:47:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 90C863857352 Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 32GL8lrW000584; Thu, 16 Mar 2023 21:47:45 GMT Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3pbs2ajb7n-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 16 Mar 2023 21:47:45 +0000 Received: from pps.filterd (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 32GLO3vU029692; Thu, 16 Mar 2023 21:47:44 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2177.outbound.protection.outlook.com [104.47.56.177]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3pbqq5w3rs-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 16 Mar 2023 21:47:44 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Qnwdim81ol9z1EEnvI5suppIJW93SOlCqEZOnTL4asilyrkZ3tououOdMMIHKCuITTDl7Zq2C0b0GgTBUmyQQzJC/JEn5nnsrs4Y20e58d9rs8LDFXvvhDQ2M71P2+qo32aP2ZBLG5bY8QAOCkit14qbqi9sMQDcRbBtwXNgY7nRrRMHvp9WJlHAGX2iaoKwrNOYg5x4ZLAwGKVYVsemrQ6rkDpHecqAzKHbRCEKp8mbcLCpWWPgsUYUC3Y/WFSEDmKEzpcTsWJWb0H+lnm3paq8G7nz0F79Paiz6/Ok41qGNMfKTQ/hL80+x+/WDcQ3UMDcWXtPri4ckdy6PN6COA== 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=DSsoVXYRqMOERy1Z8kIAk97XFipJ1pYRiTePwxpacak=; b=Qr/kNDs1W5rpR57pCx0vstPWbb2+YuOqzkdn0iC2M5NOIZpwDk0y6NpE+i/+Rm95c+BYShRen8YN1YUm+X5sa5nNIkE3KOWR0s7/Gm3x8zWryp/2UhirQN7MyeqPkah7eRf/cESRbwVEB1BSE1fwsFqPF/LajV7jvv5JQLCW3KtzKFrZL+U0XNZfaOR5YS4jil9ervU+/7QizPH7qAiEDdD6X8rwLjXajMpVKAzaL46kNda9p4+oWYTFFDapci96m8CtzoUGrbUbi8hs4gt6Yoogg3GlVEnLf4n+MzpYaELXAUquJq6HPnUbBWzi7VRbYI/l719F81FNdh/HtVcb7w== 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 DS0PR10MB7091.namprd10.prod.outlook.com (2603:10b6:8:14d::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.31; Thu, 16 Mar 2023 21:47:42 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7030:ed2e:20d0:cd5b]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7030:ed2e:20d0:cd5b%8]) with mapi id 15.20.6178.029; Thu, 16 Mar 2023 21:47:42 +0000 To: joseph@codesourcery.com, jakub@redhat.com, sandra@codesourcery.com Cc: rguenther@suse.de, siddhesh@gotplt.org, keescook@chromium.org, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [V5][PATCH 2/2] Update documentation to clarify a GCC extension Date: Thu, 16 Mar 2023 21:47:15 +0000 Message-Id: <20230316214715.604671-3-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230316214715.604671-1-qing.zhao@oracle.com> References: <20230316214715.604671-1-qing.zhao@oracle.com> X-ClientProxiedBy: SJ0PR13CA0132.namprd13.prod.outlook.com (2603:10b6:a03:2c6::17) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|DS0PR10MB7091:EE_ X-MS-Office365-Filtering-Correlation-Id: 391b8f09-093d-4378-e682-08db266811de X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 3BpgC++i8M+u6NxtYycVIJ1TUU0ZpMFyIfp2i1zDI393PkQIr2P3y81HGk5BlIuwrNTVrfp6k0dUHr/lpe2uSq8iI2xydDvareGYCMi4ZnCxTe7QjD14yD/F/fhEGZeDKseWWd2korEnR5OCDUVBxQuRE+qzwyBPJU/DV9JJJTZ8U1yrrxVvyxnnfFpDJ6zAWqz1qCwYAhIMbOv9U+vpcCx5BOQgjT+D4Tur44hDQ/k7a9PXOI9A31iutVab4WWmVlQJ++pI9ZbNOCJSJ3kjcvAcJGXvrQJrslwySyfLyQybAre13cexVVRN65LDKMxZWKZP13QmAPxC7gtr+V8jBoc0OjEqNxi0f42RyBzJmJ2o19GakcoG0pdx2+dIdqirfBl4JG9VibDELo98uc0Ece3OuZXiagT7mi1ujx8Ny4kn/kcWMSCLWXUFhbB2OyMglDNgK4XLiYCVH4MosKNMpcHdDLoy0f5ccybm4erv2ykGiH7UlzV4Pe4JKQE+jx8YfXrDeipymgSNOiWJR7zq32naZAVHtEdPY4AioEgmKfQfF/vgt0Brre9tjm4JiGEcNEZPH+AbqxhYLdNU16Z322N6sCcqa3Q5lDVESiuwhD4TVDIIl3pKYs+SFSx+u7y+ 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)(376002)(346002)(39860400002)(136003)(396003)(366004)(451199018)(83380400001)(2616005)(84970400001)(44832011)(186003)(5660300002)(66556008)(8676002)(66946007)(66476007)(41300700001)(107886003)(6666004)(38100700002)(2906002)(1076003)(36756003)(26005)(6506007)(8936002)(6512007)(86362001)(478600001)(4326008)(6486002)(316002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: RqjjKyFvQnKuRMbHdsapubffOummuFKSKaDsiFAjcsdiYs6tBau80vBJjPcfiuBZYbpnddHtECVBudyNPey4BSpdAWRnNU0p3lmzqalolFrZk17Ov8hKANwCFnvpvqA474oCSDy2ZQIBj552xZV6luHgHDX8bM4PMu5L4InHiXonAunwVt0R8vIZPSEN3wVWMfiKWIIz99ka3JEdyujBH7dYSZRmm1CmNgmRh5aQtEpoh/jiyvp4bgvmd3OwojyZxRADSduIUn16N/CZZxD7oczQI28KEco4+4PzDrUzVlmJvi/y3M4hhrszXV3Yh03AEWlSqPMtYJUhyNoSHy8mqBn6jFRg9Ft+yw11ZCWSyjWMyDfXUAT6p8j2QPbJV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E0XhtVxvCw675P7bRUBKps6vSsxi35obw7Yf3ktYLWJFvWC6nyelcEGbnTPaw4XKpPpgSK8Nf3IligbOfk= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 391b8f09-093d-4378-e682-08db266811de X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Mar 2023 21:47:42.3987 (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: kItHD6g/tG6Ls5smmIENsDyZwbRYZlZJdOVqX+oAd87l13ldVzkf/5IuUhxkbhE9JpPJh0pyPmxaQWtiwskFWA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR10MB7091 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-16_14,2023-03-16_02,2023-02-09_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 mlxlogscore=999 bulkscore=0 mlxscore=0 adultscore=0 suspectscore=0 phishscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2303150002 definitions=main-2303160162 X-Proofpoint-GUID: C0JAqpBwxz0327z_lRX7ymsvin6fznEY X-Proofpoint-ORIG-GUID: C0JAqpBwxz0327z_lRX7ymsvin6fznEY 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" on a structure with a C99 flexible array member being nested in another structure. (PR77650) "GCC extension accepts a structure containing an ISO C99 "flexible array member", or a union containing such a structure (possibly recursively) to be a member of a structure. There are two situations: * A structure or a union with a C99 flexible array member is the last field of another structure, for example: struct flex { int length; char data[]; }; union union_flex { int others; struct flex f; }; struct out_flex_struct { int m; struct flex flex_data; }; struct out_flex_union { int n; union union_flex flex_data; }; In the above, both 'out_flex_struct.flex_data.data[]' and 'out_flex_union.flex_data.f.data[]' are considered as flexible arrays too. * A structure or a union with a C99 flexible array member is the middle field of another structure, for example: struct flex { int length; char data[]; }; struct mid_flex { int m; struct flex flex_data; int n; }; In the above, 'mid_flex.flex_data.data[]' has undefined behavior. Compilers do not handle such case consistently, Any code relying on such case should be modified to ensure that flexible array members only end up at the ends of structures. Please use warning option '-Wgnu-variable-sized-type-not-at-end' to identify all such cases in the source code and modify them. This extension will be deprecated from gcc in the next release. " gcc/c-family/ChangeLog: * c.opt: New option -Wgnu-variable-sized-type-not-at-end. gcc/c/ChangeLog: * c-decl.cc (finish_struct): Issue warnings for new option. gcc/ChangeLog: * doc/extend.texi: Document GCC extension on a structure containing a flexible array member to be a member of another structure. gcc/testsuite/ChangeLog: * gcc.dg/variable-sized-type-flex-array.c: New test. --- gcc/c-family/c.opt | 5 +++ gcc/c/c-decl.cc | 8 ++++ gcc/doc/extend.texi | 45 ++++++++++++++++++- .../gcc.dg/variable-sized-type-flex-array.c | 31 +++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/variable-sized-type-flex-array.c diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 3333cddeece..660ac07f3d4 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -737,6 +737,11 @@ Wformat-truncation= C ObjC C++ LTO ObjC++ Joined RejectNegative UInteger Var(warn_format_trunc) Warning LangEnabledBy(C ObjC C++ LTO ObjC++,Wformat=, warn_format >= 1, 0) IntegerRange(0, 2) Warn about calls to snprintf and similar functions that truncate output. +Wgnu-variable-sized-type-not-at-end +C C++ Var(warn_variable_sized_type_not_at_end) Warning +Warn about structures or unions with C99 flexible array members are not +at the end of a structure. + Wif-not-aligned C ObjC C++ ObjC++ Var(warn_if_not_aligned) Init(1) Warning Warn when the field in a struct is not aligned. diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 14c54809b9d..1632043a8ff 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -9269,6 +9269,14 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, TYPE_INCLUDE_FLEXARRAY (t) = is_last_field && TYPE_INCLUDE_FLEXARRAY (TREE_TYPE (x)); + if (warn_variable_sized_type_not_at_end + && !is_last_field + && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)) + && TYPE_INCLUDE_FLEXARRAY (TREE_TYPE (x))) + warning_at (DECL_SOURCE_LOCATION (x), + OPT_Wgnu_variable_sized_type_not_at_end, + "variable sized type not at the end of a struct"); + if (DECL_NAME (x) || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))) saw_named_field = true; diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index fd3745c5608..0928b962a60 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -1748,7 +1748,50 @@ Flexible array members may only appear as the last member of a A structure containing a flexible array member, or a union containing such a structure (possibly recursively), may not be a member of a structure or an element of an array. (However, these uses are -permitted by GCC as extensions.) +permitted by GCC as extensions, see details below.) +@end itemize + +GCC extension accepts a structure containing an ISO C99 @dfn{flexible array +member}, or a union containing such a structure (possibly recursively) +to be a member of a structure. + +There are two situations: + +@itemize @bullet +@item +A structure or a union with a C99 flexible array member is the last field +of another structure, for example: + +@smallexample +struct flex @{ int length; char data[]; @}; +union union_flex @{ int others; struct flex f; @}; + +struct out_flex_struct @{ int m; struct flex flex_data; @}; +struct out_flex_union @{ int n; union union_flex flex_data; @}; +@end smallexample + +In the above, both @code{out_flex_struct.flex_data.data[]} and +@code{out_flex_union.flex_data.f.data[]} are considered as flexible arrays too. + + +@item +A structure or a union with a C99 flexible array member is the middle field +of another structure, for example: + +@smallexample +struct flex @{ int length; char data[]; @}; + +struct mid_flex @{ int m; struct flex flex_data; int n; @}; +@end smallexample + +In the above, @code{mid_flex.flex_data.data[]} has undefined behavior. +Compilers do not handle such case consistently, Any code relying on +such case should be modified to ensure that flexible array members +only end up at the ends of structures. + +Please use warning option @option{-Wgnu-variable-sized-type-not-at-end} to +identify all such cases in the source code and modify them. This extension +will be deprecated from gcc in the next release. @end itemize Non-empty initialization of zero-length diff --git a/gcc/testsuite/gcc.dg/variable-sized-type-flex-array.c b/gcc/testsuite/gcc.dg/variable-sized-type-flex-array.c new file mode 100644 index 00000000000..e3f65c5ed07 --- /dev/null +++ b/gcc/testsuite/gcc.dg/variable-sized-type-flex-array.c @@ -0,0 +1,31 @@ +/* Test for -Wgnu-variable-sized-type-not-at-end on structure/union with + C99 flexible array members being embedded into another structure. */ +/* { dg-do compile } */ +/* { dg-options "-Wgnu-variable-sized-type-not-at-end" } */ + +struct flex { int n; int data[]; }; +struct out_flex_end { int m; struct flex flex_data; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ +struct out_flex_mid { struct flex flex_data; int m; }; /* { dg-warning "variable sized type not at the end of a struct" } */ +/* since the warning has been issued for out_flex_mid, no need to + issue warning again when it is included in another structure/union. */ +struct outer_flex_mid { struct out_flex_mid out_flex_data; int p; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ +union flex_union_mid { int a; struct outer_flex_mid b; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ + + +struct flex0 { int n; int data[0]; }; +struct out_flex_end0 { int m; struct flex0 flex_data; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ +struct out_flex_mid0 { struct flex0 flex_data; int m; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ +struct outer_flex_mid0 { struct out_flex_mid0 out_flex_data; int p; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ +union flex_union_mid0 { int a; struct outer_flex_mid0 b; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ + +struct flex1 { int n; int data[1]; }; +struct out_flex_end1 { int m; struct flex1 flex_data; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ +struct out_flex_mid1 { struct flex1 flex_data; int m; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ +struct outer_flex_mid1 { struct out_flex_mid1 out_flex_data; int p; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ +union flex_union_mid1 { int a; struct outer_flex_mid1 b; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ + +struct flexn { int n; int data[8]; }; +struct out_flex_endn { int m; struct flexn flex_data; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ +struct out_flex_midn { struct flexn flex_data; int m; }; /* { dg-bogus"variable sized type not at the end of a struct" } */ +struct outer_flex_midn { struct out_flex_midn out_flex_data; int p; }; /* { dg-bogus"variable sized type not at the end of a struct" } */ +union flex_union_midn { int a; struct outer_flex_midn b; }; /* { dg-bogus "variable sized type not at the end of a struct" } */