[clang] [clang][Sema] Generalize HeuristicResolver::resolveTypeToRecordDecl() to resolveTypeToTagDecl() (PR #124699)
Nathan Ridge via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 27 22:06:51 PST 2025
https://github.com/HighCommander4 updated https://github.com/llvm/llvm-project/pull/124699
>From 24eab93061adf9a241a0b0b418ea0750e2b66b9d Mon Sep 17 00:00:00 2001
From: Nathan Ridge <zeratul976 at hotmail.com>
Date: Mon, 27 Jan 2025 01:22:31 -0500
Subject: [PATCH] [clang][Sema] Generalize
HeuristicResolver::resolveTypeToRecordDecl() to resolveTypeToTagDecl()
---
clang/lib/Sema/HeuristicResolver.cpp | 32 +++++++++++--------
.../unittests/Sema/HeuristicResolverTest.cpp | 19 +++++++++++
2 files changed, 37 insertions(+), 14 deletions(-)
diff --git a/clang/lib/Sema/HeuristicResolver.cpp b/clang/lib/Sema/HeuristicResolver.cpp
index 947cf3f2f0a02b..0ebeb9e66e053a 100644
--- a/clang/lib/Sema/HeuristicResolver.cpp
+++ b/clang/lib/Sema/HeuristicResolver.cpp
@@ -71,8 +71,8 @@ class HeuristicResolverImpl {
// Helper function for HeuristicResolver::resolveDependentMember()
// which takes a possibly-dependent type `T` and heuristically
- // resolves it to a CXXRecordDecl in which we can try name lookup.
- CXXRecordDecl *resolveTypeToRecordDecl(const Type *T);
+ // resolves it to a TagDecl in which we can try name lookup.
+ TagDecl *resolveTypeToTagDecl(const Type *T);
// This is a reimplementation of CXXRecordDecl::lookupDependentName()
// so that the implementation can call into other HeuristicResolver helpers.
@@ -130,7 +130,7 @@ TemplateName getReferencedTemplateName(const Type *T) {
// Helper function for HeuristicResolver::resolveDependentMember()
// which takes a possibly-dependent type `T` and heuristically
// resolves it to a CXXRecordDecl in which we can try name lookup.
-CXXRecordDecl *HeuristicResolverImpl::resolveTypeToRecordDecl(const Type *T) {
+TagDecl *HeuristicResolverImpl::resolveTypeToTagDecl(const Type *T) {
assert(T);
// Unwrap type sugar such as type aliases.
@@ -144,8 +144,9 @@ CXXRecordDecl *HeuristicResolverImpl::resolveTypeToRecordDecl(const Type *T) {
T = T->getCanonicalTypeInternal().getTypePtr();
}
- if (const auto *RT = T->getAs<RecordType>())
- return dyn_cast<CXXRecordDecl>(RT->getDecl());
+ if (auto *TT = T->getAs<TagType>()) {
+ return TT->getDecl();
+ }
if (const auto *ICNT = T->getAs<InjectedClassNameType>())
T = ICNT->getInjectedSpecializationType().getTypePtrOrNull();
@@ -278,7 +279,7 @@ HeuristicResolverImpl::resolveTypeOfCallExpr(const CallExpr *CE) {
CalleeType = FnTypePtr->getPointeeType();
if (const FunctionType *FnType = CalleeType->getAs<FunctionType>()) {
if (const auto *D =
- resolveTypeToRecordDecl(FnType->getReturnType().getTypePtr())) {
+ resolveTypeToTagDecl(FnType->getReturnType().getTypePtr())) {
return {D};
}
}
@@ -389,11 +390,11 @@ bool findOrdinaryMember(const CXXRecordDecl *RD, CXXBasePath &Path,
bool HeuristicResolverImpl::findOrdinaryMemberInDependentClasses(
const CXXBaseSpecifier *Specifier, CXXBasePath &Path,
DeclarationName Name) {
- CXXRecordDecl *RD =
- resolveTypeToRecordDecl(Specifier->getType().getTypePtr());
- if (!RD)
- return false;
- return findOrdinaryMember(RD, Path, Name);
+ TagDecl *TD = resolveTypeToTagDecl(Specifier->getType().getTypePtr());
+ if (const auto *RD = dyn_cast_if_present<CXXRecordDecl>(TD)) {
+ return findOrdinaryMember(RD, Path, Name);
+ }
+ return false;
}
std::vector<const NamedDecl *> HeuristicResolverImpl::lookupDependentName(
@@ -435,11 +436,14 @@ std::vector<const NamedDecl *> HeuristicResolverImpl::resolveDependentMember(
const Type *T = QT.getTypePtrOrNull();
if (!T)
return {};
- if (auto *ET = T->getAs<EnumType>()) {
- auto Result = ET->getDecl()->lookup(Name);
+ TagDecl *TD = resolveTypeToTagDecl(T);
+ if (!TD)
+ return {};
+ if (auto *ED = dyn_cast<EnumDecl>(TD)) {
+ auto Result = ED->lookup(Name);
return {Result.begin(), Result.end()};
}
- if (auto *RD = resolveTypeToRecordDecl(T)) {
+ if (auto *RD = dyn_cast<CXXRecordDecl>(TD)) {
if (!RD->hasDefinition())
return {};
RD = RD->getDefinition();
diff --git a/clang/unittests/Sema/HeuristicResolverTest.cpp b/clang/unittests/Sema/HeuristicResolverTest.cpp
index e5cd1254d75422..473eca669caabc 100644
--- a/clang/unittests/Sema/HeuristicResolverTest.cpp
+++ b/clang/unittests/Sema/HeuristicResolverTest.cpp
@@ -295,6 +295,25 @@ TEST(HeuristicResolver, MemberExpr_Metafunction) {
cxxMethodDecl(hasName("find")).bind("output"));
}
+TEST(HeuristicResolver, MemberExpr_Metafunction_Enumerator) {
+ std::string Code = R"cpp(
+ enum class State { Hidden };
+ template <typename T>
+ struct Meta {
+ using Type = State;
+ };
+ template <typename T>
+ void foo(typename Meta<T>::Type t) {
+ t.Hidden;
+ }
+ )cpp";
+ // Test resolution of "Hidden" in "t.Hidden".
+ expectResolution(
+ Code, &HeuristicResolver::resolveMemberExpr,
+ cxxDependentScopeMemberExpr(hasMemberName("Hidden")).bind("input"),
+ enumConstantDecl(hasName("Hidden")).bind("output"));
+}
+
TEST(HeuristicResolver, MemberExpr_DeducedNonTypeTemplateParameter) {
std::string Code = R"cpp(
template <int N>
More information about the cfe-commits
mailing list