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

via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 16 20:47:05 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Caevan Lin (voyager-jhk)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/192588.diff


1 Files Affected:

- (modified) clang/lib/Sema/SemaExpr.cpp (+7) 


``````````diff
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

``````````

</details>


https://github.com/llvm/llvm-project/pull/192588


More information about the cfe-commits mailing list