From patchwork Tue Mar 28 15:49:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 67034 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 9B42638708BE for ; Tue, 28 Mar 2023 15:51:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9B42638708BE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1680018709; bh=4nGAwciXM4v+9GRD2uQQPqWgZ3/rcDUIpCzUCQVThd4=; 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=T/mWt56wB916dttTKxA2L2kseeI93jL17CGe5kxrL0A0QTSHxnnIzDWXgMwrCJrjn 39iAU9cxH5IpWKCZpQS4GQD7AbcVlhztNGVRGLtGYZdPcWQ59qEx0+oxkjFmpR3+2U 8wPVkbKAHJnbC6ObuYnl44Iok0n8acV6W9xe67qg= 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 3AD0A38708F5 for ; Tue, 28 Mar 2023 15:50:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3AD0A38708F5 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 32SFo4XP023514; Tue, 28 Mar 2023 15:50:04 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 3pm37g000n-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 28 Mar 2023 15:50:03 +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 32SFLZOP033061; Tue, 28 Mar 2023 15:50:02 GMT Received: from nam04-dm6-obe.outbound.protection.outlook.com (mail-dm6nam04lp2048.outbound.protection.outlook.com [104.47.73.48]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3phqdebxxp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 28 Mar 2023 15:50:00 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=T4GcbvSjEtvSpGTAJm+3Xclt6YwilEWGFL4xZDJ3HGyDQH0cRfGDPf/n2wYxR1B0vh7RHzALvX7VjW/N9fOpgFGJ8YfWlxyIPzmM4iBbUvq2YHhsCkgC6oQVQmFqZlez1iQZtJ5DhPYN85sYIfi74dV4enAqomjj1p3xBbMLLMqAR5hD5nzsF3ehFuByFx41oxjMXbrkhzQ6+Ln8bQSENbNwIyHDvREgZ/+pz32ddNLdZRZSCee70f6zvF2hPRb+IhSo7tQgtimII6Ks1c/qhLvxwubX6j/TkcIW5Uzqesd1yduMdxzO3sUj9VnBywox47/JmQGXIO0+Z17UFxD67A== 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=4nGAwciXM4v+9GRD2uQQPqWgZ3/rcDUIpCzUCQVThd4=; b=YoEnq14pD+XE3xfNt1Fu+DaiF1Ko8oc9D/IjRw2HHDZNaiGb4xf6tfwsL6Z3NQnExg0AycBtKKy4rlebMicujg2pYIYOuY5P5yzJDc9kyt/ssNuGTBuyRi9FUFg3EoflILuVRop0lf2q0cFvi/6hfDQCCn9UKHtulo1Pmf0HRW0J5VXDgTXTDe4DH+v5qujvCHgF2E3coVMKxKJpDpIaK/RvwtGiHYslC6s/tKKegv/bUc3xoXRTenxuqnuLZX4JdnbFYT5gaeyptjXPrTWfSPG1oxTBsk1unOK1Peymc20PHy4Tf2VibRVAdbNXxVnBz2WsbvL9tQj1PKVS7rvZSg== 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 CO6PR10MB5619.namprd10.prod.outlook.com (2603:10b6:303:14a::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.30; Tue, 28 Mar 2023 15:49:58 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::a33f:73da:f5e2:6c0a]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::a33f:73da:f5e2:6c0a%5]) with mapi id 15.20.6222.033; Tue, 28 Mar 2023 15:49:56 +0000 To: jakub@redhat.com, joseph@codesourcery.com Cc: richard.guenther@gmail.com, keescook@chromium.org, siddhesh@gotplt.org, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [PATCH 2/2] Update documentation to clarify a GCC extension Date: Tue, 28 Mar 2023 15:49:44 +0000 Message-Id: <20230328154944.3946619-3-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230328154944.3946619-1-qing.zhao@oracle.com> References: <20230328154944.3946619-1-qing.zhao@oracle.com> X-ClientProxiedBy: SA0PR11CA0036.namprd11.prod.outlook.com (2603:10b6:806:d0::11) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|CO6PR10MB5619:EE_ X-MS-Office365-Filtering-Correlation-Id: b302a5b9-1633-40c6-eb08-08db2fa413f3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: SMLMI/eDz8jJwFg3tc7Bbz8wjnHd0zYPLJSl8oX7gTL86ZlESahPRhTDAS3Bp+LxEkm8aKuF3jgkEbngwY4jM9/FJTqbcTwat5ySToilHB0Dn8exeAd5AXOC/4WsoQHhnLfiRELmzfle6siUPaANkt6oHHDHOaCc3p6xR99XwGT2vQCfY8b3wphnb7xidYmAU3dze5ad+FUiPbhR/sJbe/6wwCbxRijapQhCzxZGxRdgBz01bfrC109/RYxcJK1K+MtFxPUKa4AzHljECZmH6j/KDjkzWxyLbSMNR1jKaoZURyO0kxZPlZ4ItJSpkafqFZWNrjoBP3vn5wlG6e8dVNcWHLEPDvyAhW3c1ael4tAtZ63Jf6haHGez6FWPbSnKNzGDuiBCrdYsxPywJBhAC1UmFyNZtJBdz+vPY/OnnECMUn3xFgpU1HqJV3pA7YTmDPMisQFd/L8wpM54w1SkfYjJ8KzdJQh/wtFjBXQtQPKpYr8a37s7BPtpTws7rqtTWQZn/J8JXX5bn8yPbLrHpJTomIT3qZ4onnKYpNNDK8A= 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:(13230028)(136003)(366004)(396003)(376002)(39860400002)(346002)(451199021)(36756003)(86362001)(186003)(2616005)(83380400001)(1076003)(6506007)(26005)(44832011)(6512007)(84970400001)(107886003)(8936002)(5660300002)(6666004)(41300700001)(6486002)(4326008)(8676002)(2906002)(38100700002)(66946007)(316002)(66476007)(66556008)(478600001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: YZ/TdUACWjk1X4scGzvr80mhJa4OdTWwmvQm/z5bEHvtklPpEcGupCjsiB4gzgNlZWLqevE4DLrr5LTVtxrhkTPbiCmKq33TuWMpSIu/YgRwu6NGTLDHB57BNHSEVjpznQ2xT0zcc3EE/QcWOn8TYYPiYx4TsWPV8LaXS2yoHoIE0fJRavj44GYIh0dvwenCBtQwO8XM7eT19SbjmduYW7MCtZSoIr7zlKFQ9aIEMqFxKanBOR4uKyLhhKMZpGVk8FqFLMWfM+TStggSogxhBe89U7SySXCqU4DAyl+qksxN2ELDJctZrm87HBSJ0IMiNlj2WQdLUAMUbXJmX9xdCSS/lU9hAi1VJSCWMKU93DgMPkqX2xM6jD6eK4VXgRB+6ThCLPKhatSfVPuk9WO/yXt4jOcHLDVInJiAJ8wx9IApV1eRLAV5mQhXWj1S6ZVuDJgprSt3A3E36O+jMh/JVa1boYlycM5j8eASZrD66lOW+sIT4wKdILfWuGNKtnKitEcrhllYJ5asFpIejeg6xgya/rO5XkwCgHCPeYI8ySWixJ18Ku5QhAvHNGNe1Etb0ToKxAEwklaI5wTSCrYfKy94N9Jwb9m0OHOfw+VYZz5rlEWq3bJLGOebJxuyvVzghjo9Qt2uh2hvKmgllAiGEFRvgxSHfmq/L8/OP8dVgIrIIrMFiSHTpxrnj6xc2llg4CFxDpeVJh1BA0Ih55x/vMMrpmWMa54m3i0fifpM7wnTeuVgQRANgzkMpEUSSFJQI3tDP73y0UP9UWT92Y56rOQR9el7gyMXA6Fqgygc+vFNeHxGU/CaOMfyNJzbld2PPpxEcGnFv9A2GNl5jdYVAoD0iNa+e8+ZVC74PQxy9oYUjkyxtRGMq4/jvsf2XcO4J9llfcC2sZs2WKweFMdPCY/NldtoELyMPfr7TxN9eTZrtXrSbO01my4wFOLeNFpSb4GzYTF3ULrI8KyEWM9k/ByD0W4hxpGBEltB9WS4XAB/Wp6Pe6Zvlz0Z1i3csCrIJ0gknb1SF57jlXo2gPTSNoMA8vEwA4sKrBIS4uR3XfSfaoH9RmPjRWVyFIX4SnqNhVF+iJljzjbDlW3h1aJW3K7fkS+IIRqo6WpfDYMhcL8FaHutMtbEKFaoQ3sjgU+m1zavdkCasdJogdfKrRbyv7eQMp0+YbF6q3kfxePUaWLnbMT4Hzp2qte811mhv/Lzx3+umxGr57w8kTJsno/YTB0fmInS+LKrzzoey9LhRrsw5CppisjoUs+bV+MfwivXMkbykmwKCs6LMIs03zBImeX84KnRbuf8qL4sp6xsFB9ZL72LstXzLCtSXAhMZoUK3/sOqPEQQLCpOZWDThGyEQTxTQfiV6cxEzT7362Z2F3VpLinNWQSVIoi/c5TjiIscPC0lzn/+5hQfnDCq9zL4wN43PBdjUoBYgBOiKArOh1q8x5y8HUT37eZD7mLqF1xZHE6G5mh7X+7mk/dEXwzrKu5g1og3J7K+U9bFFJbENnbMsFKEHHMZzaqY6U9Q6SZ3QrKPUT0f66L5f/bNirZnkG+LYpWA5hYAlHkyjdgKRlppXUmn0Q9M8nPwqA6LUXI X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: njjB2U5ladIScRivCORSF89aPZdIhTcYEb2TI4lWqT+6UaHoprNSRqd3Vg8PxLiLbvrhwS/5/JKWuf3ZMvWmZ8goMOdTrgGF4C+LpOVRTBW7wYEX1bT3jhqatrdTdJFr48ctTpn1XIP01xI9L2CQpnzHCZ9ScZMAoMLZGqOdUmOUFgYU3DKCeweo1ZvytVunqhh/n1nKeYvVh2sC4HtFLDBZtVCcrWM5Fg/lk+9xQRNEKNZ6uYjsZgcFkCfzoSi9qrNkXLU5ug+TwQ+hKc/1SHwIOjE3K392TmdrveazNLDySyLKitaaSC7mADNY5UKgurKW5inZnZMCTezQDBOLjg6P38Q01Tkb2IqXB4f5pNHBdzYWh10CFEy5C3TLKFM42fxxVOFCvfunocz2cP6ifg/odXQD190AUnBRmSnAZc6vtjbbdqmV+Uq8slZS3LnO4Oom84ZMI0HhpWjyHyJtE7ugXBovpwFK6o3/PzrIUKpuPzFRdehbB6oK52xlFkEof4DICUj3RGRvNiqL0sCkwll5755QRJU4m2A6ZVWd1lcCNaKpFYkoNlFMxjYWkV9BEyYrjyhRGBuIj9jk/WT66+pmq0VRceB48Uvj5hZH9zjMz1atVf4taKB/ty8sw643iHDktFe066vF/bSSgi5w+BWY6E7b5ll6tdM1BWAwcQKx84UiMo7rp8UWd8Zxf47KqaIfy2KSzqjP/Wb5B5plYy0e2COFspUALj6ECspUK9Ktnb40dNECS5uL1Lu62e5aEamBoUuxbTqasvB0xY4TkjXe08HSk5Ja9EoJiBOL1LXnwFP2oP/qQDvEq6GaRFPbv4NX/EUD1xj+Q3LuoKp9V/ByuYD1V68861cgPaVDdsBG46TjZYkqQrgIZ949s0sDXQyU6Ne2peedNcMnGeQ2oNzkmwEmSWoo8bKzr/JGMwM= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: b302a5b9-1633-40c6-eb08-08db2fa413f3 X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Mar 2023 15:49:56.0716 (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: nEFD1XQHsBAc9tOk7W06KsaESn80CgamPqUEGzwYS16awnELUOlJj7e8oKPARroYzaXuzdMvLascNG3Aw1oGRw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CO6PR10MB5619 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-24_11,2023-03-28_02,2023-02-09_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 bulkscore=0 spamscore=0 phishscore=0 mlxscore=0 mlxlogscore=999 suspectscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2303200000 definitions=main-2303280123 X-Proofpoint-GUID: xjpJ2pMuz1Agmaa3zd5vvkMHsj1fUPEK X-Proofpoint-ORIG-GUID: xjpJ2pMuz1Agmaa3zd5vvkMHsj1fUPEK 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 '-Wflex-array-member-not-at-end' to identify all such cases in the source code and modify them. This warning will be on by default starting from GCC 14. " gcc/c-family/ChangeLog: * c.opt: New option -Wflex-array-member-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 | 9 ++++ gcc/doc/extend.texi | 45 ++++++++++++++++++- .../gcc.dg/variable-sized-type-flex-array.c | 31 +++++++++++++ 4 files changed, 89 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..c26d9801b63 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. +Wflex-array-member-not-at-end +C C++ Var(warn_flex_array_member_not_at_end) Warning +Warn when a structure containing a C99 flexible array member as the last +field is not at the end of another 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..92304fd9c8f 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -9269,6 +9269,15 @@ 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_flex_array_member_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_Wflex_array_member_not_at_end, + "structure containing a flexible array member" + " is not at the end of another structure"); + 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 3adb67aa47a..ef46423339e 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{-Wflex-array-member-not-at-end} to +identify all such cases in the source code and modify them. This warning +will be on by default staring from GCC 14. @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..3924937bad4 --- /dev/null +++ b/gcc/testsuite/gcc.dg/variable-sized-type-flex-array.c @@ -0,0 +1,31 @@ +/* Test for -Wflex-array-member-not-at-end on structure/union with + C99 flexible array members being embedded into another structure. */ +/* { dg-do compile } */ +/* { dg-options "-Wflex-array-member-not-at-end" } */ + +struct flex { int n; int data[]; }; +struct out_flex_end { int m; struct flex flex_data; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +struct out_flex_mid { struct flex flex_data; int m; }; /* { dg-warning "structure containing a flexible array member is not at the end of another structure" } */ +/* 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 "structure containing a flexible array member is not at the end of another structure" } */ +union flex_union_mid { int a; struct outer_flex_mid b; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ + + +struct flex0 { int n; int data[0]; }; +struct out_flex_end0 { int m; struct flex0 flex_data; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +struct out_flex_mid0 { struct flex0 flex_data; int m; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +struct outer_flex_mid0 { struct out_flex_mid0 out_flex_data; int p; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +union flex_union_mid0 { int a; struct outer_flex_mid0 b; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ + +struct flex1 { int n; int data[1]; }; +struct out_flex_end1 { int m; struct flex1 flex_data; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +struct out_flex_mid1 { struct flex1 flex_data; int m; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +struct outer_flex_mid1 { struct out_flex_mid1 out_flex_data; int p; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +union flex_union_mid1 { int a; struct outer_flex_mid1 b; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ + +struct flexn { int n; int data[8]; }; +struct out_flex_endn { int m; struct flexn flex_data; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +struct out_flex_midn { struct flexn flex_data; int m; }; /* { dg-bogus"structure containing a flexible array member is not at the end of another structure" } */ +struct outer_flex_midn { struct out_flex_midn out_flex_data; int p; }; /* { dg-bogus"structure containing a flexible array member is not at the end of another structure" } */ +union flex_union_midn { int a; struct outer_flex_midn b; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */