Skip to content

Commit d74fdd2

Browse files
author
Dimitri Ratz
committed
Collect symbols tags from AST in the method supertypes.
1 parent 57fb1ac commit d74fdd2

File tree

10 files changed

+109
-102
lines changed

10 files changed

+109
-102
lines changed

clang-tools-extra/clangd/ClangdLSPServer.cpp

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,9 @@ CodeAction toCodeAction(const Fix &F, const URIForFile &File,
136136
Edit.textDocument = VersionedTextDocumentIdentifier{{File}, Version};
137137
for (const auto &E : F.Edits)
138138
Edit.edits.push_back(
139-
{E.range, E.newText,
140-
SupportChangeAnnotation ? E.annotationId : ""});
139+
{E.range, E.newText, SupportChangeAnnotation ? E.annotationId : ""});
141140
if (SupportChangeAnnotation) {
142-
for (const auto &[AID, Annotation]: F.Annotations)
141+
for (const auto &[AID, Annotation] : F.Annotations)
143142
Action.edit->changeAnnotations[AID] = Annotation;
144143
}
145144
}
@@ -909,24 +908,24 @@ void ClangdLSPServer::onRename(const RenameParams &Params,
909908
if (!Server->getDraft(File))
910909
return Reply(llvm::make_error<LSPError>(
911910
"onRename called for non-added file", ErrorCode::InvalidParams));
912-
Server->rename(File, Params.position, Params.newName, Opts.Rename,
913-
[File, Params, Reply = std::move(Reply),
914-
this](llvm::Expected<RenameResult> R) mutable {
915-
if (!R)
916-
return Reply(R.takeError());
917-
if (auto Err = validateEdits(*Server, R->GlobalChanges))
918-
return Reply(std::move(Err));
919-
WorkspaceEdit Result;
920-
// FIXME: use documentChanges if SupportDocumentChanges is
921-
// true.
922-
Result.changes.emplace();
923-
for (const auto &Rep : R->GlobalChanges) {
924-
(*Result
925-
.changes)[URI::createFile(Rep.first()).toString()] =
926-
Rep.second.asTextEdits();
927-
}
928-
Reply(Result);
929-
});
911+
Server->rename(
912+
File, Params.position, Params.newName, Opts.Rename,
913+
[File, Params, Reply = std::move(Reply),
914+
this](llvm::Expected<RenameResult> R) mutable {
915+
if (!R)
916+
return Reply(R.takeError());
917+
if (auto Err = validateEdits(*Server, R->GlobalChanges))
918+
return Reply(std::move(Err));
919+
WorkspaceEdit Result;
920+
// FIXME: use documentChanges if SupportDocumentChanges is
921+
// true.
922+
Result.changes.emplace();
923+
for (const auto &Rep : R->GlobalChanges) {
924+
(*Result.changes)[URI::createFile(Rep.first()).toString()] =
925+
Rep.second.asTextEdits();
926+
}
927+
Reply(Result);
928+
});
930929
}
931930

932931
void ClangdLSPServer::onDocumentDidClose(
@@ -1070,7 +1069,7 @@ void ClangdLSPServer::onCodeAction(const CodeActionParams &Params,
10701069
std::map<ClangdServer::DiagRef, clangd::Diagnostic> ToLSPDiags;
10711070
ClangdServer::CodeActionInputs Inputs;
10721071

1073-
for (const auto& LSPDiag : Params.context.diagnostics) {
1072+
for (const auto &LSPDiag : Params.context.diagnostics) {
10741073
if (auto DiagRef = getDiagRef(File.file(), LSPDiag)) {
10751074
ToLSPDiags[*DiagRef] = LSPDiag;
10761075
Inputs.Diagnostics.push_back(*DiagRef);
@@ -1079,13 +1078,9 @@ void ClangdLSPServer::onCodeAction(const CodeActionParams &Params,
10791078
Inputs.File = File.file();
10801079
Inputs.Selection = Params.range;
10811080
Inputs.RequestedActionKinds = Params.context.only;
1082-
Inputs.TweakFilter = [this](const Tweak &T) {
1083-
return Opts.TweakFilter(T);
1084-
};
1085-
auto CB = [this,
1086-
Reply = std::move(Reply),
1087-
ToLSPDiags = std::move(ToLSPDiags), File,
1088-
Selection = Params.range](
1081+
Inputs.TweakFilter = [this](const Tweak &T) { return Opts.TweakFilter(T); };
1082+
auto CB = [this, Reply = std::move(Reply), ToLSPDiags = std::move(ToLSPDiags),
1083+
File, Selection = Params.range](
10891084
llvm::Expected<ClangdServer::CodeActionResult> Fixits) mutable {
10901085
if (!Fixits)
10911086
return Reply(Fixits.takeError());
@@ -1094,8 +1089,7 @@ void ClangdLSPServer::onCodeAction(const CodeActionParams &Params,
10941089
for (const auto &QF : Fixits->QuickFixes) {
10951090
CAs.push_back(toCodeAction(QF.F, File, Version, SupportsDocumentChanges,
10961091
SupportsChangeAnnotation));
1097-
if (auto It = ToLSPDiags.find(QF.Diag);
1098-
It != ToLSPDiags.end()) {
1092+
if (auto It = ToLSPDiags.find(QF.Diag); It != ToLSPDiags.end()) {
10991093
CAs.back().diagnostics = {It->second};
11001094
}
11011095
}
@@ -1356,7 +1350,8 @@ void ClangdLSPServer::onResolveTypeHierarchy(
13561350
}
13571351
Reply(serializeTHIForExtension(std::move(**Resp)));
13581352
};
1359-
Server->resolveTypeHierarchy(Params.item.uri.file(), Params.item, Params.resolve, Params.direction,
1353+
Server->resolveTypeHierarchy(Params.item.uri.file(), Params.item,
1354+
Params.resolve, Params.direction,
13601355
std::move(Serialize));
13611356
}
13621357

@@ -1370,7 +1365,7 @@ void ClangdLSPServer::onPrepareTypeHierarchy(
13701365
void ClangdLSPServer::onSuperTypes(
13711366
const ResolveTypeHierarchyItemParams &Params,
13721367
Callback<std::optional<std::vector<TypeHierarchyItem>>> Reply) {
1373-
Server->superTypes(Params.item, std::move(Reply));
1368+
Server->superTypes(Params.item.uri.file(), Params.item, std::move(Reply));
13741369
}
13751370

13761371
void ClangdLSPServer::onSubTypes(

clang-tools-extra/clangd/ClangdLSPServer.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ class ClangdLSPServer : private ClangdServer::Callbacks,
133133
Callback<std::vector<Location>>);
134134
void onGoToImplementation(const TextDocumentPositionParams &,
135135
Callback<std::vector<Location>>);
136-
void onReference(const ReferenceParams &, Callback<std::vector<ReferenceLocation>>);
136+
void onReference(const ReferenceParams &,
137+
Callback<std::vector<ReferenceLocation>>);
137138
void onSwitchSourceHeader(const TextDocumentIdentifier &,
138139
Callback<std::optional<URIForFile>>);
139140
void onDocumentHighlight(const TextDocumentPositionParams &,
@@ -243,7 +244,7 @@ class ClangdLSPServer : private ClangdServer::Callbacks,
243244
/// Used to indicate the ClangdLSPServer is being destroyed.
244245
std::atomic<bool> IsBeingDestroyed = {false};
245246

246-
// FIXME: The caching is a temporary solution to get corresponding clangd
247+
// FIXME: The caching is a temporary solution to get corresponding clangd
247248
// diagnostic from a LSP diagnostic.
248249
// Ideally, ClangdServer can generate an identifier for each diagnostic,
249250
// emit them via the LSP's data field (which was newly added in LSP 3.16).
@@ -259,11 +260,9 @@ class ClangdLSPServer : private ClangdServer::Callbacks,
259260
return {LSPDiag.range, LSPDiag.message};
260261
}
261262
/// A map from LSP diagnostic to clangd-naive diagnostic.
262-
typedef std::map<DiagKey, ClangdServer::DiagRef>
263-
DiagnosticToDiagRefMap;
263+
typedef std::map<DiagKey, ClangdServer::DiagRef> DiagnosticToDiagRefMap;
264264
/// Caches the mapping LSP and clangd-naive diagnostics per file.
265-
llvm::StringMap<DiagnosticToDiagRefMap>
266-
DiagRefMap;
265+
llvm::StringMap<DiagnosticToDiagRefMap> DiagRefMap;
267266

268267
// Last semantic-tokens response, for incremental requests.
269268
std::mutex SemanticTokensMutex;

clang-tools-extra/clangd/ClangdServer.cpp

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ namespace clangd {
6262
namespace {
6363

6464
// Tracks number of times a tweak has been offered.
65-
static constexpr trace::Metric TweakAvailable(
66-
"tweak_available", trace::Metric::Counter, "tweak_id");
65+
static constexpr trace::Metric
66+
TweakAvailable("tweak_available", trace::Metric::Counter, "tweak_id");
6767

6868
// Update the FileIndex with new ASTs and plumb the diagnostics responses.
6969
struct UpdateIndexCallbacks : public ParsingCallbacks {
@@ -872,45 +872,42 @@ void ClangdServer::typeHierarchy(PathRef File, Position Pos, int Resolve,
872872
}
873873

874874
void ClangdServer::superTypes(
875-
const TypeHierarchyItem &Item,
875+
PathRef File, const TypeHierarchyItem &Item,
876876
Callback<std::optional<std::vector<TypeHierarchyItem>>> CB) {
877-
WorkScheduler->run("typeHierarchy/superTypes", /*Path=*/"",
878-
[=, CB = std::move(CB)]() mutable {
879-
CB(clangd::superTypes(Item, Index));
880-
});
877+
auto Action = [File = File.str(), Item, CB = std::move(CB),
878+
this](llvm::Expected<InputsAndAST> InpAST) mutable {
879+
if (!InpAST)
880+
return CB(InpAST.takeError());
881+
CB(clangd::superTypes(Item, Index, InpAST->AST));
882+
};
883+
WorkScheduler->runWithAST("superTypes Calls", File, std::move(Action));
881884
}
882885

883886
void ClangdServer::subTypes(PathRef File, const TypeHierarchyItem &Item,
884887
Callback<std::vector<TypeHierarchyItem>> CB) {
885888

886-
auto Action = [File = File.str(), Item, CB = std::move(CB), this](
887-
llvm::Expected<InputsAndAST> InpAST) mutable {
888-
if (!InpAST)
889-
return CB(InpAST.takeError());
890-
CB(clangd::subTypes(Item, Index, InpAST->AST));
891-
};
892-
WorkScheduler->runWithAST("subTypes Calls", File, std::move(Action));
893-
// WorkScheduler->run(
894-
// "typeHierarchy/subTypes", /*Path=*/"",
895-
// [=, CB = std::move(CB)]() mutable { CB(clangd::subTypes(Item, Index)); });
889+
auto Action = [File = File.str(), Item, CB = std::move(CB),
890+
this](llvm::Expected<InputsAndAST> InpAST) mutable {
891+
if (!InpAST)
892+
return CB(InpAST.takeError());
893+
CB(clangd::subTypes(Item, Index, InpAST->AST));
894+
};
895+
WorkScheduler->runWithAST("subTypes Calls", File, std::move(Action));
896896
}
897897

898-
void ClangdServer::resolveTypeHierarchy(PathRef File,
899-
TypeHierarchyItem Item, int Resolve, TypeHierarchyDirection Direction,
898+
void ClangdServer::resolveTypeHierarchy(
899+
PathRef File, TypeHierarchyItem Item, int Resolve,
900+
TypeHierarchyDirection Direction,
900901
Callback<std::optional<TypeHierarchyItem>> CB) {
901-
auto Action = [=, CB = std::move(CB), this](
902-
llvm::Expected<InputsAndAST> InpAST) mutable {
902+
auto Action = [=, CB = std::move(CB),
903+
this](llvm::Expected<InputsAndAST> InpAST) mutable {
903904
if (!InpAST)
904905
return CB(InpAST.takeError());
905906
clangd::resolveTypeHierarchy(Item, Resolve, Direction, Index, InpAST->AST);
906907
CB(Item);
907908
};
908-
WorkScheduler->runWithAST("resolveTypeHierarchy Calls", File, std::move(Action));
909-
// WorkScheduler->run(
910-
// "Resolve Type Hierarchy", "", [=, CB = std::move(CB)]() mutable {
911-
// clangd::resolveTypeHierarchy(Item, Resolve, Direction, Index);
912-
// CB(Item);
913-
// });
909+
WorkScheduler->runWithAST("resolveTypeHierarchy Calls", File,
910+
std::move(Action));
914911
}
915912

916913
void ClangdServer::prepareCallHierarchy(
@@ -925,11 +922,10 @@ void ClangdServer::prepareCallHierarchy(
925922
}
926923

927924
void ClangdServer::incomingCalls(
928-
PathRef File,
929-
const CallHierarchyItem &Item,
925+
PathRef File, const CallHierarchyItem &Item,
930926
Callback<std::vector<CallHierarchyIncomingCall>> CB) {
931-
auto Action = [Item, CB = std::move(CB), this](
932-
llvm::Expected<InputsAndAST> InpAST) mutable {
927+
auto Action = [Item, CB = std::move(CB),
928+
this](llvm::Expected<InputsAndAST> InpAST) mutable {
933929
if (!InpAST)
934930
return CB(InpAST.takeError());
935931
CB(clangd::incomingCalls(Item, Index, InpAST->AST));
@@ -949,11 +945,10 @@ void ClangdServer::inlayHints(PathRef File, std::optional<Range> RestrictRange,
949945
}
950946

951947
void ClangdServer::outgoingCalls(
952-
PathRef File,
953-
const CallHierarchyItem &Item,
948+
PathRef File, const CallHierarchyItem &Item,
954949
Callback<std::vector<CallHierarchyOutgoingCall>> CB) {
955-
auto Action = [Item, CB = std::move(CB), this](
956-
llvm::Expected<InputsAndAST> InpAST) mutable {
950+
auto Action = [Item, CB = std::move(CB),
951+
this](llvm::Expected<InputsAndAST> InpAST) mutable {
957952
if (!InpAST)
958953
return CB(InpAST.takeError());
959954
CB(clangd::outgoingCalls(Item, Index, InpAST->AST));

clang-tools-extra/clangd/ClangdServer.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ class ClangdServer {
283283
TypeHierarchyDirection Direction,
284284
Callback<std::vector<TypeHierarchyItem>> CB);
285285
/// Get direct parents of a type hierarchy item.
286-
void superTypes(const TypeHierarchyItem &Item,
286+
void superTypes(PathRef File, const TypeHierarchyItem &Item,
287287
Callback<std::optional<std::vector<TypeHierarchyItem>>> CB);
288288
/// Get direct children of a type hierarchy item.
289289
void subTypes(PathRef File, const TypeHierarchyItem &Item,
@@ -299,8 +299,7 @@ class ClangdServer {
299299
Callback<std::vector<CallHierarchyItem>> CB);
300300

301301
/// Resolve incoming calls for a given call hierarchy item.
302-
void incomingCalls(PathRef File,
303-
const CallHierarchyItem &Item,
302+
void incomingCalls(PathRef File, const CallHierarchyItem &Item,
304303
Callback<std::vector<CallHierarchyIncomingCall>> CB);
305304

306305
/// Resolve outgoing calls for a given call hierarchy item.

clang-tools-extra/clangd/Protocol.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,9 @@ struct TypeHierarchyItem {
15481548
/// The kind of this item.
15491549
SymbolKind kind;
15501550

1551+
/// The symbol tags for this item.
1552+
std::vector<SymbolTag> tags;
1553+
15511554
/// More detail for this item, e.g. the signature of a function.
15521555
std::optional<std::string> detail;
15531556

0 commit comments

Comments
 (0)