public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Palka <ppalka@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: jason@redhat.com, Patrick Palka <ppalka@redhat.com>
Subject: [PATCH] c++: section attribute on templates [PR70435, PR88061]
Date: Thu, 14 Dec 2023 14:17:25 -0500	[thread overview]
Message-ID: <20231214191725.1941372-1-ppalka@redhat.com> (raw)

Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?

-- >8 --

The section attribute currently has no effect on templates because the
call to set_decl_section_name only happens at parse time and not also at
instantiation time.  This patch fixes this by propagating the section
name from the template to the instantiation.

	PR c++/70435
	PR c++/88061

gcc/cp/ChangeLog:

	* pt.cc (tsubst_function_decl): Call set_decl_section_name.
	(tsubst_decl) <case VAR_DECL>: Likewise.

gcc/testsuite/ChangeLog:

	* g++.dg/ext/attr-section1.C: New test.
	* g++.dg/ext/attr-section1a.C: New test.
	* g++.dg/ext/attr-section2.C: New test.
	* g++.dg/ext/attr-section2a.C: New test.
---
 gcc/cp/pt.cc                              |  4 ++++
 gcc/testsuite/g++.dg/ext/attr-section1.C  |  9 +++++++++
 gcc/testsuite/g++.dg/ext/attr-section1a.C | 11 +++++++++++
 gcc/testsuite/g++.dg/ext/attr-section2.C  |  9 +++++++++
 gcc/testsuite/g++.dg/ext/attr-section2a.C | 14 ++++++++++++++
 5 files changed, 47 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/ext/attr-section1.C
 create mode 100644 gcc/testsuite/g++.dg/ext/attr-section1a.C
 create mode 100644 gcc/testsuite/g++.dg/ext/attr-section2.C
 create mode 100644 gcc/testsuite/g++.dg/ext/attr-section2a.C

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 50e6f062c85..8c4174fb902 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -14607,6 +14607,8 @@ tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
 	= remove_attribute ("visibility", DECL_ATTRIBUTES (r));
     }
   determine_visibility (r);
+  if (DECL_SECTION_NAME (t))
+    set_decl_section_name (r, t);
   if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
       && !processing_template_decl)
     defaulted_late_check (r);
@@ -15423,6 +15425,8 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain,
 		  = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
 	      }
 	    determine_visibility (r);
+	    if (!local_p && DECL_SECTION_NAME (t))
+	      set_decl_section_name (r, t);
 	  }
 
 	if (!local_p)
diff --git a/gcc/testsuite/g++.dg/ext/attr-section1.C b/gcc/testsuite/g++.dg/ext/attr-section1.C
new file mode 100644
index 00000000000..b8ac65baa93
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/attr-section1.C
@@ -0,0 +1,9 @@
+// PR c++/70435
+// { dg-do compile { target { c++11 && named_sections } } }
+
+template<class T>
+[[gnu::section(".foo")]] void fun() { }
+
+template void fun<int>();
+
+// { dg-final { scan-assembler {.section[ \t]+.foo} } }
diff --git a/gcc/testsuite/g++.dg/ext/attr-section1a.C b/gcc/testsuite/g++.dg/ext/attr-section1a.C
new file mode 100644
index 00000000000..be24be2fc95
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/attr-section1a.C
@@ -0,0 +1,11 @@
+// PR c++/70435
+// { dg-do compile { target { c++11 && named_sections } } }
+
+template<class T>
+struct A {
+  [[gnu::section(".foo")]] void fun() { }
+};
+
+template struct A<int>;
+
+// { dg-final { scan-assembler {.section[ \t]+.foo} } }
diff --git a/gcc/testsuite/g++.dg/ext/attr-section2.C b/gcc/testsuite/g++.dg/ext/attr-section2.C
new file mode 100644
index 00000000000..a76f43b346f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/attr-section2.C
@@ -0,0 +1,9 @@
+// PR c++/88061
+// { dg-do compile { target { c++14 && named_sections } } }
+
+template<class T>
+[[gnu::section(".foo")]] int var = 42;
+
+template int var<int>;
+
+// { dg-final { scan-assembler {.section[ \t]+.foo} } }
diff --git a/gcc/testsuite/g++.dg/ext/attr-section2a.C b/gcc/testsuite/g++.dg/ext/attr-section2a.C
new file mode 100644
index 00000000000..a0b01cd8d93
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/attr-section2a.C
@@ -0,0 +1,14 @@
+// PR c++/88061
+// { dg-do compile { target { c++11 && named_sections } } }
+
+template<class T>
+struct A {
+  [[gnu::section(".foo")]] static int var;
+};
+
+template<class T>
+int A<T>::var = 42;
+
+template struct A<int>;
+
+// { dg-final { scan-assembler {.section[ \t]+.foo} } }
-- 
2.43.0.76.g1a87c842ec


             reply	other threads:[~2023-12-14 19:17 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-14 19:17 Patrick Palka [this message]
2023-12-14 19:29 ` Marek Polacek
2023-12-14 20:36 ` Jason Merrill

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=20231214191725.1941372-1-ppalka@redhat.com \
    --to=ppalka@redhat.com \
    --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).