This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/20573] New: unable to use cast to suppress "warning: comparison is always false due to limited range of data type"
- From: "gcc-bugzilla at gcc dot gnu dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 21 Mar 2005 00:16:48 -0000
- Subject: [Bug c/20573] New: unable to use cast to suppress "warning: comparison is always false due to limited range of data type"
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
There doesn't seem to be an easy way to suppress this warning in a macro like
#define MILL(in) ((in) > 1000000)
where the macro may be passed different sizes of ints including shorts.
I would have expected the warning to be suppressed when a cast is present
like:
#define MILL(in) ((int)(in) > 1000000)
but it isn't.
Environment:
System: CYGWIN_NT-5.1 DHX98431 1.5.14s(0.124/4/2) 20050319 16:46:09 i686 unknown unknown Cygwin
host: i686-pc-cygwin
build: i686-pc-cygwin
target: i686-pc-cygwin
configured with: /gcc/3.4/gcc-3.4.1-1/configure --verbose --prefix=/usr --exec-prefix=/usr --sysconfdir=/etc --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --enable-languages=c,ada,c++,f77,java,objc --enable-nls --without-included-gettext --enable-libgcj --with-system-zlib --enable-interpreter --enable-threads=posix --enable-java-gc=boehm --enable-sjlj-exceptions --disable-version-specific-runtime-libs --disable-win32-registry
How-To-Repeat:
Given warnme.c:
#define MILL(in) ((in) > 1000000)
int foo(short s) { return MILL(s); }
int bar(short s) { return MILL((int)s); }
int baz(short s) { int i=s; return MILL(i); }
with preprocessor output warnme.i:
# 1 "warnme.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "warnme.c"
int foo(short s) { return ((s) > 1000000); }
int bar(short s) { return (((int)s) > 1000000); }
int baz(short s) { int i=s; return ((i) > 1000000); }
$ gcc -Wall -save-temps -c warnme.c
warnme.c: In function `foo':
warnme.c:2: warning: comparison is always false due to limited range of data type
warnme.c: In function `bar':
warnme.c:3: warning: comparison is always false due to limited range of data type
The warning in foo is expected; the warning in bar is not. baz is an ugly
workaround.
------- Additional Comments From sthoenna at efn dot org 2005-03-21 00:16 -------
Fix:
An ugly workaround is to assign to a temporary variable of large enough data
type where the macro is called, and pass the temporary variable instead.
--
Summary: unable to use cast to suppress "warning: comparison is
always false due to limited range of data type"
Product: gcc
Version: 3.4.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: sthoenna at efn dot org
CC: gcc-bugs at gcc dot gnu dot org
GCC build triplet: i686-pc-cygwin
GCC host triplet: i686-pc-cygwin
GCC target triplet: i686-pc-cygwin
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20573