Patchwork [C++] PR 57869

login
register
mail settings
Submitter Paolo Carlini
Date July 9, 2013, 11:46 p.m.
Message ID <51DCA0B8.9020502@oracle.com>
Download mbox | patch
Permalink /patch/257912/
State New
Headers show

Comments

Paolo Carlini - July 9, 2013, 11:46 p.m.
Hi,

Daniel noticed that, per to the resolution of DR195, we shouldn't warn 
in c++11 mode, not even with -pedantic, for this kind of 
reinterpret_cast (*). Straightforward change tested x86_64-linux, not 
sure if we want to do something slightly different for C++98, or 
something more.

Thanks,
Paolo.

(*) Current clang++ concurs.

///////////////////////////
/cp
2013-07-10  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/57869
	* typeck.c (build_reinterpret_cast_1): In c++11 mode even with
	-pedantic don't warn about casting between pointer-to-function
	and pointer-to-object.

/testsuite
2013-07-10  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/57869
	* g++.dg/cpp0x/reinterpret_cast1.C: New.
	* g++.dg/conversion/dr195.C: Update.
	* g++.dg/expr/cast2.C: Likewise.
Jason Merrill - July 10, 2013, 12:40 a.m.
On 07/09/2013 07:46 PM, Paolo Carlini wrote:
> Daniel noticed that, per to the resolution of DR195, we shouldn't warn
> in c++11 mode, not even with -pedantic, for this kind of
> reinterpret_cast (*). Straightforward change tested x86_64-linux, not
> sure if we want to do something slightly different for C++98, or
> something more.

I think we should apply DR195 to C++98, too.  We might add the warning 
to a new -Wconditionally-supported flag that could later warn about 
other conditionally-supported constructs as well.

Jason

Patch

Index: cp/typeck.c
===================================================================
--- cp/typeck.c	(revision 200852)
+++ cp/typeck.c	(working copy)
@@ -6722,12 +6722,15 @@  build_reinterpret_cast_1 (tree type, tree expr, bo
   else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
 	   || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
     {
-      if (pedantic && (complain & tf_warning))
+      if (cxx_dialect == cxx98
+	  && pedantic && (complain & tf_warning))
 	/* Only issue a warning, as we have always supported this
 	   where possible, and it is necessary in some cases.  DR 195
-	   addresses this issue, but as of 2004/10/26 is still in
-	   drafting.  */
-	warning (0, "ISO C++ forbids casting between pointer-to-function and pointer-to-object");
+	   addressed this issue, and C++11 5.2.10 p8 now says that
+	   "Converting a function pointer to an object pointer type
+	   or vice versa is conditionally-supported."  */
+	warning (0, "ISO C++ 1998 forbids casting between pointer-to-function "
+		 "and pointer-to-object");
       return fold_if_not_in_template (build_nop (type, expr));
     }
   else if (TREE_CODE (type) == VECTOR_TYPE)
Index: testsuite/g++.dg/conversion/dr195.C
===================================================================
--- testsuite/g++.dg/conversion/dr195.C	(revision 200852)
+++ testsuite/g++.dg/conversion/dr195.C	(working copy)
@@ -2,10 +2,11 @@ 
 // Contributed by Nathan Sidwell 20 Oct 2004 <nathan@codesourcery.com>
 
 // DR 195 will allow conversions between function and object pointers
-// under some circumstances. It is in drafting, so we don't implement
-// it (yet).
+// under some circumstances.  The issue got resolved for C++11, which,
+// in 5.2.10 p8 says that: "Converting a function pointer to an object
+// pointer type or vice versa is conditionally-supported."  */
 
-// This checks we warn when being pedantic.
+// This checks we warn when being pedantic in c++98 mode.
 
 typedef void (*PF)(void);
 typedef void *PV;
@@ -18,12 +19,12 @@  void foo ()
   PO po;
 
   /* the following two will almost definitly be ok with 195.  */
-  pf = reinterpret_cast <PF>(pv); // { dg-warning "casting between" "" }
-  pv = reinterpret_cast <PV>(pf); // { dg-warning "casting between" "" }
+  pf = reinterpret_cast <PF>(pv); // { dg-warning "casting between" "" { target c++98 } }
+  pv = reinterpret_cast <PV>(pf); // { dg-warning "casting between" "" { target c++98 } }
 
   /* the following two might or might not be ok with 195.  */
-  pf = reinterpret_cast <PF>(po); // { dg-warning "casting between" "" }
-  po = reinterpret_cast <PO>(pf); // { dg-warning "casting between" "" }
+  pf = reinterpret_cast <PF>(po); // { dg-warning "casting between" "" { target c++98 } }
+  po = reinterpret_cast <PO>(pf); // { dg-warning "casting between" "" { target c++98 } }
 
   /* These will never be ok, as they are implicit.  */
   pv = pf; // { dg-error "invalid conversion" "" }
Index: testsuite/g++.dg/cpp0x/reinterpret_cast1.C
===================================================================
--- testsuite/g++.dg/cpp0x/reinterpret_cast1.C	(revision 0)
+++ testsuite/g++.dg/cpp0x/reinterpret_cast1.C	(working copy)
@@ -0,0 +1,6 @@ 
+// PR c++/57869
+// { dg-do compile { target c++11 } }
+
+void* po = 0;
+void (*pf)() = reinterpret_cast<decltype(pf)>(po);
+static_assert(sizeof(po) >= sizeof(pf), "Conversion not supported");
Index: testsuite/g++.dg/expr/cast2.C
===================================================================
--- testsuite/g++.dg/expr/cast2.C	(revision 200852)
+++ testsuite/g++.dg/expr/cast2.C	(working copy)
@@ -1,5 +1,5 @@ 
 void (*p)();
 
 void f() {
-  (void *)p; // { dg-warning "forbids cast" }
+  (void *)p; // { dg-warning "forbids cast" "" { target c++98 } }
 }