From: Patrick Palka Date: Fri, 15 Dec 2023 15:03:31 +0000 (-0500) Subject: c++: section attribute on templates [PR70435, PR88061] X-Git-Tag: basepoints/gcc-15~3544 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ea7bebff7cc5a5eb780a6ca646cb77cad1b625d6;p=thirdparty%2Fgcc.git c++: section attribute on templates [PR70435, PR88061] The section attribute currently has no effect on templates because the call to set_decl_section_name only happens at parse time (on the dependent decl) 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): Propagate DECL_SECTION_NAME via set_decl_section_name. (tsubst_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. * g++.dg/ext/attr-section2b.C: New test. --- diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 50e6f062c859..a82d7ae93aae 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 || TREE_STATIC (t)) && 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 000000000000..b8ac65baa93b --- /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 +[[gnu::section(".foo")]] void fun() { } + +template void fun(); + +// { 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 000000000000..be24be2fc952 --- /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 +struct A { + [[gnu::section(".foo")]] void fun() { } +}; + +template struct A; + +// { 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 000000000000..a76f43b346f9 --- /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 +[[gnu::section(".foo")]] int var = 42; + +template int var; + +// { 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 000000000000..a0b01cd8d93b --- /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 +struct A { + [[gnu::section(".foo")]] static int var; +}; + +template +int A::var = 42; + +template struct A; + +// { dg-final { scan-assembler {.section[ \t]+.foo} } } diff --git a/gcc/testsuite/g++.dg/ext/attr-section2b.C b/gcc/testsuite/g++.dg/ext/attr-section2b.C new file mode 100644 index 000000000000..7b8313bba700 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/attr-section2b.C @@ -0,0 +1,12 @@ +// PR c++/88061 +// { dg-do compile { target { c++11 && named_sections } } } + +template +int* fun() { + [[gnu::section(".foo")]] static int var; + return &var; +}; + +template int* fun(); + +// { dg-final { scan-assembler {.section[ \t]+.foo} } }