Index: lib/Sema/SemaTemplateInstantiateDecl.cpp =================================================================== --- lib/Sema/SemaTemplateInstantiateDecl.cpp +++ lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -2175,6 +2175,11 @@ Owner->addDecl(Method); } + // PR17480: Honor the used attribute to instantiate member function + // definitions + if (Method->hasAttr()) + SemaRef.MarkFunctionReferenced(SourceLocation(), Method); + 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 -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 Index: test/SemaTemplate/attr-used-member-function-implicit-instantiation.cpp =================================================================== --- /dev/null +++ test/SemaTemplate/attr-used-member-function-implicit-instantiation.cpp @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +// Check that __attribute__((used)) in member functions of templated classes +// does not break explicit specializations of those functions + +// expected-no-diagnostics +template +struct a { + void verify() __attribute__((__used__)); +}; +using b = a; +template<> void b::verify() {}