public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Richard Biener <rguenther@suse.de>
Cc: gcc-patches@gcc.gnu.org
Subject: [PATCH] fold-const: Handle C++ dependent COMPONENT_REFs in operand_equal_p [PR105035]
Date: Thu, 24 Mar 2022 10:19:12 +0100	[thread overview]
Message-ID: <Yjw3kGNwQ1sCO7KC@tucnak> (raw)

Hi!

As mentioned in the PR, operand_equal_p already contains some hacks so that
it can be called already on pre-instantiation C++ trees from templates,
but the recent change to compare DECL_FIELD_OFFSET in the COMPONENT_REF
case broke this.  Many such COMPONENT_REFs are already punted on earlier
because they have NULL TREE_TYPE, but in this case the code knows what
type they have but still uses an IDENTIFIER_NODE as second operand
of COMPONENT_REF (I think SCOPE_REF is something that could be used too).

The following patch looks at those DECL_FIELD_*OFFSET fields only if
both field[01] args are FIELD_DECLs and otherwise keeps it to the
earlier OP_SAME (1) check that guards this whole block.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2022-03-24  Jakub Jelinek  <jakub@redhat.com>

	PR c++/105035
	* fold-const.cc (operand_equal_p) <case COMPONENT_REF>: If either
	field0 or field1 is not a FIELD_DECL, return false.

	* g++.dg/warn/Wduplicated-cond2.C: New test.

--- gcc/fold-const.cc.jj	2022-03-18 18:32:36.175078948 +0100
+++ gcc/fold-const.cc	2022-03-23 13:40:49.893266570 +0100
@@ -3357,8 +3357,11 @@ operand_compare::operand_equal_p (const_
 		    tree field0 = TREE_OPERAND (arg0, 1);
 		    tree field1 = TREE_OPERAND (arg1, 1);
 
-		    if (!operand_equal_p (DECL_FIELD_OFFSET (field0),
-					  DECL_FIELD_OFFSET (field1), flags)
+		    /* 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))
--- gcc/testsuite/g++.dg/warn/Wduplicated-cond2.C.jj	2022-03-23 13:47:36.041577821 +0100
+++ gcc/testsuite/g++.dg/warn/Wduplicated-cond2.C	2022-03-23 13:46:54.820155190 +0100
@@ -0,0 +1,29 @@
+// PR c++/105035
+// { dg-do compile }
+// { dg-options "-Wduplicated-cond" }
+
+class A {
+  struct B { int c; int f; } e;
+  template <typename> void foo ();
+  void bar ();
+};
+
+template <typename> void
+A::foo ()
+{
+  int g;
+  if (&g == &e.c)
+    ;
+  else if (&g == &e.f)
+    ;
+}
+
+void
+A::bar ()
+{
+  int g;
+  if (&g == &e.c)	// { dg-message "previously used here" }
+    ;
+  else if (&g == &e.c)	// { dg-warning "duplicated 'if' condition" }
+    ;
+}

	Jakub


             reply	other threads:[~2022-03-24  9:19 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-24  9:19 Jakub Jelinek [this message]
2022-03-24 10:10 ` Richard Biener

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Yjw3kGNwQ1sCO7KC@tucnak \
    --to=jakub@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=rguenther@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).