Skip to content

Commit 4b70204

Browse files
committed
Support attribute used in member funcs of class templates
Summary: 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 llvm-svn: 352740
1 parent f392bc8 commit 4b70204

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,6 +2175,20 @@ TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
21752175
Owner->addDecl(Method);
21762176
}
21772177

2178+
// PR17480: Honor the used attribute to instantiate member function
2179+
// definitions
2180+
if (Method->hasAttr<UsedAttr>()) {
2181+
if (const auto *A = dyn_cast<CXXRecordDecl>(Owner)) {
2182+
SourceLocation Loc;
2183+
if (const MemberSpecializationInfo *MSInfo =
2184+
A->getMemberSpecializationInfo())
2185+
Loc = MSInfo->getPointOfInstantiation();
2186+
else if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(A))
2187+
Loc = Spec->getPointOfInstantiation();
2188+
SemaRef.MarkFunctionReferenced(Loc, Method);
2189+
}
2190+
}
2191+
21782192
return Method;
21792193
}
21802194

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -o - %s | FileCheck %s
2+
3+
// Check that PR17480 is fixed: __attribute__((used)) ignored in templated
4+
// classes
5+
namespace InstantiateUsedMemberDefinition {
6+
template <typename T>
7+
struct S {
8+
int __attribute__((used)) f() {
9+
return 0;
10+
}
11+
};
12+
13+
void test() {
14+
// Check that InstantiateUsedMemberDefinition::S<int>::f() is defined
15+
// as a result of the S class template implicit instantiation
16+
// CHECK: define linkonce_odr i32 @_ZN31InstantiateUsedMemberDefinition1SIiE1fEv
17+
S<int> inst;
18+
}
19+
} // namespace InstantiateUsedMemberDefinition

0 commit comments

Comments
 (0)