Skip to content

Commit 12f2a1c

Browse files
committed
Merge from '"main"' to '"sycl-web"' (2 commits)
CONFLICT (content): Merge conflict in clang/lib/CodeGen/CodeGenModule.h
2 parents b4b7dd2 + d1b127b commit 12f2a1c

File tree

562 files changed

+13229
-8679
lines changed

Some content is hidden

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

562 files changed

+13229
-8679
lines changed

clang-tools-extra/clangd/ClangdLSPServer.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ void ClangdLSPServer::onInitialize(const InitializeParams &Params,
608608
if (Opts.FoldingRanges)
609609
ServerCaps["foldingRangeProvider"] = true;
610610

611+
// FIXME: once inlayHints can be disabled in config, always advertise.
611612
if (Opts.InlayHints)
612613
ServerCaps["clangdInlayHintsProvider"] = true;
613614

@@ -1210,7 +1211,8 @@ void ClangdLSPServer::onCallHierarchyIncomingCalls(
12101211

12111212
void ClangdLSPServer::onInlayHints(const InlayHintsParams &Params,
12121213
Callback<std::vector<InlayHint>> Reply) {
1213-
Server->inlayHints(Params.textDocument.uri.file(), std::move(Reply));
1214+
Server->inlayHints(Params.textDocument.uri.file(), Params.range,
1215+
std::move(Reply));
12141216
}
12151217

12161218
void ClangdLSPServer::applyConfiguration(

clang-tools-extra/clangd/ClangdLSPServer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ class ClangdLSPServer : private ClangdServer::Callbacks,
6363
return !T.hidden(); // only enable non-hidden tweaks.
6464
};
6565

66-
/// Enable preview of InlayHints feature.
67-
bool InlayHints = false;
66+
/// Enable InlayHints feature.
67+
bool InlayHints = true;
6868

6969
/// Limit the number of references returned (0 means no limit).
7070
size_t ReferencesLimit = 0;

clang-tools-extra/clangd/ClangdServer.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -759,12 +759,13 @@ void ClangdServer::incomingCalls(
759759
});
760760
}
761761

762-
void ClangdServer::inlayHints(PathRef File,
762+
void ClangdServer::inlayHints(PathRef File, llvm::Optional<Range> RestrictRange,
763763
Callback<std::vector<InlayHint>> CB) {
764-
auto Action = [CB = std::move(CB)](Expected<InputsAndAST> InpAST) mutable {
764+
auto Action = [RestrictRange(std::move(RestrictRange)),
765+
CB = std::move(CB)](Expected<InputsAndAST> InpAST) mutable {
765766
if (!InpAST)
766767
return CB(InpAST.takeError());
767-
CB(clangd::inlayHints(InpAST->AST));
768+
CB(clangd::inlayHints(InpAST->AST, std::move(RestrictRange)));
768769
};
769770
WorkScheduler->runWithAST("InlayHints", File, std::move(Action));
770771
}

clang-tools-extra/clangd/ClangdServer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ class ClangdServer {
264264
Callback<std::vector<CallHierarchyIncomingCall>>);
265265

266266
/// Resolve inlay hints for a given document.
267-
void inlayHints(PathRef File, Callback<std::vector<InlayHint>>);
267+
void inlayHints(PathRef File, llvm::Optional<Range> RestrictRange,
268+
Callback<std::vector<InlayHint>>);
268269

269270
/// Retrieve the top symbols from the workspace matching a query.
270271
void workspaceSymbols(StringRef Query, int Limit,

clang-tools-extra/clangd/InlayHints.cpp

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,24 @@
88
#include "InlayHints.h"
99
#include "HeuristicResolver.h"
1010
#include "ParsedAST.h"
11-
#include "support/Logger.h"
1211
#include "clang/AST/DeclarationName.h"
1312
#include "clang/AST/ExprCXX.h"
1413
#include "clang/AST/RecursiveASTVisitor.h"
1514
#include "clang/Basic/SourceManager.h"
16-
#include "llvm/Support/raw_ostream.h"
1715

1816
namespace clang {
1917
namespace clangd {
18+
namespace {
19+
20+
// For now, inlay hints are always anchored at the left or right of their range.
21+
enum class HintSide { Left, Right };
2022

2123
class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
2224
public:
23-
InlayHintVisitor(std::vector<InlayHint> &Results, ParsedAST &AST)
25+
InlayHintVisitor(std::vector<InlayHint> &Results, ParsedAST &AST,
26+
llvm::Optional<Range> RestrictRange)
2427
: Results(Results), AST(AST.getASTContext()),
28+
RestrictRange(std::move(RestrictRange)),
2529
MainFileID(AST.getSourceManager().getMainFileID()),
2630
Resolver(AST.getHeuristicResolver()),
2731
TypeHintPolicy(this->AST.getPrintingPolicy()),
@@ -88,7 +92,7 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
8892
QualType Deduced = AT->getDeducedType();
8993
if (!Deduced.isNull()) {
9094
addTypeHint(D->getFunctionTypeLoc().getRParenLoc(), D->getReturnType(),
91-
"-> ");
95+
/*Prefix=*/"-> ");
9296
}
9397
}
9498

@@ -100,7 +104,7 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
100104
// but show hints for the individual bindings.
101105
if (auto *DD = dyn_cast<DecompositionDecl>(D)) {
102106
for (auto *Binding : DD->bindings()) {
103-
addTypeHint(Binding->getLocation(), Binding->getType(), ": ",
107+
addTypeHint(Binding->getLocation(), Binding->getType(), /*Prefix=*/": ",
104108
StructuredBindingPolicy);
105109
}
106110
return true;
@@ -113,7 +117,7 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
113117
// (e.g. for `const auto& x = 42`, print `const int&`).
114118
// Alternatively, we could place the hint on the `auto`
115119
// (and then just print the type deduced for the `auto`).
116-
addTypeHint(D->getLocation(), D->getType(), ": ");
120+
addTypeHint(D->getLocation(), D->getType(), /*Prefix=*/": ");
117121
}
118122
}
119123
return true;
@@ -160,8 +164,9 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
160164
if (!shouldHint(Args[I], Name))
161165
continue;
162166

163-
addInlayHint(Args[I]->getSourceRange(), InlayHintKind::ParameterHint,
164-
Name.str() + ": ");
167+
addInlayHint(Args[I]->getSourceRange(), HintSide::Left,
168+
InlayHintKind::ParameterHint, /*Prefix=*/"", Name,
169+
/*Suffix=*/": ");
165170
}
166171
}
167172

@@ -313,20 +318,28 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
313318
return Result;
314319
}
315320

316-
void addInlayHint(SourceRange R, InlayHintKind Kind, llvm::StringRef Label) {
321+
// We pass HintSide rather than SourceLocation because we want to ensure
322+
// it is in the same file as the common file range.
323+
void addInlayHint(SourceRange R, HintSide Side, InlayHintKind Kind,
324+
llvm::StringRef Prefix, llvm::StringRef Label,
325+
llvm::StringRef Suffix) {
317326
auto FileRange =
318327
toHalfOpenFileRange(AST.getSourceManager(), AST.getLangOpts(), R);
319328
if (!FileRange)
320329
return;
330+
Range LSPRange{
331+
sourceLocToPosition(AST.getSourceManager(), FileRange->getBegin()),
332+
sourceLocToPosition(AST.getSourceManager(), FileRange->getEnd())};
333+
Position LSPPos = Side == HintSide::Left ? LSPRange.start : LSPRange.end;
334+
if (RestrictRange &&
335+
(LSPPos < RestrictRange->start || !(LSPPos < RestrictRange->end)))
336+
return;
321337
// The hint may be in a file other than the main file (for example, a header
322338
// file that was included after the preamble), do not show in that case.
323339
if (!AST.getSourceManager().isWrittenInMainFile(FileRange->getBegin()))
324340
return;
325-
Results.push_back(InlayHint{
326-
Range{
327-
sourceLocToPosition(AST.getSourceManager(), FileRange->getBegin()),
328-
sourceLocToPosition(AST.getSourceManager(), FileRange->getEnd())},
329-
Kind, Label.str()});
341+
Results.push_back(
342+
InlayHint{LSPPos, LSPRange, Kind, (Prefix + Label + Suffix).str()});
330343
}
331344

332345
void addTypeHint(SourceRange R, QualType T, llvm::StringRef Prefix) {
@@ -341,11 +354,13 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
341354

342355
std::string TypeName = T.getAsString(Policy);
343356
if (TypeName.length() < TypeNameLimit)
344-
addInlayHint(R, InlayHintKind::TypeHint, std::string(Prefix) + TypeName);
357+
addInlayHint(R, HintSide::Right, InlayHintKind::TypeHint, Prefix,
358+
TypeName, /*Suffix=*/"");
345359
}
346360

347361
std::vector<InlayHint> &Results;
348362
ASTContext &AST;
363+
llvm::Optional<Range> RestrictRange;
349364
FileID MainFileID;
350365
StringRef MainFileBuf;
351366
const HeuristicResolver *Resolver;
@@ -361,9 +376,12 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
361376
static const size_t TypeNameLimit = 32;
362377
};
363378

364-
std::vector<InlayHint> inlayHints(ParsedAST &AST) {
379+
} // namespace
380+
381+
std::vector<InlayHint> inlayHints(ParsedAST &AST,
382+
llvm::Optional<Range> RestrictRange) {
365383
std::vector<InlayHint> Results;
366-
InlayHintVisitor Visitor(Results, AST);
384+
InlayHintVisitor Visitor(Results, AST, std::move(RestrictRange));
367385
Visitor.TraverseAST(AST.getASTContext());
368386

369387
// De-duplicate hints. Duplicates can sometimes occur due to e.g. explicit

clang-tools-extra/clangd/InlayHints.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ namespace clang {
2222
namespace clangd {
2323
class ParsedAST;
2424

25-
// Compute and return all inlay hints for a file.
26-
std::vector<InlayHint> inlayHints(ParsedAST &AST);
25+
/// Compute and return inlay hints for a file.
26+
/// If RestrictRange is set, return only hints whose location is in that range.
27+
std::vector<InlayHint> inlayHints(ParsedAST &AST,
28+
llvm::Optional<Range> RestrictRange);
2729

2830
} // namespace clangd
2931
} // namespace clang

clang-tools-extra/clangd/Protocol.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,7 @@ llvm::json::Value toJSON(const CallHierarchyOutgoingCall &C) {
13171317
bool fromJSON(const llvm::json::Value &Params, InlayHintsParams &R,
13181318
llvm::json::Path P) {
13191319
llvm::json::ObjectMapper O(Params, P);
1320-
return O && O.map("textDocument", R.textDocument);
1320+
return O && O.map("textDocument", R.textDocument) && O.map("range", R.range);
13211321
}
13221322

13231323
llvm::json::Value toJSON(InlayHintKind K) {
@@ -1331,16 +1331,18 @@ llvm::json::Value toJSON(InlayHintKind K) {
13311331
}
13321332

13331333
llvm::json::Value toJSON(const InlayHint &H) {
1334-
return llvm::json::Object{
1335-
{"range", H.range}, {"kind", H.kind}, {"label", H.label}};
1334+
return llvm::json::Object{{"position", H.position},
1335+
{"range", H.range},
1336+
{"kind", H.kind},
1337+
{"label", H.label}};
13361338
}
13371339
bool operator==(const InlayHint &A, const InlayHint &B) {
1338-
return std::tie(A.kind, A.range, A.label) ==
1339-
std::tie(B.kind, B.range, B.label);
1340+
return std::tie(A.position, A.range, A.kind, A.label) ==
1341+
std::tie(B.position, B.range, B.kind, B.label);
13401342
}
13411343
bool operator<(const InlayHint &A, const InlayHint &B) {
1342-
return std::tie(A.kind, A.range, A.label) <
1343-
std::tie(B.kind, B.range, B.label);
1344+
return std::tie(A.position, A.range, A.kind, A.label) <
1345+
std::tie(B.position, B.range, B.kind, B.label);
13441346
}
13451347

13461348
static const char *toString(OffsetEncoding OE) {

clang-tools-extra/clangd/Protocol.h

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,10 +1512,15 @@ struct CallHierarchyOutgoingCall {
15121512
};
15131513
llvm::json::Value toJSON(const CallHierarchyOutgoingCall &);
15141514

1515-
/// The parameter of a `textDocument/inlayHints` request.
1515+
/// The parameter of a `clangd/inlayHints` request.
1516+
///
1517+
/// This is a clangd extension.
15161518
struct InlayHintsParams {
15171519
/// The text document for which inlay hints are requested.
15181520
TextDocumentIdentifier textDocument;
1521+
1522+
/// If set, requests inlay hints for only part of the document.
1523+
llvm::Optional<Range> range;
15191524
};
15201525
bool fromJSON(const llvm::json::Value &, InlayHintsParams &, llvm::json::Path);
15211526

@@ -1542,19 +1547,27 @@ enum class InlayHintKind {
15421547
llvm::json::Value toJSON(InlayHintKind);
15431548

15441549
/// An annotation to be displayed inline next to a range of source code.
1550+
///
1551+
/// This is a clangd extension.
15451552
struct InlayHint {
1553+
/// The position between two characters where the hint should be displayed.
1554+
///
1555+
/// For example, n parameter hint may be positioned before an argument.
1556+
Position position;
1557+
15461558
/// The range of source code to which the hint applies.
1547-
/// We provide the entire range, rather than just the endpoint
1548-
/// relevant to `position` (e.g. the start of the range for
1549-
/// InlayHintPosition::Before), to give clients the flexibility
1550-
/// to make choices like only displaying the hint while the cursor
1551-
/// is over the range, rather than displaying it all the time.
1559+
///
1560+
/// For example, a parameter hint may have the argument as its range.
1561+
/// The range allows clients more flexibility of when/how to display the hint.
15521562
Range range;
15531563

1554-
/// The type of hint.
1564+
/// The type of hint, such as a parameter hint.
15551565
InlayHintKind kind;
15561566

1557-
/// The label that is displayed in the editor.
1567+
/// The label that is displayed in the editor, such as a parameter name.
1568+
///
1569+
/// The label may contain punctuation and/or whitespace to allow it to read
1570+
/// naturally when placed inline with the code.
15581571
std::string label;
15591572
};
15601573
llvm::json::Value toJSON(const InlayHint &);

clang-tools-extra/clangd/SemanticHighlighting.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ class HighlightingsBuilder {
468468
const LangOptions &LangOpts;
469469
std::vector<HighlightingToken> Tokens;
470470
std::map<Range, llvm::SmallVector<HighlightingModifier, 1>> ExtraModifiers;
471-
const HeuristicResolver *Resolver;
471+
const HeuristicResolver *Resolver = nullptr;
472472
// returned from addToken(InvalidLoc)
473473
HighlightingToken InvalidHighlightingToken;
474474
};

clang-tools-extra/clangd/TUScheduler.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,6 @@ void ASTWorker::update(ParseInputs Inputs, WantDiagnostics WantDiags,
856856
// LatestPreamble is only populated by ASTWorker thread.
857857
return LatestPreamble || !PreambleRequests.empty() || Done;
858858
});
859-
return;
860859
};
861860
startTask(TaskName, std::move(Task), UpdateType{WantDiags, ContentChanged},
862861
TUScheduler::NoInvalidation);

0 commit comments

Comments
 (0)