OSDN Git Service

2005-04-20 Michael Pogue <michael.pogue@sun.com>
authorjsm28 <jsm28@138bc75d-0d04-0410-961f-82ee72b054a4>
Wed, 20 Apr 2005 01:10:00 +0000 (01:10 +0000)
committerjsm28 <jsm28@138bc75d-0d04-0410-961f-82ee72b054a4>
Wed, 20 Apr 2005 01:10:00 +0000 (01:10 +0000)
            Joseph S. Myers  <joseph@codesourcery.com>

* 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.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@98429 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ChangeLog
gcc/c-typeck.c
gcc/c.opt
gcc/doc/invoke.texi
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/Wint-to-pointer-cast-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wint-to-pointer-cast-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wint-to-pointer-cast-3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wpointer-to-int-cast-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wpointer-to-int-cast-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wpointer-to-int-cast-3.c [new file with mode: 0644]

index 42cea0a..6b1c040 100644 (file)
@@ -1,3 +1,10 @@
+2005-04-20  Michael Pogue  <michael.pogue@sun.com>
+            Joseph S. Myers  <joseph@codesourcery.com>
+
+       * 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  <kazu@cs.umass.edu>
 
        * tree-ssa-phiopt.c: Update a comment about the pass.
index 9c82bbe..a39d703 100644 (file)
@@ -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.  */
index 2f643b3..ee456fc 100644 (file)
--- 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
index cb6f175..cb1e776 100644 (file)
@@ -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
index bf8cccd..8b2e027 100644 (file)
@@ -1,3 +1,10 @@
+2005-04-20  Joseph S. Myers  <joseph@codesourcery.com>
+
+       * 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  <coudert@clipper.ens.fr>
 
        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 (file)
index 0000000..ddb1057
--- /dev/null
@@ -0,0 +1,12 @@
+/* Test -Wint-to-pointer-cast - on by default.  */
+/* Origin: Joseph Myers <joseph@codesourcery.com> */
+/* { 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 (file)
index 0000000..e4c980d
--- /dev/null
@@ -0,0 +1,12 @@
+/* Test -Wint-to-pointer-cast.  */
+/* Origin: Joseph Myers <joseph@codesourcery.com> */
+/* { 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 (file)
index 0000000..457a53a
--- /dev/null
@@ -0,0 +1,20 @@
+/* Test -Wno-int-to-pointer-cast.  */
+/* Origin: Joseph Myers <joseph@codesourcery.com> */
+/* { 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 (file)
index 0000000..d9362a2
--- /dev/null
@@ -0,0 +1,12 @@
+/* Test -Wpointer-to-int-cast - on by default.  */
+/* Origin: Joseph Myers <joseph@codesourcery.com> */
+/* { 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 (file)
index 0000000..a8d97ea
--- /dev/null
@@ -0,0 +1,12 @@
+/* Test -Wpointer-to-int-cast.  */
+/* Origin: Joseph Myers <joseph@codesourcery.com> */
+/* { 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 (file)
index 0000000..12bfd42
--- /dev/null
@@ -0,0 +1,21 @@
+/* Test -Wno-pointer-to-int-cast.  */
+/* Origin: Joseph Myers <joseph@codesourcery.com> */
+/* { 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" } */
+}