aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
Diffstat (limited to 'gcc')
-rw-r--r--gcc/c-family/c-warn.c28
-rw-r--r--gcc/testsuite/c-c++-common/pr101537.c26
-rw-r--r--gcc/testsuite/c-c++-common/pr103881.c20
3 files changed, 74 insertions, 0 deletions
diff --git a/gcc/c-family/c-warn.c b/gcc/c-family/c-warn.c
index 005c9ac..1ce2202 100644
--- a/gcc/c-family/c-warn.c
+++ b/gcc/c-family/c-warn.c
@@ -1304,6 +1304,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 0000000..c0c3cfc
--- /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 0000000..bb3f53b
--- /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++ } } */
+}