Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
61 changes: 20 additions & 41 deletions clang-tools-extra/clangd/unittests/RenameTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -862,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 @@ -2468,47 +2488,6 @@ TEST(CrossFileRenameTests, adjustmentCost) {
T.ExpectedCost);
}
}

TEST(RenameTest, RenameWithExplicitObjectPararameter) {
Annotations Test = {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"};

auto TU = TestTU::withCode(Test.code());
TU.ExtraArgs.push_back("-std=c++23");
auto AST = TU.build();

llvm::StringRef NewName = "m_member";
auto Index = TU.index();

for (const auto &RenamePos : Test.points()) {
auto RenameResult = rename({RenamePos, NewName, AST, testPath(TU.Filename),
getVFSFromAST(AST), Index.get()});

ASSERT_TRUE(bool(RenameResult)) << RenameResult.takeError();
auto Res = RenameResult.get();

ASSERT_TRUE(bool(RenameResult)) << RenameResult.takeError();
ASSERT_EQ(1u, RenameResult->GlobalChanges.size());
EXPECT_EQ(applyEdits(std::move(RenameResult->GlobalChanges)).front().second,
expectedResult(Test, NewName));
}
}

} // namespace
} // namespace clangd
} // namespace clang
43 changes: 21 additions & 22 deletions clang/lib/Sema/HeuristicResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,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 @@ -302,33 +317,17 @@ std::vector<const NamedDecl *> HeuristicResolverImpl::resolveMemberExpr(
return {};
}

// check if member expr is in the context of an explicit object method
// If so, it's safe to assume the templated arg is of type of the record
const auto ExplicitMemberHeuristic = [&](const Expr *Base) -> QualType {
if (auto *DeclRef = dyn_cast_if_present<DeclRefExpr>(Base)) {
auto *PrDecl = dyn_cast_if_present<ParmVarDecl>(DeclRef->getDecl());

if (PrDecl && PrDecl->isExplicitObjectParameter()) {
// get the parent, a cxxrecord
return Ctx.getTypeDeclType(
dyn_cast<TypeDecl>(PrDecl->getDeclContext()->getParent()));
}
}

return {};
};

// Try resolving the member inside the expression's base type.
Expr *Base = ME->isImplicitAccess() ? nullptr : ME->getBase();
QualType BaseType = ME->getBaseType();
BaseType = simplifyType(BaseType, Base, ME->isArrow());

if (!BaseType.isNull() &&
(BaseType->isUndeducedAutoType() || BaseType->isTemplateTypeParmType())) {
if (auto Type = ExplicitMemberHeuristic(Base); !Type.isNull()) {
BaseType = Type;
}
}
// 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