-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][Sema] Handle pointer and reference type more robustly in HeuristicResolver::resolveMemberExpr() #124451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-clang Author: Nathan Ridge (HighCommander4) ChangesPartially fixes #124450 Full diff: https://github.com/llvm/llvm-project/pull/124451.diff 2 Files Affected:
diff --git a/clang/lib/Sema/HeuristicResolver.cpp b/clang/lib/Sema/HeuristicResolver.cpp
index e893afed71d268..6157191bafa6fc 100644
--- a/clang/lib/Sema/HeuristicResolver.cpp
+++ b/clang/lib/Sema/HeuristicResolver.cpp
@@ -133,8 +133,8 @@ TemplateName getReferencedTemplateName(const Type *T) {
CXXRecordDecl *HeuristicResolverImpl::resolveTypeToRecordDecl(const Type *T) {
assert(T);
- // Unwrap type sugar such as type aliases.
- T = T->getCanonicalTypeInternal().getTypePtr();
+ // Unwrap references and type sugar such as type aliases.
+ T = T->getCanonicalTypeInternal().getNonReferenceType().getTypePtr();
if (const auto *DNT = T->getAs<DependentNameType>()) {
T = resolveDeclsToType(resolveDependentNameType(DNT), Ctx)
diff --git a/clang/unittests/Sema/HeuristicResolverTest.cpp b/clang/unittests/Sema/HeuristicResolverTest.cpp
index 2b775b11719ea7..00e19aecdae0a9 100644
--- a/clang/unittests/Sema/HeuristicResolverTest.cpp
+++ b/clang/unittests/Sema/HeuristicResolverTest.cpp
@@ -213,6 +213,27 @@ TEST(HeuristicResolver, MemberExpr_Chained) {
cxxMethodDecl(hasName("foo")).bind("output"));
}
+TEST(HeuristicResolver, MemberExpr_ReferenceType) {
+ std::string Code = R"cpp(
+ struct B {
+ int waldo;
+ };
+ template <typename T>
+ struct A {
+ B &b;
+ };
+ template <typename T>
+ void foo(A<T> &a) {
+ a.b.waldo;
+ }
+ )cpp";
+ // Test resolution of "waldo" in "a.b.waldo".
+ expectResolution(
+ Code, &HeuristicResolver::resolveMemberExpr,
+ cxxDependentScopeMemberExpr(hasMemberName("waldo")).bind("input"),
+ fieldDecl(hasName("waldo")).bind("output"));
+}
+
TEST(HeuristicResolver, MemberExpr_TemplateArgs) {
std::string Code = R"cpp(
struct Foo {
|
clang/lib/Sema/HeuristicResolver.cpp
Outdated
// Unwrap type sugar such as type aliases. | ||
T = T->getCanonicalTypeInternal().getTypePtr(); | ||
// Unwrap references and type sugar such as type aliases. | ||
T = T->getCanonicalTypeInternal().getNonReferenceType().getTypePtr(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it work if b
were a pointer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the pointer case is a bit more involved because we bail here after failing to unwrap the pointee type here.
To handle it we will need to do something like:
- Check for pointer type and unwrap it if found
- Resolve
BuiltinType::Dependent
- If a pointer type was not unwrapped at step (1), check for it again after step (2) and unwrap it if found
I was going to do this in a follow-up patch but it might make more sense to do it in the same patch; I will revise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the revised version of the patch, we handle both reference and pointer types and we do it more robustly, checking for them after each step of simplification of the type.
…isticResolver::resolveMemberExpr() Partially fixes #124450
9c8349f
to
4ab3d58
Compare
(The Windows buildkite failure looks unrelated.) |
FWIW, i bisected a performance regression (and possibly an infinite loop) in clangd to this PR. trying to get a reproducer now. |
Fixes #124450