Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 22 additions & 1 deletion 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
15 changes: 15 additions & 0 deletions clang/lib/Sema/HeuristicResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,21 @@ QualType HeuristicResolverImpl::simplifyType(QualType Type, const Expr *E,
}
}
}
// 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.
if (!T.Type.isNull() &&
(T.Type->isUndeducedAutoType() || T.Type->isTemplateTypeParmType())) {
if (auto *DRE = dyn_cast_if_present<DeclRefExpr>(T.E)) {
auto *PrDecl = dyn_cast<ParmVarDecl>(DRE->getDecl());
if (PrDecl && PrDecl->isExplicitObjectParameter()) {
const auto *Parent =
dyn_cast<TagDecl>(PrDecl->getDeclContext()->getParent());
return {Ctx.getCanonicalTagType(Parent)};
}
}
}

return T;
};
// As an additional protection against infinite loops, bound the number of
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