https://github.com/knightXun updated https://github.com/llvm/llvm-project/pull/70280
>From 23a1a74bfef2c974f2fd99569e124ee4a82bbadf Mon Sep 17 00:00:00 2001 From: knightXun <badgangkil...@gmail.com> Date: Thu, 26 Oct 2023 09:25:58 +0800 Subject: [PATCH] [clang][Sema] Resolving Panic Caused by Inconsistent Arguments in Variadic Template Variables When template variables are variadic, sema may panic, potentially leading to a situation where the number of function variables exceeds the number of template variables. The sema compares the template signature and function signature parameters one by one, which can trigger an assertion error. This PR, when encountering variadic templates, avoids comparing the template signature and function signature parameters one by one. issue: https://github.com/llvm/llvm-project/issues/70191 --- clang/docs/ReleaseNotes.rst | 3 +++ clang/lib/Sema/SemaOverload.cpp | 13 +++++++++++-- clang/test/SemaCXX/GH70280.cpp | 9 +++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 clang/test/SemaCXX/GH70280.cpp diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 4cc148905d4e13..be6161f12ceeea 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -804,6 +804,9 @@ Static Analyzer `#65888 <https://github.com/llvm/llvm-project/pull/65888>`_, and `#65887 <https://github.com/llvm/llvm-project/pull/65887>`_ +- Resolving Inconsistent Arguments Panic in Variadic Template Variables. + (`#70280: <https://github.com/llvm/llvm-project/pull/70280>`_). + .. _release-notes-sanitizers: Sanitizers diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index db386fef0661c0..4e89f4f50b8a6f 100644 --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -11044,6 +11044,14 @@ static void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, I--; } + bool isVariadic = false; + for (unsigned N = 0; N < Fn->getNumParams(); N++) { + if (Fn->getParamDecl(N)->isParameterPack()) { + isVariadic = true; + break; + } + } + std::string FnDesc; std::pair<OverloadCandidateKind, OverloadCandidateSelect> FnKindPair = ClassifyOverloadCandidate(S, Cand->FoundDecl, Fn, Cand->getRewriteKind(), @@ -11052,8 +11060,9 @@ static void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, Expr *FromExpr = Conv.Bad.FromExpr; QualType FromTy = Conv.Bad.getFromType(); QualType ToTy = Conv.Bad.getToType(); - SourceRange ToParamRange = - !isObjectArgument ? Fn->getParamDecl(I)->getSourceRange() : SourceRange(); + SourceRange ToParamRange = !isObjectArgument && !isVariadic + ? Fn->getParamDecl(I)->getSourceRange() + : SourceRange(); if (FromTy == S.Context.OverloadTy) { assert(FromExpr && "overload set argument came from implicit argument?"); diff --git a/clang/test/SemaCXX/GH70280.cpp b/clang/test/SemaCXX/GH70280.cpp new file mode 100644 index 00000000000000..c7f62ffbf27b35 --- /dev/null +++ b/clang/test/SemaCXX/GH70280.cpp @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +namespace PR70280 { + typedef a; // expected-error {{a type specifier is required for all declarations}} + using b = char*; + template <typename... c> void d(c...) = d<b, a>(0, ""); // expected-error {{no matching function for call to 'd'}} + // expected-error@-1 {{illegal initializer (only variables can be initialized)}} + // expected-note@-2 {{candidate function template not viable: no known conversion from 'const char[1]' to 'a' (aka 'int') for 2nd argument}} +} \ No newline at end of file _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits