[clang] [clang-repl] Re-emit implicitly instantiated templates on subsequent uses (PR #192588)

Caevan Lin via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 16 20:46:17 PDT 2026


https://github.com/voyager-jhk created https://github.com/llvm/llvm-project/pull/192588

Parsing failures in incremental mode cause CodeGen to discard the current module. However, Sema retains implicitly instantiated function bodies. On subsequent valid references, Sema skips emission, leading to unresolved JIT symbols.

This patch forces re-emission of valid implicit instantiations in IncrementalExtensions mode. CodeGen's GlobalDeclMap naturally deduplicates redundant emissions.

Fixes #146770

>From a6711208b4d1a2629f55781203c74433a26b5104 Mon Sep 17 00:00:00 2001
From: voyager-jhk <voyager.lpq at gmail.com>
Date: Fri, 17 Apr 2026 11:39:28 +0800
Subject: [PATCH] [clang-repl] Re-emit implicitly instantiated templates on
 subsequent uses

Parsing failures in incremental mode cause CodeGen to discard the current module. However, Sema retains implicitly instantiated function bodies. On subsequent valid references, Sema skips emission, leading to unresolved JIT symbols.

This patch forces re-emission of valid implicit instantiations in IncrementalExtensions mode. CodeGen's GlobalDeclMap naturally deduplicates redundant emissions.

Fixes #146770
---
 clang/lib/Sema/SemaExpr.cpp | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 9fd8c6a0a5451..95625c765b266 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -19045,6 +19045,13 @@ void Sema::MarkFunctionReferenced(SourceLocation Loc, FunctionDecl *Func,
         }
       }
     });
+  } else if (NeedDefinition && getLangOpts().IncrementalExtensions) {
+    const FunctionDecl *Def = nullptr;
+    if (Func->getBody(Def) && 
+        Def->getTemplateSpecializationKind() == TSK_ImplicitInstantiation &&
+        !Def->isInvalidDecl()) {
+      Consumer.HandleTopLevelDecl(DeclGroupRef(const_cast<FunctionDecl*>(Def)));
+    }
   }
 
   // If a constructor was defined in the context of a default parameter



More information about the cfe-commits mailing list