From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id 56C693858C2C; Thu, 24 Mar 2022 11:24:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 56C693858C2C MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-7797] fold-const: Handle C++ dependent COMPONENT_REFs in operand_equal_p [PR105035] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/master X-Git-Oldrev: d937c6e44ba64694c0fc88f40f42390149d1d624 X-Git-Newrev: 8698ff67cdff4364c8adad2921ed532359a155ec Message-Id: <20220324112423.56C693858C2C@sourceware.org> Date: Thu, 24 Mar 2022 11:24:23 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 24 Mar 2022 11:24:23 -0000 https://gcc.gnu.org/g:8698ff67cdff4364c8adad2921ed532359a155ec commit r12-7797-g8698ff67cdff4364c8adad2921ed532359a155ec Author: Jakub Jelinek Date: Thu Mar 24 12:23:51 2022 +0100 fold-const: Handle C++ dependent COMPONENT_REFs in operand_equal_p [PR105035] 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. 2022-03-24 Jakub Jelinek PR c++/105035 * fold-const.cc (operand_equal_p) : If either field0 or field1 is not a FIELD_DECL, return false. * g++.dg/warn/Wduplicated-cond2.C: New test. Diff: --- gcc/fold-const.cc | 7 +++++-- gcc/testsuite/g++.dg/warn/Wduplicated-cond2.C | 29 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc index 39a5a52958d..b647e5305aa 100644 --- a/gcc/fold-const.cc +++ b/gcc/fold-const.cc @@ -3357,8 +3357,11 @@ operand_compare::operand_equal_p (const_tree arg0, const_tree arg1, 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)) diff --git a/gcc/testsuite/g++.dg/warn/Wduplicated-cond2.C b/gcc/testsuite/g++.dg/warn/Wduplicated-cond2.C new file mode 100644 index 00000000000..c977389a2e4 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wduplicated-cond2.C @@ -0,0 +1,29 @@ +// PR c++/105035 +// { dg-do compile } +// { dg-options "-Wduplicated-cond" } + +class A { + struct B { int c; int f; } e; + template void foo (); + void bar (); +}; + +template 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" } + ; +}