aboutsummaryrefslogtreecommitdiff
path: root/gcc/c-common.c
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2007-11-23 14:39:44 +0100
committerJakub Jelinek <jakub@gcc.gnu.org>2007-11-23 14:39:44 +0100
commitcfdaefecf6ad134e393cd67fe54f4f5cdd21c5a0 (patch)
treee85ad82f029ca77413c4971bdede1ccb117867cc /gcc/c-common.c
parentbb1f5840b580ba1672d97cf0c8eccbcb50c3d3c0 (diff)
downloadgcc-cfdaefecf6ad134e393cd67fe54f4f5cdd21c5a0.zip
gcc-cfdaefecf6ad134e393cd67fe54f4f5cdd21c5a0.tar.gz
gcc-cfdaefecf6ad134e393cd67fe54f4f5cdd21c5a0.tar.bz2
re PR c++/34198 (-Wconversion gives apparent erroneous warning with g++ 4.3-20071109)
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. From-SVN: r130377
Diffstat (limited to 'gcc/c-common.c')
-rw-r--r--gcc/c-common.c35
1 files changed, 30 insertions, 5 deletions
diff --git a/gcc/c-common.c b/gcc/c-common.c
index edc9b2c..6872e3a 100644
--- a/gcc/c-common.c
+++ b/gcc/c-common.c
@@ -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