Skip to content

Commit 3ec19bd

Browse files
committed
[clangd] Turn Path and PathRef into classes
1 parent 32de3b9 commit 3ec19bd

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

+618
-358
lines changed

clang-tools-extra/clangd/ASTSignals.cpp

Lines changed: 1 addition & 1 deletion
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(

clang-tools-extra/clangd/ClangdLSPServer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -935,11 +935,11 @@ void ClangdLSPServer::onDocumentDidClose(
935935

936936
{
937937
std::lock_guard<std::mutex> Lock(DiagRefMutex);
938-
DiagRefMap.erase(File);
938+
DiagRefMap.erase(File.raw());
939939
}
940940
{
941941
std::lock_guard<std::mutex> HLock(SemanticTokensMutex);
942-
LastSemanticTokens.erase(File);
942+
LastSemanticTokens.erase(File.raw());
943943
}
944944
// clangd will not send updates for this file anymore, so we empty out the
945945
// list of diagnostics shown on the client (e.g. in the "Problems" pane of
@@ -1832,7 +1832,7 @@ void ClangdLSPServer::onDiagnosticsReady(PathRef File, llvm::StringRef Version,
18321832
// Cache DiagRefMap
18331833
{
18341834
std::lock_guard<std::mutex> Lock(DiagRefMutex);
1835-
DiagRefMap[File] = LocalDiagMap;
1835+
DiagRefMap[File.raw()] = LocalDiagMap;
18361836
}
18371837

18381838
// Send a notification to the LSP client.

clang-tools-extra/clangd/ClangdServer.cpp

Lines changed: 37 additions & 34 deletions
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()};
@@ -474,7 +475,8 @@ void ClangdServer::codeComplete(PathRef File, Position Pos,
474475
return;
475476
if (SpecFuzzyFind->NewReq) {
476477
std::lock_guard<std::mutex> Lock(CachedCompletionFuzzyFindRequestMutex);
477-
CachedCompletionFuzzyFindRequestByFile[File] = *SpecFuzzyFind->NewReq;
478+
CachedCompletionFuzzyFindRequestByFile[File.raw()] =
479+
*SpecFuzzyFind->NewReq;
478480
}
479481
// Explicitly block until async task completes, this is fine as we've
480482
// already provided reply to the client and running as a preamble task
@@ -496,7 +498,7 @@ void ClangdServer::signatureHelp(PathRef File, Position Pos,
496498
MarkupKind DocumentationFormat,
497499
Callback<SignatureHelp> CB) {
498500

499-
auto Action = [Pos, File = File.str(), CB = std::move(CB),
501+
auto Action = [Pos, File = File.owned(), CB = std::move(CB),
500502
DocumentationFormat,
501503
this](llvm::Expected<InputsAndPreamble> IP) mutable {
502504
if (!IP)
@@ -544,22 +546,22 @@ void ClangdServer::formatFile(PathRef File, const std::vector<Range> &Rngs,
544546
}
545547

546548
// Call clang-format.
547-
auto Action = [File = File.str(), Code = std::move(*Code),
549+
auto Action = [File = File.owned(), Code = std::move(*Code),
548550
Ranges = std::move(RequestedRanges), CB = std::move(CB),
549551
this]() mutable {
550-
format::FormatStyle Style = getFormatStyleForFile(File, Code, TFS, true);
552+
format::FormatStyle Style = getFormatStyleForFile(File.raw(), Code, TFS, true);
551553
tooling::Replacements IncludeReplaces =
552-
format::sortIncludes(Style, Code, Ranges, File);
554+
format::sortIncludes(Style, Code, Ranges, File.raw());
553555
auto Changed = tooling::applyAllReplacements(Code, IncludeReplaces);
554556
if (!Changed)
555557
return CB(Changed.takeError());
556558

557559
CB(IncludeReplaces.merge(format::reformat(
558560
Style, *Changed,
559561
tooling::calculateRangesAfterReplacements(IncludeReplaces, Ranges),
560-
File)));
562+
File.raw())));
561563
};
562-
WorkScheduler->runQuick("Format", File, std::move(Action));
564+
WorkScheduler->runQuick("Format", File.raw(), std::move(Action));
563565
}
564566

565567
void ClangdServer::formatOnType(PathRef File, Position Pos,
@@ -572,24 +574,24 @@ void ClangdServer::formatOnType(PathRef File, Position Pos,
572574
llvm::Expected<size_t> CursorPos = positionToOffset(*Code, Pos);
573575
if (!CursorPos)
574576
return CB(CursorPos.takeError());
575-
auto Action = [File = File.str(), Code = std::move(*Code),
577+
auto Action = [File = File.owned(), Code = std::move(*Code),
576578
TriggerText = TriggerText.str(), CursorPos = *CursorPos,
577579
CB = std::move(CB), this]() mutable {
578-
auto Style = getFormatStyleForFile(File, Code, TFS, false);
580+
auto Style = getFormatStyleForFile(File.raw(), Code, TFS, false);
579581
std::vector<TextEdit> Result;
580582
for (const tooling::Replacement &R :
581583
formatIncremental(Code, CursorPos, TriggerText, Style))
582584
Result.push_back(replacementToEdit(Code, R));
583585
return CB(Result);
584586
};
585-
WorkScheduler->runQuick("FormatOnType", File, std::move(Action));
587+
WorkScheduler->runQuick("FormatOnType", File.raw(), std::move(Action));
586588
}
587589

588590
void ClangdServer::prepareRename(PathRef File, Position Pos,
589591
std::optional<std::string> NewName,
590592
const RenameOptions &RenameOpts,
591593
Callback<RenameResult> CB) {
592-
auto Action = [Pos, File = File.str(), CB = std::move(CB),
594+
auto Action = [Pos, File = File.owned(), CB = std::move(CB),
593595
NewName = std::move(NewName),
594596
RenameOpts](llvm::Expected<InputsAndAST> InpAST) mutable {
595597
if (!InpAST)
@@ -598,7 +600,7 @@ void ClangdServer::prepareRename(PathRef File, Position Pos,
598600
// only need main-file references
599601
auto Results =
600602
clangd::rename({Pos, NewName.value_or("__clangd_rename_placeholder"),
601-
InpAST->AST, File, /*FS=*/nullptr,
603+
InpAST->AST, File.raw(), /*FS=*/nullptr,
602604
/*Index=*/nullptr, RenameOpts});
603605
if (!Results) {
604606
// LSP says to return null on failure, but that will result in a generic
@@ -614,21 +616,21 @@ void ClangdServer::prepareRename(PathRef File, Position Pos,
614616
void ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName,
615617
const RenameOptions &Opts,
616618
Callback<RenameResult> CB) {
617-
auto Action = [File = File.str(), NewName = NewName.str(), Pos, Opts,
619+
auto Action = [File = File.owned(), NewName = NewName.str(), Pos, Opts,
618620
CB = std::move(CB),
619621
this](llvm::Expected<InputsAndAST> InpAST) mutable {
620622
// Tracks number of files edited per invocation.
621623
static constexpr trace::Metric RenameFiles("rename_files",
622624
trace::Metric::Distribution);
623625
if (!InpAST)
624626
return CB(InpAST.takeError());
625-
auto R = clangd::rename({Pos, NewName, InpAST->AST, File,
627+
auto R = clangd::rename({Pos, NewName, InpAST->AST, File.raw(),
626628
DirtyFS->view(std::nullopt), Index, Opts});
627629
if (!R)
628630
return CB(R.takeError());
629631

630632
if (Opts.WantFormat) {
631-
auto Style = getFormatStyleForFile(File, InpAST->Inputs.Contents,
633+
auto Style = getFormatStyleForFile(File.raw(), InpAST->Inputs.Contents,
632634
*InpAST->Inputs.TFS, false);
633635
llvm::Error Err = llvm::Error::success();
634636
for (auto &E : R->GlobalChanges)
@@ -760,7 +762,7 @@ void ClangdServer::applyTweak(PathRef File, Range Sel, StringRef TweakID,
760762
static constexpr trace::Metric TweakFailed(
761763
"tweak_failed", trace::Metric::Counter, "tweak_id");
762764
TweakAttempt.record(1, TweakID);
763-
auto Action = [File = File.str(), Sel, TweakID = TweakID.str(),
765+
auto Action = [File = File.owned(), Sel, TweakID = TweakID.str(),
764766
CB = std::move(CB),
765767
this](Expected<InputsAndAST> InpAST) mutable {
766768
if (!InpAST)
@@ -786,7 +788,7 @@ void ClangdServer::applyTweak(PathRef File, Range Sel, StringRef TweakID,
786788
for (auto &It : (*Effect)->ApplyEdits) {
787789
Edit &E = It.second;
788790
format::FormatStyle Style =
789-
getFormatStyleForFile(File, E.InitialCode, TFS, false);
791+
getFormatStyleForFile(File.raw(), E.InitialCode, TFS, false);
790792
if (llvm::Error Err = reformatEdit(E, Style))
791793
elog("Failed to format {0}: {1}", It.first(), std::move(Err));
792794
}
@@ -821,7 +823,7 @@ void ClangdServer::switchSourceHeader(
821823
if (auto CorrespondingFile =
822824
getCorrespondingHeaderOrSource(Path, TFS.view(std::nullopt)))
823825
return CB(std::move(CorrespondingFile));
824-
auto Action = [Path = Path.str(), CB = std::move(CB),
826+
auto Action = [Path = Path.owned(), CB = std::move(CB),
825827
this](llvm::Expected<InputsAndAST> InpAST) mutable {
826828
if (!InpAST)
827829
return CB(InpAST.takeError());
@@ -844,12 +846,12 @@ void ClangdServer::findDocumentHighlights(
844846

845847
void ClangdServer::findHover(PathRef File, Position Pos,
846848
Callback<std::optional<HoverInfo>> CB) {
847-
auto Action = [File = File.str(), Pos, CB = std::move(CB),
849+
auto Action = [File = File.owned(), Pos, CB = std::move(CB),
848850
this](llvm::Expected<InputsAndAST> InpAST) mutable {
849851
if (!InpAST)
850852
return CB(InpAST.takeError());
851853
format::FormatStyle Style = getFormatStyleForFile(
852-
File, InpAST->Inputs.Contents, *InpAST->Inputs.TFS, false);
854+
File.raw(), InpAST->Inputs.Contents, *InpAST->Inputs.TFS, false);
853855
CB(clangd::getHover(InpAST->AST, Pos, std::move(Style), Index));
854856
};
855857

@@ -859,7 +861,8 @@ void ClangdServer::findHover(PathRef File, Position Pos,
859861
void ClangdServer::typeHierarchy(PathRef File, Position Pos, int Resolve,
860862
TypeHierarchyDirection Direction,
861863
Callback<std::vector<TypeHierarchyItem>> CB) {
862-
auto Action = [File = File.str(), Pos, Resolve, Direction, CB = std::move(CB),
864+
auto Action = [File = File.owned(), Pos, Resolve, Direction,
865+
CB = std::move(CB),
863866
this](Expected<InputsAndAST> InpAST) mutable {
864867
if (!InpAST)
865868
return CB(InpAST.takeError());
@@ -898,7 +901,7 @@ void ClangdServer::resolveTypeHierarchy(
898901

899902
void ClangdServer::prepareCallHierarchy(
900903
PathRef File, Position Pos, Callback<std::vector<CallHierarchyItem>> CB) {
901-
auto Action = [File = File.str(), Pos,
904+
auto Action = [File = File.owned(), Pos,
902905
CB = std::move(CB)](Expected<InputsAndAST> InpAST) mutable {
903906
if (!InpAST)
904907
return CB(InpAST.takeError());
@@ -980,7 +983,7 @@ void ClangdServer::foldingRanges(llvm::StringRef File,
980983
WorkScheduler->runQuick("FoldingRanges", File, std::move(Action));
981984
}
982985

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

0 commit comments

Comments
 (0)