https://github.com/anondeveg updated https://github.com/llvm/llvm-project/pull/218571
>From 2d82a728bf281cc549c3ca3e65bb4f137d25ef20 Mon Sep 17 00:00:00 2001 From: Anondev <[email protected]> Date: Tue, 25 Aug 2026 04:11:48 +0300 Subject: [PATCH] [clang][Parse] Fix DeclContext reentry to use the template's lexical TU, not the current fragment's TU Fixes 217073 --- clang/lib/Parse/ParseTemplate.cpp | 20 +++++++++++++------ .../delayed-template-parsing-incremental.cpp | 6 ++++++ 2 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 clang/test/Interpreter/delayed-template-parsing-incremental.cpp diff --git a/clang/lib/Parse/ParseTemplate.cpp b/clang/lib/Parse/ParseTemplate.cpp index 735a9bd1f9f1c..cba4a29888516 100644 --- a/clang/lib/Parse/ParseTemplate.cpp +++ b/clang/lib/Parse/ParseTemplate.cpp @@ -1451,17 +1451,25 @@ void Parser::ParseLateTemplatedFuncDef(LateParsedTemplate &LPT) { // Track template parameter depth. TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); - // To restore the context after late parsing. - Sema::ContextRAII GlobalSavedContext( - Actions, Actions.Context.getTranslationUnitDecl()); - MultiParseScope Scopes(*this); // Get the list of DeclContexts to reenter. SmallVector<DeclContext*, 4> DeclContextsToReenter; - for (DeclContext *DC = FunD; DC && !DC->isTranslationUnit(); - DC = DC->getLexicalParent()) + DeclContext *LexicalTU = nullptr; + for (DeclContext *DC = FunD; DC; DC = DC->getLexicalParent()) { + if (DC->isTranslationUnit()) { + LexicalTU = DC; + break; + } DeclContextsToReenter.push_back(DC); + } + + if (!LexicalTU) { + LexicalTU = Actions.Context.getTranslationUnitDecl(); + } + + // To restore the context after late parsing. + Sema::ContextRAII GlobalSavedContext(Actions, LexicalTU); // Reenter scopes from outermost to innermost. for (DeclContext *DC : reverse(DeclContextsToReenter)) { diff --git a/clang/test/Interpreter/delayed-template-parsing-incremental.cpp b/clang/test/Interpreter/delayed-template-parsing-incremental.cpp new file mode 100644 index 0000000000000..7fc1757e82d1b --- /dev/null +++ b/clang/test/Interpreter/delayed-template-parsing-incremental.cpp @@ -0,0 +1,6 @@ +// REQUIRES: host-supports-jit +// RUN: cat %s | clang-repl -Xcc -fdelayed-template-parsing 2>&1 +// see ISSUE 217073 and PR 218571 + +namespace ns { template <typename T> int f(T) { return 0; } } +ns::f(1); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
