diff mbox

Ping: demangle C++ extern "C" functions

Message ID alpine.DEB.2.02.1110212053030.6478@laptop-mg.saclay.inria.fr
State New
Headers show

Commit Message

Marc Glisse Oct. 21, 2011, 6:59 p.m. UTC
Ping (changing the Cc to bother a different person...).

On Sat, 3 Sep 2011, Marc Glisse wrote:

> Hello,
>
> this patch is obviously related to PR c++/2316 ("g++ fails to overload on 
> language linkage") but seems fairly independent. Currently, the demangler 
> recognizes 'Y' for extern "C" functions and ignores it. The patch makes it 
> print extern "C" after the function type:
> _Z1aIFYviEEvPT_
> void a<void (int) extern "C">(void (*)(int) extern "C")
>
> Writing it before the return type seems more natural, but it is ambiguous. I 
> guess it could also be printed in the middle (next to the star that indicates 
> a function pointer), but placing it like the cv-qualifiers of member 
> functions seemed good (plus, that's where Oracle puts it in its 
> implementation of c++filt).
>
> Since g++ doesn't generate such mangling, the effect should be hard to notice 
> ;-)
>
> (Even if the patch was ok, I am not a committer)
>
> 2011-09-03  Marc Glisse  <marc.glisse@inria.fr>
>
>        * include/demangle.h (demangle_component_type)
>        [DEMANGLE_COMPONENT_EXTERN_C]: Handle extern "C".
>        * libiberty/cp-demangle.c (d_dump): Likewise.
>        (d_make_comp): Likewise.
>        (d_function_type): Likewise.
>        (d_print_comp): Likewise.
>        (d_print_mod_list): Likewise.
>        (d_print_mod): Likewise.
>        (d_print_function_type): Likewise.
>        * libiberty/testsuite/demangle-expected: Test it.

Comments

Ian Lance Taylor Oct. 22, 2011, 4:53 a.m. UTC | #1
Marc Glisse <marc.glisse@inria.fr> writes:

>> _Z1aIFYviEEvPT_
>> void a<void (int) extern "C">(void (*)(int) extern "C")

Does g++ ever actually generate a symbol name like this?  Is it worth
worrying about?


Personally if we are going to print this at all I think the extern "C"
should be in square brackets.  E.g.,

void a<void (int) [extern "C"]>(void (*)(int) [extern "C"])

Ian
Marc Glisse Oct. 22, 2011, 7:06 a.m. UTC | #2
Thank you for your comments.

On Fri, 21 Oct 2011, Ian Lance Taylor wrote:

> Marc Glisse <marc.glisse@inria.fr> writes:
>
>>> _Z1aIFYviEEvPT_
>>> void a<void (int) extern "C">(void (*)(int) extern "C")
>
> Does g++ ever actually generate a symbol name like this?  Is it worth
> worrying about?

g++ doesn't currently generate those, but I would like to change that 
(c++/2316). And nothing prevents another compiler from actually 
implementing the itanium C++ ABI (this code is mostly for binutils if I 
understand correctly).

I thought it made sense to submit independent pieces first, but if it is 
preferable to wait until everything is ready (not soon) I understand.

> Personally if we are going to print this at all I think the extern "C"
> should be in square brackets.  E.g.,
>
> void a<void (int) [extern "C"]>(void (*)(int) [extern "C"])

Hey, that looks nice!

It doesn't look like attributes (in double square brackets) get mangled, 
so it shouldn't look strange (and it could always be changed in that 
case).
Ian Lance Taylor Oct. 22, 2011, 3:18 p.m. UTC | #3
Marc Glisse <marc.glisse@inria.fr> writes:

> g++ doesn't currently generate those, but I would like to change that
> (c++/2316). And nothing prevents another compiler from actually
> implementing the itanium C++ ABI (this code is mostly for binutils if
> I understand correctly).
>
> I thought it made sense to submit independent pieces first, but if it
> is preferable to wait until everything is ready (not soon) I
> understand.

Independent pieces is fine.  I just wanted to make sure there was some
reason for it.

Ian
diff mbox

Patch

Index: include/demangle.h
===================================================================
--- include/demangle.h	(revision 178498)
+++ include/demangle.h	(working copy)
@@ -288,6 +288,9 @@ 
   /* The const qualifier.  The one subtree is the type which is being
      qualified.  */
   DEMANGLE_COMPONENT_CONST,
+  /* extern "C" linkage.  The one subtree is the function type which
+     is being qualified.  */
+  DEMANGLE_COMPONENT_EXTERN_C,
   /* The restrict qualifier modifying a member function.  The one
      subtree is the type which is being qualified.  */
   DEMANGLE_COMPONENT_RESTRICT_THIS,
Index: libiberty/testsuite/demangle-expected
===================================================================
--- libiberty/testsuite/demangle-expected	(revision 178498)
+++ libiberty/testsuite/demangle-expected	(working copy)
@@ -4151,3 +4151,8 @@ 
 --format=auto
 _ZN3Psi7VariantIIcPKcEE5visitIIRZN11VariantTest9TestVisit11test_methodEvEUlS2_E0_RZNS6_11test_methodEvEUlcE1_RZNS6_11test_methodEvEUlNS_4NoneEE_EEENS_13VariantDetail19SelectVisitorResultIIDpT_EE4typeEDpOSG_
 Psi::VariantDetail::SelectVisitorResult<VariantTest::TestVisit::test_method()::{lambda(char const*)#2}&, VariantTest::TestVisit::test_method()::{lambda(char)#3}&, VariantTest::TestVisit::test_method()::{lambda(Psi::None)#1}&>::type Psi::Variant<char, char const*>::visit<VariantTest::TestVisit::test_method()::{lambda(char const*)#2}&, VariantTest::TestVisit::test_method()::{lambda(char)#3}&, VariantTest::TestVisit::test_method()::{lambda(Psi::None)#1}&>((VariantTest::TestVisit::test_method()::{lambda(Psi::None)#1}&)...)
+#
+# extern "C" linkage for function types.
+--format=gnu-v3
+_Z1aIFYviEEvPT_
+void a<void (int) extern "C">(void (*)(int) extern "C")
Index: libiberty/cp-demangle.c
===================================================================
--- libiberty/cp-demangle.c	(revision 178498)
+++ libiberty/cp-demangle.c	(working copy)
@@ -591,6 +591,9 @@ 
     case DEMANGLE_COMPONENT_CONST:
       printf ("const\n");
       break;
+    case DEMANGLE_COMPONENT_EXTERN_C:
+      printf ("extern \"C\"\n");
+      break;
     case DEMANGLE_COMPONENT_RESTRICT_THIS:
       printf ("restrict this\n");
       break;
@@ -807,6 +810,7 @@ 
       break;
 
       /* These types only require one parameter.  */
+    case DEMANGLE_COMPONENT_EXTERN_C:
     case DEMANGLE_COMPONENT_VTABLE:
     case DEMANGLE_COMPONENT_VTT:
     case DEMANGLE_COMPONENT_TYPEINFO:
@@ -2324,18 +2328,22 @@ 
 d_function_type (struct d_info *di)
 {
   struct demangle_component *ret;
+  int is_extern_c = 0;
 
   if (! d_check_char (di, 'F'))
     return NULL;
   if (d_peek_char (di) == 'Y')
     {
-      /* Function has C linkage.  We don't print this information.
-	 FIXME: We should print it in verbose mode.  */
+      /* Function has C linkage.  */
+      is_extern_c = 1;
       d_advance (di, 1);
+      di->expansion += sizeof "extern \"C\"";
     }
   ret = d_bare_function_type (di, 1);
   if (! d_check_char (di, 'E'))
     return NULL;
+  if (is_extern_c)
+    ret = d_make_comp (di, DEMANGLE_COMPONENT_EXTERN_C, ret, NULL);
   return ret;
 }
 
@@ -3925,6 +3933,7 @@ 
     case DEMANGLE_COMPONENT_RESTRICT_THIS:
     case DEMANGLE_COMPONENT_VOLATILE_THIS:
     case DEMANGLE_COMPONENT_CONST_THIS:
+    case DEMANGLE_COMPONENT_EXTERN_C:
     case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
     case DEMANGLE_COMPONENT_POINTER:
     case DEMANGLE_COMPONENT_COMPLEX:
@@ -4537,7 +4546,8 @@ 
       || (! suffix
 	  && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
 	      || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
-	      || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
+	      || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS
+	      || mods->mod->type == DEMANGLE_COMPONENT_EXTERN_C)))
     {
       d_print_mod_list (dpi, options, mods->next, suffix);
       return;
@@ -4628,6 +4638,9 @@ 
     case DEMANGLE_COMPONENT_CONST_THIS:
       d_append_string (dpi, " const");
       return;
+    case DEMANGLE_COMPONENT_EXTERN_C:
+      d_append_string (dpi, " extern \"C\"");
+      return;
     case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
       d_append_char (dpi, ' ');
       d_print_comp (dpi, options, d_right (mod));
@@ -4711,6 +4724,7 @@ 
 	case DEMANGLE_COMPONENT_RESTRICT_THIS:
 	case DEMANGLE_COMPONENT_VOLATILE_THIS:
 	case DEMANGLE_COMPONENT_CONST_THIS:
+	case DEMANGLE_COMPONENT_EXTERN_C:
 	  break;
 	default:
 	  break;