From patchwork Sat Feb 19 16:24:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 51240 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 DB8463858006 for ; Sat, 19 Feb 2022 16:25:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DB8463858006 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1645287957; bh=AmBf0IwuT80mVKcMsvw7kd4dWs9n1WS2GdpLDt75cvw=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=lcW67T5y3u7uNrlT5a69JUFia6l7QfAuQ8K7Mn3obbdSCU9ddyHr2gXgTzSZ0pb57 VNKJvbka7yOJIVLeHMDnvhmKkQq0zJuSqquix2d+QDneb86gxUHxEAScKBERIiPlQR nOlooThdY45/HUKIlE7dRnFnfuDwRM4nH1Vp9Tbg= 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 534DE3864C64 for ; Sat, 19 Feb 2022 16:24:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 534DE3864C64 Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 21JCZbPl007199; Sat, 19 Feb 2022 16:24:13 GMT Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by mx0b-00069f02.pphosted.com with ESMTP id 3eas3v0q38-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 19 Feb 2022 16:24:13 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 21JGBWwZ072878; Sat, 19 Feb 2022 16:24:11 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2176.outbound.protection.outlook.com [104.47.58.176]) by userp3030.oracle.com with ESMTP id 3eannrupep-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 19 Feb 2022 16:24:11 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=myKucMjugiHpzU+OSJT8/TnPyYjJKRyx+KDHX29PLEBP5KWBRoUDk4Z7YLtoFAvqdZIY07+gxLeVNYT6tGxts24bvfnw/pat0kb6SYe7I/nX3jCK/yXjeNoiniq+tHqL2ASFQ8FGPtd4015iF66GWJZknLWOONIYmeqIfrjjletNmTFs2VQkgIQG54ivJ4Iq92fjj3n2AxWafzVchl3Xv94EYg5Ryyp91x+/PXwDfZza5B7eUJPPH61f6y5QX4IRQOstmhJhlkoCp+gKsICLCwrektsb3hJ92bUx56TDqdRORGk3UZxdlJTZEt26yEL8I0UOYrC5F0yRQNvZG/rcTg== 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=AmBf0IwuT80mVKcMsvw7kd4dWs9n1WS2GdpLDt75cvw=; b=SoxNe0iFTYG+VTmfPVECb04EVLEyPI28D6m0hHlyxu4/hjNhe8uhI2xF29Xm+RSqCftgNsepjoEEub0uwe+4FrsgdWv3jpl9D9gVVcxJ3j6C6iVCeNxlHqSaR6K6okySfM4KVd+9YJHyiwHMjjgtn14b03Ruk9eEAnHtNp7K38K1FS7YTbOziEBn6socKwhRl33f3Co0k1/5df1fon0UMHCGdjs/KoOgR+rybQ6IHyz+iSTJBU5bpnjcWghkkWggg5e9O07ynpYZ53PbG1DAe+KjMKQEVJrLnQF4floS8jMRxRLNRVQ2pJgiFvmRwj+J7b/l5TfXxTwxZUJA0d4ydw== 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 DM6PR10MB3611.namprd10.prod.outlook.com (2603:10b6:5:179::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4995.16; Sat, 19 Feb 2022 16:24:09 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::50e0:10b6:4c07:3728]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::50e0:10b6:4c07:3728%8]) with mapi id 15.20.4995.025; Sat, 19 Feb 2022 16:24:09 +0000 To: richard Biener , Jakub Jelinek Subject: [PATCH 2/2][middle-end/102276] Adding -Wtrivial-auto-var-init and update documentation. Thread-Topic: [PATCH 2/2][middle-end/102276] Adding -Wtrivial-auto-var-init and update documentation. Thread-Index: AQHYJa0f74Jbmopny0K50ybEQG+TZg== Date: Sat, 19 Feb 2022 16:24:09 +0000 Message-ID: <8884571B-837F-40B0-8308-F6F2D26587C9@oracle.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-mailer: Apple Mail (2.3608.120.23.2.7) x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 80fda1c1-e9cc-4b7a-6185-08d9f3c441da x-ms-traffictypediagnostic: DM6PR10MB3611:EE_ x-microsoft-antispam-prvs: x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: 3vz/CnwUaPSTWBg9L4n4C0kWT1/xODJoWvo4uB/c3KqOk8tGOBLxbkd2IELvsEaLukR3d94+79wL0lKtcgPpykSChcF6C3R3ojUZy8/KaGl84Ljo8q2dlXd45yJGibZJJWFItHPIk54gQbuIMmSdqGq0Q7FomSNfedtOsNAGaR2QrbO0fZjoAE5ilAz9DhJqK/MvOjZFzClKHykZiHH7pBd8eJVuy+TYnQeKcZcXhUjEtKCscB2iF8PadGrDd/yU/OdqatJhTKaD2vtl5riOw2R/C3Raj3NivHBwm9JU0l+PyvqS164B90z1bx1Lo2CbjUATRkyhjc73AmkZ1j/SbNY7qE6VrjtONDHv/KCTIs3ta2YEUttCdoiiwOl9VLJy4gKV4H9ODQTNOr0R+lXsq8Xi4aC+n21iX88zrz6f5ezPDAU6b/va6INzWwcLhHEGE7SAk+hS6bPW/ewLpw8XsUGh3j39cXd/wmMnWMFwwDJyrbUzWp+vH/KKuq0NQnZ8dj1x6DbUvXNMQ8Kbn50cyNHr2iRNnWr56Wgd14w/LR4tRjI/5fhMoy10K+1ysm53peBlp765B9lzmGwivyb6kymP4WwV104FJjkxBu1qNdb/l5OQagWlgosn0taDpJpqNG8hbQ6QcUHfHTOYFBJSUC3N0AES6TclSuU3X9zq8THqlWzRTG2c13ujJ+zjFeFhyRnvN8MEZJiGadoV+63FT9AfSkMZqTrrbJOD20NGI1JuQ+FoPNB6pQcq5Zq4vnZ4wC08fkuf84VFvdl+ZKeepFgEWe2oqX/73UiMDzNTd/QN+Sbeoa4U9Ka2QKap+qnLCiISgl0BIeyCOhS36EsnmYaG9Ohiw1CHnjDifec0hbHWIa6IaNDmCIPH/3RmHt7j 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:(13230001)(366004)(84970400001)(33656002)(86362001)(38100700002)(5660300002)(122000001)(38070700005)(316002)(54906003)(110136005)(6486002)(508600001)(76116006)(66946007)(66556008)(2906002)(91956017)(66476007)(66446008)(4326008)(64756008)(8676002)(83380400001)(15650500001)(53546011)(6512007)(6506007)(2616005)(71200400001)(186003)(8936002)(30864003)(36756003)(44832011)(45980500001); DIR:OUT; SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: /p2ObDd38UrQhvM9ZAXlC//8sDMv1C8Vo0Mr1jhxzar7v2H+D5/xnBTqZwtp59oCDwMRcgc+V+fmkvzvlbzNsEFUySufeNYZ1jYOGt9q65HcfyWIpmGFilS5IwzHwdj80yakXA6wXsi8CaB7+t2y/sfxqXfltFY5lyWQ/W00hFo7qWM+KUx6QvJcqEIllqUeQL0YHgCTZbMxKKoJtIzD2FYxwsPnPq9qZhDfI+rOxowJpnyBZphOKpEh5jpmCR0WBounsZ0eT5gx6MvDAT/vxl/X2+qrWv2AzNeFTrUJRqmCHhmFbQKqzxDfz22MbKwJpN4sYQcZnydaf4My5s7oaBJjwVtPEPd2k/XXX9Buat6tLPj97WS4gXmUooJWTfnJl5pkn45z0Qjs+MjLxd7vBrLbYF74y4o/UquNnKNHEEl/IPvM/tb/A8QwKSYw71V2TyTPTwzK9PeGJ8iUpMojOdQhNzj95Yz16+AYt0y1QjpAFzsSma9CFjHmrxhAghS04PG9pXQNXbw+iVof7iIK80e0zBpyV13hLDzOXQc0yVUfpz0t8wtQ2763z9YhthX66+KCAhcE5rpLWclAM1EY8bcT61PaEwGWe96ZX83v0E64t13sSv6Pzm9IbOnxfsRfIMykrwRd0y/Hmm0SFEyCWm3fgXU7zPgAdRKfolXs4m20srOPVVI1+/tjiK9HF0Fgk4cQLD/+yiW655tG0SKHu6Xi4IXHToKcpT/bYhaOvX5zcp/4nfvkuGI2OgX+1jLo0ulPahDyjysTL79RkYdmISzknYjHIul+netGbn/bJIUPMF4x896CySf9KnorkMKHcMXLCRKkn+ZKEMXHZ/1vEtvHaRxSnHJF4IAQVYojwqRPw+80Jr7lY2pS15PXFP2Z7qMak4eIlRz+K/qt/thR3fb1oSrxjOosiYeZQo+8JXTwEtSA0JLTV/rohQymGThjbB2cRIeyEXPO+5ldmzsiy/S14M6RhbbxFxG16Xm8xosFuyqSPSiIcau0KR6avGZoQoun6j++eyrYoBxke3Real7TP8x5g81lmobe1EtQgidjdRp8ymvRcM4axypGEHjKJsZ4yh7oLFqT30aFX1wRXnd2HEuGSvZ06FcYcXc/HBqn15JyaNB09mtmOlVmyh5L+nQgcn5Ci/YrTiq/FsDNozKr8hKXa3Fb2J9HJwDrqX1lo7rxKPpjwiggkZNRe16KljZNoBapPDTVDiJkWlSjxN2BU3DJ64ZioSeQZR6gf2bLcm/13itQ9DMv3xgS41+jDNDQU/SL1fE1bIQ90SS0a++iBwkAwJNLgRgsmoR76eDyU22/rguD7SOP8ENjcmvTcHjB4sHqh1DOxixe/TnulBldA5tpN7MQ/alggt90Tcx9T7GoP4DAT0oJyrsDZlsfPyjsMqqxA2AtFfYCtXi8emnbVDRHdQW0Zwl2CI+K+W6xsPqmYErcJ21z4wMlBn7Jt8Olr2nO7fmKGRnCXTHHKHq/Q7AvW9AHLcBAB2mSYOuVohNeGMf+C6ut9PcDp760g8QBdlpLL4r2bPJo3HHB1LmdMJQLH7voHij3wqE4WLNORL8YEsB94kdUDa/GhS2YKxRNeYYmNB4dtQl59dLslBdIWmcL85UX8FeZPDhmeSGn0MFV7W8XOhG5BTIpYzBINv09sDQ5Z2sJp+ozAJL+YA== Content-ID: <8A5D88745993E945B72B15E55EFA2106@namprd10.prod.outlook.com> MIME-Version: 1.0 X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 80fda1c1-e9cc-4b7a-6185-08d9f3c441da X-MS-Exchange-CrossTenant-originalarrivaltime: 19 Feb 2022 16:24:09.2668 (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: aE98wwHW9UXICv4IZ/Do+Zw5Ez6DIsMhZCSUVU1b7AFfhvl4swwhNbpuuUK9yqqlsziz+sIfU59JrRD2bL5RBw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB3611 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10263 signatures=677614 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 suspectscore=0 adultscore=0 malwarescore=0 phishscore=0 mlxlogscore=999 mlxscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2202190106 X-Proofpoint-GUID: wq_Hayk707E1oKkd6StGDymj1yZXTN8z X-Proofpoint-ORIG-GUID: wq_Hayk707E1oKkd6StGDymj1yZXTN8z X-Spam-Status: No, score=-11.3 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_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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 Cc: gcc-patches Paul A Clarke via , kees Cook Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Hi, This is the 2nd patch for fixing pr102276. Adding -Wtrivial-auto-var-init and update documentation. Adding a new warning option -Wtrivial-auto-var-init to report cases when -ftrivial-auto-var-init cannot initialize the auto variable. At the same time, update documentation for -ftrivial-auto-var-init to connect it with the new warning option -Wtrivial-auto-var-init, and add documentation for -Wtrivial-auto-var-init. Bootstraped and regression tested on both x86 and aarch64. Okay for committing? thanks. Qing. ============================== From 4346890b8f4258489c4841f1992ba3ce816d7689 Mon Sep 17 00:00:00 2001 From: Qing Zhao Date: Fri, 18 Feb 2022 15:53:15 +0000 Subject: [PATCH 2/2] Adding -Wtrivial-auto-var-init and update documentation. Adding a new warning option -Wtrivial-auto-var-init to report cases when -ftrivial-auto-var-init cannot initialize the auto variable. At the same time, update documentation for -ftrivial-auto-var-init to connect it with the new warning option -Wtrivial-auto-var-init, and add documentation for -Wtrivial-auto-var-init. 2022-02-18 Qing Zhao gcc/ChangeLog: * common.opt (-Wtrivial-auto-var-init): New option. * doc/invoke.texi (-Wtrivial-auto-var-init): Document new option. (-ftrivial-auto-var-init): Update option; * gimplify.cc (maybe_warn_switch_unreachable): Rename... (maybe_warn_switch_unreachable_and_auto_init): ...to this. (gimplify_switch_expr): Call new function. gcc/testsuite/ChangeLog: * gcc.dg/auto-init-pr102276-3.c: New test. * gcc.dg/auto-init-pr102276-4.c: New test. --- gcc/common.opt | 4 + gcc/doc/invoke.texi | 14 ++- gcc/gimplify.cc | 100 +++++++++++++++----- gcc/testsuite/gcc.dg/auto-init-pr102276-3.c | 40 ++++++++ gcc/testsuite/gcc.dg/auto-init-pr102276-4.c | 40 ++++++++ 5 files changed, 175 insertions(+), 23 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/auto-init-pr102276-3.c create mode 100644 gcc/testsuite/gcc.dg/auto-init-pr102276-4.c diff --git a/gcc/common.opt b/gcc/common.opt index c21e5273ae3..22c95dbfa49 100644 --- a/gcc/common.opt +++ b/gcc/common.opt @@ -801,6 +801,10 @@ Wtrampolines Common Var(warn_trampolines) Warning Warn whenever a trampoline is generated. +Wtrivial-auto-var-init +Common Var(warn_trivial_auto_var_init) Warning Init(0) +Warn about where -ftrivial-auto-var-init cannot initialize the auto variable. + Wtype-limits Common Var(warn_type_limits) Warning EnabledBy(Wextra) Warn if a comparison is always true or always false due to the limited range of the data type. diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index e1a00c80307..c61a5b4b4a5 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -399,7 +399,7 @@ Objective-C and Objective-C++ Dialects}. -Wswitch -Wno-switch-bool -Wswitch-default -Wswitch-enum @gol -Wno-switch-outside-range -Wno-switch-unreachable -Wsync-nand @gol -Wsystem-headers -Wtautological-compare -Wtrampolines -Wtrigraphs @gol --Wtsan -Wtype-limits -Wundef @gol +-Wtrivial-auto-var-init -Wtsan -Wtype-limits -Wundef @gol -Wuninitialized -Wunknown-pragmas @gol -Wunsuffixed-float-constants -Wunused @gol -Wunused-but-set-parameter -Wunused-but-set-variable @gol @@ -6953,6 +6953,14 @@ This warning is enabled by default for C and C++ programs. Warn when @code{__sync_fetch_and_nand} and @code{__sync_nand_and_fetch} built-in functions are used. These functions changed semantics in GCC 4.4. +@item -Wtrivial-auto-var-init +@opindex Wtrivial-auto-var-init +@opindex Wno-trivial-auto-var-init +Warn when @code{-ftrivial-auto-var-init} cannot initialize the automatic +variable. A common situation is an automatic variable that is declared +between the controlling expression and the first case lable of a @code{switch} +statement. + @item -Wunused-but-set-parameter @opindex Wunused-but-set-parameter @opindex Wno-unused-but-set-parameter @@ -12314,6 +12322,10 @@ initializer as uninitialized, @option{-Wuninitialized} and warning messages on such automatic variables. With this option, GCC will also initialize any padding of automatic variables that have structure or union types to zeroes. +However, the current implementation cannot initialize automatic variables that +are declared between the controlling expression and the first case of a +@code{switch} statement. Using @option{-Wtrivial-auto-var-init} to report all +such cases. The three values of @var{choice} are: diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc index 4e3bbf5314d..7e52794691f 100644 --- a/gcc/gimplify.cc +++ b/gcc/gimplify.cc @@ -2079,13 +2079,59 @@ warn_switch_unreachable_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p, return NULL_TREE; } +/* Callback for walk_gimple_seq. */ + +static tree +warn_switch_unreachable_auto_init_r (gimple_stmt_iterator *gsi_p, + bool *handled_ops_p, + struct walk_stmt_info *) +{ + gimple *stmt = gsi_stmt (*gsi_p); + + *handled_ops_p = true; + switch (gimple_code (stmt)) + { + case GIMPLE_TRY: + case GIMPLE_BIND: + case GIMPLE_CATCH: + case GIMPLE_EH_FILTER: + case GIMPLE_TRANSACTION: + /* Walk the sub-statements. */ + *handled_ops_p = false; + break; + case GIMPLE_CALL: + if (gimple_call_internal_p (stmt, IFN_DEFERRED_INIT) + && flag_auto_var_init > AUTO_INIT_UNINITIALIZED) + { + /* Get the variable name from the 3rd argument of call. */ + tree var_name = gimple_call_arg (stmt, 2); + var_name = TREE_OPERAND (TREE_OPERAND (var_name, 0), 0); + const char *var_name_str = TREE_STRING_POINTER (var_name); + + warning_at (gimple_location (stmt), OPT_Wtrivial_auto_var_init, + "%qs cannot be initialized with" + "%<-ftrivial-auto-var_init%>", + var_name_str); + } + break; + case GIMPLE_LABEL: + /* Stop till the first Label. */ + return integer_zero_node; + default: + break; + } + return NULL_TREE; +} + /* Possibly warn about unreachable statements between switch's controlling - expression and the first case. SEQ is the body of a switch expression. */ + expression and the first case. Also warn about -ftrivial-auto-var-init + cannot initialize the auto variable under such situation. + SEQ is the body of a switch expression. */ static void -maybe_warn_switch_unreachable (gimple_seq seq) +maybe_warn_switch_unreachable_and_auto_init (gimple_seq seq) { - if (!warn_switch_unreachable + if ((!warn_switch_unreachable && !warn_trivial_auto_var_init) /* This warning doesn't play well with Fortran when optimizations are on. */ || lang_GNU_Fortran () @@ -2093,26 +2139,36 @@ maybe_warn_switch_unreachable (gimple_seq seq) return; struct walk_stmt_info wi; - memset (&wi, 0, sizeof (wi)); - walk_gimple_seq (seq, warn_switch_unreachable_r, NULL, &wi); - gimple *stmt = (gimple *) wi.info; - if (stmt && gimple_code (stmt) != GIMPLE_LABEL) + if (warn_switch_unreachable) { - if (gimple_code (stmt) == GIMPLE_GOTO - && TREE_CODE (gimple_goto_dest (stmt)) == LABEL_DECL - && DECL_ARTIFICIAL (gimple_goto_dest (stmt))) - /* Don't warn for compiler-generated gotos. These occur - in Duff's devices, for example. */ - ; - else if ((flag_auto_var_init > AUTO_INIT_UNINITIALIZED) - && (gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))) - /* Don't warn for compiler-generated initializations for - -ftrivial-auto-var-init. */ - ; - else - warning_at (gimple_location (stmt), OPT_Wswitch_unreachable, - "statement will never be executed"); + memset (&wi, 0, sizeof (wi)); + walk_gimple_seq (seq, warn_switch_unreachable_r, NULL, &wi); + gimple *stmt = (gimple *) wi.info; + + if (stmt && gimple_code (stmt) != GIMPLE_LABEL) + { + if (gimple_code (stmt) == GIMPLE_GOTO + && TREE_CODE (gimple_goto_dest (stmt)) == LABEL_DECL + && DECL_ARTIFICIAL (gimple_goto_dest (stmt))) + /* Don't warn for compiler-generated gotos. These occur + in Duff's devices, for example. */ + ; + else if ((flag_auto_var_init > AUTO_INIT_UNINITIALIZED) + && (gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))) + /* Don't warn for compiler-generated initializations for + -ftrivial-auto-var-init. */ + ; + else + warning_at (gimple_location (stmt), OPT_Wswitch_unreachable, + "statement will never be executed"); + } + } + + if (warn_trivial_auto_var_init) + { + memset (&wi, 0, sizeof (wi)); + walk_gimple_seq (seq, warn_switch_unreachable_auto_init_r, NULL, &wi); } } @@ -2646,7 +2702,7 @@ gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p) gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq); gimplify_ctxp->in_switch_expr = old_in_switch_expr; - maybe_warn_switch_unreachable (switch_body_seq); + maybe_warn_switch_unreachable_and_auto_init (switch_body_seq); maybe_warn_implicit_fallthrough (switch_body_seq); /* Only do this for the outermost GIMPLE_SWITCH. */ if (!gimplify_ctxp->in_switch_expr) diff --git a/gcc/testsuite/gcc.dg/auto-init-pr102276-3.c b/gcc/testsuite/gcc.dg/auto-init-pr102276-3.c new file mode 100644 index 00000000000..f113f46e29d --- /dev/null +++ b/gcc/testsuite/gcc.dg/auto-init-pr102276-3.c @@ -0,0 +1,40 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -Wtrivial-auto-var-init -ftrivial-auto-var-init=zero" } */ + +int g(int *, int *); +int f() +{ + switch (0) { + int x; /* { dg-warning "cannot be initialized with" } */ + int y; /* { dg-warning "cannot be initialized with" } */ + default: + return g(&x, &y); + } +} + +int g1(int, int); +int f1() +{ + switch (0) { + int x; /* { dg-warning "cannot be initialized with" } */ + int y; /* { dg-warning "cannot be initialized with" } */ + default: + return g1(x, y); + } +} + +struct S +{ + char a; + int b; +}; +int g2(int); +int f2(int input) +{ + switch (0) { + struct S x; /* { dg-warning "cannot be initialized with" } */ + struct S y; /* { dg-warning "cannot be initialized with" } */ + default: + return g2(input) + x.b + y.b; + } +} diff --git a/gcc/testsuite/gcc.dg/auto-init-pr102276-4.c b/gcc/testsuite/gcc.dg/auto-init-pr102276-4.c new file mode 100644 index 00000000000..662e0d1182e --- /dev/null +++ b/gcc/testsuite/gcc.dg/auto-init-pr102276-4.c @@ -0,0 +1,40 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -Wtrivial-auto-var-init -ftrivial-auto-var-init=pattern" } */ + +int g(int *, int *); +int f() +{ + switch (0) { + int x; /* { dg-warning "cannot be initialized with" } */ + int y; /* { dg-warning "cannot be initialized with" } */ + default: + return g(&x, &y); + } +} + +int g1(int, int); +int f1() +{ + switch (0) { + int x; /* { dg-warning "cannot be initialized with" } */ + int y; /* { dg-warning "cannot be initialized with" } */ + default: + return g1(x, y); + } +} + +struct S +{ + char a; + int b; +}; +int g2(int); +int f2(int input) +{ + switch (0) { + struct S x; /* { dg-warning "cannot be initialized with" } */ + struct S y; /* { dg-warning "cannot be initialized with" } */ + default: + return g2(input) + x.b + y.b; + } +}