https://github.com/Serosh-commits updated https://github.com/llvm/llvm-project/pull/180407
>From 86dbd5bf64585ab936802d15e8d34a83f5b2a7bb Mon Sep 17 00:00:00 2001 From: Serosh-commits <[email protected]> Date: Sun, 8 Feb 2026 17:40:06 +0530 Subject: [PATCH] [clang][sema] fix crash on builtin template ids with dependent packs (gh180307) --- clang/lib/Sema/SemaTemplate.cpp | 19 +++++++------ .../builtin_templates_invalid_parameters.cpp | 27 +++++++++++++++++++ 2 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 clang/test/SemaCXX/builtin_templates_invalid_parameters.cpp diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp index 3497ff7856eed..5c7e3e60b12a6 100644 --- a/clang/lib/Sema/SemaTemplate.cpp +++ b/clang/lib/Sema/SemaTemplate.cpp @@ -3406,25 +3406,31 @@ static QualType checkBuiltinTemplateIdType( Sema &SemaRef, ElaboratedTypeKeyword Keyword, BuiltinTemplateDecl *BTD, ArrayRef<TemplateArgument> Converted, SourceLocation TemplateLoc, TemplateArgumentListInfo &TemplateArgs) { + if (llvm::any_of(Converted, + [](const TemplateArgument &A) { return A.isDependent(); }) || + Converted.size() < + BTD->getTemplateParameters()->getMinRequiredArguments()) + return QualType(); + ASTContext &Context = SemaRef.getASTContext(); switch (BTD->getBuiltinTemplateKind()) { case BTK__make_integer_seq: { + assert(Converted.size() == 3); // Specializations of __make_integer_seq<S, T, N> are treated like // S<T, 0, ..., N-1>. QualType OrigType = Converted[1].getAsType(); + assert(!OrigType->isDependentType()); // C++14 [inteseq.intseq]p1: // T shall be an integer type. - if (!OrigType->isDependentType() && !OrigType->isIntegralType(Context)) { + if (!OrigType->isIntegralType(Context)) { SemaRef.Diag(TemplateArgs[1].getLocation(), diag::err_integer_sequence_integral_element_type); return QualType(); } TemplateArgument NumArgsArg = Converted[2]; - if (NumArgsArg.isDependent()) - return QualType(); TemplateArgumentListInfo SyntheticTemplateArgs; // The type argument, wrapped in substitution sugar, gets reused as the @@ -3466,8 +3472,6 @@ static QualType checkBuiltinTemplateIdType( "__type_pack_element should be given an index and a parameter pack"); TemplateArgument IndexArg = Converted[0], Ts = Converted[1]; - if (IndexArg.isDependent() || Ts.isDependent()) - return QualType(); llvm::APSInt Index = IndexArg.getAsIntegral(); assert(Index >= 0 && "the index used with __type_pack_element should be of " @@ -3486,8 +3490,6 @@ static QualType checkBuiltinTemplateIdType( case BTK__builtin_common_type: { assert(Converted.size() == 4); - if (llvm::any_of(Converted, [](auto &C) { return C.isDependent(); })) - return QualType(); TemplateName BaseTemplate = Converted[0].getAsTemplate(); ArrayRef<TemplateArgument> Ts = Converted[3].getPackAsArray(); @@ -3514,9 +3516,6 @@ static QualType checkBuiltinTemplateIdType( SemaRef.Diag(TemplateLoc, diag::err_hlsl_spirv_only) << BTD; } - if (llvm::any_of(Converted, [](auto &C) { return C.isDependent(); })) - return QualType(); - uint64_t Opcode = Converted[0].getAsIntegral().getZExtValue(); uint64_t Size = Converted[1].getAsIntegral().getZExtValue(); uint64_t Alignment = Converted[2].getAsIntegral().getZExtValue(); diff --git a/clang/test/SemaCXX/builtin_templates_invalid_parameters.cpp b/clang/test/SemaCXX/builtin_templates_invalid_parameters.cpp new file mode 100644 index 0000000000000..2841f6ac14d04 --- /dev/null +++ b/clang/test/SemaCXX/builtin_templates_invalid_parameters.cpp @@ -0,0 +1,27 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s + +using SizeT = decltype(sizeof(int)); + +// Test insufficient parameters for built-in templates. +// These should be caught even if arguments are dependent. + +template <SizeT... Seq> +void test_type_pack_element_insufficient(__type_pack_element<Seq...>) {} + +template <typename T> +void test_make_integer_seq_insufficient(__make_integer_seq<T>) {} + +template <typename T> +using test_common_type_insufficient = __builtin_common_type<T>; + +// Standard usage checks for non-dependent cases that are still broken. +using t1 = __type_pack_element<0>; // expected-error {{a parameter pack may not be accessed at an out of bounds index}} +using t2 = __make_integer_seq<int>; // expected-error {{too few template arguments for class template '__make_integer_seq'}} +using t3 = __builtin_common_type<int>; // expected-error {{too few template arguments for class template '__builtin_common_type'}} + +// Dependent cases that previously crashed but now return QualType() gracefully. +template <SizeT... Seq> +using gh180307 = __type_pack_element<Seq...>; + +template <typename T> +using gh180307_bis = __make_integer_seq<gh180307, T>; // expected-error {{too few template arguments for class template '__make_integer_seq'}} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
