Message ID | 20230126221732.617749-1-polacek@redhat.com |
---|---|
State | New |
Headers |
Return-Path: <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> 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 538C13858417 for <patchwork@sourceware.org>; Thu, 26 Jan 2023 22:18:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 538C13858417 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674771495; bh=dTrbmjw1wAtZIIFHidYy8YEGSIxeHviQlHN/LhAoj7I=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=sCmAgDstA5H+IjOLEgfFp5Gh8U7dN+CFBejbLpOKOW06M0Fhi2D+WdOEFAYH1Th6B jG0cgngMVp1uUbrX5v1OAOEBQ9nbSQGihJGxqUtfXdUa5X86STZfFukW5vcY1nXzen AJQ2MrSWZkrPOXfH6SpVvkBrJWuJ9D9r1KEvYC4Q= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 20D893858D20 for <gcc-patches@gcc.gnu.org>; Thu, 26 Jan 2023 22:17:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 20D893858D20 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-304--0TPTCprPM6sUkIYzMJItg-1; Thu, 26 Jan 2023 17:17:41 -0500 X-MC-Unique: -0TPTCprPM6sUkIYzMJItg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9D053185A78B for <gcc-patches@gcc.gnu.org>; Thu, 26 Jan 2023 22:17:41 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.17.130]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7E34940C1141; Thu, 26 Jan 2023 22:17:41 +0000 (UTC) To: Jason Merrill <jason@redhat.com>, GCC Patches <gcc-patches@gcc.gnu.org> Subject: [PATCH] c++: fix ICE with -Wduplicated-cond [PR107593] Date: Thu, 26 Jan 2023 17:17:32 -0500 Message-Id: <20230126221732.617749-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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 <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Marek Polacek via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Marek Polacek <polacek@redhat.com> Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> |
Series |
c++: fix ICE with -Wduplicated-cond [PR107593]
|
|
Commit Message
Marek Polacek
Jan. 26, 2023, 10:17 p.m. UTC
Here we crash because a CAST_EXPR, representing T(), doesn't have its operand, and operand_equal_p's STRIP_ANY_LOCATION_WRAPPER doesn't expect that. (o_e_p is called from warn_duplicated_cond_add_or_warn.) In the past we've adjusted o_e_p to better cope with template codes, but in this case I think we just want to avoid attempting to warn about inst-dependent expressions; I don't think I've ever envisioned -Wduplicated-cond to warn about them. The ICE started with r12-6022, two-stage name lookup for overloaded operators, which gave dependent operators a TREE_TYPE (in particular, DEPENDENT_OPERATOR_TYPE), so we no longer bail out here in o_e_p: /* Similar, if either does not have a type (like a template id), they aren't equal. */ if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1)) return false; Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? PR c++/107593 gcc/cp/ChangeLog: * parser.cc (cp_parser_selection_statement): Don't do -Wduplicated-cond when the condition is dependent. gcc/testsuite/ChangeLog: * g++.dg/warn/Wduplicated-cond3.C: New test. --- gcc/cp/parser.cc | 3 +- gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C base-commit: 94673a121cfc7f9d51c9d05e31795477f4dc8dc7
Comments
On Thu, 26 Jan 2023, Marek Polacek via Gcc-patches wrote: > Here we crash because a CAST_EXPR, representing T(), doesn't have > its operand, and operand_equal_p's STRIP_ANY_LOCATION_WRAPPER doesn't > expect that. (o_e_p is called from warn_duplicated_cond_add_or_warn.) > > In the past we've adjusted o_e_p to better cope with template codes, > but in this case I think we just want to avoid attempting to warn > about inst-dependent expressions; I don't think I've ever envisioned > -Wduplicated-cond to warn about them. > > The ICE started with r12-6022, two-stage name lookup for overloaded > operators, which gave dependent operators a TREE_TYPE (in particular, > DEPENDENT_OPERATOR_TYPE), so we no longer bail out here in o_e_p: > > /* Similar, if either does not have a type (like a template id), > they aren't equal. */ > if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1)) > return false; > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > PR c++/107593 > > gcc/cp/ChangeLog: > > * parser.cc (cp_parser_selection_statement): Don't do > -Wduplicated-cond when the condition is dependent. > > gcc/testsuite/ChangeLog: > > * g++.dg/warn/Wduplicated-cond3.C: New test. > --- > gcc/cp/parser.cc | 3 +- > gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C | 38 +++++++++++++++++++ > 2 files changed, 40 insertions(+), 1 deletion(-) > create mode 100644 gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C > > diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc > index 4cdc1cd472f..3df85d49e16 100644 > --- a/gcc/cp/parser.cc > +++ b/gcc/cp/parser.cc > @@ -13209,7 +13209,8 @@ cp_parser_selection_statement (cp_parser* parser, bool *if_p, > /* Add the condition. */ > condition = finish_if_stmt_cond (condition, statement); > > - if (warn_duplicated_cond) > + if (warn_duplicated_cond > + && !instantiation_dependent_expression_p (condition)) > warn_duplicated_cond_add_or_warn (token->location, condition, > &chain); I noticed warn_duplicated_cond_add_or_warn already has logic to handle TREE_SIDE_EFFECTS conditions by invaliding the entire chain. I wonder if we'd want to do the same for instantiation-dep conditions? > > diff --git a/gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C b/gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C > new file mode 100644 > index 00000000000..3da054e5485 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C > @@ -0,0 +1,38 @@ > +// PR c++/107593 > +// { dg-do compile } > +// { dg-options "-Wduplicated-cond" } > + > +template <typename T> > +void > +foo () > +{ > + if (T() && T() && int()) > + ; > + else if (T() && T() && int()) > + ; > +} > + > +template <typename T> > +void bar(T a) > +{ > + if (a) > + ; > + else if (a) > + ; > +} > + > +template <typename> > +void baz(int a) > +{ > + if (a) > + ; > + else if (a) // { dg-warning "duplicated" } > + ; > +} > +void > +f () > +{ > + foo<int>(); > + bar(1); > + baz<int>(1); > +} > > base-commit: 94673a121cfc7f9d51c9d05e31795477f4dc8dc7 > -- > 2.39.1 > >
On 1/27/23 17:15, Patrick Palka wrote: > On Thu, 26 Jan 2023, Marek Polacek via Gcc-patches wrote: > >> Here we crash because a CAST_EXPR, representing T(), doesn't have >> its operand, and operand_equal_p's STRIP_ANY_LOCATION_WRAPPER doesn't >> expect that. (o_e_p is called from warn_duplicated_cond_add_or_warn.) >> >> In the past we've adjusted o_e_p to better cope with template codes, >> but in this case I think we just want to avoid attempting to warn >> about inst-dependent expressions; I don't think I've ever envisioned >> -Wduplicated-cond to warn about them. >> >> The ICE started with r12-6022, two-stage name lookup for overloaded >> operators, which gave dependent operators a TREE_TYPE (in particular, >> DEPENDENT_OPERATOR_TYPE), so we no longer bail out here in o_e_p: >> >> /* Similar, if either does not have a type (like a template id), >> they aren't equal. */ >> if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1)) >> return false; >> >> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? >> >> PR c++/107593 >> >> gcc/cp/ChangeLog: >> >> * parser.cc (cp_parser_selection_statement): Don't do >> -Wduplicated-cond when the condition is dependent. >> >> gcc/testsuite/ChangeLog: >> >> * g++.dg/warn/Wduplicated-cond3.C: New test. >> --- >> gcc/cp/parser.cc | 3 +- >> gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C | 38 +++++++++++++++++++ >> 2 files changed, 40 insertions(+), 1 deletion(-) >> create mode 100644 gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C >> >> diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc >> index 4cdc1cd472f..3df85d49e16 100644 >> --- a/gcc/cp/parser.cc >> +++ b/gcc/cp/parser.cc >> @@ -13209,7 +13209,8 @@ cp_parser_selection_statement (cp_parser* parser, bool *if_p, >> /* Add the condition. */ >> condition = finish_if_stmt_cond (condition, statement); >> >> - if (warn_duplicated_cond) >> + if (warn_duplicated_cond >> + && !instantiation_dependent_expression_p (condition)) >> warn_duplicated_cond_add_or_warn (token->location, condition, >> &chain); > > I noticed warn_duplicated_cond_add_or_warn already has logic to handle > TREE_SIDE_EFFECTS conditions by invaliding the entire chain. I wonder > if we'd want to do the same for instantiation-dep conditions? Makes sense. >> >> diff --git a/gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C b/gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C >> new file mode 100644 >> index 00000000000..3da054e5485 >> --- /dev/null >> +++ b/gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C >> @@ -0,0 +1,38 @@ >> +// PR c++/107593 >> +// { dg-do compile } >> +// { dg-options "-Wduplicated-cond" } >> + >> +template <typename T> >> +void >> +foo () >> +{ >> + if (T() && T() && int()) >> + ; >> + else if (T() && T() && int()) >> + ; >> +} >> + >> +template <typename T> >> +void bar(T a) >> +{ >> + if (a) >> + ; >> + else if (a) >> + ; >> +} >> + >> +template <typename> >> +void baz(int a) >> +{ >> + if (a) >> + ; >> + else if (a) // { dg-warning "duplicated" } >> + ; >> +} >> +void >> +f () >> +{ >> + foo<int>(); >> + bar(1); >> + baz<int>(1); >> +} >> >> base-commit: 94673a121cfc7f9d51c9d05e31795477f4dc8dc7 >> -- >> 2.39.1 >> >> >
On Fri, Jan 27, 2023 at 05:15:00PM -0500, Patrick Palka wrote: > On Thu, 26 Jan 2023, Marek Polacek via Gcc-patches wrote: > > > Here we crash because a CAST_EXPR, representing T(), doesn't have > > its operand, and operand_equal_p's STRIP_ANY_LOCATION_WRAPPER doesn't > > expect that. (o_e_p is called from warn_duplicated_cond_add_or_warn.) > > > > In the past we've adjusted o_e_p to better cope with template codes, > > but in this case I think we just want to avoid attempting to warn > > about inst-dependent expressions; I don't think I've ever envisioned > > -Wduplicated-cond to warn about them. > > > > The ICE started with r12-6022, two-stage name lookup for overloaded > > operators, which gave dependent operators a TREE_TYPE (in particular, > > DEPENDENT_OPERATOR_TYPE), so we no longer bail out here in o_e_p: > > > > /* Similar, if either does not have a type (like a template id), > > they aren't equal. */ > > if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1)) > > return false; > > > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > > > PR c++/107593 > > > > gcc/cp/ChangeLog: > > > > * parser.cc (cp_parser_selection_statement): Don't do > > -Wduplicated-cond when the condition is dependent. > > > > gcc/testsuite/ChangeLog: > > > > * g++.dg/warn/Wduplicated-cond3.C: New test. > > --- > > gcc/cp/parser.cc | 3 +- > > gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C | 38 +++++++++++++++++++ > > 2 files changed, 40 insertions(+), 1 deletion(-) > > create mode 100644 gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C > > > > diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc > > index 4cdc1cd472f..3df85d49e16 100644 > > --- a/gcc/cp/parser.cc > > +++ b/gcc/cp/parser.cc > > @@ -13209,7 +13209,8 @@ cp_parser_selection_statement (cp_parser* parser, bool *if_p, > > /* Add the condition. */ > > condition = finish_if_stmt_cond (condition, statement); > > > > - if (warn_duplicated_cond) > > + if (warn_duplicated_cond > > + && !instantiation_dependent_expression_p (condition)) > > warn_duplicated_cond_add_or_warn (token->location, condition, > > &chain); > > I noticed warn_duplicated_cond_add_or_warn already has logic to handle > TREE_SIDE_EFFECTS conditions by invaliding the entire chain. I wonder > if we'd want to do the same for instantiation-dep conditions? warn_duplicated_cond_add_or_warn lives in c-family/c-warn.cc so I can't use instantiation_dependent_expression_p there. Sure, I could write a C++ wrapper but with my patch we just won't add CONDITION to the chain which I thought would work just as well. Marek
On Fri, 27 Jan 2023, Marek Polacek wrote: > On Fri, Jan 27, 2023 at 05:15:00PM -0500, Patrick Palka wrote: > > On Thu, 26 Jan 2023, Marek Polacek via Gcc-patches wrote: > > > > > Here we crash because a CAST_EXPR, representing T(), doesn't have > > > its operand, and operand_equal_p's STRIP_ANY_LOCATION_WRAPPER doesn't > > > expect that. (o_e_p is called from warn_duplicated_cond_add_or_warn.) > > > > > > In the past we've adjusted o_e_p to better cope with template codes, > > > but in this case I think we just want to avoid attempting to warn > > > about inst-dependent expressions; I don't think I've ever envisioned > > > -Wduplicated-cond to warn about them. > > > > > > The ICE started with r12-6022, two-stage name lookup for overloaded > > > operators, which gave dependent operators a TREE_TYPE (in particular, > > > DEPENDENT_OPERATOR_TYPE), so we no longer bail out here in o_e_p: > > > > > > /* Similar, if either does not have a type (like a template id), > > > they aren't equal. */ > > > if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1)) > > > return false; > > > > > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > > > > > PR c++/107593 > > > > > > gcc/cp/ChangeLog: > > > > > > * parser.cc (cp_parser_selection_statement): Don't do > > > -Wduplicated-cond when the condition is dependent. > > > > > > gcc/testsuite/ChangeLog: > > > > > > * g++.dg/warn/Wduplicated-cond3.C: New test. > > > --- > > > gcc/cp/parser.cc | 3 +- > > > gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C | 38 +++++++++++++++++++ > > > 2 files changed, 40 insertions(+), 1 deletion(-) > > > create mode 100644 gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C > > > > > > diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc > > > index 4cdc1cd472f..3df85d49e16 100644 > > > --- a/gcc/cp/parser.cc > > > +++ b/gcc/cp/parser.cc > > > @@ -13209,7 +13209,8 @@ cp_parser_selection_statement (cp_parser* parser, bool *if_p, > > > /* Add the condition. */ > > > condition = finish_if_stmt_cond (condition, statement); > > > > > > - if (warn_duplicated_cond) > > > + if (warn_duplicated_cond > > > + && !instantiation_dependent_expression_p (condition)) > > > warn_duplicated_cond_add_or_warn (token->location, condition, > > > &chain); > > > > I noticed warn_duplicated_cond_add_or_warn already has logic to handle > > TREE_SIDE_EFFECTS conditions by invaliding the entire chain. I wonder > > if we'd want to do the same for instantiation-dep conditions? > > warn_duplicated_cond_add_or_warn lives in c-family/c-warn.cc so I can't > use instantiation_dependent_expression_p there. Sure, I could write a > C++ wrapper but with my patch we just won't add CONDITION to the chain > which I thought would work just as well. Ah that's unfortunate :( ISTM desirable to conservatively assume an inst-dep cond has side effects though (possibly directly from cp_parser_selection_statement), to avoid false positives as in: int n; template<class T> bool g() { n = 42; } template<class T> void f() { if (n) ; else if (g<T>()) ; else if (n) ; }
On Fri, 27 Jan 2023, Patrick Palka wrote: > On Fri, 27 Jan 2023, Marek Polacek wrote: > > > On Fri, Jan 27, 2023 at 05:15:00PM -0500, Patrick Palka wrote: > > > On Thu, 26 Jan 2023, Marek Polacek via Gcc-patches wrote: > > > > > > > Here we crash because a CAST_EXPR, representing T(), doesn't have > > > > its operand, and operand_equal_p's STRIP_ANY_LOCATION_WRAPPER doesn't > > > > expect that. (o_e_p is called from warn_duplicated_cond_add_or_warn.) > > > > > > > > In the past we've adjusted o_e_p to better cope with template codes, > > > > but in this case I think we just want to avoid attempting to warn > > > > about inst-dependent expressions; I don't think I've ever envisioned > > > > -Wduplicated-cond to warn about them. > > > > > > > > The ICE started with r12-6022, two-stage name lookup for overloaded > > > > operators, which gave dependent operators a TREE_TYPE (in particular, > > > > DEPENDENT_OPERATOR_TYPE), so we no longer bail out here in o_e_p: > > > > > > > > /* Similar, if either does not have a type (like a template id), > > > > they aren't equal. */ > > > > if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1)) > > > > return false; > > > > > > > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > > > > > > > PR c++/107593 > > > > > > > > gcc/cp/ChangeLog: > > > > > > > > * parser.cc (cp_parser_selection_statement): Don't do > > > > -Wduplicated-cond when the condition is dependent. > > > > > > > > gcc/testsuite/ChangeLog: > > > > > > > > * g++.dg/warn/Wduplicated-cond3.C: New test. > > > > --- > > > > gcc/cp/parser.cc | 3 +- > > > > gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C | 38 +++++++++++++++++++ > > > > 2 files changed, 40 insertions(+), 1 deletion(-) > > > > create mode 100644 gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C > > > > > > > > diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc > > > > index 4cdc1cd472f..3df85d49e16 100644 > > > > --- a/gcc/cp/parser.cc > > > > +++ b/gcc/cp/parser.cc > > > > @@ -13209,7 +13209,8 @@ cp_parser_selection_statement (cp_parser* parser, bool *if_p, > > > > /* Add the condition. */ > > > > condition = finish_if_stmt_cond (condition, statement); > > > > > > > > - if (warn_duplicated_cond) > > > > + if (warn_duplicated_cond > > > > + && !instantiation_dependent_expression_p (condition)) > > > > warn_duplicated_cond_add_or_warn (token->location, condition, > > > > &chain); > > > > > > I noticed warn_duplicated_cond_add_or_warn already has logic to handle > > > TREE_SIDE_EFFECTS conditions by invaliding the entire chain. I wonder > > > if we'd want to do the same for instantiation-dep conditions? > > > > warn_duplicated_cond_add_or_warn lives in c-family/c-warn.cc so I can't > > use instantiation_dependent_expression_p there. Sure, I could write a > > C++ wrapper but with my patch we just won't add CONDITION to the chain > > which I thought would work just as well. > > Ah that's unfortunate :( ISTM desirable to conservatively assume an > inst-dep cond has side effects though (possibly directly from oops, "has side effects and clear the chain" rather > cp_parser_selection_statement), to avoid false positives as in: > > int n; > > template<class T> bool g() { n = 42; } > > template<class T> > void f() { > if (n) > ; > else if (g<T>()) > ; > else if (n) > ; > } >
diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 4cdc1cd472f..3df85d49e16 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -13209,7 +13209,8 @@ cp_parser_selection_statement (cp_parser* parser, bool *if_p, /* Add the condition. */ condition = finish_if_stmt_cond (condition, statement); - if (warn_duplicated_cond) + if (warn_duplicated_cond + && !instantiation_dependent_expression_p (condition)) warn_duplicated_cond_add_or_warn (token->location, condition, &chain); diff --git a/gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C b/gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C new file mode 100644 index 00000000000..3da054e5485 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C @@ -0,0 +1,38 @@ +// PR c++/107593 +// { dg-do compile } +// { dg-options "-Wduplicated-cond" } + +template <typename T> +void +foo () +{ + if (T() && T() && int()) + ; + else if (T() && T() && int()) + ; +} + +template <typename T> +void bar(T a) +{ + if (a) + ; + else if (a) + ; +} + +template <typename> +void baz(int a) +{ + if (a) + ; + else if (a) // { dg-warning "duplicated" } + ; +} +void +f () +{ + foo<int>(); + bar(1); + baz<int>(1); +}