From: Rafael Auler Date: Thu, 7 Mar 2019 19:14:30 +0000 (+0000) Subject: Recommit "Support attribute used in member funcs of class templates" X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8297e93480c636dc90fd14653c5a66406193363f;p=clang Recommit "Support attribute used in member funcs of class templates" The patch originally broke code that was incompatible with GCC, but we want to follow GCC behavior here according to the discussion in https://reviews.llvm.org/D58216 Original commit message: As PR17480 describes, clang does not support the used attribute for member functions of class templates. This means that if the member function is not used, its definition is never instantiated. This patch changes clang to emit the definition if it has the used attribute. Test Plan: Added a testcase Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D56928 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@355627 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaTemplateInstantiateDecl.cpp b/lib/Sema/SemaTemplateInstantiateDecl.cpp index 73909fe6b5..d4baa454d7 100644 --- a/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -2232,6 +2232,20 @@ TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D, Owner->addDecl(Method); } + // PR17480: Honor the used attribute to instantiate member function + // definitions + if (Method->hasAttr()) { + if (const auto *A = dyn_cast(Owner)) { + SourceLocation Loc; + if (const MemberSpecializationInfo *MSInfo = + A->getMemberSpecializationInfo()) + Loc = MSInfo->getPointOfInstantiation(); + else if (const auto *Spec = dyn_cast(A)) + Loc = Spec->getPointOfInstantiation(); + SemaRef.MarkFunctionReferenced(Loc, Method); + } + } + return Method; } diff --git a/test/CodeGenCXX/attr-used-member-function-implicit-instantiation.cpp b/test/CodeGenCXX/attr-used-member-function-implicit-instantiation.cpp new file mode 100644 index 0000000000..7d2062f989 --- /dev/null +++ b/test/CodeGenCXX/attr-used-member-function-implicit-instantiation.cpp @@ -0,0 +1,19 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -o - %s | FileCheck %s + +// Check that PR17480 is fixed: __attribute__((used)) ignored in templated +// classes +namespace InstantiateUsedMemberDefinition { +template +struct S { + int __attribute__((used)) f() { + return 0; + } +}; + +void test() { + // Check that InstantiateUsedMemberDefinition::S::f() is defined + // as a result of the S class template implicit instantiation + // CHECK: define linkonce_odr i32 @_ZN31InstantiateUsedMemberDefinition1SIiE1fEv + S inst; +} +} // namespace InstantiateUsedMemberDefinition