Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions clang-tools-extra/clangd/unittests/RenameTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
#include "clang/Tooling/Core/Replacement.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/MemoryBuffer.h"
#include <algorithm>
#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include <algorithm>

namespace clang {
namespace clangd {
namespace {
Expand Down Expand Up @@ -861,13 +862,33 @@ TEST(RenameTest, WithinFileRename) {

void func([[Fo^o]] *f) {}
)cpp",

// rename with explicit object parameter
R"cpp(
struct Foo {
int [[memb^er]] {};
auto&& getter1(this auto&& self) {
auto local = [&] {
return self.[[memb^er]];
}();
return local + self.[[memb^er]];
}
auto&& getter2(this Foo&& self) {
return self.[[memb^er]];
}
int normal() {
return this->[[mem^ber]] + [[memb^er]];
}
};
)cpp",
};
llvm::StringRef NewName = "NewName";
for (llvm::StringRef T : Tests) {
SCOPED_TRACE(T);
Annotations Code(T);
auto TU = TestTU::withCode(Code.code());
TU.ExtraArgs.push_back("-xobjective-c++");
TU.ExtraArgs.push_back("-std=c++23");
auto AST = TU.build();
auto Index = TU.index();
for (const auto &RenamePos : Code.points()) {
Expand Down Expand Up @@ -2467,7 +2488,6 @@ TEST(CrossFileRenameTests, adjustmentCost) {
T.ExpectedCost);
}
}

} // namespace
} // namespace clangd
} // namespace clang
24 changes: 24 additions & 0 deletions clang/lib/Sema/HeuristicResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "clang/AST/TemplateBase.h"
#include "clang/AST/Type.h"
#include "llvm/ADT/identity.h"
#include <optional>

namespace clang {

Expand Down Expand Up @@ -255,6 +256,21 @@ QualType HeuristicResolverImpl::simplifyType(QualType Type, const Expr *E,
}
}
}
// check if member expr is in the context of an explicit object method
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would reword / expand on this as:

// Check if the expression refers to an explicit object parameter of templated type.
// If so, heuristically treat it as having the type of the enclosing class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

if (!T.Type.isNull() &&
(T.Type->isUndeducedAutoType() || T.Type->isTemplateTypeParmType())) {
if (auto *DRE = dyn_cast_if_present<DeclRefExpr>(T.E)) {
auto *PrDecl = dyn_cast_if_present<ParmVarDecl>(DRE->getDecl());
// Then the type of 'this' should be type of the record the method is
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(with the above change, this comment can be dropped)

// defined in
if (PrDecl && PrDecl->isExplicitObjectParameter()) {
const auto *Parent =
dyn_cast<TypeDecl>(PrDecl->getDeclContext()->getParent());
return {Ctx.getTypeDeclType(Parent)};
}
}
}

return T;
};
// As an additional protection against infinite loops, bound the number of
Expand Down Expand Up @@ -305,6 +321,14 @@ std::vector<const NamedDecl *> HeuristicResolverImpl::resolveMemberExpr(
Expr *Base = ME->isImplicitAccess() ? nullptr : ME->getBase();
QualType BaseType = ME->getBaseType();
BaseType = simplifyType(BaseType, Base, ME->isArrow());

// fflush(stdout);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some debugging code snuck in here

Copy link
Contributor Author

@MythreyaK MythreyaK Aug 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh whoops, my bad 😅, removed, thanks!

// fflush(stderr);
// std::flush(std::cout);
// std::flush(std::cerr);
// using namespace std::chrono_literals;
// std::this_thread::sleep_for(10ms);

return resolveDependentMember(BaseType, ME->getMember(), NoFilter);
}

Expand Down
19 changes: 18 additions & 1 deletion clang/unittests/Sema/HeuristicResolverTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ template <typename InputNode, typename ParamT, typename InputMatcher,
typename... OutputMatchers>
void expectResolution(llvm::StringRef Code, ResolveFnT<ParamT> ResolveFn,
const InputMatcher &IM, const OutputMatchers &...OMS) {
auto TU = tooling::buildASTFromCodeWithArgs(Code, {"-std=c++20"});
auto TU = tooling::buildASTFromCodeWithArgs(Code, {"-std=c++23"});
auto &Ctx = TU->getASTContext();
auto InputMatches = match(IM, Ctx);
ASSERT_EQ(1u, InputMatches.size());
Expand Down Expand Up @@ -449,6 +449,23 @@ TEST(HeuristicResolver, MemberExpr_DefaultTemplateArgument_Recursive) {
cxxMethodDecl(hasName("foo")).bind("output"));
}

TEST(HeuristicResolver, MemberExpr_ExplicitObjectParameter) {
std::string Code = R"cpp(
struct Foo {
int m_int;

int bar(this auto&& self) {
return self.m_int;
}
};
)cpp";
// Test resolution of "m_int" in "self.m_int()".
expectResolution(
Code, &HeuristicResolver::resolveMemberExpr,
cxxDependentScopeMemberExpr(hasMemberName("m_int")).bind("input"),
fieldDecl(hasName("m_int")).bind("output"));
}

TEST(HeuristicResolver, DeclRefExpr_StaticMethod) {
std::string Code = R"cpp(
template <typename T>
Expand Down