[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Suggested strengthening of -Wconversion

>Submitter-Id:	net
>Originator:	Matteo Frigo
>Confidential:	no
>Synopsis:	gcc -Wconversion should warn on implicit 64-bit => 32-bit integral conversions
>Severity:	non-critical
>Priority:	low
>Category:	c
>Class:		change-request 
>Release:	4.0.3 20051201 (prerelease) (Debian 4.0.2-5) (Debian testing/unstable)
System: Linux glauke 2.6.15 #1 Tue Jan 3 20:46:04 CST 2006 ppc GNU/Linux
Architecture: ppc

host: powerpc-unknown-linux-gnu
build: powerpc-unknown-linux-gnu
target: powerpc-unknown-linux-gnu
configured with: ../src/configure -v --enable-languages=c,c++,java,f95,objc,ada,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.0 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-java-awt=gtk-default --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-4.0- --enable-mpfr --disable-softfloat --enable-targets=powerpc-linux,powerpc64-linux --with-cpu=default32 --disable-werror --enable-checking=release powerpc-linux-gnu

This report suggests a feature that would aid making 32-bit
applications 64-bit clean.

Specifically, consider the following program fragment:

int foo(char *p, char *q)
     return p - q;

The return type should be ptrdiff_t, but legacy 32-bit code did not
distinguish int from ptrdiff_t.  The implicit conversion ptrdiff_t ->
int is a no-op on ILP32, but loses significant bits on LP64, and most
likely does not correspond to the programmer's intent.

Currently, gcc -m64 -O -Wall -W -Wconversion does not issue any
warning in such situations.  I maintain that a warning would be
useful, and that gcc should support some flag to enable such a
warning. (I emphasize that, with gcc-4.0.3, -Wconversion is not

I attach below a patch that issues a warning upon problematic implicit
conversions.  (The second warning is probably too obnoxious and should
only be enabled with -pedantic or similar flags.)

I found this patch useful in making fftw 64-bit clean, and I believe
that other programmers would benefit from such a feature.  I noted
that Linus uses the ``sparse'' tool to find problematic narrowing
conversions in the linux kernel, and that even VC++ offers such a
warning.  gcc ought to offer this feature as well.

Thanks for your time.

Matteo Frigo



--- /tmp/c-common.c	2005-09-01 11:46:46.000000000 -0500
+++ gcc/c-common.c	2006-01-09 22:36:45.000000000 -0600
@@ -987,6 +987,19 @@
 convert_and_check (tree type, tree expr)
   tree t = convert (type, expr);
+  if (warn_conversion) {
+       if (TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (expr))) {
+	    if (TYPE_PRECISION (type) == 32 &&
+		TYPE_PRECISION (TREE_TYPE (expr)) == 64 &&
+		 warning ("implicit conversion from integral 64-bit to 32-bit type");
+	    } else {
+		 warning ("implicit conversion to narrower type");
+	    }
+       }
+  }
   if (TREE_CODE (t) == INTEGER_CST)
       if (TREE_OVERFLOW (t))

Reply to: