c-family: Fix up -W*conversion on bitwise &/|/^ [PR101537]
authorJakub Jelinek <jakub@redhat.com>
Tue, 11 Jan 2022 18:11:51 +0000 (19:11 +0100)
committerJakub Jelinek <jakub@redhat.com>
Tue, 11 Jan 2022 18:11:51 +0000 (19:11 +0100)
commit20e4a5e573e76f4379b353cc736215a5f10cdb84
tree5be1205aa8335ac1918920a00c4ec85587f2838e
parent0378f563b0321c44c4a9c98cf46d2a22b9160f76
c-family: Fix up -W*conversion on bitwise &/|/^ [PR101537]

The following testcases emit a bogus -Wconversion warning.  This is because
conversion_warning function doesn't handle BIT_*_EXPR (only unsafe_conversion_p
that is called during the default: case, and that one doesn't handle
SAVE_EXPRs added because the unsigned char & or | operands promoted to int
have side-effects and =| or =& is used.

The patch handles BIT_IOR_EXPR/BIT_XOR_EXPR like the last 2 operands of
COND_EXPR by recursing on the two operands, if either of them doesn't fit
into the narrower type, complain.  BIT_AND_EXPR too, but first it needs to
handle some special cases that unsafe_conversion_p does, namely when one
of the two operands is a constant.

This fixes completely the pr101537.c test and for C also pr103881.c
and doesn't regress anything in the testsuite, for C++ pr103881.c still
emits the bogus warnings.
This is because while the C FE emits in that case a SAVE_EXPR that
conversion_warning can handle already, C++ FE emits
TARGET_EXPR <D.whatever, ...>, something | D.whatever
etc. and conversion_warning handles COMPOUND_EXPR by "recursing" on the
rhs.  To handle that case, we'd need for TARGET_EXPR on the lhs remember
in some hash map the mapping from D.whatever to the TARGET_EXPR and when
we see D.whatever, use corresponding TARGET_EXPR initializer instead.

2022-01-11  Jakub Jelinek  <jakub@redhat.com>

PR c/101537
PR c/103881
gcc/c-family/
* c-warn.c (conversion_warning): Handle BIT_AND_EXPR, BIT_IOR_EXPR
and BIT_XOR_EXPR.
gcc/testsuite/
* c-c++-common/pr101537.c: New test.
* c-c++-common/pr103881.c: New test.
gcc/c-family/c-warn.c
gcc/testsuite/c-c++-common/pr101537.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/pr103881.c [new file with mode: 0644]