From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 34702385840B for ; Thu, 13 Jan 2022 21:09:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 34702385840B Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-455-xGNyOiRYPrKFn-WJ-SrYpg-1; Thu, 13 Jan 2022 16:09:25 -0500 X-MC-Unique: xGNyOiRYPrKFn-WJ-SrYpg-1 Received: by mail-qt1-f199.google.com with SMTP id p7-20020a05622a00c700b002b2f6944e7dso6094212qtw.10 for ; Thu, 13 Jan 2022 13:09:24 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=QtYDBMJJdTjPRoP5HzPkTrxdYDrYXigE1MrlZV8oDkU=; b=wfI5dQpbVsF3bnMvWohbi0tkw51CjI0FelfVhXx6l6oUpLHghgMUTUzTapjtv0bH9c 4mTze6ZN4cTKFVzODNy6nmgN5OkgR2Ef42wG2ElW+4oa2SjKFtAYn3d3m9ZigNY8U+uC vftrokTUUCrfaMrjhnVnPmPwHRPc5nI0AvtDNTY9losSUiYEc0KyS2icSIigUaAv9ydM 6R8uwNoKA/xpnsadNjKxgjDLFoFaQEokPrfdO0E2hWv8PTpwwlkILI4awwIdaeeOpesV Aq9bBCYc+cEupBmWhsQnRyWCnBn70U90AguhscBdihrFEdo5FkssTOwqzgL4YzoCKYhv MACg== X-Gm-Message-State: AOAM5336ys9JN3QaxPdfxaHH3aoZ4sBaLDLOG2kNK6l7iAK9sGbn74oh YJ2PTm1FxExk5Pwz/FORpFobZXSd4bJrnUEazxCzMM0bA2oe7rNblxUGzHo0cnh8grr/DOw1jwQ dIK5LY0cOC/sSn1Of/A== X-Received: by 2002:a05:6214:500a:: with SMTP id jo10mr5689227qvb.33.1642108164201; Thu, 13 Jan 2022 13:09:24 -0800 (PST) X-Google-Smtp-Source: ABdhPJypHfUl+UYvOjzQ22ORrxNB1d7cI7rydjjJD614HfptxEgEOAHKaKtE87embrFD/fQp3vapXw== X-Received: by 2002:a05:6214:500a:: with SMTP id jo10mr5689202qvb.33.1642108163768; Thu, 13 Jan 2022 13:09:23 -0800 (PST) Received: from [192.168.1.149] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id y16sm2225983qki.41.2022.01.13.13.09.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 13 Jan 2022 13:09:23 -0800 (PST) Message-ID: <0109f07c-0593-50d8-73be-e9f45354487a@redhat.com> Date: Thu, 13 Jan 2022 16:09:22 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.1 Subject: Re: [PATCH] c++: Avoid some -Wreturn-type false positives with const{expr,eval} if [PR103991] To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org References: <20220113093921.GT2646553@tucnak> From: Jason Merrill In-Reply-To: <20220113093921.GT2646553@tucnak> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, NICE_REPLY_A, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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: Thu, 13 Jan 2022 21:09:29 -0000 On 1/13/22 04:39, Jakub Jelinek wrote: > Hi! > > The changes done to genericize_if_stmt in order to improve > -Wunreachable-code* warning (which Richi didn't actually commit > for GCC 12) are I think fine for normal ifs, but for constexpr if > and consteval if we have two competing warnings. > The problem is that we replace the non-taken clause (then or else) > with void_node and keep the if (cond) { something } else {} > or if (cond) {} else { something }; in the IL. > This helps -Wunreachable-code*, if something can't fallthru but the > non-taken clause can, we don't warn about code after it because it > is still (in theory) reachable. > But if the non-taken branch can't fallthru, we can get false positive > -Wreturn-type warnings (which are enabled by default) if there is > nothing after the if and the taken branch can't fallthru either. Perhaps we should replace the non-taken clause with __builtin_unreachable() instead of void_node? And/or block_may_fallthru could handle INTEGER_CST op0? > One possibility to fix this is revert at least temporarily > to the previous behavior for constexpr and consteval if, yes, we > can get false positive -Wunreachable-code* warnings but the warning > isn't present in GCC 12. > The patch below implements that for constexpr if which throws its > clauses very early (either during parsing or during instantiation), > and for consteval if it decides based on block_may_fallthru on the > non-taken (for constant evaluation only) clause - if the non-taken > branch may fallthru, it does what you did in genericize_if_stmt > for consteval if, if it can't fallthru, it uses the older way > of pretending there wasn't an if and just replacing it with the > taken clause. There are some false positive risks with this though, > block_may_fallthru is optimistic and doesn't handle some statements > at all (like FOR_STMT, WHILE_STMT, DO_STMT - of course handling those > is quite hard). > For constexpr if (but perhaps for GCC 13?) we could try to > block_may_fallthru before we throw it away and remember it in some > flag on the IF_STMT, but am not sure how dangerous would it be to call > it on the discarded stmts. Or if it is too dangerous e.g. just > remember whether the discarded block of consteval if wasn't present > or was empty, in that case assume fallthru, and otherwise assume > it can't fallthru (-Wunreachable-code possible false positives). > > Bootstrapped/regtested on x86_64-linux and i686-linux, if needed, > I can also test the safer variant with just > if (IF_STMT_CONSTEVAL_P (stmt)) > stmt = else_; > for consteval if. > > 2022-01-13 Jakub Jelinek > > PR c++/103991 > * cp-objcp-common.c (cxx_block_may_fallthru) : For > IF_STMT_CONSTEXPR_P with constant false or true condition only > check if the taken clause may fall through. > * cp-gimplify.c (genericize_if_stmt): For consteval if, revert > to r12-5638^ behavior if then_ block can't fall through. For > constexpr if, revert to r12-5638^ behavior. > > * g++.dg/warn/Wreturn-type-13.C: New test. > > --- gcc/cp/cp-objcp-common.c.jj 2022-01-11 23:11:22.091294356 +0100 > +++ gcc/cp/cp-objcp-common.c 2022-01-12 17:57:18.232202275 +0100 > @@ -313,6 +313,13 @@ cxx_block_may_fallthru (const_tree stmt) > return false; > > case IF_STMT: > + if (IF_STMT_CONSTEXPR_P (stmt)) > + { > + if (integer_nonzerop (IF_COND (stmt))) > + return block_may_fallthru (THEN_CLAUSE (stmt)); > + if (integer_zerop (IF_COND (stmt))) > + return block_may_fallthru (ELSE_CLAUSE (stmt)); > + } > if (block_may_fallthru (THEN_CLAUSE (stmt))) > return true; > return block_may_fallthru (ELSE_CLAUSE (stmt)); > --- gcc/cp/cp-gimplify.c.jj 2022-01-11 23:11:22.090294370 +0100 > +++ gcc/cp/cp-gimplify.c 2022-01-12 21:22:17.585212804 +0100 > @@ -166,8 +166,15 @@ genericize_if_stmt (tree *stmt_p) > can contain unfolded immediate function calls, we have to discard > the then_ block regardless of whether else_ has side-effects or not. */ > if (IF_STMT_CONSTEVAL_P (stmt)) > - stmt = build3 (COND_EXPR, void_type_node, boolean_false_node, > - void_node, else_); > + { > + if (block_may_fallthru (then_)) > + stmt = build3 (COND_EXPR, void_type_node, boolean_false_node, > + void_node, else_); > + else > + stmt = else_; > + } > + else if (IF_STMT_CONSTEXPR_P (stmt)) > + stmt = integer_nonzerop (cond) ? then_ : else_; > else > stmt = build3 (COND_EXPR, void_type_node, cond, then_, else_); > protected_set_expr_location_if_unset (stmt, locus); > --- gcc/testsuite/g++.dg/warn/Wreturn-type-13.C.jj 2022-01-12 21:21:36.567794238 +0100 > +++ gcc/testsuite/g++.dg/warn/Wreturn-type-13.C 2022-01-12 21:20:48.487475787 +0100 > @@ -0,0 +1,35 @@ > +// PR c++/103991 > +// { dg-do compile { target c++17 } } > + > +struct S { ~S(); }; > +int > +foo () > +{ > + S s; > + if constexpr (true) > + return 0; > + else > + return 1; > +} // { dg-bogus "control reaches end of non-void function" } > + > +#if __cpp_if_consteval >= 202106L > +constexpr int > +bar () > +{ > + S s; > + if consteval > + { > + return 0; > + } > + else > + { > + return 1; > + } > +} // { dg-bogus "control reaches end of non-void function" } > + > +int > +baz () > +{ > + return bar (); > +} > +#endif > > Jakub >