From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by sourceware.org (Postfix) with ESMTPS id 2FC06385500C for ; Fri, 13 Aug 2021 18:41:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2FC06385500C Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 17DIYgad028508 for ; Fri, 13 Aug 2021 11:41:03 -0700 Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0b-0016f401.pphosted.com with ESMTP id 3ad8x9kx6a-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Fri, 13 Aug 2021 11:41:03 -0700 Received: from DC5-EXCH01.marvell.com (10.69.176.38) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Fri, 13 Aug 2021 11:41:01 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server id 15.0.1497.18 via Frontend Transport; Fri, 13 Aug 2021 11:41:01 -0700 Received: from linux.wrightpinski.org.com (unknown [10.69.242.197]) by maili.marvell.com (Postfix) with ESMTP id DFDF13F707F; Fri, 13 Aug 2021 11:41:00 -0700 (PDT) From: To: CC: Andrew Pinski Subject: [PATCH] Fix PR c++/66590: incorrect warning "reaches end of non-void function" for switch Date: Fri, 13 Aug 2021 11:40:56 -0700 Message-ID: <1628880056-10874-1-git-send-email-apinski@marvell.com> X-Mailer: git-send-email 1.8.3.1 MIME-Version: 1.0 Content-Type: text/plain X-Proofpoint-GUID: hrXn3zS9d4RhKNgRXC8St5H9i5HgxfSJ X-Proofpoint-ORIG-GUID: hrXn3zS9d4RhKNgRXC8St5H9i5HgxfSJ X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.391, 18.0.790 definitions=2021-08-13_07:2021-08-13, 2021-08-13 signatures=0 X-Spam-Status: No, score=-14.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, TXREP 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-List-Received-Date: Fri, 13 Aug 2021 18:41:14 -0000 From: Andrew Pinski So the problem here is there is code in the C++ front-end not to add a break statement (to the IR) if the previous block does not fall through. The problem is the code which does the check to see if the block may fallthrough does not check a CLEANUP_STMT; it assumes it is always fall through. Anyways this adds the code for the case of a CLEANUP_STMT that is only for !CLEANUP_EH_ONLY (the try/finally case). OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/cp/ChangeLog: * cp-objcp-common.c (cxx_block_may_fallthru): Handle CLEANUP_STMT for the case which will be try/finally. gcc/testsuite/ChangeLog: * g++.dg/warn/Wreturn-5.C: New test. --- gcc/cp/cp-objcp-common.c | 9 +++++++++ gcc/testsuite/g++.dg/warn/Wreturn-5.C | 15 +++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 gcc/testsuite/g++.dg/warn/Wreturn-5.C diff --git a/gcc/cp/cp-objcp-common.c b/gcc/cp/cp-objcp-common.c index beef0123b04..43888507b85 100644 --- a/gcc/cp/cp-objcp-common.c +++ b/gcc/cp/cp-objcp-common.c @@ -317,6 +317,15 @@ cxx_block_may_fallthru (const_tree stmt) return true; return block_may_fallthru (ELSE_CLAUSE (stmt)); + case CLEANUP_STMT: + /* Just handle the try/finally cases. */ + if (!CLEANUP_EH_ONLY (stmt)) + { + return (block_may_fallthru (CLEANUP_BODY (stmt)) + && block_may_fallthru (CLEANUP_EXPR (stmt))); + } + return true; + default: return c_block_may_fallthru (stmt); } diff --git a/gcc/testsuite/g++.dg/warn/Wreturn-5.C b/gcc/testsuite/g++.dg/warn/Wreturn-5.C new file mode 100644 index 00000000000..543e33e905d --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wreturn-5.C @@ -0,0 +1,15 @@ +// PR C++/66590 +// { dg-do compile } +// { dg-options "-Wall" } + +struct A{ ~A();}; + +int f(int x) +{ + A a; + switch (x) + { + case 1: { A tmp; return 1; } break; + default: return 0; + } +} // { dg-bogus "control reaches end of non-void function" } -- 2.27.0