From patchwork Tue Aug 16 23:18:48 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 110252 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id BD6E5B6F65 for ; Wed, 17 Aug 2011 09:19:12 +1000 (EST) Received: (qmail 11215 invoked by alias); 16 Aug 2011 23:19:10 -0000 Received: (qmail 11206 invoked by uid 22791); 16 Aug 2011 23:19:10 -0000 X-SWARE-Spam-Status: No, hits=-7.0 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, SPF_HELO_PASS X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 16 Aug 2011 23:18:50 +0000 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p7GNIocN002299 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Tue, 16 Aug 2011 19:18:50 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id p7GNInRO014413 for ; Tue, 16 Aug 2011 19:18:49 -0400 Received: from [0.0.0.0] (ovpn-113-29.phx2.redhat.com [10.3.113.29]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id p7GNImis023476 for ; Tue, 16 Aug 2011 19:18:49 -0400 Message-ID: <4E4AFAD8.3010209@redhat.com> Date: Tue, 16 Aug 2011 19:18:48 -0400 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; Linux i686; rv:5.0) Gecko/20110719 Thunderbird/5.0 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH to make attribute ((used)) force instantiation of class template members Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org I recently got a request to make attribute((used)) cause the implicit instantiation of a member function of a class template whenever the class is instantiated. This seemed like an appropriate extension of the current semantics of that attribute, so this patch implements it. Tested x86_64-pc-linux-gnu, applying to trunk. commit fafc02c5c94f8f978b350478ac416d7a705e850b Author: Jason Merrill Date: Mon Aug 15 23:39:48 2011 -0400 * pt.c (instantiate_class_template_1): If DECL_PRESERVE_P is set on a member function or static data member, call mark_used. diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 10fdced..9a4419a 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -8675,6 +8675,9 @@ instantiate_class_template_1 (tree type) --processing_template_decl; set_current_access_from_decl (r); finish_member_declaration (r); + /* Instantiate members marked with attribute used. */ + if (r != error_mark_node && DECL_PRESERVE_P (r)) + mark_used (r); } else { @@ -8724,6 +8727,9 @@ instantiate_class_template_1 (tree type) /*init_const_expr_p=*/false, /*asmspec_tree=*/NULL_TREE, /*flags=*/0); + /* Instantiate members marked with attribute used. */ + if (r != error_mark_node && DECL_PRESERVE_P (r)) + mark_used (r); } else if (TREE_CODE (r) == FIELD_DECL) { diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 49a8125..786c18d 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -3647,6 +3647,10 @@ for the function even if it appears that the function is not referenced. This is useful, for example, when the function is referenced only in inline assembly. +When applied to a member function of a C++ class template, the +attribute also means that the function will be instantiated if the +class itself is instantiated. + @item version_id @cindex @code{version_id} attribute This IA64 HP-UX attribute, attached to a global variable or function, renames a @@ -4457,6 +4461,10 @@ variable. This attribute, attached to a variable, means that the variable must be emitted even if it appears that the variable is not referenced. +When applied to a static data member of a C++ class template, the +attribute also means that the member will be instantiated if the +class itself is instantiated. + @item vector_size (@var{bytes}) This attribute specifies the vector size for the variable, measured in bytes. For example, the declaration: diff --git a/gcc/testsuite/g++.dg/ext/attr-used-1.C b/gcc/testsuite/g++.dg/ext/attr-used-1.C new file mode 100644 index 0000000..6754c7f --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/attr-used-1.C @@ -0,0 +1,17 @@ +// Attribute used on a member function or static data member +// of a template should cause them to be instantiated along +// with the class itself. + +// { dg-final { scan-assembler "_ZN1AIiE1fEv" } } +// { dg-final { scan-assembler "_ZN1AIiE1tE" } } + +template struct A +{ + void f() __attribute ((used)); + static T t __attribute ((used)); +}; + +template void A::f() { } +template T A::t; + +A a;