Index: lib/Sema/SemaTemplateInstantiateDecl.cpp =================================================================== --- lib/Sema/SemaTemplateInstantiateDecl.cpp +++ lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -2176,6 +2176,24 @@ Owner->addDecl(Method); } + // facebook begin T10336705 + // PR17480: Honor the used attribute to instantiate member function + // definitions + if (Method->hasAttr()) { + if (auto *A = dyn_cast(Owner)) { + SourceLocation Loc; + if (MemberSpecializationInfo *MSInfo = A->getMemberSpecializationInfo()) { + Loc = MSInfo->getPointOfInstantiation(); + } else if (ClassTemplateSpecializationDecl *Spec = + dyn_cast(A)) { + Loc = Spec->getPointOfInstantiation(); + } + + SemaRef.MarkFunctionReferenced(Loc, Method, /*MightBeOdrUse=*/true); + } + } + // end facebook + return Method; } Index: test/CodeGenCXX/attr-used-member-function-implicit-instantiation.cpp =================================================================== --- /dev/null +++ test/CodeGenCXX/attr-used-member-function-implicit-instantiation.cpp @@ -0,0 +1,19 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -O0 -o - %s \ +// RUN: | FileCheck %s + +#define USED __attribute__((used)) + +namespace InstantiateUsedMemberDefinition { + template struct S { + int USED f() { + return 0; + } + }; + + void test() { + // Check that InstantiateUsedMemberDefinition::S::f() is defined + // as a result of the S class template implicit instantiation + // CHECK-DAG: define linkonce_odr i32 @_ZN31InstantiateUsedMemberDefinition1SIiE1fEv + S inst; + } +}