[clang] [clang][Sema] Resolving Inconsistent Arguments Panic in Variadic Template Variables (PR #70280)

via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 11 23:07:15 PST 2023


https://github.com/knightXun updated https://github.com/llvm/llvm-project/pull/70280

>From 95180e1765fea3ec6de822d0b9926056d0d12404 Mon Sep 17 00:00:00 2001
From: knightXun <badgangkiller at 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 d6719680d2ac70..456be476a95447 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -1069,6 +1069,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 5026e1d603e5ee..320f53a7159f3e 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -11092,6 +11092,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(),
@@ -11100,8 +11108,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 at -1 {{illegal initializer (only variables can be initialized)}}
+  // expected-note at -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



More information about the cfe-commits mailing list