Skip to content

Commit 45bcf1e

Browse files
[clangd] Use HeuristicResolver to try to resolve dependent 'auto'
1 parent 34a3aea commit 45bcf1e

File tree

9 files changed

+47
-17
lines changed

9 files changed

+47
-17
lines changed

clang-tools-extra/clangd/AST.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "clang/Basic/SourceManager.h"
3030
#include "clang/Basic/Specifiers.h"
3131
#include "clang/Index/USRGeneration.h"
32+
#include "clang/Sema/HeuristicResolver.h"
3233
#include "llvm/ADT/ArrayRef.h"
3334
#include "llvm/ADT/STLExtras.h"
3435
#include "llvm/ADT/SmallSet.h"
@@ -479,10 +480,12 @@ namespace {
479480
/// a deduced type set. The AST should be improved to simplify this scenario.
480481
class DeducedTypeVisitor : public RecursiveASTVisitor<DeducedTypeVisitor> {
481482
SourceLocation SearchedLocation;
483+
const HeuristicResolver *Resolver;
482484

483485
public:
484-
DeducedTypeVisitor(SourceLocation SearchedLocation)
485-
: SearchedLocation(SearchedLocation) {}
486+
DeducedTypeVisitor(SourceLocation SearchedLocation,
487+
const HeuristicResolver *Resolver)
488+
: SearchedLocation(SearchedLocation), Resolver(Resolver) {}
486489

487490
// Handle auto initializers:
488491
//- auto i = 1;
@@ -499,6 +502,14 @@ class DeducedTypeVisitor : public RecursiveASTVisitor<DeducedTypeVisitor> {
499502
return true;
500503

501504
if (auto *AT = D->getType()->getContainedAutoType()) {
505+
if (AT->isUndeducedAutoType()) {
506+
if (const auto *VD = dyn_cast<VarDecl>(D)) {
507+
if (Resolver && VD->hasInit()) {
508+
DeducedType = Resolver->resolveExprToType(VD->getInit());
509+
return true;
510+
}
511+
}
512+
}
502513
DeducedType = AT->desugar();
503514
}
504515
return true;
@@ -608,10 +619,12 @@ class DeducedTypeVisitor : public RecursiveASTVisitor<DeducedTypeVisitor> {
608619
};
609620
} // namespace
610621

611-
std::optional<QualType> getDeducedType(ASTContext &ASTCtx, SourceLocation Loc) {
622+
std::optional<QualType> getDeducedType(ASTContext &ASTCtx,
623+
const HeuristicResolver *Resolver,
624+
SourceLocation Loc) {
612625
if (!Loc.isValid())
613626
return {};
614-
DeducedTypeVisitor V(Loc);
627+
DeducedTypeVisitor V(Loc, Resolver);
615628
V.TraverseAST(ASTCtx);
616629
if (V.DeducedType.isNull())
617630
return std::nullopt;

clang-tools-extra/clangd/AST.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace clang {
3131
class SourceManager;
3232
class Decl;
3333
class DynTypedNode;
34+
class HeuristicResolver;
3435

3536
namespace clangd {
3637

@@ -167,7 +168,8 @@ QualType declaredType(const TypeDecl *D);
167168
/// Retrieves the deduced type at a given location (auto, decltype).
168169
/// It will return the underlying type.
169170
/// If the type is an undeduced auto, returns the type itself.
170-
std::optional<QualType> getDeducedType(ASTContext &, SourceLocation Loc);
171+
std::optional<QualType> getDeducedType(ASTContext &, const HeuristicResolver *,
172+
SourceLocation Loc);
171173

172174
// Find the abbreviated-function-template `auto` within a type, or returns null.
173175
// Similar to getContainedAutoTypeLoc, but these `auto`s are

clang-tools-extra/clangd/Hover.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,9 @@ std::optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
13091309
}
13101310
} else if (Tok.kind() == tok::kw_auto || Tok.kind() == tok::kw_decltype) {
13111311
HoverCountMetric.record(1, "keyword");
1312-
if (auto Deduced = getDeducedType(AST.getASTContext(), Tok.location())) {
1312+
if (auto Deduced =
1313+
getDeducedType(AST.getASTContext(), AST.getHeuristicResolver(),
1314+
Tok.location())) {
13131315
HI = getDeducedTypeHoverContents(*Deduced, Tok, AST.getASTContext(), PP,
13141316
Index);
13151317
HighlightRange = Tok.range(SM).toCharRange(SM);

clang-tools-extra/clangd/XRefs.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,9 @@ std::vector<LocatedSymbol> locateSymbolAt(ParsedAST &AST, Position Pos,
806806
if (Tok.kind() == tok::kw_auto || Tok.kind() == tok::kw_decltype) {
807807
// go-to-definition on auto should find the definition of the deduced
808808
// type, if possible
809-
if (auto Deduced = getDeducedType(AST.getASTContext(), Tok.location())) {
809+
if (auto Deduced =
810+
getDeducedType(AST.getASTContext(), AST.getHeuristicResolver(),
811+
Tok.location())) {
810812
auto LocSym = locateSymbolForType(AST, *Deduced, Index);
811813
if (!LocSym.empty())
812814
return LocSym;
@@ -1965,7 +1967,7 @@ std::vector<const CXXRecordDecl *> findRecordTypeAt(ParsedAST &AST,
19651967

19661968
// Return the type most associated with an AST node.
19671969
// This isn't precisely defined: we want "go to type" to do something useful.
1968-
static QualType typeForNode(const ASTContext &Ctx,
1970+
static QualType typeForNode(const ASTContext &Ctx, const HeuristicResolver *H,
19691971
const SelectionTree::Node *N) {
19701972
// If we're looking at a namespace qualifier, walk up to what it's qualifying.
19711973
// (If we're pointing at a *class* inside a NNS, N will be a TypeLoc).
@@ -1978,7 +1980,7 @@ static QualType typeForNode(const ASTContext &Ctx,
19781980
if (const TypeLoc *TL = N->ASTNode.get<TypeLoc>()) {
19791981
if (llvm::isa<DeducedType>(TL->getTypePtr()))
19801982
if (auto Deduced = getDeducedType(
1981-
N->getDeclContext().getParentASTContext(), TL->getBeginLoc()))
1983+
N->getDeclContext().getParentASTContext(), H, TL->getBeginLoc()))
19821984
return *Deduced;
19831985
// Exception: an alias => underlying type.
19841986
if (llvm::isa<TypedefType>(TL->getTypePtr()))
@@ -2161,7 +2163,8 @@ std::vector<LocatedSymbol> findType(ParsedAST &AST, Position Pos,
21612163
// information about the type you may have not known before
21622164
// (since unique_ptr<unique_ptr<T>> != unique_ptr<T>).
21632165
for (const QualType &Type : unwrapFindType(
2164-
typeForNode(AST.getASTContext(), N), AST.getHeuristicResolver()))
2166+
typeForNode(AST.getASTContext(), AST.getHeuristicResolver(), N),
2167+
AST.getHeuristicResolver()))
21652168
llvm::copy(locateSymbolForType(AST, Type, Index),
21662169
std::back_inserter(LocatedSymbols));
21672170

clang-tools-extra/clangd/refactor/tweaks/ExpandDeducedType.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ Expected<Tweak::Effect> ExpandDeducedType::apply(const Selection &Inputs) {
133133
auto &SrcMgr = Inputs.AST->getSourceManager();
134134

135135
std::optional<clang::QualType> DeducedType =
136-
getDeducedType(Inputs.AST->getASTContext(), Range.getBegin());
136+
getDeducedType(Inputs.AST->getASTContext(),
137+
Inputs.AST->getHeuristicResolver(), Range.getBegin());
137138

138139
// if we can't resolve the type, return an error message
139140
if (DeducedType == std::nullopt || (*DeducedType)->isUndeducedAutoType())

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ TEST(GetDeducedType, KwAutoKwDecltypeExpansion) {
244244
for (Position Pos : File.points()) {
245245
auto Location = sourceLocationInMainFile(SM.get(), Pos);
246246
ASSERT_TRUE(!!Location) << llvm::toString(Location.takeError());
247-
auto DeducedType = getDeducedType(AST.getASTContext(), *Location);
247+
auto DeducedType = getDeducedType(AST.getASTContext(),
248+
AST.getHeuristicResolver(), *Location);
248249
if (T.DeducedType == nullptr) {
249250
EXPECT_FALSE(DeducedType);
250251
} else {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ class Foo final {})cpp";
481481
[](HoverInfo &HI) {
482482
HI.Name = "auto";
483483
HI.Kind = index::SymbolKind::TypeAlias;
484-
HI.Definition = "/* not deduced */";
484+
HI.Definition = "T";
485485
}},
486486
// constrained auto
487487
{R"cpp(
@@ -2657,7 +2657,7 @@ TEST(Hover, All) {
26572657
[](HoverInfo &HI) {
26582658
HI.Name = "auto";
26592659
HI.Kind = index::SymbolKind::TypeAlias;
2660-
HI.Definition = "/* not deduced */";
2660+
HI.Definition = "T";
26612661
}},
26622662
{
26632663
R"cpp(// Undeduced auto return type

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,11 +924,19 @@ TEST(LocateSymbol, All) {
924924
}
925925
)cpp",
926926

927+
R"cpp(// auto with dependent type
928+
template <typename>
929+
struct [[A]] {};
930+
template <typename T>
931+
void foo(A<T> a) {
932+
^auto copy = a;
933+
}
934+
)cpp",
935+
927936
R"cpp(// Override specifier jumps to overridden method
928937
class Y { virtual void $decl[[a]]() = 0; };
929938
class X : Y { void a() ^override {} };
930939
)cpp",
931-
932940
R"cpp(// Final specifier jumps to overridden method
933941
class Y { virtual void $decl[[a]]() = 0; };
934942
class X : Y { void a() ^final {} };

clang-tools-extra/clangd/unittests/tweaks/ExpandDeducedTypeTests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ TEST_F(ExpandDeducedTypeTest, Test) {
4747
"namespace ns { void f() { Class C = Class(); } }");
4848
// undefined functions should not be replaced
4949
EXPECT_THAT(apply("au^to x = doesnt_exist(); // error-ok"),
50-
StartsWith("fail: Could not deduce type for 'auto' type"));
50+
StartsWith("fail: Could not expand a dependent type"));
5151
// function pointers should not be replaced
5252
EXPECT_THAT(apply("au^to x = &ns::Func;"),
5353
StartsWith("fail: Could not expand type"));
@@ -91,7 +91,7 @@ TEST_F(ExpandDeducedTypeTest, Test) {
9191

9292
// unknown types in a template should not be replaced
9393
EXPECT_THAT(apply("template <typename T> void x() { ^auto y = T::z(); }"),
94-
StartsWith("fail: Could not deduce type for 'auto' type"));
94+
StartsWith("fail: Could not expand a dependent type"));
9595

9696
// check primitive type
9797
EXPECT_EQ(apply("decl^type(0) i;"), "int i;");

0 commit comments

Comments
 (0)