https://github.com/daniel-petrovic updated https://github.com/llvm/llvm-project/pull/242618
>From 8768d5273a458905b2513254d733d014f6de3bfa Mon Sep 17 00:00:00 2001 From: Daniel Petrovic <[email protected]> Date: Sun, 15 Nov 2026 18:46:14 +0100 Subject: [PATCH] [clang] Avoid stack overflows in recursive ADL and diagnostics Use the existing ADL worklist to traverse template argument types. Abbreviate nested types in TypePrinter when stack space runs low. Fixes #242311 --- clang/docs/ReleaseNotes.md | 3 +++ clang/lib/AST/TypePrinter.cpp | 22 ++++++++++++++------ clang/lib/Sema/SemaLookup.cpp | 16 +++++++------- clang/test/SemaTemplate/stack-exhaustion.cpp | 13 ++++++++++++ 4 files changed, 41 insertions(+), 13 deletions(-) diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index aab88904167abc..76659c62d55d3c 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -1035,6 +1035,9 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and type. (#GH204756) #### Bug Fixes to C++ Support +- Fixed stack overflows during recursive argument-dependent lookup and while + printing deeply nested template arguments in diagnostics. (#GH242311) + - Clang now recovers from invalid single pointer declarators in structured bindings (such as ``auto *[x, y] = s;``) by removing the pointer declarator. This allows type deduction to proceed and avoids redundant follow-up diff --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp index a90b2d2c0cb94d..47fcc96ec20830 100644 --- a/clang/lib/AST/TypePrinter.cpp +++ b/clang/lib/AST/TypePrinter.cpp @@ -32,6 +32,7 @@ #include "clang/Basic/SourceLocation.h" #include "clang/Basic/SourceManager.h" #include "clang/Basic/Specifiers.h" +#include "clang/Basic/Stack.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/SmallString.h" @@ -192,6 +193,13 @@ void TypePrinter::print(const Type *T, Qualifiers Quals, raw_ostream &OS, return; } + if (isStackNearlyExhausted()) { + OS << "..."; + if (!PlaceHolder.empty()) + OS << ' ' << PlaceHolder; + return; + } + SaveAndRestore PHVal(HasEmptyPlaceHolder, PlaceHolder.empty()); printBefore(T, Quals, OS); @@ -2572,14 +2580,16 @@ printTo(raw_ostream &OS, ArrayRef<TA> Args, const PrintingPolicy &Policy, SmallString<128> Buf; llvm::raw_svector_ostream ArgOS(Buf); const TemplateArgument &Argument = getArgument(Arg); - if (Argument.getKind() == TemplateArgument::Pack) { - if (Argument.pack_size() && !FirstArg) - OS << Comma; + bool IsArgumentPack = Argument.getKind() == TemplateArgument::Pack; + bool HasArgument = !IsArgumentPack || Argument.pack_size() != 0; + if (HasArgument && !FirstArg) + OS << Comma; + if (HasArgument && isStackNearlyExhausted()) { + ArgOS << "..."; + } else if (IsArgumentPack && HasArgument) { printTo(ArgOS, Argument.getPackAsArray(), Policy, TPL, /*IsPack*/ true, ParmIndex); - } else { - if (!FirstArg) - OS << Comma; + } else if (!IsArgumentPack) { // Tries to print the argument with location info if exists. printArgument(Arg, Policy, ArgOS, TemplateParameterList::shouldIncludeTypeForArgument( diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp index 5e061103437670..d20aea52ad03bd 100644 --- a/clang/lib/Sema/SemaLookup.cpp +++ b/clang/lib/Sema/SemaLookup.cpp @@ -3019,7 +3019,8 @@ static void CollectEnclosingNamespace(Sema::AssociatedNamespaceSet &Namespaces, // lookup that involves a template argument (C++ [basic.lookup.argdep]p2). static void addAssociatedClassesAndNamespaces(AssociatedLookup &Result, - const TemplateArgument &Arg) { + const TemplateArgument &Arg, + SmallVectorImpl<const Type *> &Queue) { // C++ [basic.lookup.argdep]p2, last bullet: // -- [...] ; switch (Arg.getKind()) { @@ -3030,7 +3031,7 @@ addAssociatedClassesAndNamespaces(AssociatedLookup &Result, // [...] the namespaces and classes associated with the types of the // template arguments provided for template type parameters (excluding // template template parameters) - addAssociatedClassesAndNamespaces(Result, Arg.getAsType()); + Queue.push_back(Arg.getAsType()->getCanonicalTypeInternal().getTypePtr()); break; case TemplateArgument::Template: @@ -3061,7 +3062,7 @@ addAssociatedClassesAndNamespaces(AssociatedLookup &Result, case TemplateArgument::Pack: for (const auto &P : Arg.pack_elements()) - addAssociatedClassesAndNamespaces(Result, P); + addAssociatedClassesAndNamespaces(Result, P, Queue); break; } } @@ -3070,7 +3071,8 @@ addAssociatedClassesAndNamespaces(AssociatedLookup &Result, // with an argument of class type (C++ [basic.lookup.argdep]p2). static void addAssociatedClassesAndNamespaces(AssociatedLookup &Result, - CXXRecordDecl *Class) { + CXXRecordDecl *Class, + SmallVectorImpl<const Type *> &Queue) { // Just silently ignore anything whose name is __va_list_tag. if (Class->getDeclName() == Result.S.VAListTagName) @@ -3115,7 +3117,7 @@ addAssociatedClassesAndNamespaces(AssociatedLookup &Result, const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) - addAssociatedClassesAndNamespaces(Result, TemplateArgs[I]); + addAssociatedClassesAndNamespaces(Result, TemplateArgs[I], Queue); } // Add the class itself. If we've already transitively visited this class, @@ -3220,7 +3222,7 @@ addAssociatedClassesAndNamespaces(AssociatedLookup &Result, QualType Ty) { // FIXME: This should use the original decl. auto *Class = cast<CXXRecordDecl>(cast<RecordType>(T)->getDecl()) ->getDefinitionOrSelf(); - addAssociatedClassesAndNamespaces(Result, Class); + addAssociatedClassesAndNamespaces(Result, Class, Queue); break; } @@ -3273,7 +3275,7 @@ addAssociatedClassesAndNamespaces(AssociatedLookup &Result, QualType Ty) { case Type::MemberPointer: { const MemberPointerType *MemberPtr = cast<MemberPointerType>(T); if (CXXRecordDecl *Class = MemberPtr->getMostRecentCXXRecordDecl()) - addAssociatedClassesAndNamespaces(Result, Class); + addAssociatedClassesAndNamespaces(Result, Class, Queue); T = MemberPtr->getPointeeType().getTypePtr(); continue; } diff --git a/clang/test/SemaTemplate/stack-exhaustion.cpp b/clang/test/SemaTemplate/stack-exhaustion.cpp index 61014c7724ce2b..fa60a5e66314c4 100644 --- a/clang/test/SemaTemplate/stack-exhaustion.cpp +++ b/clang/test/SemaTemplate/stack-exhaustion.cpp @@ -4,6 +4,7 @@ // RUN: %clang_cc1 -verify %s -DTEST=1 -fexperimental-new-constant-interpreter // RUN: %clang_cc1 -verify %s -DTEST=2 -fexperimental-new-constant-interpreter // RUN: %clang_cc1 -verify %s -DTEST=3 -fexperimental-new-constant-interpreter +// RUN: not %clang_cc1 -fsyntax-only -ftemplate-backtrace-limit=1 %s -DTEST=4 2>&1 | FileCheck %s // REQUIRES: thread_support @@ -64,6 +65,18 @@ namespace template_parameter_type_recursion { void printFunctionalType(ostream &os, mlir::Value &v) { os << v; } } +#elif TEST == 4 + +// Check that Clang can print the nested binding after reaching the depth limit. +// The innermost type may be abbreviated depending on stack usage. +// CHECK: fatal error: recursive template instantiation exceeded maximum depth +// CHECK: note: while substituting deduced template arguments into function template 'g' [with T = {{(A<)+(\.\.\.|(std::)?nullptr_t)>+( \*>)* \*}}] + +typedef decltype(nullptr) nullptr_t; +template <class T> struct A {}; +template <typename T> auto g(T t) -> decltype(g(new A<T>)) { return nullptr; } +template <> nullptr_t g(A<nullptr_t>) { return nullptr; } + #else #error unknown test #endif _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
