https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99565

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <ja...@gcc.gnu.org>:

https://gcc.gnu.org/g:660eb7e9dee46ef1c986d5a4fa5cbd182b435518

commit r11-7826-g660eb7e9dee46ef1c986d5a4fa5cbd182b435518
Author: Jakub Jelinek <ja...@redhat.com>
Date:   Thu Mar 25 11:33:35 2021 +0100

    c-family: Fix up -Wduplicated-branches for union members [PR99565]

    Honza has fairly recently changed operand_equal_p to compare
    DECL_FIELD_OFFSET for COMPONENT_REFs when comparing addresses.
    As the first testcase in this patch shows, while that is very nice
    for optimizations, for the -Wduplicated-branches warning it causes
    regressions.  Pedantically a union in both C and C++ has only one
    active member at a time, so using some other union member even if it has
the
    same type is UB, so I think the warning shouldn't warn when it sees access
    to different fields that happen to have the same offset and should consider
    them different.
    In my first attempt to fix this I've keyed the old behavior on
    OEP_LEXICOGRAPHIC, but unfortunately that has various problems, the warning
    has a quick non-lexicographic compare in build_conditional_expr* and
another
    lexicographic more expensive one later during genericization and turning
the
    first one into lexicographic would mean wasting compile time on large
    conditionals.
    So, this patch instead introduces a new OEP_ flag and makes sure to pass it
    to operand_equal_p in all -Wduplicated-branches cases.

    The cvt.c changes are because on the other testcase we were warning with
    UNKNOWN_LOCATION, so the user wouldn't really know where the questionable
    code is.

    2021-03-25  Jakub Jelinek  <ja...@redhat.com>

            PR c++/99565
            * tree-core.h (enum operand_equal_flag): Add
OEP_ADDRESS_OF_SAME_FIELD.
            * fold-const.c (operand_compare::operand_equal_p): Don't compare
            field offsets if OEP_ADDRESS_OF_SAME_FIELD.

            * c-warn.c (do_warn_duplicated_branches): Pass also
            OEP_ADDRESS_OF_SAME_FIELD to operand_equal_p.

            * c-typeck.c (build_conditional_expr): Pass
OEP_ADDRESS_OF_SAME_FIELD
            to operand_equal_p.

            * call.c (build_conditional_expr_1): Pass OEP_ADDRESS_OF_SAME_FIELD
            to operand_equal_p.
            * cvt.c (convert_to_void): Preserve location_t on COND_EXPR or
            or COMPOUND_EXPR.

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

Reply via email to