public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r10-10666] c-family: Fix up -W*conversion on bitwise &/|/^ [PR101537]
@ 2022-05-10 8:22 Jakub Jelinek
0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2022-05-10 8:22 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:4e8c32ecfb87e723229dc6c08df8ea602e947f1e
commit r10-10666-g4e8c32ecfb87e723229dc6c08df8ea602e947f1e
Author: Jakub Jelinek <jakub@redhat.com>
Date: Tue Jan 11 19:11:51 2022 +0100
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.
(cherry picked from commit 20e4a5e573e76f4379b353cc736215a5f10cdb84)
Diff:
---
gcc/c-family/c-warn.c | 28 ++++++++++++++++++++++++++++
gcc/testsuite/c-c++-common/pr101537.c | 26 ++++++++++++++++++++++++++
gcc/testsuite/c-c++-common/pr103881.c | 20 ++++++++++++++++++++
3 files changed, 74 insertions(+)
diff --git a/gcc/c-family/c-warn.c b/gcc/c-family/c-warn.c
index 92b3b55bd07..a4baf37cf1c 100644
--- a/gcc/c-family/c-warn.c
+++ b/gcc/c-family/c-warn.c
@@ -1302,6 +1302,34 @@ conversion_warning (location_t loc, tree type, tree expr, tree result)
|| conversion_warning (loc, type, op2, result));
}
+ case BIT_AND_EXPR:
+ if (TREE_CODE (expr_type) == INTEGER_TYPE
+ && TREE_CODE (type) == INTEGER_TYPE)
+ for (int i = 0; i < 2; ++i)
+ {
+ tree op = TREE_OPERAND (expr, i);
+ if (TREE_CODE (op) != INTEGER_CST)
+ continue;
+
+ /* If one of the operands is a non-negative constant
+ that fits in the target type, then the type of the
+ other operand does not matter. */
+ if (int_fits_type_p (op, c_common_signed_type (type))
+ && int_fits_type_p (op, c_common_unsigned_type (type)))
+ return false;
+
+ /* If constant is unsigned and fits in the target
+ type, then the result will also fit. */
+ if (TYPE_UNSIGNED (TREE_TYPE (op)) && int_fits_type_p (op, type))
+ return false;
+ }
+ /* FALLTHRU */
+ case BIT_IOR_EXPR:
+ case BIT_XOR_EXPR:
+ return (conversion_warning (loc, type, TREE_OPERAND (expr, 0), result)
+ || conversion_warning (loc, type, TREE_OPERAND (expr, 1),
+ result));
+
default_:
default:
conversion_kind = unsafe_conversion_p (type, expr, result, true);
diff --git a/gcc/testsuite/c-c++-common/pr101537.c b/gcc/testsuite/c-c++-common/pr101537.c
new file mode 100644
index 00000000000..c0c3cfc45cb
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/pr101537.c
@@ -0,0 +1,26 @@
+/* PR c/101537 */
+/* { dg-do compile } */
+/* { dg-options "-Wconversion" } */
+
+int
+foo ()
+{
+ int aaa = 1;
+ unsigned char bbb = 0;
+ bbb |= aaa ? 1 : 0;
+ return bbb;
+}
+
+int
+bar (unsigned char x, int f)
+{
+ x |= f ? 1 : 0;
+ return x;
+}
+
+int
+baz (unsigned char x, int f)
+{
+ x = x | f ? 1 : 0;
+ return x;
+}
diff --git a/gcc/testsuite/c-c++-common/pr103881.c b/gcc/testsuite/c-c++-common/pr103881.c
new file mode 100644
index 00000000000..bb3f53bcf7b
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/pr103881.c
@@ -0,0 +1,20 @@
+/* PR c/103881 */
+/* { dg-do compile } */
+/* { dg-options "-Wconversion" } */
+
+unsigned char bar (void);
+
+void
+foo (void)
+{
+ unsigned char t = 0;
+ t |= bar ();
+ t |= bar () & bar (); /* { dg-bogus "conversion from 'int' to 'unsigned char' may change value" "" { xfail c++ } } */
+ t &= bar () & bar (); /* { dg-bogus "conversion from 'int' to 'unsigned char' may change value" "" { xfail c++ } } */
+ t = bar () & bar ();
+
+ unsigned char a = bar ();
+ t |= a & a;
+ t |= bar () & a; /* { dg-bogus "conversion from 'int' to 'unsigned char' may change value" "" { xfail c++ } } */
+ t |= a & bar (); /* { dg-bogus "conversion from 'int' to 'unsigned char' may change value" "" { xfail c++ } } */
+}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2022-05-10 8:22 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-10 8:22 [gcc r10-10666] c-family: Fix up -W*conversion on bitwise &/|/^ [PR101537] Jakub Jelinek
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).