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+yw11ZCWSyjWMyDfXUAT6p8j2QPbJVPozY+rywfC4F8Yr+BsNgMMi0rM+CYjUW8dClgNnrgYdf8vplRlpCNs1PPi2mRtZeBbP4jIl1SejIqTCtMojJwrgJUA6gVNgM0wjUuo965HxZ8me3x9K4idiZ75NAcpxgEk9843X6bcn3Vl6eTKTxFf6n83fcoxWWPZY+V0k3FKaOcCaLJ4mohVBxe2vrcv3yRJnjAZIrMz9TJ2mq29O37C2HG6zf2+0Um54W3lNhRAE2bUButQosRv+16Ltd7lfMMm72cBz4hYTVsRBOyyJzOMHgikm+sY/pxmD1xFlN1B9YIvxsberGvhspNnjB4FifCkQfdMpQYXQoGC9nHJw68lZRvmIheOLJ9fakwOj5xu8hR64TI2cBXcfK+CUW39SsDL6WvAL48xYQvgLMikC/rNQdw1q67QN+cHId+llecrK6uTPfPmSQUoX6VoYqqf/+OBoKCyosnRrA+qKn/G4YK2N12fF5UVy82c81JZ2QpHzhUkEnAlOHhJIZUMk4lKWxCmEisYN1tb4/Eab9uqJEOG7dcLPJo4NV7EqDyU8ULEW+lUI3q+OO+k904LuTomScvIwNZMg+0sQr6O99494fzPHHMkcV+J36Ta8NgSc06MyAlMoeKsjF573Wl6nD2Pa/WpqyRZrAwiLivkHfQ71yjrndwJI4HzpyZGo3tkl9th4jHvsbxR7oMTz6PoOr4WMLrBSWUoiwPOsfHhprSoUFd/w40fs9X8GA/r0jE9hz1aSb9igGmfczZ3Exut28FQ2d3vAhIg5FcaQ1P6rblRL9fTH5UTwqK2bYM4E3yPnjp8VNxlzyB4Y1lr34kA1XmeIa9QFWbBGVC18MqFsrZrCIS8EWyMQe1n6k/5L6BkqC14IdrEJuThGAaTmD2V6NdI4fAwrW4W/3hFgjF8tQWwfMB4WmJ4IO5qYiFTAOvd/+lancRzAbB3fEA5d07ESH33rAA6pOQT8S3Bf0DUPh7VUwLageu+HxFIVhhYJb83ds9+uxk6we3qYaRxh3XocUIqGZestfg6RQNJ0xQkjn/BUcSd4xZLyZPEwcY0kU0PLRXydwncSCYN6jxc5CXJh9TZJiGwDP2mfu4oztXzlWvOsPRs0yUrOI+i17LkjL+LNrIa3JlaAmCYumuyUsoJABS/IYYPeZYMavC8VaGz0uQmi8mU2m6xgvFR5kwj9LVmiUAlJuE2fvU8agQ0wsF2MxOQp0AzM X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: HgpGe2HprA5QlTrJ0jwkdf4LG2BG8sGnYO4n5THgd7fL12OzlPfS5bmobuEGjyZhXcUUukW8ij5f/WAwIyb3WUaB9fbfAcqCOmxGczwddMZWpC2PkD908wwaXL3Z9uHjXhC4YXRsVTbYn9pDcGY+Hww5qr2uy/mW3YQPD7bjUdhE018QSnnre5vUL1o4LjdboAZnU3wb17Ap890/+DK8/oMLSPgyfS8i6WMf0rfJ9OoJnmElp9PNDCftSnlHXHOzoV50w2ZkMV9qkuJSN2u1Dydym1bfcxLvYKZmlztGQ13KMIcDmCSXfLwpXsTZiHXwZt2u9OJ9h+ZrxauRonhkIjDnXZ1wrrfhKcUTPfR6TCgaP071YW7tm0U2RiO/yvwQN3Ux5W8h7PHj51faXBiSxk+mQmX38xHNRgNg2nFZep7Vpz7hMSK/Dg0q3UIo1+mhEY1fwBvfI0oJP0HePhjuy9cPfef7Tu7ayGPkosWgpHRDrS4Lsq2UqOPok5OLZpWwyNXwuBXoMuSuAKNeYN2W3sAh8EZ8bhHSi7NoEfWTFsRP4sXAS+7vmFsSScdxn3ma2YN3jKc4Y0GAXI/tE7Xio2pxRNVFVQW8hztMV1m0YrWT1qyaweZbO8gTzQHYeeWMd4XcWaFisQB+qKTRlNujDol25HIK/FxXqKgYTv/fSmZ8ymPvbawZ72WlXR14YOtuWNmk7rHriuy4dd5ATfOS/N+Zi8CdBWBV/OEqgsz1PFr7CB1gbh24a0voM8T6pQbFgqDuTy6smH+Zbwu7uNo2xTtxwQXROQFQWhRK1MDjZY7kQB4vq5Lrs3Bewq2BSfNKbLrw9yidcSGdu7JhFBpbZitpBE0XhtVxvCw675P7bRUBKps6vSsxi35obw7Yf3ktYLWJFvWC6nyelcEGbnTPaw4XKpPpgSK8Nf3IligbOfk= 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" } */