public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Arsen Arsenović" <arsen@aarsen.me>
To: Jason Merrill <jason@redhat.com>
Cc: gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] contracts: Stop relying on mangling for naming .pre/.post clones
Date: Thu, 15 Dec 2022 19:00:56 +0100	[thread overview]
Message-ID: <86bko4ttho.fsf@aarsen.me> (raw)
In-Reply-To: <5cc46cd6-5f08-e73a-0db2-ea165a6e7734@redhat.com>


[-- Attachment #1.1: Type: text/plain, Size: 717 bytes --]

Hi Jason,

Jason Merrill <jason@redhat.com> writes:

> On 12/10/22 08:13, Arsen Arsenović wrote:
>> If the mangler is relied on, functions with extern "C" on them emit multiple
>> definitions of the same name.
>
> But doing it here interferes with lazy mangling.  How about appending the
> suffix into write_mangled_name instead of write_encoding?  The demangler
> already expects "clone" suffixes at the end of the mangled name.

Ah, sorry.  I'm not well versed in the mangler code, so I didn't realize
(frankly, I was initially surprised when I saw that DECL_ASSEMBLER_NAME
was set that early, but went with it).  That makes sense.

How about this?  Tested on x86_64-pc-linux-gnu via check-g++.


[-- Attachment #1.2: c++: Mangle contracts in write_mangled_name unconditionally --]
[-- Type: text/plain, Size: 2644 bytes --]

From 2a2d98e94bdd7a8d7f862b2accda849927e4509e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Arsen=20Arsenovi=C4=87?= <arsen@aarsen.me>
Date: Thu, 15 Dec 2022 18:56:59 +0100
Subject: [PATCH v2] c++: Mangle contracts in write_mangled_name
 unconditionally

This fixes contract-checked extern "C" functions.

gcc/cp/ChangeLog:

	* mangle.cc (write_encoding): Move contract pre/post function mangling
	from here...
	(write_mangled_name): ... to here, and make it happen always.

gcc/testsuite/ChangeLog:

	* g++.dg/contracts/contracts-externC.C: New test.
---
 gcc/cp/mangle.cc                              | 14 +++++++-------
 .../g++.dg/contracts/contracts-externC.C      | 19 +++++++++++++++++++
 2 files changed, 26 insertions(+), 7 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/contracts/contracts-externC.C

diff --git a/gcc/cp/mangle.cc b/gcc/cp/mangle.cc
index e363ef35b9f..074cf27ec7a 100644
--- a/gcc/cp/mangle.cc
+++ b/gcc/cp/mangle.cc
@@ -798,6 +798,13 @@ write_mangled_name (const tree decl, bool top_level)
       write_string ("_Z");
       write_encoding (decl);
     }
+
+  /* If this is the pre/post function for a guarded function, append
+     .pre/post, like something from create_virtual_clone.  */
+  if (DECL_IS_PRE_FN_P (decl))
+    write_string (".pre");
+  else if (DECL_IS_POST_FN_P (decl))
+    write_string (".post");
 }
 
 /* Returns true if the return type of DECL is part of its signature, and
@@ -856,13 +863,6 @@ write_encoding (const tree decl)
 				mangle_return_type_p (decl),
 				d);
 
-      /* If this is the pre/post function for a guarded function, append
-	 .pre/post, like something from create_virtual_clone.  */
-      if (DECL_IS_PRE_FN_P (decl))
-	write_string (".pre");
-      else if (DECL_IS_POST_FN_P (decl))
-	write_string (".post");
-
       /* If this is a coroutine helper, then append an appropriate string to
 	 identify which.  */
       if (tree ramp = DECL_RAMP_FN (decl))
diff --git a/gcc/testsuite/g++.dg/contracts/contracts-externC.C b/gcc/testsuite/g++.dg/contracts/contracts-externC.C
new file mode 100644
index 00000000000..873056b742b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/contracts/contracts-externC.C
@@ -0,0 +1,19 @@
+// simple check to ensure we don't emit a function with the same name twice,
+// when wrapping functions in pre- and postconditions.
+// { dg-do link }
+// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
+
+volatile int x = 10;
+
+extern "C" void
+f ()
+  [[ pre: x < 10 ]]
+{
+}
+
+int
+main ()
+  [[ post: x > 10 ]]
+{
+  f();
+}
-- 
2.39.0


[-- Attachment #1.3: Type: text/plain, Size: 554 bytes --]


I did run c++filt (afaik, it uses the libiberty demangler) on this
revision, and I got:

          .type	f(int) [clone .pre], @function
  f(int) [clone .pre]:

out of ``void f(int x) [[ pre: x > 10 ]] {}'', which seems to match your
description.

If I understand this right, write_xxx corresponds to xxx in the Itanium
ABI mangling BNF, in which case, I believe I have the correct spot here.
In that case, a similar change should happen for coroutines; I think
Iain was working on that.

Thanks, have a great day.
-- 
Arsen Arsenović

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 381 bytes --]

  reply	other threads:[~2022-12-15 18:25 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-10 13:13 Arsen Arsenović
2022-12-10 13:14 ` Arsen Arsenović
2022-12-10 19:51   ` Bernhard Reutner-Fischer
2022-12-11 15:56     ` Arsen Arsenović
2022-12-15 15:40 ` Jason Merrill
2022-12-15 18:00   ` Arsen Arsenović [this message]
2022-12-15 20:29     ` Iain Sandoe
2022-12-17 11:30       ` Arsen Arsenović
2022-12-15 21:48     ` Jason Merrill
2022-12-17 11:30       ` Arsen Arsenović

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86bko4ttho.fsf@aarsen.me \
    --to=arsen@aarsen.me \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).