public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Nathan Sidwell <nathan@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-6255] [c++] Adjust mark used member in instantiated class scope
Date: Wed,  5 Jan 2022 12:16:43 +0000 (GMT)	[thread overview]
Message-ID: <20220105121643.33C93385843B@sourceware.org> (raw)

https://gcc.gnu.org/g:b1e701dc4adb11a5ed5f45c2fb31ba4689b718d0

commit r12-6255-gb1e701dc4adb11a5ed5f45c2fb31ba4689b718d0
Author: Nathan Sidwell <nathan@acm.org>
Date:   Tue Jan 4 13:36:44 2022 -0800

    [c++] Adjust mark used member in instantiated class scope
    
    The fix for PR97966 caused a regression with (non-template) member
    functions of template classes.  We need to mark them used in the
    instantiated class's scope, rather than the scope we were in before
    instantiating, as the latter may itself be in template and change the
    behaviour of marking a function as used.
    
            gcc/cp/
            * pt.c (instantiate_class_template_1): Process attribute((used)) set
            in class's context.
            gcc/testsuite/
            * g++.dg/template/attr-used.C: New.

Diff:
---
 gcc/cp/pt.c                               | 13 ++++++++-----
 gcc/testsuite/g++.dg/template/attr-used.C | 16 ++++++++++++++++
 2 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index c587966adbe..0fa4a162354 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -12277,6 +12277,14 @@ instantiate_class_template_1 (tree type)
 
   perform_instantiation_time_access_checks (pattern, args);
   perform_deferred_access_checks (tf_warning_or_error);
+
+  /* Now that we've gone through all the members, instantiate those
+     marked with attribute used.  We must do this in the context of
+     the class -- not the context we pushed from, as that might be
+     inside a template and change the behaviour of mark_used.  */
+  for (tree x : used)
+    mark_used (x);
+
   pop_nested_class ();
   maximum_field_alignment = saved_maximum_field_alignment;
   if (!fn_context)
@@ -12290,11 +12298,6 @@ instantiate_class_template_1 (tree type)
   if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
     vec_safe_push (keyed_classes, type);
 
-  /* Now that we've gone through all the members, instantiate those
-     marked with attribute used.  */
-  for (tree x : used)
-    mark_used (x);
-
   return type;
 }
 
diff --git a/gcc/testsuite/g++.dg/template/attr-used.C b/gcc/testsuite/g++.dg/template/attr-used.C
new file mode 100644
index 00000000000..e12bf4caa3f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/attr-used.C
@@ -0,0 +1,16 @@
+// { dg-do compile }
+
+template<typename T> struct X {
+  void g () {}
+  void f () __attribute__((__used__)) {}
+};
+
+extern X<int> x; // X<int> incomplete here
+
+template <typename T>
+void Frob (T t) {
+  x.g(); // X<int> completed here, X<int>::f's body marked for instantiation
+}
+
+// Make sure X<int>::f is emitted
+// { dg-final { scan-assembler "_ZN1XIiE1fEv:" } }


                 reply	other threads:[~2022-01-05 12:16 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220105121643.33C93385843B@sourceware.org \
    --to=nathan@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /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).