[PATCH] D128119: [clang] enforce instantiation of constexpr template functions

serge via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Jun 18 04:58:10 PDT 2022


serge-sans-paille updated this revision to Diff 438114.
serge-sans-paille added a comment.

(rebased on main branch)


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D128119/new/

https://reviews.llvm.org/D128119

Files:
  clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
  clang/test/SemaCXX/constexpr-late-instantiation.cpp


Index: clang/test/SemaCXX/constexpr-late-instantiation.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/constexpr-late-instantiation.cpp
@@ -0,0 +1,17 @@
+// Make sure foo is sinstantiated and we don't get a link error
+// RUN: %clang_cc1 -S -emit-llvm %s -o- | FileCheck %s
+
+template <typename T>
+constexpr T foo(T a);
+
+// CHECK-LABEL: define {{.*}} @main
+int main() {
+  // CHECK: call {{.*}} @_Z3fooIiET_S0_
+  int k = foo<int>(5);
+}
+
+// CHECK-LABEL: define {{.*}} @_Z3fooIiET_S0_
+template <typename T>
+constexpr T foo(T a) {
+  return a;
+}
Index: clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
===================================================================
--- clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -4855,6 +4855,10 @@
         if (getLangOpts().CPlusPlus11)
           Diag(PointOfInstantiation, diag::note_inst_declaration_hint)
             << Function;
+      } else if (!Recursive) {
+        Function->setInstantiationIsPending(true);
+        PendingInstantiations.push_back(
+            std::make_pair(Function, PointOfInstantiation));
       }
     }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128119.438114.patch
Type: text/x-patch
Size: 1207 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220618/6bbe4baf/attachment.bin>


More information about the cfe-commits mailing list