This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: Walways-true:Warn if a comparison is always true or always false due to the limited range of the data type
- From: Rask Ingemann Lambertsen <rask at sygehus dot dk>
- To: Manuel López-Ibáñez <lopezibanez at gmail dot com>
- Cc: gcc-patches <gcc-patches at gcc dot gnu dot org>
- Date: Sun, 26 Aug 2007 20:19:04 +0200
- Subject: Re: Walways-true:Warn if a comparison is always true or always false due to the limited range of the data type
- References: <6c33472e0703040528x1b231f3freb9e1cbbb8e52082@mail.gmail.com>
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