diff mbox series

c++: section attribute on templates [PR70435, PR88061]

Message ID 20231214191725.1941372-1-ppalka@redhat.com
State New
Headers show
Series c++: section attribute on templates [PR70435, PR88061] | expand

Commit Message

Patrick Palka Dec. 14, 2023, 7:17 p.m. UTC
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

Comments

Marek Polacek Dec. 14, 2023, 7:29 p.m. UTC | #1
On Thu, Dec 14, 2023 at 02:17:25PM -0500, Patrick Palka wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?

LGTM.
 
> -- >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
> 

Marek
Jason Merrill Dec. 14, 2023, 8:36 p.m. UTC | #2
On 12/14/23 14:17, Patrick Palka wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?

OK.

> -- >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} } }
diff mbox series

Patch

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} } }