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

via cfe-commits cfe-commits at lists.llvm.org
Fri May 3 01:22:20 PDT 2024


https://github.com/cor3ntin 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 1/3] [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

>From f1242c3627db8449a78912e8d62ff9f1a2951068 Mon Sep 17 00:00:00 2001
From: knightXun <badgangkiller at gmail.com>
Date: Wed, 13 Dec 2023 08:36:46 +0800
Subject: [PATCH 2/3] fix conflicts

---
 clang/docs/ReleaseNotes.rst | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 456be476a95447..38493aac38a273 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -836,6 +836,9 @@ Bug Fixes to C++ Support
   completes (except deduction guides). Fixes:
   (`#59827 <https://github.com/llvm/llvm-project/issues/59827>`_)
 
+- Fixed crash when parsing inconsistent arguments in variadic template variables. Fixes:
+  `(#70280: <hhttps://github.com/llvm/llvm-project/issues/70191>`_)
+
 - Fix crash when parsing nested requirement. Fixes:
   (`#73112 <https://github.com/llvm/llvm-project/issues/73112>`_)
 
@@ -1069,9 +1072,6 @@ 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

>From 89230fa724fe371c66ea2e46fd8b436babec2004 Mon Sep 17 00:00:00 2001
From: cor3ntin <corentinjabot at gmail.com>
Date: Fri, 3 May 2024 10:22:11 +0200
Subject: [PATCH 3/3] Add EOL

---
 clang/test/SemaCXX/GH70280.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/SemaCXX/GH70280.cpp b/clang/test/SemaCXX/GH70280.cpp
index c7f62ffbf27b35..3a90018fbf174c 100644
--- a/clang/test/SemaCXX/GH70280.cpp
+++ b/clang/test/SemaCXX/GH70280.cpp
@@ -6,4 +6,4 @@ namespace PR70280 {
   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