Skip to content

Commit bf73e27

Browse files
committed
code review
1 parent c2b8ac4 commit bf73e27

File tree

2 files changed

+41
-63
lines changed

2 files changed

+41
-63
lines changed

clang-tools-extra/clangd/unittests/RenameTests.cpp

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -862,13 +862,33 @@ TEST(RenameTest, WithinFileRename) {
862862
863863
void func([[Fo^o]] *f) {}
864864
)cpp",
865+
866+
// rename with explicit object parameter
867+
R"cpp(
868+
struct Foo {
869+
int [[memb^er]] {};
870+
auto&& getter1(this auto&& self) {
871+
auto local = [&] {
872+
return self.[[memb^er]];
873+
}();
874+
return local + self.[[memb^er]];
875+
}
876+
auto&& getter2(this Foo&& self) {
877+
return self.[[memb^er]];
878+
}
879+
int normal() {
880+
return this->[[mem^ber]] + [[memb^er]];
881+
}
882+
};
883+
)cpp",
865884
};
866885
llvm::StringRef NewName = "NewName";
867886
for (llvm::StringRef T : Tests) {
868887
SCOPED_TRACE(T);
869888
Annotations Code(T);
870889
auto TU = TestTU::withCode(Code.code());
871890
TU.ExtraArgs.push_back("-xobjective-c++");
891+
TU.ExtraArgs.push_back("-std=c++23");
872892
auto AST = TU.build();
873893
auto Index = TU.index();
874894
for (const auto &RenamePos : Code.points()) {
@@ -2468,47 +2488,6 @@ TEST(CrossFileRenameTests, adjustmentCost) {
24682488
T.ExpectedCost);
24692489
}
24702490
}
2471-
2472-
TEST(RenameTest, RenameWithExplicitObjectPararameter) {
2473-
Annotations Test = {R"cpp(
2474-
struct Foo {
2475-
int [[memb^er]] {};
2476-
auto&& getter1(this auto&& self) {
2477-
auto local = [&] {
2478-
return self.[[memb^er]];
2479-
}();
2480-
return local + self.[[memb^er]];
2481-
}
2482-
auto&& getter2(this Foo&& self) {
2483-
return self.[[memb^er]];
2484-
}
2485-
int normal() {
2486-
return this->[[mem^ber]] + [[memb^er]];
2487-
}
2488-
};
2489-
)cpp"};
2490-
2491-
auto TU = TestTU::withCode(Test.code());
2492-
TU.ExtraArgs.push_back("-std=c++23");
2493-
auto AST = TU.build();
2494-
2495-
llvm::StringRef NewName = "m_member";
2496-
auto Index = TU.index();
2497-
2498-
for (const auto &RenamePos : Test.points()) {
2499-
auto RenameResult = rename({RenamePos, NewName, AST, testPath(TU.Filename),
2500-
getVFSFromAST(AST), Index.get()});
2501-
2502-
ASSERT_TRUE(bool(RenameResult)) << RenameResult.takeError();
2503-
auto Res = RenameResult.get();
2504-
2505-
ASSERT_TRUE(bool(RenameResult)) << RenameResult.takeError();
2506-
ASSERT_EQ(1u, RenameResult->GlobalChanges.size());
2507-
EXPECT_EQ(applyEdits(std::move(RenameResult->GlobalChanges)).front().second,
2508-
expectedResult(Test, NewName));
2509-
}
2510-
}
2511-
25122491
} // namespace
25132492
} // namespace clangd
25142493
} // namespace clang

clang/lib/Sema/HeuristicResolver.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,21 @@ QualType HeuristicResolverImpl::simplifyType(QualType Type, const Expr *E,
256256
}
257257
}
258258
}
259+
// check if member expr is in the context of an explicit object method
260+
if (!T.Type.isNull() &&
261+
(T.Type->isUndeducedAutoType() || T.Type->isTemplateTypeParmType())) {
262+
if (auto *DRE = dyn_cast_if_present<DeclRefExpr>(T.E)) {
263+
auto *PrDecl = dyn_cast_if_present<ParmVarDecl>(DRE->getDecl());
264+
// Then the type of 'this' should be type of the record the method is
265+
// defined in
266+
if (PrDecl && PrDecl->isExplicitObjectParameter()) {
267+
const auto *Parent =
268+
dyn_cast<TypeDecl>(PrDecl->getDeclContext()->getParent());
269+
return {Ctx.getTypeDeclType(Parent)};
270+
}
271+
}
272+
}
273+
259274
return T;
260275
};
261276
// As an additional protection against infinite loops, bound the number of
@@ -302,33 +317,17 @@ std::vector<const NamedDecl *> HeuristicResolverImpl::resolveMemberExpr(
302317
return {};
303318
}
304319

305-
// check if member expr is in the context of an explicit object method
306-
// If so, it's safe to assume the templated arg is of type of the record
307-
const auto ExplicitMemberHeuristic = [&](const Expr *Base) -> QualType {
308-
if (auto *DeclRef = dyn_cast_if_present<DeclRefExpr>(Base)) {
309-
auto *PrDecl = dyn_cast_if_present<ParmVarDecl>(DeclRef->getDecl());
310-
311-
if (PrDecl && PrDecl->isExplicitObjectParameter()) {
312-
// get the parent, a cxxrecord
313-
return Ctx.getTypeDeclType(
314-
dyn_cast<TypeDecl>(PrDecl->getDeclContext()->getParent()));
315-
}
316-
}
317-
318-
return {};
319-
};
320-
321320
// Try resolving the member inside the expression's base type.
322321
Expr *Base = ME->isImplicitAccess() ? nullptr : ME->getBase();
323322
QualType BaseType = ME->getBaseType();
324323
BaseType = simplifyType(BaseType, Base, ME->isArrow());
325324

326-
if (!BaseType.isNull() &&
327-
(BaseType->isUndeducedAutoType() || BaseType->isTemplateTypeParmType())) {
328-
if (auto Type = ExplicitMemberHeuristic(Base); !Type.isNull()) {
329-
BaseType = Type;
330-
}
331-
}
325+
// fflush(stdout);
326+
// fflush(stderr);
327+
// std::flush(std::cout);
328+
// std::flush(std::cerr);
329+
// using namespace std::chrono_literals;
330+
// std::this_thread::sleep_for(10ms);
332331

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

0 commit comments

Comments
 (0)