r352740 - Support attribute used in member funcs of class templates

Rafael Auler via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 31 01:38:31 PST 2019


Author: rafauler
Date: Thu Jan 31 01:38:31 2019
New Revision: 352740

URL: http://llvm.org/viewvc/llvm-project?rev=352740&view=rev
Log:
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

Added:
    cfe/trunk/test/CodeGenCXX/attr-used-member-function-implicit-instantiation.cpp
Modified:
    cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp

Modified: cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp?rev=352740&r1=352739&r2=352740&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp Thu Jan 31 01:38:31 2019
@@ -2175,6 +2175,20 @@ TemplateDeclInstantiator::VisitCXXMethod
     Owner->addDecl(Method);
   }
 
+  // PR17480: Honor the used attribute to instantiate member function
+  // definitions
+  if (Method->hasAttr<UsedAttr>()) {
+    if (const auto *A = dyn_cast<CXXRecordDecl>(Owner)) {
+      SourceLocation Loc;
+      if (const MemberSpecializationInfo *MSInfo =
+              A->getMemberSpecializationInfo())
+        Loc = MSInfo->getPointOfInstantiation();
+      else if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(A))
+        Loc = Spec->getPointOfInstantiation();
+      SemaRef.MarkFunctionReferenced(Loc, Method);
+    }
+  }
+
   return Method;
 }
 

Added: cfe/trunk/test/CodeGenCXX/attr-used-member-function-implicit-instantiation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/attr-used-member-function-implicit-instantiation.cpp?rev=352740&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/attr-used-member-function-implicit-instantiation.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/attr-used-member-function-implicit-instantiation.cpp Thu Jan 31 01:38:31 2019
@@ -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 <typename T>
+struct S {
+  int __attribute__((used)) f() {
+    return 0;
+  }
+};
+
+void test() {
+  // Check that InstantiateUsedMemberDefinition::S<int>::f() is defined
+  // as a result of the S class template implicit instantiation
+  // CHECK: define linkonce_odr i32 @_ZN31InstantiateUsedMemberDefinition1SIiE1fEv
+  S<int> inst;
+}
+} // namespace InstantiateUsedMemberDefinition




More information about the cfe-commits mailing list