[clang] [Clang][Sema] Check the number of lambda non-concept tempate parameters (PR #74885)

via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 9 06:44:41 PST 2023


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

>From 12cc1fe332fbab94c1368ea95374d5a1289a22f8 Mon Sep 17 00:00:00 2001
From: knightXun <badgangkiller at gmail.com>
Date: Sat, 9 Dec 2023 04:57:15 +0800
Subject: [PATCH 1/4] [Clang][Sema] Check the number of lambda non-concept
 tempate parameters Check that the number of non-concept template parameters
 is greater than zero during lambda template instantiation to aviod panic Fix
 issue: https://github.com/llvm/llvm-project/issues/70601

---
 .../include/clang/Basic/DiagnosticParseKinds.td  |  2 ++
 clang/lib/Sema/TreeTransform.h                   | 16 ++++++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 6150fc36430ab1..e46fa69d013b61 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -851,6 +851,8 @@ def err_friend_explicit_instantiation : Error<
 def err_explicit_instantiation_enum : Error<
   "enumerations cannot be explicitly instantiated">;
 def err_expected_template_parameter : Error<"expected template parameter">;
+def err_expected_non_concept_template_parameter : Error<
+  "expected non-concept template parameter">;
 def err_empty_requires_expr : Error<
   "a requires expression must contain at least one requirement">;
 def err_requires_expr_parameter_list_ellipsis : Error<
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 1ad843d0bf4e0c..a140bbbc0c43d5 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -13662,6 +13662,22 @@ TreeTransform<Derived>::TransformLambdaExpr(LambdaExpr *E) {
     return ExprError();
   }
 
+  // Check the number of the Concept template parameters
+  size_t conceptParams = 0;
+  for (auto P : *E->getTemplateParameterList()) {
+    const TemplateTypeParmDecl *CD = dyn_cast<TemplateTypeParmDecl>(P);
+    if (CD && CD->hasTypeConstraint()) {
+      conceptParams++;
+    }
+  }
+
+  if (conceptParams > 0 &&
+      conceptParams == E->getTemplateParameterList()->size()) {
+    getSema().Diag(E->getTemplateParameterList()->getLAngleLoc(),
+                   diag::err_expected_non_concept_template_parameter);
+    return ExprError();
+  }
+
   // Copy the LSI before ActOnFinishFunctionBody removes it.
   // FIXME: This is dumb. Store the lambda information somewhere that outlives
   // the call operator.

>From e53ed4531d6918384be40de2b76bea558eac06ea Mon Sep 17 00:00:00 2001
From: knightXun <badgangkiller at gmail.com>
Date: Sat, 9 Dec 2023 08:38:23 +0800
Subject: [PATCH 2/4] fix ut

---
 clang/lib/Sema/TreeTransform.h | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)

diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index a140bbbc0c43d5..9fb426c1a044b9 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -13594,6 +13594,24 @@ TreeTransform<Derived>::TransformLambdaExpr(LambdaExpr *E) {
     getSema().AddTemplateParametersToLambdaCallOperator(NewCallOperator, Class,
                                                         TPL);
 
+  if (E->getTemplateParameterList()) {
+    // Check the number of the Concept template parameters
+    size_t conceptParams = 0;
+    for (auto P : *E->getTemplateParameterList()) {
+      const TemplateTypeParmDecl *CD = dyn_cast<TemplateTypeParmDecl>(P);
+      if (CD && CD->hasTypeConstraint()) {
+        conceptParams++;
+      }
+    }
+
+    if (conceptParams > 0 &&
+        conceptParams == E->getTemplateParameterList()->size()) {
+      getSema().Diag(E->getTemplateParameterList()->getLAngleLoc(),
+                     diag::err_expected_non_concept_template_parameter);
+      return ExprError();
+    }
+  }
+
   // Transform the type of the original lambda's call operator.
   // The transformation MUST be done in the CurrentInstantiationScope since
   // it introduces a mapping of the original to the newly created
@@ -13662,22 +13680,6 @@ TreeTransform<Derived>::TransformLambdaExpr(LambdaExpr *E) {
     return ExprError();
   }
 
-  // Check the number of the Concept template parameters
-  size_t conceptParams = 0;
-  for (auto P : *E->getTemplateParameterList()) {
-    const TemplateTypeParmDecl *CD = dyn_cast<TemplateTypeParmDecl>(P);
-    if (CD && CD->hasTypeConstraint()) {
-      conceptParams++;
-    }
-  }
-
-  if (conceptParams > 0 &&
-      conceptParams == E->getTemplateParameterList()->size()) {
-    getSema().Diag(E->getTemplateParameterList()->getLAngleLoc(),
-                   diag::err_expected_non_concept_template_parameter);
-    return ExprError();
-  }
-
   // Copy the LSI before ActOnFinishFunctionBody removes it.
   // FIXME: This is dumb. Store the lambda information somewhere that outlives
   // the call operator.

>From 7d2c8743d1e28342e3e6ac44424d789bba4e698b Mon Sep 17 00:00:00 2001
From: knightXun <badgangkiller at gmail.com>
Date: Sat, 9 Dec 2023 21:40:36 +0800
Subject: [PATCH 3/4] fx

---
 clang/include/clang/Basic/DiagnosticParseKinds.td | 2 --
 clang/lib/Sema/TreeTransform.h                    | 2 --
 2 files changed, 4 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index e46fa69d013b61..6150fc36430ab1 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -851,8 +851,6 @@ def err_friend_explicit_instantiation : Error<
 def err_explicit_instantiation_enum : Error<
   "enumerations cannot be explicitly instantiated">;
 def err_expected_template_parameter : Error<"expected template parameter">;
-def err_expected_non_concept_template_parameter : Error<
-  "expected non-concept template parameter">;
 def err_empty_requires_expr : Error<
   "a requires expression must contain at least one requirement">;
 def err_requires_expr_parameter_list_ellipsis : Error<
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 9fb426c1a044b9..82be2c800ff7ae 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -13606,8 +13606,6 @@ TreeTransform<Derived>::TransformLambdaExpr(LambdaExpr *E) {
 
     if (conceptParams > 0 &&
         conceptParams == E->getTemplateParameterList()->size()) {
-      getSema().Diag(E->getTemplateParameterList()->getLAngleLoc(),
-                     diag::err_expected_non_concept_template_parameter);
       return ExprError();
     }
   }

>From 6680a4d1a47d69f37286268106919eafe34d61d9 Mon Sep 17 00:00:00 2001
From: knightXun <badgangkiller at gmail.com>
Date: Sat, 9 Dec 2023 22:44:27 +0800
Subject: [PATCH 4/4] try -to fix

---
 clang/lib/Sema/TreeTransform.h | 51 ++++++++++++++++++++++++----------
 1 file changed, 36 insertions(+), 15 deletions(-)

diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 82be2c800ff7ae..005e174ea8b1c8 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -6439,6 +6439,25 @@ QualType TreeTransform<Derived>::TransformDecltypeType(TypeLocBuilder &TLB,
       SemaRef, Sema::ExpressionEvaluationContext::Unevaluated, nullptr,
       Sema::ExpressionEvaluationContextRecord::EK_Decltype);
 
+  auto EE = T->getUnderlyingExpr();
+  if (EE->isInstantiationDependent()) {
+    // Check the number of the Concept template parameters
+    size_t conceptParams = 0;
+    if (auto lambdaExpr = dyn_cast<LambdaExpr>(EE)) {
+      for (auto P : *lambdaExpr->getTemplateParameterList()) {
+        const TemplateTypeParmDecl *CD = dyn_cast<TemplateTypeParmDecl>(P);
+        if (CD && CD->hasTypeConstraint()) {
+          conceptParams++;
+        }
+      }
+
+      if (conceptParams > 0 &&
+          conceptParams == lambdaExpr->getTemplateParameterList()->size()) {
+        return QualType();
+      }
+    }
+  }
+
   ExprResult E = getDerived().TransformExpr(T->getUnderlyingExpr());
   if (E.isInvalid())
     return QualType();
@@ -13594,21 +13613,23 @@ TreeTransform<Derived>::TransformLambdaExpr(LambdaExpr *E) {
     getSema().AddTemplateParametersToLambdaCallOperator(NewCallOperator, Class,
                                                         TPL);
 
-  if (E->getTemplateParameterList()) {
-    // Check the number of the Concept template parameters
-    size_t conceptParams = 0;
-    for (auto P : *E->getTemplateParameterList()) {
-      const TemplateTypeParmDecl *CD = dyn_cast<TemplateTypeParmDecl>(P);
-      if (CD && CD->hasTypeConstraint()) {
-        conceptParams++;
-      }
-    }
-
-    if (conceptParams > 0 &&
-        conceptParams == E->getTemplateParameterList()->size()) {
-      return ExprError();
-    }
-  }
+  // if (E->getTemplateParameterList()) {
+  //   // Check the number of the Concept template parameters
+  //   size_t conceptParams = 0;
+  //   for (auto P : *E->getTemplateParameterList()) {
+  //     const TemplateTypeParmDecl *CD = dyn_cast<TemplateTypeParmDecl>(P);
+  //     if (CD && CD->hasTypeConstraint()) {
+  //       conceptParams++;
+  //     }
+  //   }
+
+  //   if (conceptParams > 0 &&
+  //       conceptParams == E->getTemplateParameterList()->size()) {
+  //     getSema().Diag(E->getTemplateParameterList()->getLAngleLoc(),
+  //                    diag::err_expected_non_concept_template_parameter);
+  //     return ExprError();
+  //   }
+  // }
 
   // Transform the type of the original lambda's call operator.
   // The transformation MUST be done in the CurrentInstantiationScope since



More information about the cfe-commits mailing list