[clang] [clang][Sema] Improve `collectViableConversionCandidates` (PR #97908)

via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 11 11:58:18 PDT 2024


https://github.com/MagentaTreehouse updated https://github.com/llvm/llvm-project/pull/97908

>From def9d161e4ea79ee1b24a8d662a5c0dd17c2dd8f Mon Sep 17 00:00:00 2001
From: Mingyi Chen <cmingyi01 at gmail.com>
Date: Sat, 6 Jul 2024 15:41:08 -0400
Subject: [PATCH 1/3] [clang][Sema] Improve `collectViableConversionCandidates`

* Use range-based for
* The value of `Conv` is not used when `ConvTemplate` is not null, so we do not need to compute it on that path
---
 clang/lib/Sema/SemaOverload.cpp | 26 ++++++++++----------------
 1 file changed, 10 insertions(+), 16 deletions(-)

diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 5ea6b06121c7cd..95c433dab4da43 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -6504,29 +6504,23 @@ static void
 collectViableConversionCandidates(Sema &SemaRef, Expr *From, QualType ToType,
                                   UnresolvedSetImpl &ViableConversions,
                                   OverloadCandidateSet &CandidateSet) {
-  for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) {
-    DeclAccessPair FoundDecl = ViableConversions[I];
+  for (DeclAccessPair FoundDecl : ViableConversions.pairs()) {
     NamedDecl *D = FoundDecl.getDecl();
     CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext());
     if (isa<UsingShadowDecl>(D))
       D = cast<UsingShadowDecl>(D)->getTargetDecl();
 
-    CXXConversionDecl *Conv;
-    FunctionTemplateDecl *ConvTemplate;
-    if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D)))
-      Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl());
-    else
-      Conv = cast<CXXConversionDecl>(D);
-
-    if (ConvTemplate)
+    if (auto *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D)) {
       SemaRef.AddTemplateConversionCandidate(
           ConvTemplate, FoundDecl, ActingContext, From, ToType, CandidateSet,
-          /*AllowObjCConversionOnExplicit=*/false, /*AllowExplicit*/ true);
-    else
-      SemaRef.AddConversionCandidate(Conv, FoundDecl, ActingContext, From,
-                                     ToType, CandidateSet,
-                                     /*AllowObjCConversionOnExplicit=*/false,
-                                     /*AllowExplicit*/ true);
+          /*AllowObjCConversionOnExplicit=*/false, /*AllowExplicit=*/true);
+      continue;
+    }
+    CXXConversionDecl *Conv = cast<CXXConversionDecl>(D);
+    SemaRef.AddConversionCandidate(Conv, FoundDecl, ActingContext, From, ToType,
+                                   CandidateSet,
+                                   /*AllowObjCConversionOnExplicit=*/false,
+                                   /*AllowExplicit=*/true);
   }
 }
 

>From 55267b7cfa948d62620fcf12f6ac4afea9a6f3ed Mon Sep 17 00:00:00 2001
From: Mingyi Chen <cmingyi01 at gmail.com>
Date: Sat, 6 Jul 2024 16:41:57 -0400
Subject: [PATCH 2/3] Format

---
 clang/lib/Sema/SemaOverload.cpp | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 95c433dab4da43..f63495731315eb 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -6517,10 +6517,9 @@ collectViableConversionCandidates(Sema &SemaRef, Expr *From, QualType ToType,
       continue;
     }
     CXXConversionDecl *Conv = cast<CXXConversionDecl>(D);
-    SemaRef.AddConversionCandidate(Conv, FoundDecl, ActingContext, From, ToType,
-                                   CandidateSet,
-                                   /*AllowObjCConversionOnExplicit=*/false,
-                                   /*AllowExplicit=*/true);
+    SemaRef.AddConversionCandidate(
+        Conv, FoundDecl, ActingContext, From, ToType, CandidateSet,
+        /*AllowObjCConversionOnExplicit=*/false, /*AllowExplicit=*/true);
   }
 }
 

>From 22ca93a4814998160e8ac8c27410733861cd0d16 Mon Sep 17 00:00:00 2001
From: MagentaTreehouse <99200384+MagentaTreehouse at users.noreply.github.com>
Date: Wed, 11 Sep 2024 14:58:09 -0400
Subject: [PATCH 3/3] Use `const DeclAccessPair &`

---
 clang/lib/Sema/SemaOverload.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index f63495731315eb..cb1ad01beccad3 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -6504,7 +6504,7 @@ static void
 collectViableConversionCandidates(Sema &SemaRef, Expr *From, QualType ToType,
                                   UnresolvedSetImpl &ViableConversions,
                                   OverloadCandidateSet &CandidateSet) {
-  for (DeclAccessPair FoundDecl : ViableConversions.pairs()) {
+  for (const DeclAccessPair &FoundDecl : ViableConversions.pairs()) {
     NamedDecl *D = FoundDecl.getDecl();
     CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext());
     if (isa<UsingShadowDecl>(D))



More information about the cfe-commits mailing list