[clang] da6a14b - [clang] Enforce instantiation of constexpr template functions during non-constexpr evaluation

via cfe-commits cfe-commits at lists.llvm.org
Sat Jul 9 23:40:14 PDT 2022


Author: serge-sans-paille
Date: 2022-07-10T08:40:03+02:00
New Revision: da6a14b91ad999327b41a9040577273591e4ad1d

URL: https://github.com/llvm/llvm-project/commit/da6a14b91ad999327b41a9040577273591e4ad1d
DIFF: https://github.com/llvm/llvm-project/commit/da6a14b91ad999327b41a9040577273591e4ad1d.diff

LOG: [clang] Enforce instantiation of constexpr template functions during non-constexpr evaluation

Otherwise these functions are not instantiated and we end up with an undefined
symbol.

Fix #55560

Differential Revision: https://reviews.llvm.org/D128119

Added: 
    clang/test/CodeGenCXX/constexpr-late-instantiation.cpp
    clang/test/SemaCXX/constexpr-late-instantiation.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 5dae6205efa05..27ba4f1c6a422 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -181,6 +181,8 @@ Bug Fixes
   emitted as a dynamic initializer. Previously the variable would
   incorrectly be zero-initialized. In contexts where a dynamic
   initializer is not allowed this is now diagnosed as an error.
+- Clang now correctly emits symbols for implicitly instantiated constexpr
+  template function. Fixes `Issue 55560 <https://github.com/llvm/llvm-project/issues/55560>`_.
 
 Improvements to Clang's diagnostics
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index d7558017948a2..10dc04b7e09a4 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -4840,7 +4840,8 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
                                      /*Complain*/DefinitionRequired)) {
     if (DefinitionRequired)
       Function->setInvalidDecl();
-    else if (TSK == TSK_ExplicitInstantiationDefinition) {
+    else if (TSK == TSK_ExplicitInstantiationDefinition ||
+             (Function->isConstexpr() && !Recursive)) {
       // Try again at the end of the translation unit (at which point a
       // definition will be required).
       assert(!Recursive);
@@ -4855,7 +4856,7 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
         Diag(PatternDecl->getLocation(), diag::note_forward_template_decl);
         if (getLangOpts().CPlusPlus11)
           Diag(PointOfInstantiation, diag::note_inst_declaration_hint)
-            << Function;
+              << Function;
       }
     }
 

diff  --git a/clang/test/CodeGenCXX/constexpr-late-instantiation.cpp b/clang/test/CodeGenCXX/constexpr-late-instantiation.cpp
new file mode 100644
index 0000000000000..1c8eef73f2dda
--- /dev/null
+++ b/clang/test/CodeGenCXX/constexpr-late-instantiation.cpp
@@ -0,0 +1,17 @@
+// Make sure foo is instantiated and we don't get a link error
+// RUN: %clang_cc1 -S -emit-llvm -triple %itanium_abi_triple %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: }
+
+template <typename T>
+constexpr T foo(T a) {
+  return a;
+}

diff  --git a/clang/test/SemaCXX/constexpr-late-instantiation.cpp b/clang/test/SemaCXX/constexpr-late-instantiation.cpp
new file mode 100644
index 0000000000000..ec8e071217c1d
--- /dev/null
+++ b/clang/test/SemaCXX/constexpr-late-instantiation.cpp
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 %s -fsyntax-only -verify
+
+template <typename T>
+constexpr T foo(T a);   // expected-note {{declared here}}
+
+int main() {
+  int k = foo<int>(5);  // Ok
+  constexpr int j =     // expected-error {{constexpr variable 'j' must be initialized by a constant expression}}
+          foo<int>(5);  // expected-note {{undefined function 'foo<int>' cannot be used in a constant expression}}
+}
+
+template <typename T>
+constexpr T foo(T a) {
+  return a;
+}


        


More information about the cfe-commits mailing list