]> git.ipfire.org Git - thirdparty/gcc.git/commit
c-family: Fix up -Wduplicated-branches for union members [PR99565]
authorJakub Jelinek <jakub@redhat.com>
Thu, 25 Mar 2021 10:33:35 +0000 (11:33 +0100)
committerJakub Jelinek <jakub@redhat.com>
Thu, 25 Mar 2021 12:41:55 +0000 (13:41 +0100)
commit660eb7e9dee46ef1c986d5a4fa5cbd182b435518
treecd7a49ca83a5124d018bbce775aaa288b7787190
parent72982851d70dfbc547d83ed2bb45356b9ebe3ff0
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  <jakub@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.
gcc/c-family/c-warn.c
gcc/c/c-typeck.c
gcc/cp/call.c
gcc/cp/cvt.c
gcc/fold-const.c
gcc/testsuite/g++.dg/warn/Wduplicated-branches6.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wduplicated-branches7.C [new file with mode: 0644]
gcc/tree-core.h