Skip to content

Commit fdab9b9

Browse files
committed
[clangd] Turn Path and PathRef into classes
1 parent cfc035a commit fdab9b9

Some content is hidden

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

50 files changed

+613
-357
lines changed

Diff for: clang-tools-extra/clangd/ASTSignals.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ ASTSignals ASTSignals::derive(const ParsedAST &AST) {
1919
trace::Span Span("ASTSignals::derive");
2020
ASTSignals Signals;
2121
Signals.InsertionDirective = preferredIncludeDirective(
22-
AST.tuPath(), AST.getLangOpts(),
22+
AST.tuPath().raw(), AST.getLangOpts(),
2323
AST.getIncludeStructure().MainFileIncludes, AST.getLocalTopLevelDecls());
2424
const SourceManager &SM = AST.getSourceManager();
2525
findExplicitReferences(

Diff for: clang-tools-extra/clangd/ClangdLSPServer.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -926,11 +926,11 @@ void ClangdLSPServer::onDocumentDidClose(
926926

927927
{
928928
std::lock_guard<std::mutex> Lock(DiagRefMutex);
929-
DiagRefMap.erase(File);
929+
DiagRefMap.erase(File.raw());
930930
}
931931
{
932932
std::lock_guard<std::mutex> HLock(SemanticTokensMutex);
933-
LastSemanticTokens.erase(File);
933+
LastSemanticTokens.erase(File.raw());
934934
}
935935
// clangd will not send updates for this file anymore, so we empty out the
936936
// list of diagnostics shown on the client (e.g. in the "Problems" pane of
@@ -1816,7 +1816,7 @@ void ClangdLSPServer::onDiagnosticsReady(PathRef File, llvm::StringRef Version,
18161816
// Cache DiagRefMap
18171817
{
18181818
std::lock_guard<std::mutex> Lock(DiagRefMutex);
1819-
DiagRefMap[File] = LocalDiagMap;
1819+
DiagRefMap[File.raw()] = LocalDiagMap;
18201820
}
18211821

18221822
// Send a notification to the LSP client.

Diff for: clang-tools-extra/clangd/ClangdServer.cpp

+38-34
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ struct UpdateIndexCallbacks : public ParsingCallbacks {
8888
indexStdlib(CI, std::move(*Loc));
8989

9090
// FIndex outlives the UpdateIndexCallbacks.
91-
auto Task = [FIndex(FIndex), Path(Path.str()), Version(Version.str()),
91+
auto Task = [FIndex(FIndex), Path(Path.owned()), Version(Version.str()),
9292
ASTCtx(std::move(ASTCtx)), PI(std::move(PI))]() mutable {
9393
trace::Span Tracer("PreambleIndexing");
94-
FIndex->updatePreamble(Path, Version, ASTCtx.getASTContext(),
94+
FIndex->updatePreamble(Path.raw(), Version, ASTCtx.getASTContext(),
9595
ASTCtx.getPreprocessor(), *PI);
9696
};
9797

9898
if (Tasks) {
99-
Tasks->runAsync("Preamble indexing for:" + Path + Version,
99+
Tasks->runAsync("Preamble indexing for:" + Path.raw() + Version,
100100
std::move(Task));
101101
} else
102102
Task();
@@ -262,7 +262,7 @@ ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB,
262262
BackgroundIdx = std::make_unique<BackgroundIndex>(
263263
TFS, CDB,
264264
BackgroundIndexStorage::createDiskBackedStorageFactory(
265-
[&CDB](llvm::StringRef File) { return CDB.getProjectInfo(File); }),
265+
[&CDB](PathRef File) { return CDB.getProjectInfo(File); }),
266266
std::move(BGOpts));
267267
AddIndex(BackgroundIdx.get());
268268
}
@@ -316,14 +316,14 @@ void ClangdServer::addDocument(PathRef File, llvm::StringRef Contents,
316316
bool NewFile = WorkScheduler->update(File, Inputs, WantDiags);
317317
// If we loaded Foo.h, we want to make sure Foo.cpp is indexed.
318318
if (NewFile && BackgroundIdx)
319-
BackgroundIdx->boostRelated(File);
319+
BackgroundIdx->boostRelated(File.raw());
320320
}
321321

322322
void ClangdServer::reparseOpenFilesIfNeeded(
323323
llvm::function_ref<bool(llvm::StringRef File)> Filter) {
324324
// Reparse only opened files that were modified.
325325
for (const Path &FilePath : DraftMgr.getActiveFiles())
326-
if (Filter(FilePath))
326+
if (Filter(FilePath.raw()))
327327
if (auto Draft = DraftMgr.getDraft(FilePath)) // else disappeared in race?
328328
addDocument(FilePath, *Draft->Contents, Draft->Version,
329329
WantDiagnostics::Auto);
@@ -340,7 +340,7 @@ std::function<Context(PathRef)>
340340
ClangdServer::createConfiguredContextProvider(const config::Provider *Provider,
341341
Callbacks *Publish) {
342342
if (!Provider)
343-
return [](llvm::StringRef) { return Context::current().clone(); };
343+
return [](PathRef) { return Context::current().clone(); };
344344

345345
struct Impl {
346346
const config::Provider *Provider;
@@ -408,8 +408,8 @@ ClangdServer::createConfiguredContextProvider(const config::Provider *Provider,
408408
};
409409

410410
// Copyable wrapper.
411-
return [I(std::make_shared<Impl>(Provider, Publish))](llvm::StringRef Path) {
412-
return (*I)(Path);
411+
return [I(std::make_shared<Impl>(Provider, Publish))](PathRef Path) {
412+
return (*I)(Path.raw());
413413
};
414414
}
415415

@@ -426,7 +426,7 @@ void ClangdServer::codeComplete(PathRef File, Position Pos,
426426
if (!CodeCompleteOpts.Index) // Respect overridden index.
427427
CodeCompleteOpts.Index = Index;
428428

429-
auto Task = [Pos, CodeCompleteOpts, File = File.str(), CB = std::move(CB),
429+
auto Task = [Pos, CodeCompleteOpts, File = File.owned(), CB = std::move(CB),
430430
this](llvm::Expected<InputsAndPreamble> IP) mutable {
431431
if (!IP)
432432
return CB(IP.takeError());
@@ -442,7 +442,8 @@ void ClangdServer::codeComplete(PathRef File, Position Pos,
442442
SpecFuzzyFind.emplace();
443443
{
444444
std::lock_guard<std::mutex> Lock(CachedCompletionFuzzyFindRequestMutex);
445-
SpecFuzzyFind->CachedReq = CachedCompletionFuzzyFindRequestByFile[File];
445+
SpecFuzzyFind->CachedReq =
446+
CachedCompletionFuzzyFindRequestByFile[File.raw()];
446447
}
447448
}
448449
ParseInputs ParseInput{IP->Command, &getHeaderFS(), IP->Contents.str()};
@@ -473,7 +474,8 @@ void ClangdServer::codeComplete(PathRef File, Position Pos,
473474
return;
474475
if (SpecFuzzyFind->NewReq) {
475476
std::lock_guard<std::mutex> Lock(CachedCompletionFuzzyFindRequestMutex);
476-
CachedCompletionFuzzyFindRequestByFile[File] = *SpecFuzzyFind->NewReq;
477+
CachedCompletionFuzzyFindRequestByFile[File.raw()] =
478+
*SpecFuzzyFind->NewReq;
477479
}
478480
// Explicitly block until async task completes, this is fine as we've
479481
// already provided reply to the client and running as a preamble task
@@ -495,7 +497,7 @@ void ClangdServer::signatureHelp(PathRef File, Position Pos,
495497
MarkupKind DocumentationFormat,
496498
Callback<SignatureHelp> CB) {
497499

498-
auto Action = [Pos, File = File.str(), CB = std::move(CB),
500+
auto Action = [Pos, File = File.owned(), CB = std::move(CB),
499501
DocumentationFormat,
500502
this](llvm::Expected<InputsAndPreamble> IP) mutable {
501503
if (!IP)
@@ -540,22 +542,23 @@ void ClangdServer::formatFile(PathRef File, std::optional<Range> Rng,
540542
}
541543

542544
// Call clang-format.
543-
auto Action = [File = File.str(), Code = std::move(*Code),
545+
auto Action = [File = File.owned(), Code = std::move(*Code),
544546
Ranges = std::vector<tooling::Range>{RequestedRange},
545547
CB = std::move(CB), this]() mutable {
546-
format::FormatStyle Style = getFormatStyleForFile(File, Code, TFS, true);
548+
format::FormatStyle Style =
549+
getFormatStyleForFile(File.raw(), Code, TFS, true);
547550
tooling::Replacements IncludeReplaces =
548-
format::sortIncludes(Style, Code, Ranges, File);
551+
format::sortIncludes(Style, Code, Ranges, File.raw());
549552
auto Changed = tooling::applyAllReplacements(Code, IncludeReplaces);
550553
if (!Changed)
551554
return CB(Changed.takeError());
552555

553556
CB(IncludeReplaces.merge(format::reformat(
554557
Style, *Changed,
555558
tooling::calculateRangesAfterReplacements(IncludeReplaces, Ranges),
556-
File)));
559+
File.raw())));
557560
};
558-
WorkScheduler->runQuick("Format", File, std::move(Action));
561+
WorkScheduler->runQuick("Format", File.raw(), std::move(Action));
559562
}
560563

561564
void ClangdServer::formatOnType(PathRef File, Position Pos,
@@ -568,24 +571,24 @@ void ClangdServer::formatOnType(PathRef File, Position Pos,
568571
llvm::Expected<size_t> CursorPos = positionToOffset(*Code, Pos);
569572
if (!CursorPos)
570573
return CB(CursorPos.takeError());
571-
auto Action = [File = File.str(), Code = std::move(*Code),
574+
auto Action = [File = File.owned(), Code = std::move(*Code),
572575
TriggerText = TriggerText.str(), CursorPos = *CursorPos,
573576
CB = std::move(CB), this]() mutable {
574-
auto Style = getFormatStyleForFile(File, Code, TFS, false);
577+
auto Style = getFormatStyleForFile(File.raw(), Code, TFS, false);
575578
std::vector<TextEdit> Result;
576579
for (const tooling::Replacement &R :
577580
formatIncremental(Code, CursorPos, TriggerText, Style))
578581
Result.push_back(replacementToEdit(Code, R));
579582
return CB(Result);
580583
};
581-
WorkScheduler->runQuick("FormatOnType", File, std::move(Action));
584+
WorkScheduler->runQuick("FormatOnType", File.raw(), std::move(Action));
582585
}
583586

584587
void ClangdServer::prepareRename(PathRef File, Position Pos,
585588
std::optional<std::string> NewName,
586589
const RenameOptions &RenameOpts,
587590
Callback<RenameResult> CB) {
588-
auto Action = [Pos, File = File.str(), CB = std::move(CB),
591+
auto Action = [Pos, File = File.owned(), CB = std::move(CB),
589592
NewName = std::move(NewName),
590593
RenameOpts](llvm::Expected<InputsAndAST> InpAST) mutable {
591594
if (!InpAST)
@@ -594,7 +597,7 @@ void ClangdServer::prepareRename(PathRef File, Position Pos,
594597
// only need main-file references
595598
auto Results =
596599
clangd::rename({Pos, NewName.value_or("__clangd_rename_placeholder"),
597-
InpAST->AST, File, /*FS=*/nullptr,
600+
InpAST->AST, File.raw(), /*FS=*/nullptr,
598601
/*Index=*/nullptr, RenameOpts});
599602
if (!Results) {
600603
// LSP says to return null on failure, but that will result in a generic
@@ -610,21 +613,21 @@ void ClangdServer::prepareRename(PathRef File, Position Pos,
610613
void ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName,
611614
const RenameOptions &Opts,
612615
Callback<RenameResult> CB) {
613-
auto Action = [File = File.str(), NewName = NewName.str(), Pos, Opts,
616+
auto Action = [File = File.owned(), NewName = NewName.str(), Pos, Opts,
614617
CB = std::move(CB),
615618
this](llvm::Expected<InputsAndAST> InpAST) mutable {
616619
// Tracks number of files edited per invocation.
617620
static constexpr trace::Metric RenameFiles("rename_files",
618621
trace::Metric::Distribution);
619622
if (!InpAST)
620623
return CB(InpAST.takeError());
621-
auto R = clangd::rename({Pos, NewName, InpAST->AST, File,
624+
auto R = clangd::rename({Pos, NewName, InpAST->AST, File.raw(),
622625
DirtyFS->view(std::nullopt), Index, Opts});
623626
if (!R)
624627
return CB(R.takeError());
625628

626629
if (Opts.WantFormat) {
627-
auto Style = getFormatStyleForFile(File, InpAST->Inputs.Contents,
630+
auto Style = getFormatStyleForFile(File.raw(), InpAST->Inputs.Contents,
628631
*InpAST->Inputs.TFS, false);
629632
llvm::Error Err = llvm::Error::success();
630633
for (auto &E : R->GlobalChanges)
@@ -756,7 +759,7 @@ void ClangdServer::applyTweak(PathRef File, Range Sel, StringRef TweakID,
756759
static constexpr trace::Metric TweakFailed(
757760
"tweak_failed", trace::Metric::Counter, "tweak_id");
758761
TweakAttempt.record(1, TweakID);
759-
auto Action = [File = File.str(), Sel, TweakID = TweakID.str(),
762+
auto Action = [File = File.owned(), Sel, TweakID = TweakID.str(),
760763
CB = std::move(CB),
761764
this](Expected<InputsAndAST> InpAST) mutable {
762765
if (!InpAST)
@@ -782,7 +785,7 @@ void ClangdServer::applyTweak(PathRef File, Range Sel, StringRef TweakID,
782785
for (auto &It : (*Effect)->ApplyEdits) {
783786
Edit &E = It.second;
784787
format::FormatStyle Style =
785-
getFormatStyleForFile(File, E.InitialCode, TFS, false);
788+
getFormatStyleForFile(File.raw(), E.InitialCode, TFS, false);
786789
if (llvm::Error Err = reformatEdit(E, Style))
787790
elog("Failed to format {0}: {1}", It.first(), std::move(Err));
788791
}
@@ -817,7 +820,7 @@ void ClangdServer::switchSourceHeader(
817820
if (auto CorrespondingFile =
818821
getCorrespondingHeaderOrSource(Path, TFS.view(std::nullopt)))
819822
return CB(std::move(CorrespondingFile));
820-
auto Action = [Path = Path.str(), CB = std::move(CB),
823+
auto Action = [Path = Path.owned(), CB = std::move(CB),
821824
this](llvm::Expected<InputsAndAST> InpAST) mutable {
822825
if (!InpAST)
823826
return CB(InpAST.takeError());
@@ -840,12 +843,12 @@ void ClangdServer::findDocumentHighlights(
840843

841844
void ClangdServer::findHover(PathRef File, Position Pos,
842845
Callback<std::optional<HoverInfo>> CB) {
843-
auto Action = [File = File.str(), Pos, CB = std::move(CB),
846+
auto Action = [File = File.owned(), Pos, CB = std::move(CB),
844847
this](llvm::Expected<InputsAndAST> InpAST) mutable {
845848
if (!InpAST)
846849
return CB(InpAST.takeError());
847850
format::FormatStyle Style = getFormatStyleForFile(
848-
File, InpAST->Inputs.Contents, *InpAST->Inputs.TFS, false);
851+
File.raw(), InpAST->Inputs.Contents, *InpAST->Inputs.TFS, false);
849852
CB(clangd::getHover(InpAST->AST, Pos, std::move(Style), Index));
850853
};
851854

@@ -855,7 +858,8 @@ void ClangdServer::findHover(PathRef File, Position Pos,
855858
void ClangdServer::typeHierarchy(PathRef File, Position Pos, int Resolve,
856859
TypeHierarchyDirection Direction,
857860
Callback<std::vector<TypeHierarchyItem>> CB) {
858-
auto Action = [File = File.str(), Pos, Resolve, Direction, CB = std::move(CB),
861+
auto Action = [File = File.owned(), Pos, Resolve, Direction,
862+
CB = std::move(CB),
859863
this](Expected<InputsAndAST> InpAST) mutable {
860864
if (!InpAST)
861865
return CB(InpAST.takeError());
@@ -894,7 +898,7 @@ void ClangdServer::resolveTypeHierarchy(
894898

895899
void ClangdServer::prepareCallHierarchy(
896900
PathRef File, Position Pos, Callback<std::vector<CallHierarchyItem>> CB) {
897-
auto Action = [File = File.str(), Pos,
901+
auto Action = [File = File.owned(), Pos,
898902
CB = std::move(CB)](Expected<InputsAndAST> InpAST) mutable {
899903
if (!InpAST)
900904
return CB(InpAST.takeError());
@@ -976,7 +980,7 @@ void ClangdServer::foldingRanges(llvm::StringRef File,
976980
WorkScheduler->runQuick("FoldingRanges", File, std::move(Action));
977981
}
978982

979-
void ClangdServer::findType(llvm::StringRef File, Position Pos,
983+
void ClangdServer::findType(PathRef File, Position Pos,
980984
Callback<std::vector<LocatedSymbol>> CB) {
981985
auto Action = [Pos, CB = std::move(CB),
982986
this](llvm::Expected<InputsAndAST> InpAST) mutable {

0 commit comments

Comments
 (0)