From patchwork Tue Sep 17 20:32:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1986574 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=CV9nT97H; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4X7YQ83gjtz1y1t for ; Wed, 18 Sep 2024 06:33:20 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1FE84385B82F for ; Tue, 17 Sep 2024 20:33:18 +0000 (GMT) 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 ESMTP id 901F73858414 for ; Tue, 17 Sep 2024 20:32:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 901F73858414 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 901F73858414 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726605179; cv=none; b=flYnEhwjrPX4MyOGUw390PjOhn3j4tHRALI/w9pisxE75k/bz3Z397+F4kzUL6OdG2WZTrtKsGWyKC16MB1RiG7abDguatf4yTlmPsoUS4kThftmhp5bXdUhz6hRS+XfoCosedRGa3M4Hgr+S20HP7LhYw9kGlw8tnJMTdAkzCA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726605179; c=relaxed/simple; bh=eU2ryUT6GwJis2QkwWMIcJcz5SSfd2EWwrPx6GK7djU=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=pWBCx7fqi4Lma1Gv7+2FWqoSXkcqUUtGzZDD/jnAGYFDa3vmYR8vE/Nz5IkbuuMw736nBwRTi8kOpPS4Ll5UhkibN5IOMNFmIJvAiZHlOvymdpitu6YG8NeuteoaQXiKpvRsyIeo46qo0N4QuyY7EcAYcbQqkB02mZZk3136G0Y= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726605177; 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: in-reply-to:in-reply-to:references:references; bh=Ai1GZNNISSteyv3+RQ8iK39cEYJrRF7XdHfkm9plhk8=; b=CV9nT97HezvEeFdBM+s3YalSj9V6XkUgU4pVpMlE3oJ74p3SRVKuaLgGNPORcXRUbP8eQX lDSmaU7AWnjucYTeKPgrKVBmLA25gpUU0/e4ncTQ3+vPatGpMFV7LXOo9ev2Bd8rXJOSTk C2sgD1HE61xN7cpoWh/SvBb4Oc3c0F4= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-608-gjQeqbBvNZms1ATa3RPN5g-1; Tue, 17 Sep 2024 16:32:56 -0400 X-MC-Unique: gjQeqbBvNZms1ATa3RPN5g-1 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-6c368bbf8faso3005636d6.0 for ; Tue, 17 Sep 2024 13:32:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726605175; x=1727209975; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ai1GZNNISSteyv3+RQ8iK39cEYJrRF7XdHfkm9plhk8=; b=CPT7PwAjakICWRdq8ezzUvkjZ1LpxF9TrqqjioGm8dQQ2qFDJsb9wMGQ/7YPGS3An4 2hsYszP58iD8GmTIQU8ZxGfx6gMVeOK+YCVgqr1cvJ4TMIwW5oPvw1S8e/PblS9MomaS pkU3N3bggMDxr2yZafUSSrS61wTbLiRI//69OZC25R/UKNPcAXPi87LswNOykhWdhI3V R/60UaJI9Cgx984J0RQJh1RmNScnGQ8nYZA/tJvXWG/jDibAQBoPwUqXhl+16+9FpmAL ph0kLXxJTQ8vWkPZySI3xomvNwvJ4StCj3s6vdB3jQYkz4XTG0WEZDTp60asDQg4uey7 CyjQ== X-Gm-Message-State: AOJu0Yye65CLuUAKpBGY6smuT64TqhC4OkfQ9WJTWqioHTTSHTdLYzws 3l1zLql3gKuJoujR1umS0noxXZlq+fGFPRZYfUiLw+Vr0vXeRpw7iwoRDcNmNWrxxA53hqMjXjb FLTpRAChKyRyrTwc9DhVmTgjgFX2MV0vmgVj7o/BJVz1zyOcp5Hifj7c= X-Received: by 2002:a05:6214:5985:b0:6c3:5c75:d2b1 with SMTP id 6a1803df08f44-6c573aaca6cmr362092246d6.5.1726605175562; Tue, 17 Sep 2024 13:32:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHBX65HCdCs/IvsqXiYuNkN1RVGcOWaoaDgJowgy9AQVkKWZQnlJHB0O8WpoMJxYiP+Wt695w== X-Received: by 2002:a05:6214:5985:b0:6c3:5c75:d2b1 with SMTP id 6a1803df08f44-6c573aaca6cmr362091826d6.5.1726605175104; Tue, 17 Sep 2024 13:32:55 -0700 (PDT) Received: from redhat.com ([2603:7000:9500:34a5::1db4]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6c58c7af23esm38358016d6.120.2024.09.17.13.32.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Sep 2024 13:32:54 -0700 (PDT) Date: Tue, 17 Sep 2024 16:32:53 -0400 From: Marek Polacek To: Jason Merrill Cc: GCC Patches Subject: [PATCH v3] c++: ICE with -Wtautological-compare in template [PR116534] Message-ID: References: <20240829162335.100132-1-polacek@redhat.com> <26f33a33-7fba-46d4-b296-a557e8b38a32@redhat.com> <7e769fff-0577-4259-b0b1-2f6efc29b902@redhat.com> MIME-Version: 1.0 In-Reply-To: <7e769fff-0577-4259-b0b1-2f6efc29b902@redhat.com> User-Agent: Mutt/2.2.12 (2023-09-09) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline 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_H3, RCVD_IN_MSPIKE_WL, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org On Tue, Sep 17, 2024 at 12:53:25PM -0400, Jason Merrill wrote: > On 9/16/24 7:08 PM, Marek Polacek wrote: > > On Tue, Sep 10, 2024 at 10:37:31AM -0400, Jason Merrill wrote: > > > On 8/29/24 12:23 PM, Marek Polacek wrote: > > > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/14? > > > > > > > > -- >8 -- > > > > Pre r14-4793, we'd call warn_tautological_cmp -> operand_equal_p > > > > with operands wrapped in NON_DEPENDENT_EXPR, which works, since > > > > o_e_p bails for codes it doesn't know. But now we pass operands > > > > not encapsulated in NON_DEPENDENT_EXPR, and crash, because the > > > > template tree for &a[x] has null DECL_FIELD_OFFSET. > > > > > > Why are we trying to compare DECL_FIELD_OFFSET in C++, anyway? I'd think we > > > should limit that to C and in C++ rely on FIELD_DECL identity. > > > > This goes back to r12-7797. I suppose it's trying to handle C++ because we > > can't use c_dialect_cxx () in fold-const. I know there's lang_GNU_CXX but > > I suggest we do the following. > > > > I don't think I meant this warning to be called in a template, > > > > so let's avoid the problem this easy way. > > > > > > Hmm, it seems potentially useful in a template, especially since we suppress > > > it in instantiations. > > > > Ack. > > > > How about this? > > > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > > > -- >8 -- > > Pre r14-4793, we'd call warn_tautological_cmp -> operand_equal_p > > with operands wrapped in NON_DEPENDENT_EXPR, which works, since > > o_e_p bails for codes it doesn't know. But now we pass operands > > not encapsulated in NON_DEPENDENT_EXPR, and crash, because the > > template tree for &a[x] has null DECL_FIELD_OFFSET. > > > > This patch extends r12-7797 to cover the case when DECL_FIELD_OFFSET > > is null. > > > > PR c++/116534 > > > > gcc/ChangeLog: > > > > * fold-const.cc (operand_compare::operand_equal_p): Check > > DECL_FIELD_OFFSET. > > > > gcc/testsuite/ChangeLog: > > > > * g++.dg/warn/Wtautological-compare4.C: New test. > > --- > > gcc/fold-const.cc | 16 ++++++++------ > > .../g++.dg/warn/Wtautological-compare4.C | 21 +++++++++++++++++++ > > 2 files changed, 31 insertions(+), 6 deletions(-) > > create mode 100644 gcc/testsuite/g++.dg/warn/Wtautological-compare4.C > > > > diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc > > index 70db16759d0..3da09621584 100644 > > --- a/gcc/fold-const.cc > > +++ b/gcc/fold-const.cc > > @@ -3601,12 +3601,16 @@ operand_compare::operand_equal_p (const_tree arg0, const_tree arg1, > > /* Non-FIELD_DECL operands can appear in C++ templates. */ > > if (TREE_CODE (field0) != FIELD_DECL > > - || TREE_CODE (field1) != FIELD_DECL > > - || !operand_equal_p (DECL_FIELD_OFFSET (field0), > > - DECL_FIELD_OFFSET (field1), flags) > > - || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0), > > - DECL_FIELD_BIT_OFFSET (field1), > > - flags)) > > + || TREE_CODE (field1) != FIELD_DECL) > > + return false; > > + > > + if (DECL_FIELD_OFFSET (field0) > > + && DECL_FIELD_OFFSET (field1) > > + && (!operand_equal_p (DECL_FIELD_OFFSET (field0), > > + DECL_FIELD_OFFSET (field1), flags) > > + || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0), > > + DECL_FIELD_BIT_OFFSET (field1), > > + flags))) > > If at least one has null FIELD_OFFSET, I'd think we still want to compare > them for being ==, this seems to assume that a field with null offset is > equal to any other field. Oop, you're right, that's no good. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/14? -- >8 -- Pre r14-4793, we'd call warn_tautological_cmp -> operand_equal_p with operands wrapped in NON_DEPENDENT_EXPR, which works, since o_e_p bails for codes it doesn't know. But now we pass operands not encapsulated in NON_DEPENDENT_EXPR, and crash, because the template tree for &a[x] has null DECL_FIELD_OFFSET. This patch extends r12-7797 to cover the case when DECL_FIELD_OFFSET is null. PR c++/116534 gcc/ChangeLog: * fold-const.cc (operand_compare::operand_equal_p): If either field's DECL_FIELD_OFFSET is null, compare the fields with ==. gcc/testsuite/ChangeLog: * g++.dg/warn/Wtautological-compare4.C: New test. --- gcc/fold-const.cc | 12 ++++++++--- .../g++.dg/warn/Wtautological-compare4.C | 21 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wtautological-compare4.C base-commit: dfe0d4389a3ce43179563a63046ad3e74d615a08 diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc index 70db16759d0..0578f42ac0c 100644 --- a/gcc/fold-const.cc +++ b/gcc/fold-const.cc @@ -3601,9 +3601,15 @@ operand_compare::operand_equal_p (const_tree arg0, const_tree arg1, /* Non-FIELD_DECL operands can appear in C++ templates. */ if (TREE_CODE (field0) != FIELD_DECL - || TREE_CODE (field1) != FIELD_DECL - || !operand_equal_p (DECL_FIELD_OFFSET (field0), - DECL_FIELD_OFFSET (field1), flags) + || TREE_CODE (field1) != FIELD_DECL) + return false; + + if (!DECL_FIELD_OFFSET (field0) + || !DECL_FIELD_OFFSET (field1)) + return field0 == field1; + + if (!operand_equal_p (DECL_FIELD_OFFSET (field0), + DECL_FIELD_OFFSET (field1), flags) || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0), DECL_FIELD_BIT_OFFSET (field1), flags)) diff --git a/gcc/testsuite/g++.dg/warn/Wtautological-compare4.C b/gcc/testsuite/g++.dg/warn/Wtautological-compare4.C new file mode 100644 index 00000000000..96308f49a42 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wtautological-compare4.C @@ -0,0 +1,21 @@ +// PR c++/116534 +// { dg-do compile } +// { dg-options "-Wall" } + +template +struct Test { + bool foo(unsigned x, unsigned y) { + bool test = &a[x] == &b[y]; + return test; + } + unsigned *a; + unsigned *b; +}; + +void +g () +{ + Test t; + t.foo (0u, 1u); + t.foo (0u, 0u); +}