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: [PATCH] Warn when casting a pointer (constant) to an integer of different size.


On Mon, 12 Dec 2005, Carlos O'Donell wrote:

> gcc/testsuite/
> 
> 2005-12-10  Carlos O'Donell <carlos@codesourcery.com>
> 
> 	* gcc.dg/cast-1.c: Add new warning.
> 	* gcc.dg/cast-2.c: Add new warning.
> 	* gcc.dg/cast-3.c: Add new warning.
> 	* gcc.dg/format/cast-1.c: Add new warning.
> 
> gcc/
> 
> 2005-12-10  Carlos O'Donell <carlos@codesourcery.com>
> 
> 	* gcc/c-typeck.c (): Always warn when casting from a pointer to
> 	an integer of different size, even if the node was constant.

Should name the function changed in the ChangeLog entry.

>        if (TREE_CODE (type) == INTEGER_TYPE
>  	  && TREE_CODE (otype) == POINTER_TYPE
> -	  && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
> -	  && !TREE_CONSTANT (value))
> +	  && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
> +      /* Always Warn about converting constants. Constant to larger integer
> +         may cause unwanted sign extension. */

"warn" not "Warn" in the middle of a sentence.  Two spaces after ".".  
This comment doesn't make it clear why the *absence* of a check for being 
constant is worth remarking upon (it's by contrast to the check in the 
interger-to-pointer case which is needed for SIG_* constants, not by 
contrast to the former behavior of the compiler).  It might be better for 
the first sentence to say something like "Unlike conversion of integers to 
pointers, where the warning is disabled for converting constants because 
of cases such as SIG_*, warn about converting constant pointers to 
integers.".  I think an additional test along the lines of

extern int i;
char c;
void f(void) { c = (char)&i; }

(doesn't warn because &i is constant, but should warn after the patch) 
would also be useful to add as another illustration of how disabling for 
constants stopped various useful warnings.

OK with those changes.

-- 
Joseph S. Myers               http://www.srcf.ucam.org/~jsm28/gcc/
    jsm@polyomino.org.uk (personal mail)
    joseph@codesourcery.com (CodeSourcery mail)
    jsm28@gcc.gnu.org (Bugzilla assignments and CCs)


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