re PR c/2707 (gcc does not warn on truncate)

2006-11-24  Manuel Lopez-Ibanez <manu@gcc.gnu.org>

	PR c/2707
	PR c++/26167
	* c-common.c (conversion_warning): New.
	(convert_and_check): Call conversion_warning unless there is an
	overflow warning.
	* doc/invoke.texi (-Wconversion): Update description.

testsuite/

	* gcc.dg/Wconversion-integer.c: New. Supersedes	
	Wconversion-negative-constants.c 
	* gcc.dg/Wconversion-real.c: New.
	* gcc.dg/Wconversion-real-integer.c: New.
	* gcc.dg/Wconversion-negative-constants.c: Deleted.
	* g++.dg/warn/Wconversion1.C: Modified.

From-SVN: r119143
parent 2941f691
2006-11-24 Manuel Lopez-Ibanez <manu@gcc.gnu.org>
PR c/2707
PR c++/26167
* c-common.c (conversion_warning): New.
(convert_and_check): Call conversion_warning unless there is an
overflow warning.
* doc/invoke.texi (-Wconversion): Update description.
2006-11-23 Daniel Berlin <dberlin@dberlin.org> 2006-11-23 Daniel Berlin <dberlin@dberlin.org>
* tree-ssa-alias.c (tree_pointer_compare): New function. * tree-ssa-alias.c (tree_pointer_compare): New function.
......
...@@ -1127,6 +1127,113 @@ vector_types_convertible_p (tree t1, tree t2) ...@@ -1127,6 +1127,113 @@ vector_types_convertible_p (tree t1, tree t2)
== INTEGRAL_TYPE_P (TREE_TYPE (t2))); == INTEGRAL_TYPE_P (TREE_TYPE (t2)));
} }
/* Warns if the conversion of EXPR to TYPE may alter a value.
This function is called from convert_and_check. */
static void
conversion_warning (tree type, tree expr)
{
bool give_warning = false;
unsigned int formal_prec = TYPE_PRECISION (type);
if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
{
/* Warn for real constant that is not an exact integer converted
to integer type. */
if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
&& TREE_CODE (type) == INTEGER_TYPE)
{
if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (TREE_TYPE (expr))))
give_warning = true;
}
/* Warn for an integer constant that does not fit into integer
type. However, warnings for negative constants converted to
unsigned types are detected by unsigned_conversion_warning. */
else if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
&& TREE_CODE (type) == INTEGER_TYPE
&& !int_fits_type_p (expr, type)
&& !(TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (TREE_TYPE (expr))))
give_warning = true;
else if (TREE_CODE (type) == REAL_TYPE)
{
/* Warn for an integer constant that does not fit into real type. */
if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE)
{
REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
if (!exact_real_truncate (TYPE_MODE (type), &a))
give_warning = true;
}
/* Warn for a real constant that does not fit into a smaller
real type. */
else if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
&& formal_prec < TYPE_PRECISION (TREE_TYPE (expr)))
{
REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
if (!exact_real_truncate (TYPE_MODE (type), &a))
give_warning = true;
}
}
if (give_warning)
warning (OPT_Wconversion,
"conversion to %qT alters %qT constant value",
type, TREE_TYPE (expr));
}
else /* 'expr' is not a constant. */
{
/* Warn for real types converted to integer types. */
if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
&& TREE_CODE (type) == INTEGER_TYPE)
give_warning = true;
else if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
&& TREE_CODE (type) == INTEGER_TYPE)
{
/* Warn for integer types converted to smaller integer types. */
if (formal_prec < TYPE_PRECISION (TREE_TYPE (expr))
/* When they are the same width but different signedness,
then the value may change. */
|| (formal_prec == TYPE_PRECISION (TREE_TYPE (expr))
&& TYPE_UNSIGNED (TREE_TYPE (expr)) != 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))))
give_warning = true;
}
/* Warn for integer types converted to real types if and only if
all the range of values of the integer type cannot be
represented by the real type. */
else if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
&& TREE_CODE (type) == REAL_TYPE)
{
tree type_low_bound = TYPE_MIN_VALUE (TREE_TYPE (expr));
tree type_high_bound = TYPE_MAX_VALUE (TREE_TYPE (expr));
REAL_VALUE_TYPE real_low_bound = real_value_from_int_cst (0, type_low_bound);
REAL_VALUE_TYPE real_high_bound = real_value_from_int_cst (0, type_high_bound);
if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
|| !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
give_warning = true;
}
/* Warn for real types converted to smaller real types. */
else if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
&& TREE_CODE (type) == REAL_TYPE
&& formal_prec < TYPE_PRECISION (TREE_TYPE (expr)))
give_warning = true;
if (give_warning)
warning (OPT_Wconversion,
"conversion to %qT from %qT may alter its value",
type, TREE_TYPE (expr));
}
}
/* Convert EXPR to TYPE, warning about conversion problems with constants. /* Convert EXPR to TYPE, warning about conversion problems with constants.
Invoke this function on every expression that is converted implicitly, Invoke this function on every expression that is converted implicitly,
i.e. because of language rules and not because of an explicit cast. */ i.e. because of language rules and not because of an explicit cast. */
...@@ -1135,33 +1242,39 @@ tree ...@@ -1135,33 +1242,39 @@ tree
convert_and_check (tree type, tree expr) convert_and_check (tree type, tree expr)
{ {
tree t = convert (type, expr); tree t = convert (type, expr);
if (TREE_CODE (t) == INTEGER_CST) if (TREE_CODE (t) == INTEGER_CST && TREE_OVERFLOW (t))
{
TREE_OVERFLOW (t) = 0;
/* Do not diagnose overflow in a constant expression merely
because a conversion overflowed. */
TREE_CONSTANT_OVERFLOW (t) = CONSTANT_CLASS_P (expr)
&& TREE_CONSTANT_OVERFLOW (expr);
/* No warning for converting 0x80000000 to int. */
if (!(TYPE_UNSIGNED (type) < TYPE_UNSIGNED (TREE_TYPE (expr))
&& TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
&& TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
{
/* If EXPR fits in the unsigned version of TYPE,
don't warn unless pedantic. */
if ((pedantic
|| TYPE_UNSIGNED (type)
|| !constant_fits_type_p (expr,
c_common_unsigned_type (type)))
&& skip_evaluation == 0)
warning (OPT_Woverflow,
"overflow in implicit constant conversion");
}
else if (warn_conversion && !skip_evaluation)
conversion_warning (type, expr);
}
else
{ {
if (TREE_OVERFLOW (t)) if (warn_conversion && !skip_evaluation)
{ conversion_warning (type, expr);
TREE_OVERFLOW (t) = 0;
unsigned_conversion_warning (t, expr);
/* Do not diagnose overflow in a constant expression merely
because a conversion overflowed. */
TREE_CONSTANT_OVERFLOW (t) = CONSTANT_CLASS_P (expr)
&& TREE_CONSTANT_OVERFLOW (expr);
/* No warning for converting 0x80000000 to int. */
if (!(TYPE_UNSIGNED (type) < TYPE_UNSIGNED (TREE_TYPE (expr))
&& TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
&& TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
/* If EXPR fits in the unsigned version of TYPE,
don't warn unless pedantic. */
if ((pedantic
|| TYPE_UNSIGNED (type)
|| !constant_fits_type_p (expr,
c_common_unsigned_type (type)))
&& skip_evaluation == 0)
warning (OPT_Woverflow,
"overflow in implicit constant conversion");
}
else
unsigned_conversion_warning (t, expr);
} }
return t; return t;
} }
......
...@@ -3156,10 +3156,13 @@ this is why we did not make @option{-Wall} request these warnings. ...@@ -3156,10 +3156,13 @@ this is why we did not make @option{-Wall} request these warnings.
@item -Wconversion @item -Wconversion
@opindex Wconversion @opindex Wconversion
Warn if a negative integer constant expression is implicitly converted Warn for implicit conversions that may alter a value. This includes
to an unsigned type. For example, warn about the assignment conversions between real and integer, like @code{abs (x)} when
@code{unsigned x = -1} if @code{x} is unsigned. But do not warn about @code{x} is @code{double}; conversions between signed and unsigned,
explicit casts like @code{(unsigned) -1}. like @code{unsigned ui = -1}; and conversions to smaller types, like
@code{sqrtf (M_PI)}. Do not warn for explicit casts like @code{abs
((int) x)} and @code{ui = (unsigned) -1}, or if the value is not
changed by the conversion like in @code{abs (2.0)}.
@item -Wsign-compare @item -Wsign-compare
@opindex Wsign-compare @opindex Wsign-compare
......
2006-11-24 Manuel Lopez-Ibanez <manu@gcc.gnu.org>
PR c/2707
PR c++/26167
* gcc.dg/Wconversion-integer.c: New. Supersedes
Wconversion-negative-constants.c
* gcc.dg/Wconversion-real.c: New.
* gcc.dg/Wconversion-real-integer.c: New.
* gcc.dg/Wconversion-negative-constants.c: Deleted.
* g++.dg/warn/Wconversion1.C: Modified.
2006-11-23 Tobias Schlueter <tobias.schlueter@physik.uni-muenchen.de> 2006-11-23 Tobias Schlueter <tobias.schlueter@physik.uni-muenchen.de>
* gfortran.dg/overload_1.f90: New test. * gfortran.dg/overload_1.f90: New test.
...@@ -9,4 +9,4 @@ unsigned char uc1 = -129; // { dg-warning "unsigned" } ...@@ -9,4 +9,4 @@ unsigned char uc1 = -129; // { dg-warning "unsigned" }
bool b1 = -3; bool b1 = -3;
int i1 = 0x80000000; int i1 = 0x80000000; // { dg-warning "conversion" }
/* Test for diagnostics for implicit conversions between integer types
These tests come from gcc/testsuite/gcc.dg/overflow-warn-2.c */
/* { dg-do compile } */
/* { dg-options "-std=c99 -Wconversion" } */
#include <limits.h>
void fsc (signed char sc);
void fuc (unsigned char uc);
unsigned fui (unsigned int ui);
void fsi (signed int ui);
void h (int x)
{
unsigned int ui = 3;
int si = 3;
unsigned char uc = 3;
signed char sc = 3;
fuc (-1); /* { dg-warning "negative integer implicitly converted to unsigned type" } */
uc = -1; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
fuc ('\xa0'); /* { dg-warning "negative integer implicitly converted to unsigned type" } */
uc = '\xa0'; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
uc = x ? 1U : -1; /* { dg-warning "conversion" } */
/* { dg-warning "negative integer implicitly converted to unsigned type" "" { target *-*-* } 25 } */
uc = x ? SCHAR_MIN : 1U; /* { dg-warning "conversion" } */
/* { dg-warning "negative integer implicitly converted to unsigned type" "" { target *-*-* } 27 } */
uc = x ? 1 : -1; /* { dg-warning "conversion" } */
uc = x ? SCHAR_MIN : 1; /* { dg-warning "conversion" } */
fuc ('A');
uc = 'A';
uc = (unsigned char) -1;
fui (-1); /* { dg-warning "negative integer implicitly converted to unsigned type" } */
ui = -1; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
ui = x ? 1U : -1; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
ui = x ? INT_MIN : 1U; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
ui = ui ? SCHAR_MIN : 1U; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
ui = 1U * -1; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
ui = ui + INT_MIN; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
ui = x ? 1 : -1; /* { dg-warning "conversion" } */
ui = ui ? SCHAR_MIN : 1; /* { dg-warning "conversion" } */
ui = -1 * (1 * -1);
ui = (unsigned) -1;
fsc (uc); /* { dg-warning "conversion" } */
sc = uc; /* { dg-warning "conversion" } */
fuc (sc); /* { dg-warning "conversion" } */
uc = sc; /* { dg-warning "conversion" } */
fsi (ui); /* { dg-warning "conversion" } */
si = ui; /* { dg-warning "conversion" } */
fui (si); /* { dg-warning "conversion" } */
ui = si; /* { dg-warning "conversion" } */
fui (sc); /* { dg-warning "conversion" } */
ui = sc; /* { dg-warning "conversion" } */
fui ('\xa0');/* { dg-warning "negative integer implicitly converted to unsigned type" } */
ui = '\xa0'; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
fsi (si);
fui (ui);
fsi (uc);
si = uc;
fui (uc);
ui = uc;
fui ('A');
ui = 'A';
fsi ('A');
si = 'A';
fsi (UINT_MAX - 1); /* { dg-warning "conversion" } */
si = UINT_MAX - 1; /* { dg-warning "conversion" } */
fsi (UINT_MAX - 1U); /* { dg-warning "conversion" } */
si = UINT_MAX - 1U; /* { dg-warning "conversion" } */
fsi (UINT_MAX/3U);
si = UINT_MAX/3U;
fsi (UINT_MAX/3);
si = UINT_MAX/3;
fui (UINT_MAX - 1);
ui = UINT_MAX - 1;
fsi (0x80000000); /* { dg-warning "conversion" } */
si = 0x80000000; /* { dg-warning "conversion" } */
}
unsigned fui (unsigned a) { return a + -1; } /* { dg-warning "negative integer implicitly converted to unsigned type" } */
/* Test for diagnostics for negative constants converted to unsigned types.
These tests come from gcc/testsuite/gcc.dg/overflow-warn-2.c */
/* { dg-do compile } */
/* { dg-options "-std=c99 -Wconversion" } */
#include <limits.h>
void fuc (unsigned char);
void hc (int x)
{
unsigned char uc;
fuc (-1); /* { dg-warning "negative integer implicitly converted to unsigned type" } */
uc = -1; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
uc = x ? 1U : -1; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
uc = x ? SCHAR_MIN : 1U; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
uc = '\xa0'; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
fuc('A');
uc = 'A';
uc = x ? 1 : -1;
uc = x ? SCHAR_MIN : 1;
}
unsigned fui (unsigned int ui);
void hi (int x)
{
unsigned ui;
fui (-1); /* { dg-warning "negative integer implicitly converted to unsigned type" } */
ui = -1; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
ui = x ? 1U : -1; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
ui = x ? INT_MIN : 1U; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
ui = ui ? SCHAR_MIN : 1U; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
ui = 1U * -1; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
ui = ui + INT_MIN; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
ui = -1 * (1 * -1);
ui = (unsigned) -1;
ui = x ? 1 : -1;
ui = x ? INT_MIN : 1;
ui = ui ? SCHAR_MIN : 1;
}
unsigned fui(unsigned a) { return a + -1; } /* { dg-warning "negative integer implicitly converted to unsigned type" } */
/* Test for diagnostics for Wconversion between floating-point and
integers. */
/* { dg-do compile }
/* { dg-options "-std=c99 -Wconversion" } */
#include <limits.h>
void fsi (signed int x);
void fui (unsigned int x);
void ffloat (float x);
void fdouble (double x);
float vfloat;
double vdouble;
void h (void)
{
unsigned int ui = 3;
int si = 3;
unsigned char uc = 3;
signed char sc = 3;
float f = 3;
double d = 3;
fsi (3.1f); /* { dg-warning "conversion" } */
si = 3.1f; /* { dg-warning "conversion" } */
fsi (3.1); /* { dg-warning "conversion" } */
si = 3.1; /* { dg-warning "conversion" } */
fsi (d); /* { dg-warning "conversion" } */
si = d; /* { dg-warning "conversion" } */
fui (-1.0); /* { dg-warning "overflow" } */
ui = -1.0; /* { dg-warning "overflow" } */
ffloat (INT_MAX); /* { dg-warning "conversion" } */
vfloat = INT_MAX; /* { dg-warning "conversion" } */
ffloat (16777217); /* { dg-warning "conversion" } */
vfloat = 16777217; /* { dg-warning "conversion" } */
ffloat (si); /* { dg-warning "conversion" } */
vfloat = si; /* { dg-warning "conversion" } */
ffloat (ui); /* { dg-warning "conversion" } */
vfloat = ui; /* { dg-warning "conversion" } */
fsi (3);
si = 3;
fsi (3.0f);
si = 3.0f;
fsi (3.0);
si = 3.0;
fsi (16777217.0f);
si = 16777217.0f;
fsi ((int) 3.1);
si = (int) 3.1;
ffloat (3U);
vfloat = 3U;
ffloat (3);
vfloat = 3;
ffloat (INT_MIN);
vfloat = INT_MIN;
ffloat (uc);
vfloat = uc;
ffloat (sc);
vfloat = sc;
fdouble (UINT_MAX);
vdouble = UINT_MAX;
fdouble (ui);
vdouble = ui;
fdouble (si);
vdouble = si;
}
/* Test for diagnostics for Wconversion for floating-point. */
/* { dg-do compile }
/* { dg-options "-std=c99 -Wconversion" } */
float vfloat;
double vdouble;
long double vlongdouble;
void ffloat (float f);
void fdouble (double d);
void flongdouble (long double ld);
void h (void)
{
float f = 0;
double d = 0;
long double ld = 0;
ffloat (3.1); /* { dg-warning "conversion" } */
vfloat = 3.1; /* { dg-warning "conversion" } */
ffloat (3.1L); /* { dg-warning "conversion" } */
vfloat = 3.1L; /* { dg-warning "conversion" } */
fdouble (3.1L); /* { dg-warning "conversion" } */
vdouble = 3.1L; /* { dg-warning "conversion" } */
ffloat (vdouble); /* { dg-warning "conversion" } */
vfloat = vdouble; /* { dg-warning "conversion" } */
ffloat (vlongdouble); /* { dg-warning "conversion" } */
vfloat = vlongdouble; /* { dg-warning "conversion" } */
fdouble (vlongdouble); /* { dg-warning "conversion" } */
vdouble = vlongdouble; /* { dg-warning "conversion" } */
ffloat ((float) 3.1);
vfloat = (float) 3.1;
ffloat ((float) 3.1L);
vfloat = (float) 3.1L;
fdouble ((double) 3.1L);
vdouble = (double) 3.1L;
ffloat ((float) vdouble);
vfloat = (float) vdouble;
ffloat ((float) vlongdouble);
vfloat = (float) vlongdouble;
fdouble ((double) vlongdouble);
vdouble = (double) vlongdouble;
ffloat (3.0);
vfloat = 3.0;
ffloat (3.1f);
vfloat = 3.1f;
ffloat (0.25L);
vfloat = 0.25L;
fdouble (3.0);
vdouble = 3.0;
fdouble (3.1f);
vdouble = 3.1f;
fdouble (0.25L);
vdouble = 0.25L;
flongdouble (3.0);
vlongdouble = 3.0;
flongdouble (3.1f);
vlongdouble = 3.1f;
flongdouble (0.25L);
vlongdouble = 0.25L;
ffloat (f);
vfloat = f;
fdouble (f);
vdouble = f;
fdouble (d);
vdouble = d;
flongdouble (f);
vlongdouble = f;
flongdouble (d);
vlongdouble = d;
flongdouble (ld);
vlongdouble = ld;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment