PR c++/28584 Cast to pointer from integer of different size

Manuel López-Ibáñez lopezibanez@gmail.com
Mon Feb 22 19:39:00 GMT 2010


Bootstrapped and regression tested x86_64-unknown-linux-gnu.

OK for 4.5 or 4.6?

2010-02-22  Manuel López-Ibáñez  <manu@gcc.gnu.org>

	PR c++/28584
	* c.opt (Wint-to-pointer-cast): Available in C++.
	* doc/invoke.texi (Wint-to-pointer-cast): Available in C++.
cp/
	* typeck.c (cp_build_c_cast): Warn for casting integer to larger
	pointer type.
testsuite/
	* gcc.dg/Wint-to-pointer-cast-1.c: Move to...
	* c-c++-common/Wint-to-pointer-cast-1.c: ...  here.
	* gcc.dg/Wint-to-pointer-cast-2.c: Move to...	
	* c-c++-common/Wint-to-pointer-cast-2.c: ...  here.
	* gcc.dg/Wint-to-pointer-cast-3.c: Move to...	
	* c-c++-common/Wint-to-pointer-cast-3.c: ...  here. Update.
	* g++.old-deja/g++.mike/warn1.C: Add -Wno-int-to-pointer-cast.
	* g++.dg/other/increment1.C: Likewise.
-------------- next part --------------
Index: gcc/doc/invoke.texi
===================================================================
--- gcc/doc/invoke.texi	(revision 156927)
+++ gcc/doc/invoke.texi	(working copy)
@@ -4219,15 +4219,17 @@ writing nonportable code and who have de
 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 and Objective-C only)}
+@item -Wno-int-to-pointer-cast
 @opindex Wno-int-to-pointer-cast
 @opindex Wint-to-pointer-cast
 Suppress warnings from casts to pointer type of an integer of a
-different size.
+different size. In C++, casting to a pointer type of smaller size is
+an error. @option{Wint-to-pointer-cast} is enabled by default.
+
 
 @item -Wno-pointer-to-int-cast @r{(C and Objective-C only)}
 @opindex Wno-pointer-to-int-cast
 @opindex Wpointer-to-int-cast
 Suppress warnings from casts from a pointer to an integer type of a
Index: gcc/testsuite/gcc.dg/Wint-to-pointer-cast-1.c
===================================================================
--- gcc/testsuite/gcc.dg/Wint-to-pointer-cast-1.c	(revision 156927)
+++ gcc/testsuite/gcc.dg/Wint-to-pointer-cast-1.c	(working copy)
@@ -1,12 +0,0 @@
-/* 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 "cast to pointer from integer of different size" } */
-}
Index: gcc/testsuite/gcc.dg/Wint-to-pointer-cast-2.c
===================================================================
--- gcc/testsuite/gcc.dg/Wint-to-pointer-cast-2.c	(revision 156927)
+++ gcc/testsuite/gcc.dg/Wint-to-pointer-cast-2.c	(working copy)
@@ -1,12 +0,0 @@
-/* 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 "cast to pointer from integer of different size" } */
-}
Index: gcc/testsuite/gcc.dg/Wint-to-pointer-cast-3.c
===================================================================
--- gcc/testsuite/gcc.dg/Wint-to-pointer-cast-3.c	(revision 156927)
+++ gcc/testsuite/gcc.dg/Wint-to-pointer-cast-3.c	(working copy)
@@ -1,20 +0,0 @@
-/* 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 "cast from pointer to integer of different size" } */
-}
Index: gcc/testsuite/g++.old-deja/g++.mike/warn1.C
===================================================================
--- gcc/testsuite/g++.old-deja/g++.mike/warn1.C	(revision 156927)
+++ gcc/testsuite/g++.old-deja/g++.mike/warn1.C	(working copy)
@@ -1,7 +1,7 @@
 // { dg-do assemble  }
-// { dg-options "-Wall" }
+// { dg-options "-Wall -Wno-int-to-pointer-cast" }
 
 typedef char * charptr;
 typedef __SIZE_TYPE__ size_t;
 char c[]={'A','B','C','D'};
 int i=size_t(&c);
Index: gcc/testsuite/g++.dg/other/increment1.C
===================================================================
--- gcc/testsuite/g++.dg/other/increment1.C	(revision 156927)
+++ gcc/testsuite/g++.dg/other/increment1.C	(working copy)
@@ -1,7 +1,8 @@
 // PR c++/37561
 // { dg-do compile }
+// { dg-options "-Wno-int-to-pointer-cast" }
 
 __PTRDIFF_TYPE__ p;
 char q;
 
 void

Property changes on: gcc/testsuite/c-c++-common/Wint-to-pointer-cast-1.c
___________________________________________________________________
Added: svn:mergeinfo


Property changes on: gcc/testsuite/c-c++-common/Wint-to-pointer-cast-2.c
___________________________________________________________________
Added: svn:mergeinfo

Index: gcc/testsuite/c-c++-common/Wint-to-pointer-cast-3.c
===================================================================
--- gcc/testsuite/c-c++-common/Wint-to-pointer-cast-3.c	(revision 156927)
+++ gcc/testsuite/c-c++-common/Wint-to-pointer-cast-3.c	(working copy)
@@ -14,7 +14,9 @@ f (void)
 void *p;
 
 char
 g (void)
 {
-  return (char) p; /* { dg-warning "cast from pointer to integer of different size" } */
+  return (char) p;
+/* { dg-warning "cast from pointer to integer of different size" "" { target c } 19 } */
+/* { dg-error "cast from 'void\\*' to 'char' loses precision" "" { target c++ } 19 } */
 }

Property changes on: gcc/testsuite/c-c++-common/Wint-to-pointer-cast-3.c
___________________________________________________________________
Added: svn:mergeinfo

Index: gcc/cp/typeck.c
===================================================================
--- gcc/cp/typeck.c	(revision 156927)
+++ gcc/cp/typeck.c	(working copy)
@@ -6260,10 +6260,19 @@ cp_build_c_cast (tree type, tree expr, t
       if (complain & tf_error)
         error ("invalid cast to function type %qT", type);
       return error_mark_node;
     }
 
+  if (TREE_CODE (type) == POINTER_TYPE
+      && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
+      /* Casting to an integer of smaller size is an error detected elsewhere.  */
+      && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
+      /* Don't warn about converting any constant.  */
+      && !TREE_CONSTANT (value))
+    warning_at (input_location, OPT_Wint_to_pointer_cast, 
+		"cast to pointer from integer of different size");
+
   /* A C-style cast can be a const_cast.  */
   result = build_const_cast_1 (type, value, /*complain=*/false,
 			       &valid_p);
   if (valid_p)
     return result;
Index: gcc/c.opt
===================================================================
--- gcc/c.opt	(revision 156927)
+++ gcc/c.opt	(working copy)
@@ -271,11 +271,11 @@ Warn when a declaration does not specify
 
 Wimport
 C ObjC C++ ObjC++ Undocumented
 
 Wint-to-pointer-cast
-C ObjC Var(warn_int_to_pointer_cast) Init(1) Warning
+C ObjC C++ ObjC++ Var(warn_int_to_pointer_cast) Init(1) Warning
 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) Warning
 Warn about invalid uses of the \"offsetof\" macro


More information about the Gcc-patches mailing list