This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Fix -Wconversion (PR c++/34198)
- From: Jakub Jelinek <jakub at redhat dot com>
- To: gcc-patches at gcc dot gnu dot org
- Cc: Manuel López-Ibáñez <manu at gcc dot gnu dot org>
- Date: Fri, 23 Nov 2007 07:01:56 -0500
- Subject: [PATCH] Fix -Wconversion (PR c++/34198)
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
Hi!
The new -Wconversion and -Wsign-conversion implementation in 4.3 warns even
when the C resp. C++ FE decides to shorten some binary operation (e.g.
bitwise ops) or even for say
unsigned char a;
...
unsigned char b = (int) a;
where the implicit narrowing conversion just undoes previous widening
conversion. The following patch fixes that by looking through any NOP_EXPRs
in EXPR seeing if the warning would be pointless.
I have tried to use get_unwidened as well, but that results in some warnings
which are desirable to be omitted. Say for:
signed char sc;
...
signed char i = (int) (unsigned short int) sc;
when using expr = get_unwidened (expr, expr) instead of get_narrower
it doesn't warn, eventhough the implicit conversion from (int) (ushort) sc
to (signed char) really can change the value. Consider sc = -1, here
(int) (unsigned short int) sc
is 0xffff, while (signed char) 0xffff is -1. get_unwidened does that,
because the outermost widening conversion is skipped over and then
there are just conversions with equal precision. With get_narrower
it does the right thing.
Bootstrapped/regtested on x86_64-linux, ok for trunk?
2007-11-23 Jakub Jelinek <jakub@redhat.com>
PR c++/34198
* c-common.c (conversion_warning): For INTEGER_TYPE to
INTEGER_TYPE conversions call get_narrower on expr to avoid
spurious warnings from binop shortening or when the implicit
conversion can't change the value.
* gcc.dg/Wconversion-5.c: New test.
* g++.dg/Wconversion3.C: New test.
--- gcc/c-common.c.jj 2007-11-20 11:31:10.000000000 +0100
+++ gcc/c-common.c 2007-11-23 09:34:50.000000000 +0100
@@ -1280,6 +1280,14 @@ conversion_warning (tree type, tree expr
else if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
&& TREE_CODE (type) == INTEGER_TYPE)
{
+ /* Don't warn about unsigned char y = 0xff, x = (int) y; */
+ int uns;
+ tree orig_expr = expr;
+ expr = get_narrower (expr, &uns);
+
+ if (expr == orig_expr)
+ uns = TYPE_UNSIGNED (TREE_TYPE (expr));
+
/* Warn for integer types converted to smaller integer types. */
if (formal_prec < TYPE_PRECISION (TREE_TYPE (expr)))
give_warning = true;
@@ -1287,14 +1295,31 @@ conversion_warning (tree type, tree expr
/* When they are the same width but different signedness,
then the value may change. */
else if ((formal_prec == TYPE_PRECISION (TREE_TYPE (expr))
- && TYPE_UNSIGNED (TREE_TYPE (expr)) != TYPE_UNSIGNED (type))
+ && uns != TYPE_UNSIGNED (type))
/* Even when converted to a bigger type, if the type is
unsigned but expr is signed, then negative values
will be changed. */
- || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (TREE_TYPE (expr))))
- warning (OPT_Wsign_conversion,
- "conversion to %qT from %qT may change the sign of the result",
- type, TREE_TYPE (expr));
+ || (TYPE_UNSIGNED (type) && !uns))
+ {
+ if (uns != TYPE_UNSIGNED (TREE_TYPE (expr)))
+ {
+ /* For signed char s1, s2 = (int) (unsigned char) s1;
+ get_narrower returns s1, but uns = 1. Find the
+ narrowest type with uns == TYPE_UNSIGNED (type). */
+ tree unsexpr = orig_expr;
+
+ while (TREE_CODE (unsexpr) == NOP_EXPR
+ && unsexpr != expr
+ && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (unsexpr,
+ 0)))
+ == uns)
+ unsexpr = TREE_OPERAND (unsexpr, 0);
+ expr = unsexpr;
+ }
+ warning (OPT_Wsign_conversion,
+ "conversion to %qT from %qT may change the sign of the result",
+ type, TREE_TYPE (expr));
+ }
}
/* Warn for integer types converted to real types if and only if
--- gcc/testsuite/gcc.dg/Wconversion-5.c.jj 2007-11-23 07:45:45.000000000 +0100
+++ gcc/testsuite/gcc.dg/Wconversion-5.c 2007-11-23 08:58:58.000000000 +0100
@@ -0,0 +1,35 @@
+/* PR c++/34198 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -Wconversion" } */
+
+signed char sc;
+unsigned char uc;
+short int ss;
+unsigned short int us;
+int si;
+unsigned int ui;
+
+void test1 (void)
+{
+ int a = uc & 0xff;
+ int b = sc & 0x7f;
+ int c = 0xff & uc;
+ int d = 0x7f & sc;
+ int e = uc & sc;
+ unsigned char f = (int) uc;
+ signed char g = (int) sc;
+ unsigned char h = (unsigned int) (short int) uc;
+ signed char i = (int) (unsigned short int) sc; /* { dg-warning "may alter its value" } */
+ unsigned char j = (unsigned int) (short int) us; /* { dg-warning "may alter its value" } */
+ signed char k = (int) (unsigned short int) ss; /* { dg-warning "may alter its value" } */
+}
+
+void test2 (void)
+{
+ signed char a = (unsigned char) sc; /* { dg-warning "may change the sign" } */
+ unsigned char b = (signed char) uc; /* { dg-warning "may change the sign" } */
+ signed char c = (int) (unsigned char) sc; /* { dg-warning "may change the sign" } */
+ unsigned char d = (int) (signed char) uc; /* { dg-warning "may change the sign" } */
+ int e = (unsigned int) si; /* { dg-warning "may change the sign" } */
+ unsigned int f = (int) ui; /* { dg-warning "may change the sign" } */
+}
--- gcc/testsuite/g++.dg/warn/Wconversion3.C.jj 2007-11-23 07:52:16.000000000 +0100
+++ gcc/testsuite/g++.dg/warn/Wconversion3.C 2007-11-23 09:00:23.000000000 +0100
@@ -0,0 +1,35 @@
+// PR c++/34198
+// { dg-do compile }
+// { dg-options "-O2 -Wconversion -Wsign-conversion" }
+
+signed char sc;
+unsigned char uc;
+short int ss;
+unsigned short int us;
+int si;
+unsigned int ui;
+
+void test1 (void)
+{
+ int a = uc & 0xff;
+ int b = sc & 0x7f;
+ int c = 0xff & uc;
+ int d = 0x7f & sc;
+ int e = uc & sc;
+ unsigned char f = (int) uc;
+ signed char g = (int) sc;
+ unsigned char h = (unsigned int) (short int) uc;
+ signed char i = (int) (unsigned short int) sc; // { dg-warning "may alter its value" }
+ unsigned char j = (unsigned int) (short int) us; // { dg-warning "may alter its value" }
+ signed char k = (int) (unsigned short int) ss; // { dg-warning "may alter its value" }
+}
+
+void test2 (void)
+{
+ signed char a = (unsigned char) sc; // { dg-warning "may change the sign" }
+ unsigned char b = (signed char) uc; // { dg-warning "may change the sign" }
+ signed char c = (int) (unsigned char) sc; // { dg-warning "may change the sign" }
+ unsigned char d = (int) (signed char) uc; // { dg-warning "may change the sign" }
+ int e = (unsigned int) si; // { dg-warning "may change the sign" }
+ unsigned int f = (int) ui; // { dg-warning "may change the sign" }
+}
Jakub