https://github.com/Serosh-commits updated https://github.com/llvm/llvm-project/pull/190484
>From b718513c1c1617f2b0f3fd941fc4cba68083cb8c Mon Sep 17 00:00:00 2001 From: Serosh-commits <[email protected]> Date: Sat, 25 Apr 2026 18:36:59 +0530 Subject: [PATCH 1/4] [Clang] Fix a name lookup crash when parsing invalid template parameters (#GH189344) --- clang/docs/ReleaseNotes.rst | 2 ++ clang/lib/Sema/SemaLookup.cpp | 5 +---- clang/test/SemaCXX/gh189344.cpp | 12 ++++++++++++ 3 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 clang/test/SemaCXX/gh189344.cpp diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index f8508ebefb199..752e37765a484 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -520,6 +520,8 @@ Bug Fixes to Attribute Support Bug Fixes to C++ Support ^^^^^^^^^^^^^^^^^^^^^^^^ + +- Fixed a name lookup crash when parsing invalid template parameters. (#GH189344) - Fixed a crash when a function template is defined as a non-template friend with a global scope qualifier. (#GH185341) - Clang now rejects constant template parameters with block pointer types, since these are not implemented anyway and would lead to crashes. (#GH189247) - Fixed a crash on error recovery when dealing with invalid templates. (#GH183075) diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp index b96065f8619d2..9ce64ed85540b 100644 --- a/clang/lib/Sema/SemaLookup.cpp +++ b/clang/lib/Sema/SemaLookup.cpp @@ -1528,10 +1528,7 @@ bool Sema::CppLookupName(LookupResult &R, Scope *S) { // If we have a context, and it's not a context stashed in the // template parameter scope for an out-of-line definition, also // look into that context. - if (!(Found && S->isTemplateParamScope())) { - assert(Ctx->isFileContext() && - "We should have been looking only at file context here already."); - + if (Ctx->isFileContext() && !(Found && S->isTemplateParamScope())) { // Look into context considering using-directives. if (CppNamespaceLookup(*this, R, Context, Ctx, UDirs)) Found = true; diff --git a/clang/test/SemaCXX/gh189344.cpp b/clang/test/SemaCXX/gh189344.cpp new file mode 100644 index 0000000000000..f656c1173c2ac --- /dev/null +++ b/clang/test/SemaCXX/gh189344.cpp @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +template <union { } alignas ( union a { c ) ( a :: ) ( :: (b +// expected-error@-1 {{cannot be defined in a type specifier}} +// expected-error@-2 {{'a' cannot be defined in a type specifier}} +// expected-error@-3 {{unknown type name 'b'}} +// expected-error@-4 2{{parameter declarator cannot be qualified}} +// expected-error@-5 {{a type specifier is required for all declarations}} +// expected-note@-6 2{{to match this '('}} +// expected-error@* 3{{expected unqualified-id}} +// expected-error@* 2{{expected ')'}} +// expected-error@* {{expected ',' or '>' in template-parameter-list}} >From 5e70ddf052e37fcc2fa4755e06270fd2c73d760c Mon Sep 17 00:00:00 2001 From: Serosh-commits <[email protected]> Date: Mon, 25 May 2026 16:34:00 +0530 Subject: [PATCH 2/4] address feedback --- clang/lib/Sema/SemaDecl.cpp | 5 ++++- clang/lib/Sema/SemaLookup.cpp | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 36538e18f297c..2f9f03d2c34b0 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -1489,6 +1489,7 @@ void Sema::EnterTemplatedContext(Scope *S, DeclContext *DC) { unsigned ScopeDepth = getTemplateDepth(S); for (; S && S->isTemplateParamScope(); S = S->getParent(), --ScopeDepth) { DeclContext *SearchDCAfterScope = DC; + bool SetLookupEntity = false; for (; DC; DC = DC->getLookupParent()) { if (const TemplateParameterList *TPL = cast<Decl>(DC)->getDescribedTemplateParams()) { @@ -1497,10 +1498,12 @@ void Sema::EnterTemplatedContext(Scope *S, DeclContext *DC) { continue; if (ScopeDepth == DCDepth) SearchDCAfterScope = DC = DC->getLookupParent(); + SetLookupEntity = true; break; } } - S->setLookupEntity(SearchDCAfterScope); + if (SetLookupEntity) + S->setLookupEntity(SearchDCAfterScope); } } diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp index 9ce64ed85540b..b96065f8619d2 100644 --- a/clang/lib/Sema/SemaLookup.cpp +++ b/clang/lib/Sema/SemaLookup.cpp @@ -1528,7 +1528,10 @@ bool Sema::CppLookupName(LookupResult &R, Scope *S) { // If we have a context, and it's not a context stashed in the // template parameter scope for an out-of-line definition, also // look into that context. - if (Ctx->isFileContext() && !(Found && S->isTemplateParamScope())) { + if (!(Found && S->isTemplateParamScope())) { + assert(Ctx->isFileContext() && + "We should have been looking only at file context here already."); + // Look into context considering using-directives. if (CppNamespaceLookup(*this, R, Context, Ctx, UDirs)) Found = true; >From 83d2d3095703304570ca6abe7690d913eabaae39 Mon Sep 17 00:00:00 2001 From: Serosh-commits <[email protected]> Date: Mon, 25 May 2026 18:12:25 +0530 Subject: [PATCH 3/4] fix the mecha properly to prevent the bad union stash --- clang/lib/Sema/SemaDecl.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 2f9f03d2c34b0..410ba470b3834 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -1489,7 +1489,7 @@ void Sema::EnterTemplatedContext(Scope *S, DeclContext *DC) { unsigned ScopeDepth = getTemplateDepth(S); for (; S && S->isTemplateParamScope(); S = S->getParent(), --ScopeDepth) { DeclContext *SearchDCAfterScope = DC; - bool SetLookupEntity = false; + bool FoundTemplateContext = false; for (; DC; DC = DC->getLookupParent()) { if (const TemplateParameterList *TPL = cast<Decl>(DC)->getDescribedTemplateParams()) { @@ -1498,12 +1498,19 @@ void Sema::EnterTemplatedContext(Scope *S, DeclContext *DC) { continue; if (ScopeDepth == DCDepth) SearchDCAfterScope = DC = DC->getLookupParent(); - SetLookupEntity = true; + FoundTemplateContext = true; break; } } - if (SetLookupEntity) - S->setLookupEntity(SearchDCAfterScope); + if (!SearchDCAfterScope) + continue; + + if (!FoundTemplateContext && !SearchDCAfterScope->isFileContext()) { + if (const auto *RD = dyn_cast<RecordDecl>(SearchDCAfterScope)) + if (RD->isUnion() && !RD->isTemplateDecl()) + continue; + } + S->setLookupEntity(SearchDCAfterScope); } } >From 1ca40760d0f8d56bfef8f4bac9c58e2c4317b399 Mon Sep 17 00:00:00 2001 From: Serosh-commits <[email protected]> Date: Mon, 25 May 2026 19:10:08 +0530 Subject: [PATCH 4/4] nit --- clang/lib/Sema/SemaDecl.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 410ba470b3834..fab0f56324539 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -1505,10 +1505,11 @@ void Sema::EnterTemplatedContext(Scope *S, DeclContext *DC) { if (!SearchDCAfterScope) continue; - if (!FoundTemplateContext && !SearchDCAfterScope->isFileContext()) { - if (const auto *RD = dyn_cast<RecordDecl>(SearchDCAfterScope)) - if (RD->isUnion() && !RD->isTemplateDecl()) - continue; + if (!FoundTemplateContext && !SearchDCAfterScope->isFileContext() && + isa<RecordDecl>(SearchDCAfterScope)) { + const auto *RD = cast<RecordDecl>(SearchDCAfterScope); + if (RD->isInvalidDecl() && !RD->isTemplateDecl()) + continue; } S->setLookupEntity(SearchDCAfterScope); } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
