Skip to content

Commit 7b05836

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

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

+619
-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: 38 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,23 @@ 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 =
553+
getFormatStyleForFile(File.raw(), Code, TFS, true);
551554
tooling::Replacements IncludeReplaces =
552-
format::sortIncludes(Style, Code, Ranges, File);
555+
format::sortIncludes(Style, Code, Ranges, File.raw());
553556
auto Changed = tooling::applyAllReplacements(Code, IncludeReplaces);
554557
if (!Changed)
555558
return CB(Changed.takeError());
556559

557560
CB(IncludeReplaces.merge(format::reformat(
558561
Style, *Changed,
559562
tooling::calculateRangesAfterReplacements(IncludeReplaces, Ranges),
560-
File)));
563+
File.raw())));
561564
};
562-
WorkScheduler->runQuick("Format", File, std::move(Action));
565+
WorkScheduler->runQuick("Format", File.raw(), std::move(Action));
563566
}
564567

565568
void ClangdServer::formatOnType(PathRef File, Position Pos,
@@ -572,24 +575,24 @@ void ClangdServer::formatOnType(PathRef File, Position Pos,
572575
llvm::Expected<size_t> CursorPos = positionToOffset(*Code, Pos);
573576
if (!CursorPos)
574577
return CB(CursorPos.takeError());
575-
auto Action = [File = File.str(), Code = std::move(*Code),
578+
auto Action = [File = File.owned(), Code = std::move(*Code),
576579
TriggerText = TriggerText.str(), CursorPos = *CursorPos,
577580
CB = std::move(CB), this]() mutable {
578-
auto Style = getFormatStyleForFile(File, Code, TFS, false);
581+
auto Style = getFormatStyleForFile(File.raw(), Code, TFS, false);
579582
std::vector<TextEdit> Result;
580583
for (const tooling::Replacement &R :
581584
formatIncremental(Code, CursorPos, TriggerText, Style))
582585
Result.push_back(replacementToEdit(Code, R));
583586
return CB(Result);
584587
};
585-
WorkScheduler->runQuick("FormatOnType", File, std::move(Action));
588+
WorkScheduler->runQuick("FormatOnType", File.raw(), std::move(Action));
586589
}
587590

588591
void ClangdServer::prepareRename(PathRef File, Position Pos,
589592
std::optional<std::string> NewName,
590593
const RenameOptions &RenameOpts,
591594
Callback<RenameResult> CB) {
592-
auto Action = [Pos, File = File.str(), CB = std::move(CB),
595+
auto Action = [Pos, File = File.owned(), CB = std::move(CB),
593596
NewName = std::move(NewName),
594597
RenameOpts](llvm::Expected<InputsAndAST> InpAST) mutable {
595598
if (!InpAST)
@@ -598,7 +601,7 @@ void ClangdServer::prepareRename(PathRef File, Position Pos,
598601
// only need main-file references
599602
auto Results =
600603
clangd::rename({Pos, NewName.value_or("__clangd_rename_placeholder"),
601-
InpAST->AST, File, /*FS=*/nullptr,
604+
InpAST->AST, File.raw(), /*FS=*/nullptr,
602605
/*Index=*/nullptr, RenameOpts});
603606
if (!Results) {
604607
// LSP says to return null on failure, but that will result in a generic
@@ -614,21 +617,21 @@ void ClangdServer::prepareRename(PathRef File, Position Pos,
614617
void ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName,
615618
const RenameOptions &Opts,
616619
Callback<RenameResult> CB) {
617-
auto Action = [File = File.str(), NewName = NewName.str(), Pos, Opts,
620+
auto Action = [File = File.owned(), NewName = NewName.str(), Pos, Opts,
618621
CB = std::move(CB),
619622
this](llvm::Expected<InputsAndAST> InpAST) mutable {
620623
// Tracks number of files edited per invocation.
621624
static constexpr trace::Metric RenameFiles("rename_files",
622625
trace::Metric::Distribution);
623626
if (!InpAST)
624627
return CB(InpAST.takeError());
625-
auto R = clangd::rename({Pos, NewName, InpAST->AST, File,
628+
auto R = clangd::rename({Pos, NewName, InpAST->AST, File.raw(),
626629
DirtyFS->view(std::nullopt), Index, Opts});
627630
if (!R)
628631
return CB(R.takeError());
629632

630633
if (Opts.WantFormat) {
631-
auto Style = getFormatStyleForFile(File, InpAST->Inputs.Contents,
634+
auto Style = getFormatStyleForFile(File.raw(), InpAST->Inputs.Contents,
632635
*InpAST->Inputs.TFS, false);
633636
llvm::Error Err = llvm::Error::success();
634637
for (auto &E : R->GlobalChanges)
@@ -760,7 +763,7 @@ void ClangdServer::applyTweak(PathRef File, Range Sel, StringRef TweakID,
760763
static constexpr trace::Metric TweakFailed(
761764
"tweak_failed", trace::Metric::Counter, "tweak_id");
762765
TweakAttempt.record(1, TweakID);
763-
auto Action = [File = File.str(), Sel, TweakID = TweakID.str(),
766+
auto Action = [File = File.owned(), Sel, TweakID = TweakID.str(),
764767
CB = std::move(CB),
765768
this](Expected<InputsAndAST> InpAST) mutable {
766769
if (!InpAST)
@@ -786,7 +789,7 @@ void ClangdServer::applyTweak(PathRef File, Range Sel, StringRef TweakID,
786789
for (auto &It : (*Effect)->ApplyEdits) {
787790
Edit &E = It.second;
788791
format::FormatStyle Style =
789-
getFormatStyleForFile(File, E.InitialCode, TFS, false);
792+
getFormatStyleForFile(File.raw(), E.InitialCode, TFS, false);
790793
if (llvm::Error Err = reformatEdit(E, Style))
791794
elog("Failed to format {0}: {1}", It.first(), std::move(Err));
792795
}
@@ -821,7 +824,7 @@ void ClangdServer::switchSourceHeader(
821824
if (auto CorrespondingFile =
822825
getCorrespondingHeaderOrSource(Path, TFS.view(std::nullopt)))
823826
return CB(std::move(CorrespondingFile));
824-
auto Action = [Path = Path.str(), CB = std::move(CB),
827+
auto Action = [Path = Path.owned(), CB = std::move(CB),
825828
this](llvm::Expected<InputsAndAST> InpAST) mutable {
826829
if (!InpAST)
827830
return CB(InpAST.takeError());
@@ -844,12 +847,12 @@ void ClangdServer::findDocumentHighlights(
844847

845848
void ClangdServer::findHover(PathRef File, Position Pos,
846849
Callback<std::optional<HoverInfo>> CB) {
847-
auto Action = [File = File.str(), Pos, CB = std::move(CB),
850+
auto Action = [File = File.owned(), Pos, CB = std::move(CB),
848851
this](llvm::Expected<InputsAndAST> InpAST) mutable {
849852
if (!InpAST)
850853
return CB(InpAST.takeError());
851854
format::FormatStyle Style = getFormatStyleForFile(
852-
File, InpAST->Inputs.Contents, *InpAST->Inputs.TFS, false);
855+
File.raw(), InpAST->Inputs.Contents, *InpAST->Inputs.TFS, false);
853856
CB(clangd::getHover(InpAST->AST, Pos, std::move(Style), Index));
854857
};
855858

@@ -859,7 +862,8 @@ void ClangdServer::findHover(PathRef File, Position Pos,
859862
void ClangdServer::typeHierarchy(PathRef File, Position Pos, int Resolve,
860863
TypeHierarchyDirection Direction,
861864
Callback<std::vector<TypeHierarchyItem>> CB) {
862-
auto Action = [File = File.str(), Pos, Resolve, Direction, CB = std::move(CB),
865+
auto Action = [File = File.owned(), Pos, Resolve, Direction,
866+
CB = std::move(CB),
863867
this](Expected<InputsAndAST> InpAST) mutable {
864868
if (!InpAST)
865869
return CB(InpAST.takeError());
@@ -898,7 +902,7 @@ void ClangdServer::resolveTypeHierarchy(
898902

899903
void ClangdServer::prepareCallHierarchy(
900904
PathRef File, Position Pos, Callback<std::vector<CallHierarchyItem>> CB) {
901-
auto Action = [File = File.str(), Pos,
905+
auto Action = [File = File.owned(), Pos,
902906
CB = std::move(CB)](Expected<InputsAndAST> InpAST) mutable {
903907
if (!InpAST)
904908
return CB(InpAST.takeError());
@@ -980,7 +984,7 @@ void ClangdServer::foldingRanges(llvm::StringRef File,
980984
WorkScheduler->runQuick("FoldingRanges", File, std::move(Action));
981985
}
982986

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

0 commit comments

Comments
 (0)