From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2122) id C9C913858281; Tue, 14 Mar 2023 18:46:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C9C913858281 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678819591; bh=YE1YLuZEc7S/Diqbv4TuKfmfRIIJNfvjpU1a0kl6CgI=; h=From:To:Subject:Date:From; b=cG4vh5csm+Wi5BjYzis6+LpYfW2TmOvg1SoiE3aVWoPuwUoWfb6k2COh2oa3j8yNE GqyvtFwrD9SHQH/1im1DuAWKgXJxPEtXrSeBrWGfrXxQYC5s2ALaYG786xrf+G5aoP RdBQRhNP8IgQG2+/iZTuC+IfHt+ST4Dlr8UXyDp4= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jason Merrill To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-6670] c++: -Wreturn-type with if (true) throw [PR107310] X-Act-Checkin: gcc X-Git-Author: Jason Merrill X-Git-Refname: refs/heads/trunk X-Git-Oldrev: 42630fadbe248717859d61c0244c821c32b4e52c X-Git-Newrev: 71b33f8fb8daa6a7a359f322b24365d9016438fc Message-Id: <20230314184631.C9C913858281@sourceware.org> Date: Tue, 14 Mar 2023 18:46:31 +0000 (GMT) List-Id: https://gcc.gnu.org/g:71b33f8fb8daa6a7a359f322b24365d9016438fc commit r13-6670-g71b33f8fb8daa6a7a359f322b24365d9016438fc Author: Jason Merrill Date: Tue Mar 14 12:20:51 2023 -0400 c++: -Wreturn-type with if (true) throw [PR107310] I removed this folding in GCC 12 because it was interfering with an experiment of richi's, but that never went in and the change causes regressions, so let's put it back. This reverts commit r12-5638-ga3e75c1491cd2d. PR c++/107310 gcc/cp/ChangeLog: * cp-gimplify.cc (genericize_if_stmt): Restore folding of constant conditions. gcc/testsuite/ChangeLog: * c-c++-common/Wimplicit-fallthrough-39.c: Adjust warning. * g++.dg/warn/Wreturn-6.C: New test. Diff: --- gcc/cp/cp-gimplify.cc | 6 ++++++ gcc/testsuite/c-c++-common/Wimplicit-fallthrough-39.c | 4 ++-- gcc/testsuite/g++.dg/warn/Wreturn-6.C | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc index b995b4f81a5..4fecd5616bd 100644 --- a/gcc/cp/cp-gimplify.cc +++ b/gcc/cp/cp-gimplify.cc @@ -190,6 +190,12 @@ genericize_if_stmt (tree *stmt_p) } else if (IF_STMT_CONSTEXPR_P (stmt)) stmt = integer_nonzerop (cond) ? then_ : else_; + /* ??? This optimization doesn't seem to belong here, but removing it + causes -Wreturn-type regressions (e.g. 107310). */ + else if (integer_nonzerop (cond) && !TREE_SIDE_EFFECTS (else_)) + stmt = then_; + else if (integer_zerop (cond) && !TREE_SIDE_EFFECTS (then_)) + stmt = else_; else stmt = build3 (COND_EXPR, void_type_node, cond, then_, else_); protected_set_expr_location_if_unset (stmt, locus); diff --git a/gcc/testsuite/c-c++-common/Wimplicit-fallthrough-39.c b/gcc/testsuite/c-c++-common/Wimplicit-fallthrough-39.c index da4aef3a318..d06bc0db5ec 100644 --- a/gcc/testsuite/c-c++-common/Wimplicit-fallthrough-39.c +++ b/gcc/testsuite/c-c++-common/Wimplicit-fallthrough-39.c @@ -37,8 +37,8 @@ fn2 (int n) switch (n) { case 0: - if (1) /* { dg-warning "statement may fall through" "" { target c++ } } */ - n++; /* { dg-warning "statement may fall through" "" { target c } } */ + if (1) + n++; /* { dg-warning "statement may fall through" } */ case 1: /* { dg-message "here" } */ return -1; } diff --git a/gcc/testsuite/g++.dg/warn/Wreturn-6.C b/gcc/testsuite/g++.dg/warn/Wreturn-6.C new file mode 100644 index 00000000000..85fef0e16df --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wreturn-6.C @@ -0,0 +1,16 @@ +// PR c++/107310 + +struct f +{ + ~f(); +}; + +int foo(int t) { + f g; + switch (t) { + case 1: + return 1; + } + if (true) + throw 1; +} // { dg-bogus "control reaches end" }