From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 029E83858015; Fri, 26 Nov 2021 10:13:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 029E83858015 From: "rguenth at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/46476] Missing Warning about unreachable code after return [-Wunreachable-code-return] Date: Fri, 26 Nov 2021 10:13:24 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 4.6.0 X-Bugzilla-Keywords: diagnostic X-Bugzilla-Severity: enhancement X-Bugzilla-Who: rguenth at gcc dot gnu.org X-Bugzilla-Status: ASSIGNED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: rguenth at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 26 Nov 2021 10:13:25 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D46476 --- Comment #26 from Richard Biener --- diff --git a/gcc/gimple-low.c b/gcc/gimple-low.c index 18e66450977..dc56e14b605 100644 --- a/gcc/gimple-low.c +++ b/gcc/gimple-low.c @@ -60,7 +60,7 @@ typedef struct return_statements_t return_statements_t; /* Helper tracking the reason a previous stmt cannot fallthru. */ struct cft_reason { - enum reason { CAN_FALLTHRU =3D false, UNKNOWN =3D true, RETURN }; + enum reason { CAN_FALLTHRU =3D false, UNKNOWN =3D true, RETURN, CTRL }; cft_reason () : m_reason (CAN_FALLTHRU) {} cft_reason (bool b) : m_reason (b ? UNKNOWN : CAN_FALLTHRU) {} cft_reason (reason r) : m_reason (r) {} @@ -272,6 +304,12 @@ lower_stmt (gimple_stmt_iterator *gsi, struct lower_da= ta *data) warning_at (gimple_location (stmt), OPT_Wunreachable_code_return, "statement after return is not reachable"); + if (data->cannot_fallthru.m_reason =3D=3D cft_reason::CTRL + && gimple_code (stmt) !=3D GIMPLE_LABEL + && LOCATION_LOCUS (gimple_location (stmt)) > BUILTINS_LOCATION) + warning_at (gimple_location (stmt), OPT_Wunreachable_code, + "statement after control statement is not reachable"); + switch (gimple_code (stmt)) { case GIMPLE_BIND: @@ -282,7 +320,7 @@ lower_stmt (gimple_stmt_iterator *gsi, struct lower_data *data) case GIMPLE_COND: case GIMPLE_GOTO: case GIMPLE_SWITCH: - data->cannot_fallthru =3D true; + data->cannot_fallthru =3D cft_reason::CTRL; gsi_next (gsi); return; would then warn about things like the following (via GIMPLE_GOTO handling), also stmts after continue. void baz(); void foo (int b) { switch (b) { case 1: break; baz (); } } Looks like there's no GIMPLE stmt for throw but we have calls to __cxa_throw so we can handle noreturn & throw here covering all throwing but not fall thru stmts or we can match the exact ABI function being called. As said the main issue will be premature IL eliding.=