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.133.124]) by sourceware.org (Postfix) with ESMTPS id F143E3858D28 for ; Tue, 31 Jan 2023 16:30:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F143E3858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675182643; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=E4K9eX+hwdR72BqR+BdEwPZdFY1MtW8JQNW9ZJzaynU=; b=VGXExjWkSFuJ/HSdQuSYbTnMcQuxAAKVMZsq3nYW1gw0mlgeworGFWzLlr8PDysh3ntKZj sTtA66M6HrmxCr6KMZZ50W9PboeUOjecfrVHVL7whjYR0DkgG/DVw89CzFBhar4vSgWpb3 ZWWuE90F5dJu2Ro+VJleRWxzjXcJyEs= Received: from mail-vk1-f198.google.com (mail-vk1-f198.google.com [209.85.221.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-574--duWtLP-OWOEmABG32N9aQ-1; Tue, 31 Jan 2023 11:30:37 -0500 X-MC-Unique: -duWtLP-OWOEmABG32N9aQ-1 Received: by mail-vk1-f198.google.com with SMTP id f123-20020a1f9c81000000b003e1a7591524so5335411vke.1 for ; Tue, 31 Jan 2023 08:30:33 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=E4K9eX+hwdR72BqR+BdEwPZdFY1MtW8JQNW9ZJzaynU=; b=GkT7CONlHLSJOsaUHE2XJ2CvqzBoOrbUA1ET/cc+3irgcIcKx6BufU4Brl27VHyKTQ /BuFgw5vBUzPoAqLhzqEypjOd9kCqZSOuWcC+BRz/hEIouR4aGW6DrkW+AA2WD4s5KsR YIvdm6dX7q/35dYx6uXdWq6OF7VWFuqYjCxSMKtgcfCGMleDHiKF3Od62OsvbYxu1sMk gWwPv6dBx+5PEq7OoUjE1JYnYRxM7f5AQSvF/bw1gwomb3bMULekFKHVvRSuTsqRWPKF 2LcR0irc+p6bQxPlDIwPA8LKpHXF0x5DjdUf827xfXx/zdmOxZZnVf/WqxDewR5B1AJo zJFA== X-Gm-Message-State: AO0yUKURuGyYDaEVdZ78MxkwCr9TtCQfSoh41XdiYznvYJJsQlXp20hh gfoPfA7GRfq1GoA7TdPRXbQyugJUEto3o3Wq5JdnyHSILoGCiu+zXVGzEFHqRVAnRfBARnLLjln 181FS0st7Ljt7mmOt4w== X-Received: by 2002:a05:6102:5486:b0:3fe:36c5:c2a5 with SMTP id bk6-20020a056102548600b003fe36c5c2a5mr1714690vsb.29.1675182632130; Tue, 31 Jan 2023 08:30:32 -0800 (PST) X-Google-Smtp-Source: AK7set9Yx3tagYCnTo1jgwaN0ace96IDTDC6TAnfvV5LalJoqXXRTFfIsuMVNXyMj5+HaeLVaFJ7zA== X-Received: by 2002:a05:6102:5486:b0:3fe:36c5:c2a5 with SMTP id bk6-20020a056102548600b003fe36c5c2a5mr1714639vsb.29.1675182631734; Tue, 31 Jan 2023 08:30:31 -0800 (PST) Received: from [192.168.1.108] (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 h6-20020a375306000000b006fa4ac86bfbsm10172089qkb.55.2023.01.31.08.30.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 31 Jan 2023 08:30:30 -0800 (PST) Message-ID: <5c38ef62-2c96-e787-392a-ea9c04a02007@redhat.com> Date: Tue, 31 Jan 2023 11:30:30 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Subject: Re: [PATCH v3] c++: fix ICE with -Wduplicated-cond [PR107593] To: Marek Polacek Cc: Patrick Palka , GCC Patches References: <20230126221732.617749-1-polacek@redhat.com> <6cd96b09-828b-8820-e1f7-7f11a90e0f54@idea> <9b418bd7-1aa6-0440-26a2-1c4f5e2a617b@redhat.com> From: Jason Merrill In-Reply-To: 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=-12.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TRACKER_ID,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 List-Id: On 1/30/23 21:34, Marek Polacek wrote: > On Mon, Jan 30, 2023 at 01:12:00PM -0500, Jason Merrill wrote: >> On 1/30/23 11:00, Marek Polacek wrote: >>> On Fri, Jan 27, 2023 at 06:17:00PM -0500, 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. >> >> Or maybe define instantiation_dependent_expression_p in the C front-end to >> just return false? > > Like this? > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? OK, thanks. > -- >8 -- > 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. Also destroy the chain when > an inst-dependent expression is encountered to not warn in > Wduplicated-cond4.C. > > 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; > > PR c++/107593 > PR c++/108597 > > gcc/c-family/ChangeLog: > > * c-common.h (instantiation_dependent_expression_p): Declare. > * c-warn.cc (warn_duplicated_cond_add_or_warn): If the condition > is dependent, invalidate the chain. > > gcc/c/ChangeLog: > > * c-objc-common.cc (instantiation_dependent_expression_p): New. > > gcc/cp/ChangeLog: > > * cp-tree.h (instantiation_dependent_expression_p): Don't > declare here. > > gcc/testsuite/ChangeLog: > > * g++.dg/warn/Wduplicated-cond3.C: New test. > * g++.dg/warn/Wduplicated-cond4.C: New test. > * g++.dg/warn/Wduplicated-cond5.C: New test. > --- > gcc/c-family/c-common.h | 1 + > gcc/c-family/c-warn.cc | 2 +- > gcc/c/c-objc-common.cc | 8 ++++ > gcc/cp/cp-tree.h | 1 - > gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C | 38 +++++++++++++++++++ > gcc/testsuite/g++.dg/warn/Wduplicated-cond4.C | 17 +++++++++ > gcc/testsuite/g++.dg/warn/Wduplicated-cond5.C | 16 ++++++++ > 7 files changed, 81 insertions(+), 2 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/warn/Wduplicated-cond3.C > create mode 100644 gcc/testsuite/g++.dg/warn/Wduplicated-cond4.C > create mode 100644 gcc/testsuite/g++.dg/warn/Wduplicated-cond5.C > > diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h > index bb6271d4a83..2dadb33476c 100644 > --- a/gcc/c-family/c-common.h > +++ b/gcc/c-family/c-common.h > @@ -1101,6 +1101,7 @@ extern tree lookup_label (tree); > extern tree lookup_name (tree); > extern bool lvalue_p (const_tree); > extern int maybe_adjust_arg_pos_for_attribute (const_tree); > +extern bool instantiation_dependent_expression_p (tree); > > extern bool vector_targets_convertible_p (const_tree t1, const_tree t2); > extern bool vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note); > diff --git a/gcc/c-family/c-warn.cc b/gcc/c-family/c-warn.cc > index 5ed7bcab16a..29efce3f2c0 100644 > --- a/gcc/c-family/c-warn.cc > +++ b/gcc/c-family/c-warn.cc > @@ -2535,7 +2535,7 @@ warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec **chain) > if (*chain == NULL) > return; > > - if (TREE_SIDE_EFFECTS (cond)) > + if (TREE_SIDE_EFFECTS (cond) || instantiation_dependent_expression_p (cond)) > { > /* Uh-oh! This condition has a side-effect, thus invalidates > the whole chain. */ > diff --git a/gcc/c/c-objc-common.cc b/gcc/c/c-objc-common.cc > index d4f0b262ebb..0350733250b 100644 > --- a/gcc/c/c-objc-common.cc > +++ b/gcc/c/c-objc-common.cc > @@ -400,3 +400,11 @@ maybe_adjust_arg_pos_for_attribute (const_tree) > { > return 0; > } > + > +/* In C, no expression is dependent. */ > + > +bool > +instantiation_dependent_expression_p (tree) > +{ > + return false; > +} > diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h > index 9f188724cf2..31fd8af4f21 100644 > --- a/gcc/cp/cp-tree.h > +++ b/gcc/cp/cp-tree.h > @@ -7428,7 +7428,6 @@ extern bool any_type_dependent_arguments_p (const vec *); > extern bool any_type_dependent_elements_p (const_tree); > extern bool type_dependent_expression_p_push (tree); > extern bool value_dependent_expression_p (tree); > -extern bool instantiation_dependent_expression_p (tree); > extern bool instantiation_dependent_uneval_expression_p (tree); > extern bool any_value_dependent_elements_p (const_tree); > extern bool dependent_omp_for_p (tree, tree, tree, tree); > 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 > +void > +foo () > +{ > + if (T() && T() && int()) > + ; > + else if (T() && T() && int()) > + ; > +} > + > +template > +void bar(T a) > +{ > + if (a) > + ; > + else if (a) > + ; > +} > + > +template > +void baz(int a) > +{ > + if (a) > + ; > + else if (a) // { dg-warning "duplicated" } > + ; > +} > +void > +f () > +{ > + foo(); > + bar(1); > + baz(1); > +} > diff --git a/gcc/testsuite/g++.dg/warn/Wduplicated-cond4.C b/gcc/testsuite/g++.dg/warn/Wduplicated-cond4.C > new file mode 100644 > index 00000000000..41bb9f09b4f > --- /dev/null > +++ b/gcc/testsuite/g++.dg/warn/Wduplicated-cond4.C > @@ -0,0 +1,17 @@ > +// PR c++/107593 > +// { dg-do compile } > +// { dg-options "-Wduplicated-cond" } > + > +int n; > + > +template bool g() { n = 42; return false; } > + > +template > +void f() { > + if (n) > + ; > + else if (g()) > + ; > + else if (n) > + ; > +} > diff --git a/gcc/testsuite/g++.dg/warn/Wduplicated-cond5.C b/gcc/testsuite/g++.dg/warn/Wduplicated-cond5.C > new file mode 100644 > index 00000000000..23a0bf212b5 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/warn/Wduplicated-cond5.C > @@ -0,0 +1,16 @@ > +// PR c++/108597 > +// { dg-do compile } > +// { dg-options "-Wduplicated-cond" } > + > +template > +struct MyStruct { > + > + void check(int &x) { > + if (&x == &_a) { > + } else if (&x == &_b) { > + } > + } > + > + int _a; > + int _b; > +}; > > base-commit: 897a0502056e6cc6613f26e0b22d1c1e06b1490f