Skip to content

Commit 5a55e1f

Browse files
committed
Merge from 'main' to 'sycl-web' (222 commits)
CONFLICT (content): Merge conflict in clang/lib/Sema/SemaDeclAttr.cpp
2 parents 7a2e290 + 0d989b2 commit 5a55e1f

File tree

740 files changed

+25513
-13559
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

740 files changed

+25513
-13559
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/InlayHints.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -633,13 +633,30 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
633633
}
634634

635635
if (auto *AT = D->getType()->getContainedAutoType()) {
636-
if (AT->isDeduced() && !D->getType()->isDependentType()) {
637-
// Our current approach is to place the hint on the variable
638-
// and accordingly print the full type
639-
// (e.g. for `const auto& x = 42`, print `const int&`).
640-
// Alternatively, we could place the hint on the `auto`
641-
// (and then just print the type deduced for the `auto`).
642-
addTypeHint(D->getLocation(), D->getType(), /*Prefix=*/": ");
636+
if (AT->isDeduced()) {
637+
QualType T;
638+
// If the type is dependent, HeuristicResolver *may* be able to
639+
// resolve it to something that's useful to print. In other
640+
// cases, it can't, and the resultng type would just be printed
641+
// as "<dependent type>", in which case don't hint it at all.
642+
if (D->getType()->isDependentType()) {
643+
if (D->hasInit()) {
644+
QualType Resolved = Resolver->resolveExprToType(D->getInit());
645+
if (Resolved != AST.DependentTy) {
646+
T = Resolved;
647+
}
648+
}
649+
} else {
650+
T = D->getType();
651+
}
652+
if (!T.isNull()) {
653+
// Our current approach is to place the hint on the variable
654+
// and accordingly print the full type
655+
// (e.g. for `const auto& x = 42`, print `const int&`).
656+
// Alternatively, we could place the hint on the `auto`
657+
// (and then just print the type deduced for the `auto`).
658+
addTypeHint(D->getLocation(), T, /*Prefix=*/": ");
659+
}
643660
}
644661
}
645662

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/InlayHintTests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1441,7 +1441,8 @@ TEST(TypeHints, DependentType) {
14411441
void bar(T arg) {
14421442
auto [a, b] = arg;
14431443
}
1444-
)cpp");
1444+
)cpp",
1445+
ExpectedHint{": T", "var2"});
14451446
}
14461447

14471448
TEST(TypeHints, LongTypeName) {

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 {} };

0 commit comments

Comments
 (0)