This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Walways-true:Warn if a comparison is always true or always false due to the limited range of the data type


On Sun, Mar 04, 2007 at 01:28:06PM +0000, Manuel López-Ibáñez wrote:
> Index: gcc/testsuite/gcc.dg/Walways-true.c
> ===================================================================
> --- gcc/testsuite/gcc.dg/Walways-true.c	(revision 0)
> +++ gcc/testsuite/gcc.dg/Walways-true.c	(revision 0)
> @@ -0,0 +1,66 @@
> +/* { dg-do compile } */
> +/* { dg-options "-Walways-true" } */
[...]
> +
> +void b (unsigned short x)
> +{
> +  if (x < 0)  return;/* { dg-warning "comparison is always false due to limited range of data type" } */
> +  if (x >= 0) return;/* { dg-warning "comparison is always true due to limited range of data type" } */
> +  if (0 > x)  return;/* { dg-warning "comparison is always false due to limited range of data type" } */
> +  if (0 <= x) return;/* { dg-warning "comparison is always true due to limited range of data type" } */
> +}

   On the 16-bit targets, we instead get

Excess errors:
/home/rask/cvssrc/ia16-gcc/gcc/testsuite/gcc.dg/Wtype-limits.c:25: warning:
comparison of unsigned expression < 0 is always false
/home/rask/cvssrc/ia16-gcc/gcc/testsuite/gcc.dg/Wtype-limits.c:26: warning:
comparison of unsigned expression >= 0 is always true
/home/rask/cvssrc/ia16-gcc/gcc/testsuite/gcc.dg/Wtype-limits.c:27: warning:
comparison of unsigned expression < 0 is always false
/home/rask/cvssrc/ia16-gcc/gcc/testsuite/gcc.dg/Wtype-limits.c:28: warning:
comparison of unsigned expression >= 0 is always true

just as in the plain int case:

> +void c (unsigned int x)
> +{
> +  if (x < 0)  return;/* { dg-warning "comparison of unsigned expression < 0 is always false" } */
> +  if (x >= 0) return;/* { dg-warning "comparison of unsigned expression >= 0 is always true" } */
> +  if (0 > x)  return;/* { dg-warning "comparison of unsigned expression < 0 is always false" } */
> +  if (0 <= x) return;/* { dg-warning "comparison of unsigned expression >= 0 is always true" } */

> Index: gcc/testsuite/gcc.dg/Walways-true-Wextra.c
> ===================================================================
> --- gcc/testsuite/gcc.dg/Walways-true-Wextra.c	(revision 0)
> +++ gcc/testsuite/gcc.dg/Walways-true-Wextra.c	(revision 0)
[...]
> +
> +void b (unsigned short x)
> +{
> +  if (x < 0)  return;/* { dg-warning "comparison is always false due to limited range of data type" } */
> +  if (x >= 0) return;/* { dg-warning "comparison is always true due to limited range of data type" } */
> +  if (0 > x)  return;/* { dg-warning "comparison is always false due to limited range of data type" } */
> +  if (0 <= x) return;/* { dg-warning "comparison is always true due to limited range of data type" } */
> +}

   Likewise. Likewise for the C++ testcases you added.

   Why do we get two different sets of warnings for the same thing in the
first place?

-- 
Rask Ingemann Lambertsen


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]