[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:06:00 PST 2023


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

>From 23a1a74bfef2c974f2fd99569e124ee4a82bbadf Mon Sep 17 00:00:00 2001
From: knightXun <badgangkiller at gmail.com>
Date: Thu, 26 Oct 2023 09:25:58 +0800
Subject: [PATCH 1/2] [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 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

>From 6ef0719dcfd3f82d779cf45e45c17e23ee4e6fb6 Mon Sep 17 00:00:00 2001
From: knightXun <badgangkiller at gmail.com>
Date: Tue, 12 Dec 2023 15:05:46 +0800
Subject: [PATCH 2/2] fix format

---
 clang/include/clang/Basic/DiagnosticParseKinds.td | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index d6652e6a610c1b..902fbf4a0d692c 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -1349,7 +1349,7 @@ def warn_omp_extra_tokens_at_eol : Warning<
   "extra tokens at the end of '#pragma omp %0' are ignored">,
   InGroup<ExtraTokens>;
 def err_omp_multiple_step_or_linear_modifier : Error<
-  "multiple %select{'step size'|'linear modifier'}0 found in linear clause">; 
+  "multiple %select{'step size'|'linear modifier'}0 found in linear clause">;
 def warn_pragma_expected_colon_r_paren : Warning<
   "missing ':' or ')' after %0 - ignoring">, InGroup<IgnoredPragmas>;
 def err_omp_unknown_directive : Error<



More information about the cfe-commits mailing list