From: Joseph Myers Date: Wed, 20 Apr 2005 01:10:00 +0000 (+0100) Subject: c.opt (Wint-to-pointer-cast, [...]): New options. X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=53a2494eea417253a25732b427963ac080923edd;p=platform%2Fupstream%2Fgcc.git c.opt (Wint-to-pointer-cast, [...]): New options. 2005-04-20 Michael Pogue Joseph S. Myers * c.opt (Wint-to-pointer-cast, Wpointer-to-int-cast): New options. * c-typeck.c (build_c_cast): Check these options. * doc/invoke.texi: Document these options. testsuite: * gcc.dg/Wint-to-pointer-cast-1.c, gcc.dg/Wint-to-pointer-cast-2.c, gcc.dg/Wint-to-pointer-cast-3.c, gcc.dg/Wpointer-to-int-cast-1.c, gcc.dg/Wpointer-to-int-cast-2.c, gcc.dg/Wpointer-to-int-cast-3.c: New tests. From-SVN: r98429 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 42cea0a..6b1c040 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2005-04-20 Michael Pogue + Joseph S. Myers + + * c.opt (Wint-to-pointer-cast, Wpointer-to-int-cast): New options. + * c-typeck.c (build_c_cast): Check these options. + * doc/invoke.texi: Document these options. + 2005-04-20 Kazu Hirata * tree-ssa-phiopt.c: Update a comment about the pass. diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c index 9c82bbe..a39d703 100644 --- a/gcc/c-typeck.c +++ b/gcc/c-typeck.c @@ -3259,7 +3259,8 @@ build_c_cast (tree type, tree expr) && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype))) warning ("cast increases required alignment of target type"); - if (TREE_CODE (type) == INTEGER_TYPE + if (warn_pointer_to_int_cast + && TREE_CODE (type) == INTEGER_TYPE && TREE_CODE (otype) == POINTER_TYPE && TYPE_PRECISION (type) != TYPE_PRECISION (otype) && !TREE_CONSTANT (value)) @@ -3271,7 +3272,8 @@ build_c_cast (tree type, tree expr) warning ("cast from function call of type %qT to non-matching " "type %qT", otype, type); - if (TREE_CODE (type) == POINTER_TYPE + if (warn_int_to_pointer_cast + && TREE_CODE (type) == POINTER_TYPE && TREE_CODE (otype) == INTEGER_TYPE && TYPE_PRECISION (type) != TYPE_PRECISION (otype) /* Don't warn about converting any constant. */ diff --git a/gcc/c.opt b/gcc/c.opt index 2f643b3..ee456fc 100644 --- a/gcc/c.opt +++ b/gcc/c.opt @@ -226,6 +226,10 @@ Wimport C ObjC C++ ObjC++ Deprecated. This switch has no effect. +Wint-to-pointer-cast +C ObjC Var(warn_int_to_pointer_cast) Init(1) +Warn when there is a cast to a pointer from an integer of a different size + Winvalid-offsetof C++ ObjC++ Var(warn_invalid_offsetof) Init(1) Warn about invalid uses of the \"offsetof\" macro @@ -314,6 +318,10 @@ Wpointer-arith C ObjC C++ ObjC++ Var(warn_pointer_arith) Warn about function pointer arithmetic +Wpointer-to-int-cast +C ObjC Var(warn_pointer_to_int_cast) Init(1) +Warn when a pointer is cast to an integer of a different size + Wprotocol ObjC ObjC++ Var(warn_protocol) Init(1) Warn if inherited methods are unimplemented diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index cb6f175..cb1e776 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -221,13 +221,15 @@ Objective-C and Objective-C++ Dialects}. -Wformat-security -Wformat-y2k @gol -Wimplicit -Wimplicit-function-declaration -Wimplicit-int @gol -Wimport -Wno-import -Winit-self -Winline @gol +-Wno-int-to-pointer-cast @gol -Wno-invalid-offsetof -Winvalid-pch @gol -Wlarger-than-@var{len} -Wlong-long @gol -Wmain -Wmissing-braces -Wmissing-field-initializers @gol -Wmissing-format-attribute -Wmissing-include-dirs @gol -Wmissing-noreturn @gol -Wno-multichar -Wnonnull -Wpacked -Wpadded @gol --Wparentheses -Wpointer-arith -Wredundant-decls @gol +-Wparentheses -Wpointer-arith -Wno-pointer-to-int-cast @gol +-Wredundant-decls @gol -Wreturn-type -Wsequence-point -Wshadow @gol -Wsign-compare -Wstrict-aliasing -Wstrict-aliasing=2 @gol -Wswitch -Wswitch-default -Wswitch-enum @gol @@ -3179,6 +3181,16 @@ warning about it. The restrictions on @samp{offsetof} may be relaxed in a future version of the C++ standard. +@item -Wno-int-to-pointer-cast @r{(C only)} +@opindex Wno-int-to-pointer-cast +Suppress warnings from casts to pointer type of an integer of a +different size. + +@item -Wno-pointer-to-int-cast @r{(C only)} +@opindex Wno-pointer-to-int-cast +Suppress warnings from casts from a pointer to an integer type of a +different size. + @item -Winvalid-pch @opindex Winvalid-pch Warn if a precompiled header (@pxref{Precompiled Headers}) is found in diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index bf8cccd..8b2e027 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,10 @@ +2005-04-20 Joseph S. Myers + + * gcc.dg/Wint-to-pointer-cast-1.c, + gcc.dg/Wint-to-pointer-cast-2.c, gcc.dg/Wint-to-pointer-cast-3.c, + gcc.dg/Wpointer-to-int-cast-1.c, gcc.dg/Wpointer-to-int-cast-2.c, + gcc.dg/Wpointer-to-int-cast-3.c: New tests. + 2005-04-18 Francois-Xavier Coudert PR fortran/16861 diff --git a/gcc/testsuite/gcc.dg/Wint-to-pointer-cast-1.c b/gcc/testsuite/gcc.dg/Wint-to-pointer-cast-1.c new file mode 100644 index 0000000..ddb1057 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wint-to-pointer-cast-1.c @@ -0,0 +1,12 @@ +/* Test -Wint-to-pointer-cast - on by default. */ +/* Origin: Joseph Myers */ +/* { dg-do compile } */ +/* { dg-options "" } */ + +char c; + +void * +f (void) +{ + return (void *) c; /* { dg-warning "warning: cast to pointer from integer of different size" } */ +} diff --git a/gcc/testsuite/gcc.dg/Wint-to-pointer-cast-2.c b/gcc/testsuite/gcc.dg/Wint-to-pointer-cast-2.c new file mode 100644 index 0000000..e4c980d --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wint-to-pointer-cast-2.c @@ -0,0 +1,12 @@ +/* Test -Wint-to-pointer-cast. */ +/* Origin: Joseph Myers */ +/* { dg-do compile } */ +/* { dg-options "-Wint-to-pointer-cast" } */ + +char c; + +void * +f (void) +{ + return (void *) c; /* { dg-warning "warning: cast to pointer from integer of different size" } */ +} diff --git a/gcc/testsuite/gcc.dg/Wint-to-pointer-cast-3.c b/gcc/testsuite/gcc.dg/Wint-to-pointer-cast-3.c new file mode 100644 index 0000000..457a53a --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wint-to-pointer-cast-3.c @@ -0,0 +1,20 @@ +/* Test -Wno-int-to-pointer-cast. */ +/* Origin: Joseph Myers */ +/* { dg-do compile } */ +/* { dg-options "-Wno-int-to-pointer-cast" } */ + +char c; + +void * +f (void) +{ + return (void *) c; +} + +void *p; + +char +g (void) +{ + return (char) p; /* { dg-warning "warning: cast from pointer to integer of different size" } */ +} diff --git a/gcc/testsuite/gcc.dg/Wpointer-to-int-cast-1.c b/gcc/testsuite/gcc.dg/Wpointer-to-int-cast-1.c new file mode 100644 index 0000000..d9362a2 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wpointer-to-int-cast-1.c @@ -0,0 +1,12 @@ +/* Test -Wpointer-to-int-cast - on by default. */ +/* Origin: Joseph Myers */ +/* { dg-do compile } */ +/* { dg-options "" } */ + +void *p; + +char +f (void) +{ + return (char) p; /* { dg-warning "warning: cast from pointer to integer of different size" } */ +} diff --git a/gcc/testsuite/gcc.dg/Wpointer-to-int-cast-2.c b/gcc/testsuite/gcc.dg/Wpointer-to-int-cast-2.c new file mode 100644 index 0000000..a8d97ea --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wpointer-to-int-cast-2.c @@ -0,0 +1,12 @@ +/* Test -Wpointer-to-int-cast. */ +/* Origin: Joseph Myers */ +/* { dg-do compile } */ +/* { dg-options "-Wpointer-to-int-cast" } */ + +void *p; + +char +f (void) +{ + return (char) p; /* { dg-warning "warning: cast from pointer to integer of different size" } */ +} diff --git a/gcc/testsuite/gcc.dg/Wpointer-to-int-cast-3.c b/gcc/testsuite/gcc.dg/Wpointer-to-int-cast-3.c new file mode 100644 index 0000000..12bfd42 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wpointer-to-int-cast-3.c @@ -0,0 +1,21 @@ +/* Test -Wno-pointer-to-int-cast. */ +/* Origin: Joseph Myers */ +/* { dg-do compile } */ +/* { dg-options "-Wno-pointer-to-int-cast" } */ + +void *p; + +char +f (void) +{ + return (char) p; +} + + +char c; + +void * +g (void) +{ + return (void *) c; /* { dg-warning "warning: cast to pointer from integer of different size" } */ +}