llvm-project
1165 строк · 44.7 Кб
1//===--- ClangdServer.cpp - Main clangd server code --------------*- C++-*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===-------------------------------------------------------------------===//
8
9#include "ClangdServer.h"
10#include "CodeComplete.h"
11#include "Config.h"
12#include "Diagnostics.h"
13#include "DumpAST.h"
14#include "FindSymbols.h"
15#include "Format.h"
16#include "HeaderSourceSwitch.h"
17#include "InlayHints.h"
18#include "ParsedAST.h"
19#include "Preamble.h"
20#include "Protocol.h"
21#include "SemanticHighlighting.h"
22#include "SemanticSelection.h"
23#include "SourceCode.h"
24#include "TUScheduler.h"
25#include "XRefs.h"
26#include "clang-include-cleaner/Record.h"
27#include "index/FileIndex.h"
28#include "index/Merge.h"
29#include "index/StdLib.h"
30#include "refactor/Rename.h"
31#include "refactor/Tweak.h"
32#include "support/Cancellation.h"
33#include "support/Context.h"
34#include "support/Logger.h"
35#include "support/MemoryTree.h"
36#include "support/ThreadsafeFS.h"
37#include "support/Trace.h"
38#include "clang/Basic/Stack.h"
39#include "clang/Format/Format.h"
40#include "clang/Lex/Preprocessor.h"
41#include "clang/Tooling/CompilationDatabase.h"
42#include "clang/Tooling/Core/Replacement.h"
43#include "llvm/ADT/ArrayRef.h"
44#include "llvm/ADT/STLExtras.h"
45#include "llvm/ADT/StringRef.h"
46#include "llvm/Support/Error.h"
47#include "llvm/Support/Path.h"
48#include "llvm/Support/raw_ostream.h"
49#include <algorithm>
50#include <chrono>
51#include <future>
52#include <memory>
53#include <mutex>
54#include <optional>
55#include <string>
56#include <type_traits>
57#include <utility>
58#include <vector>
59
60namespace clang {
61namespace clangd {
62namespace {
63
64// Tracks number of times a tweak has been offered.
65static constexpr trace::Metric TweakAvailable(
66"tweak_available", trace::Metric::Counter, "tweak_id");
67
68// Update the FileIndex with new ASTs and plumb the diagnostics responses.
69struct UpdateIndexCallbacks : public ParsingCallbacks {
70UpdateIndexCallbacks(FileIndex *FIndex,
71ClangdServer::Callbacks *ServerCallbacks,
72const ThreadsafeFS &TFS, AsyncTaskRunner *Tasks,
73bool CollectInactiveRegions)
74: FIndex(FIndex), ServerCallbacks(ServerCallbacks), TFS(TFS),
75Stdlib{std::make_shared<StdLibSet>()}, Tasks(Tasks),
76CollectInactiveRegions(CollectInactiveRegions) {}
77
78void onPreambleAST(
79PathRef Path, llvm::StringRef Version, CapturedASTCtx ASTCtx,
80std::shared_ptr<const include_cleaner::PragmaIncludes> PI) override {
81
82if (!FIndex)
83return;
84
85auto &PP = ASTCtx.getPreprocessor();
86auto &CI = ASTCtx.getCompilerInvocation();
87if (auto Loc = Stdlib->add(CI.getLangOpts(), PP.getHeaderSearchInfo()))
88indexStdlib(CI, std::move(*Loc));
89
90// FIndex outlives the UpdateIndexCallbacks.
91auto Task = [FIndex(FIndex), Path(Path.str()), Version(Version.str()),
92ASTCtx(std::move(ASTCtx)), PI(std::move(PI))]() mutable {
93trace::Span Tracer("PreambleIndexing");
94FIndex->updatePreamble(Path, Version, ASTCtx.getASTContext(),
95ASTCtx.getPreprocessor(), *PI);
96};
97
98if (Tasks) {
99Tasks->runAsync("Preamble indexing for:" + Path + Version,
100std::move(Task));
101} else
102Task();
103}
104
105void indexStdlib(const CompilerInvocation &CI, StdLibLocation Loc) {
106// This task is owned by Tasks, which outlives the TUScheduler and
107// therefore the UpdateIndexCallbacks.
108// We must be careful that the references we capture outlive TUScheduler.
109auto Task = [LO(CI.getLangOpts()), Loc(std::move(Loc)),
110CI(std::make_unique<CompilerInvocation>(CI)),
111// External values that outlive ClangdServer
112TFS(&TFS),
113// Index outlives TUScheduler (declared first)
114FIndex(FIndex),
115// shared_ptr extends lifetime
116Stdlib(Stdlib),
117// We have some FS implementations that rely on information in
118// the context.
119Ctx(Context::current().clone())]() mutable {
120// Make sure we install the context into current thread.
121WithContext C(std::move(Ctx));
122clang::noteBottomOfStack();
123IndexFileIn IF;
124IF.Symbols = indexStandardLibrary(std::move(CI), Loc, *TFS);
125if (Stdlib->isBest(LO))
126FIndex->updatePreamble(std::move(IF));
127};
128if (Tasks)
129// This doesn't have a semaphore to enforce -j, but it's rare.
130Tasks->runAsync("IndexStdlib", std::move(Task));
131else
132Task();
133}
134
135void onMainAST(PathRef Path, ParsedAST &AST, PublishFn Publish) override {
136if (FIndex)
137FIndex->updateMain(Path, AST);
138
139if (ServerCallbacks)
140Publish([&]() {
141ServerCallbacks->onDiagnosticsReady(Path, AST.version(),
142AST.getDiagnostics());
143if (CollectInactiveRegions) {
144ServerCallbacks->onInactiveRegionsReady(Path,
145getInactiveRegions(AST));
146}
147});
148}
149
150void onFailedAST(PathRef Path, llvm::StringRef Version,
151std::vector<Diag> Diags, PublishFn Publish) override {
152if (ServerCallbacks)
153Publish(
154[&]() { ServerCallbacks->onDiagnosticsReady(Path, Version, Diags); });
155}
156
157void onFileUpdated(PathRef File, const TUStatus &Status) override {
158if (ServerCallbacks)
159ServerCallbacks->onFileUpdated(File, Status);
160}
161
162void onPreamblePublished(PathRef File) override {
163if (ServerCallbacks)
164ServerCallbacks->onSemanticsMaybeChanged(File);
165}
166
167private:
168FileIndex *FIndex;
169ClangdServer::Callbacks *ServerCallbacks;
170const ThreadsafeFS &TFS;
171std::shared_ptr<StdLibSet> Stdlib;
172AsyncTaskRunner *Tasks;
173bool CollectInactiveRegions;
174};
175
176class DraftStoreFS : public ThreadsafeFS {
177public:
178DraftStoreFS(const ThreadsafeFS &Base, const DraftStore &Drafts)
179: Base(Base), DirtyFiles(Drafts) {}
180
181private:
182llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> viewImpl() const override {
183auto OFS = llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(
184Base.view(std::nullopt));
185OFS->pushOverlay(DirtyFiles.asVFS());
186return OFS;
187}
188
189const ThreadsafeFS &Base;
190const DraftStore &DirtyFiles;
191};
192
193} // namespace
194
195ClangdServer::Options ClangdServer::optsForTest() {
196ClangdServer::Options Opts;
197Opts.UpdateDebounce = DebouncePolicy::fixed(/*zero*/ {});
198Opts.StorePreamblesInMemory = true;
199Opts.AsyncThreadsCount = 4; // Consistent!
200return Opts;
201}
202
203ClangdServer::Options::operator TUScheduler::Options() const {
204TUScheduler::Options Opts;
205Opts.AsyncThreadsCount = AsyncThreadsCount;
206Opts.RetentionPolicy = RetentionPolicy;
207Opts.StorePreamblesInMemory = StorePreamblesInMemory;
208Opts.UpdateDebounce = UpdateDebounce;
209Opts.ContextProvider = ContextProvider;
210Opts.PreambleThrottler = PreambleThrottler;
211return Opts;
212}
213
214ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB,
215const ThreadsafeFS &TFS, const Options &Opts,
216Callbacks *Callbacks)
217: FeatureModules(Opts.FeatureModules), CDB(CDB), TFS(TFS),
218DynamicIdx(Opts.BuildDynamicSymbolIndex ? new FileIndex() : nullptr),
219ClangTidyProvider(Opts.ClangTidyProvider),
220UseDirtyHeaders(Opts.UseDirtyHeaders),
221LineFoldingOnly(Opts.LineFoldingOnly),
222PreambleParseForwardingFunctions(Opts.PreambleParseForwardingFunctions),
223ImportInsertions(Opts.ImportInsertions),
224PublishInactiveRegions(Opts.PublishInactiveRegions),
225WorkspaceRoot(Opts.WorkspaceRoot),
226Transient(Opts.ImplicitCancellation ? TUScheduler::InvalidateOnUpdate
227: TUScheduler::NoInvalidation),
228DirtyFS(std::make_unique<DraftStoreFS>(TFS, DraftMgr)) {
229if (Opts.AsyncThreadsCount != 0)
230IndexTasks.emplace();
231// Pass a callback into `WorkScheduler` to extract symbols from a newly
232// parsed file and rebuild the file index synchronously each time an AST
233// is parsed.
234WorkScheduler.emplace(CDB, TUScheduler::Options(Opts),
235std::make_unique<UpdateIndexCallbacks>(
236DynamicIdx.get(), Callbacks, TFS,
237IndexTasks ? &*IndexTasks : nullptr,
238PublishInactiveRegions));
239// Adds an index to the stack, at higher priority than existing indexes.
240auto AddIndex = [&](SymbolIndex *Idx) {
241if (this->Index != nullptr) {
242MergedIdx.push_back(std::make_unique<MergedIndex>(Idx, this->Index));
243this->Index = MergedIdx.back().get();
244} else {
245this->Index = Idx;
246}
247};
248if (Opts.StaticIndex)
249AddIndex(Opts.StaticIndex);
250if (Opts.BackgroundIndex) {
251BackgroundIndex::Options BGOpts;
252BGOpts.ThreadPoolSize = std::max(Opts.AsyncThreadsCount, 1u);
253BGOpts.OnProgress = [Callbacks](BackgroundQueue::Stats S) {
254if (Callbacks)
255Callbacks->onBackgroundIndexProgress(S);
256};
257BGOpts.ContextProvider = Opts.ContextProvider;
258BackgroundIdx = std::make_unique<BackgroundIndex>(
259TFS, CDB,
260BackgroundIndexStorage::createDiskBackedStorageFactory(
261[&CDB](llvm::StringRef File) { return CDB.getProjectInfo(File); }),
262std::move(BGOpts));
263AddIndex(BackgroundIdx.get());
264}
265if (DynamicIdx)
266AddIndex(DynamicIdx.get());
267
268if (Opts.FeatureModules) {
269FeatureModule::Facilities F{
270*this->WorkScheduler,
271this->Index,
272this->TFS,
273};
274for (auto &Mod : *Opts.FeatureModules)
275Mod.initialize(F);
276}
277}
278
279ClangdServer::~ClangdServer() {
280// Destroying TUScheduler first shuts down request threads that might
281// otherwise access members concurrently.
282// (Nobody can be using TUScheduler because we're on the main thread).
283WorkScheduler.reset();
284// Now requests have stopped, we can shut down feature modules.
285if (FeatureModules) {
286for (auto &Mod : *FeatureModules)
287Mod.stop();
288for (auto &Mod : *FeatureModules)
289Mod.blockUntilIdle(Deadline::infinity());
290}
291}
292
293void ClangdServer::addDocument(PathRef File, llvm::StringRef Contents,
294llvm::StringRef Version,
295WantDiagnostics WantDiags, bool ForceRebuild) {
296std::string ActualVersion = DraftMgr.addDraft(File, Version, Contents);
297ParseOptions Opts;
298Opts.PreambleParseForwardingFunctions = PreambleParseForwardingFunctions;
299Opts.ImportInsertions = ImportInsertions;
300
301// Compile command is set asynchronously during update, as it can be slow.
302ParseInputs Inputs;
303Inputs.TFS = &getHeaderFS();
304Inputs.Contents = std::string(Contents);
305Inputs.Version = std::move(ActualVersion);
306Inputs.ForceRebuild = ForceRebuild;
307Inputs.Opts = std::move(Opts);
308Inputs.Index = Index;
309Inputs.ClangTidyProvider = ClangTidyProvider;
310Inputs.FeatureModules = FeatureModules;
311bool NewFile = WorkScheduler->update(File, Inputs, WantDiags);
312// If we loaded Foo.h, we want to make sure Foo.cpp is indexed.
313if (NewFile && BackgroundIdx)
314BackgroundIdx->boostRelated(File);
315}
316
317void ClangdServer::reparseOpenFilesIfNeeded(
318llvm::function_ref<bool(llvm::StringRef File)> Filter) {
319// Reparse only opened files that were modified.
320for (const Path &FilePath : DraftMgr.getActiveFiles())
321if (Filter(FilePath))
322if (auto Draft = DraftMgr.getDraft(FilePath)) // else disappeared in race?
323addDocument(FilePath, *Draft->Contents, Draft->Version,
324WantDiagnostics::Auto);
325}
326
327std::shared_ptr<const std::string> ClangdServer::getDraft(PathRef File) const {
328auto Draft = DraftMgr.getDraft(File);
329if (!Draft)
330return nullptr;
331return std::move(Draft->Contents);
332}
333
334std::function<Context(PathRef)>
335ClangdServer::createConfiguredContextProvider(const config::Provider *Provider,
336Callbacks *Publish) {
337if (!Provider)
338return [](llvm::StringRef) { return Context::current().clone(); };
339
340struct Impl {
341const config::Provider *Provider;
342ClangdServer::Callbacks *Publish;
343std::mutex PublishMu;
344
345Impl(const config::Provider *Provider, ClangdServer::Callbacks *Publish)
346: Provider(Provider), Publish(Publish) {}
347
348Context operator()(llvm::StringRef File) {
349config::Params Params;
350// Don't reread config files excessively often.
351// FIXME: when we see a config file change event, use the event timestamp?
352Params.FreshTime =
353std::chrono::steady_clock::now() - std::chrono::seconds(5);
354llvm::SmallString<256> PosixPath;
355if (!File.empty()) {
356assert(llvm::sys::path::is_absolute(File));
357llvm::sys::path::native(File, PosixPath, llvm::sys::path::Style::posix);
358Params.Path = PosixPath.str();
359}
360
361llvm::StringMap<std::vector<Diag>> ReportableDiagnostics;
362Config C = Provider->getConfig(Params, [&](const llvm::SMDiagnostic &D) {
363// Create the map entry even for note diagnostics we don't report.
364// This means that when the file is parsed with no warnings, we
365// publish an empty set of diagnostics, clearing any the client has.
366handleDiagnostic(D, !Publish || D.getFilename().empty()
367? nullptr
368: &ReportableDiagnostics[D.getFilename()]);
369});
370// Blindly publish diagnostics for the (unopened) parsed config files.
371// We must avoid reporting diagnostics for *the same file* concurrently.
372// Source diags are published elsewhere, but those are different files.
373if (!ReportableDiagnostics.empty()) {
374std::lock_guard<std::mutex> Lock(PublishMu);
375for (auto &Entry : ReportableDiagnostics)
376Publish->onDiagnosticsReady(Entry.first(), /*Version=*/"",
377Entry.second);
378}
379return Context::current().derive(Config::Key, std::move(C));
380}
381
382void handleDiagnostic(const llvm::SMDiagnostic &D,
383std::vector<Diag> *ClientDiagnostics) {
384switch (D.getKind()) {
385case llvm::SourceMgr::DK_Error:
386elog("config error at {0}:{1}:{2}: {3}", D.getFilename(), D.getLineNo(),
387D.getColumnNo(), D.getMessage());
388break;
389case llvm::SourceMgr::DK_Warning:
390log("config warning at {0}:{1}:{2}: {3}", D.getFilename(),
391D.getLineNo(), D.getColumnNo(), D.getMessage());
392break;
393case llvm::SourceMgr::DK_Note:
394case llvm::SourceMgr::DK_Remark:
395vlog("config note at {0}:{1}:{2}: {3}", D.getFilename(), D.getLineNo(),
396D.getColumnNo(), D.getMessage());
397ClientDiagnostics = nullptr; // Don't emit notes as LSP diagnostics.
398break;
399}
400if (ClientDiagnostics)
401ClientDiagnostics->push_back(toDiag(D, Diag::ClangdConfig));
402}
403};
404
405// Copyable wrapper.
406return [I(std::make_shared<Impl>(Provider, Publish))](llvm::StringRef Path) {
407return (*I)(Path);
408};
409}
410
411void ClangdServer::removeDocument(PathRef File) {
412DraftMgr.removeDraft(File);
413WorkScheduler->remove(File);
414}
415
416void ClangdServer::codeComplete(PathRef File, Position Pos,
417const clangd::CodeCompleteOptions &Opts,
418Callback<CodeCompleteResult> CB) {
419// Copy completion options for passing them to async task handler.
420auto CodeCompleteOpts = Opts;
421if (!CodeCompleteOpts.Index) // Respect overridden index.
422CodeCompleteOpts.Index = Index;
423
424auto Task = [Pos, CodeCompleteOpts, File = File.str(), CB = std::move(CB),
425this](llvm::Expected<InputsAndPreamble> IP) mutable {
426if (!IP)
427return CB(IP.takeError());
428if (auto Reason = isCancelled())
429return CB(llvm::make_error<CancelledError>(Reason));
430
431std::optional<SpeculativeFuzzyFind> SpecFuzzyFind;
432if (!IP->Preamble) {
433// No speculation in Fallback mode, as it's supposed to be much faster
434// without compiling.
435vlog("Build for file {0} is not ready. Enter fallback mode.", File);
436} else if (CodeCompleteOpts.Index) {
437SpecFuzzyFind.emplace();
438{
439std::lock_guard<std::mutex> Lock(CachedCompletionFuzzyFindRequestMutex);
440SpecFuzzyFind->CachedReq = CachedCompletionFuzzyFindRequestByFile[File];
441}
442}
443ParseInputs ParseInput{IP->Command, &getHeaderFS(), IP->Contents.str()};
444// FIXME: Add traling new line if there is none at eof, workaround a crash,
445// see https://github.com/clangd/clangd/issues/332
446if (!IP->Contents.ends_with("\n"))
447ParseInput.Contents.append("\n");
448ParseInput.Index = Index;
449
450CodeCompleteOpts.MainFileSignals = IP->Signals;
451CodeCompleteOpts.AllScopes = Config::current().Completion.AllScopes;
452// FIXME(ibiryukov): even if Preamble is non-null, we may want to check
453// both the old and the new version in case only one of them matches.
454CodeCompleteResult Result = clangd::codeComplete(
455File, Pos, IP->Preamble, ParseInput, CodeCompleteOpts,
456SpecFuzzyFind ? &*SpecFuzzyFind : nullptr);
457{
458clang::clangd::trace::Span Tracer("Completion results callback");
459CB(std::move(Result));
460}
461if (SpecFuzzyFind && SpecFuzzyFind->NewReq) {
462std::lock_guard<std::mutex> Lock(CachedCompletionFuzzyFindRequestMutex);
463CachedCompletionFuzzyFindRequestByFile[File] = *SpecFuzzyFind->NewReq;
464}
465// SpecFuzzyFind is only destroyed after speculative fuzzy find finishes.
466// We don't want `codeComplete` to wait for the async call if it doesn't use
467// the result (e.g. non-index completion, speculation fails), so that `CB`
468// is called as soon as results are available.
469};
470
471// We use a potentially-stale preamble because latency is critical here.
472WorkScheduler->runWithPreamble(
473"CodeComplete", File,
474(Opts.RunParser == CodeCompleteOptions::AlwaysParse)
475? TUScheduler::Stale
476: TUScheduler::StaleOrAbsent,
477std::move(Task));
478}
479
480void ClangdServer::signatureHelp(PathRef File, Position Pos,
481MarkupKind DocumentationFormat,
482Callback<SignatureHelp> CB) {
483
484auto Action = [Pos, File = File.str(), CB = std::move(CB),
485DocumentationFormat,
486this](llvm::Expected<InputsAndPreamble> IP) mutable {
487if (!IP)
488return CB(IP.takeError());
489
490const auto *PreambleData = IP->Preamble;
491if (!PreambleData)
492return CB(error("Failed to parse includes"));
493
494ParseInputs ParseInput{IP->Command, &getHeaderFS(), IP->Contents.str()};
495// FIXME: Add traling new line if there is none at eof, workaround a crash,
496// see https://github.com/clangd/clangd/issues/332
497if (!IP->Contents.ends_with("\n"))
498ParseInput.Contents.append("\n");
499ParseInput.Index = Index;
500CB(clangd::signatureHelp(File, Pos, *PreambleData, ParseInput,
501DocumentationFormat));
502};
503
504// Unlike code completion, we wait for a preamble here.
505WorkScheduler->runWithPreamble("SignatureHelp", File, TUScheduler::Stale,
506std::move(Action));
507}
508
509void ClangdServer::formatFile(PathRef File, std::optional<Range> Rng,
510Callback<tooling::Replacements> CB) {
511auto Code = getDraft(File);
512if (!Code)
513return CB(llvm::make_error<LSPError>("trying to format non-added document",
514ErrorCode::InvalidParams));
515tooling::Range RequestedRange;
516if (Rng) {
517llvm::Expected<size_t> Begin = positionToOffset(*Code, Rng->start);
518if (!Begin)
519return CB(Begin.takeError());
520llvm::Expected<size_t> End = positionToOffset(*Code, Rng->end);
521if (!End)
522return CB(End.takeError());
523RequestedRange = tooling::Range(*Begin, *End - *Begin);
524} else {
525RequestedRange = tooling::Range(0, Code->size());
526}
527
528// Call clang-format.
529auto Action = [File = File.str(), Code = std::move(*Code),
530Ranges = std::vector<tooling::Range>{RequestedRange},
531CB = std::move(CB), this]() mutable {
532format::FormatStyle Style = getFormatStyleForFile(File, Code, TFS, true);
533tooling::Replacements IncludeReplaces =
534format::sortIncludes(Style, Code, Ranges, File);
535auto Changed = tooling::applyAllReplacements(Code, IncludeReplaces);
536if (!Changed)
537return CB(Changed.takeError());
538
539CB(IncludeReplaces.merge(format::reformat(
540Style, *Changed,
541tooling::calculateRangesAfterReplacements(IncludeReplaces, Ranges),
542File)));
543};
544WorkScheduler->runQuick("Format", File, std::move(Action));
545}
546
547void ClangdServer::formatOnType(PathRef File, Position Pos,
548StringRef TriggerText,
549Callback<std::vector<TextEdit>> CB) {
550auto Code = getDraft(File);
551if (!Code)
552return CB(llvm::make_error<LSPError>("trying to format non-added document",
553ErrorCode::InvalidParams));
554llvm::Expected<size_t> CursorPos = positionToOffset(*Code, Pos);
555if (!CursorPos)
556return CB(CursorPos.takeError());
557auto Action = [File = File.str(), Code = std::move(*Code),
558TriggerText = TriggerText.str(), CursorPos = *CursorPos,
559CB = std::move(CB), this]() mutable {
560auto Style = getFormatStyleForFile(File, Code, TFS, false);
561std::vector<TextEdit> Result;
562for (const tooling::Replacement &R :
563formatIncremental(Code, CursorPos, TriggerText, Style))
564Result.push_back(replacementToEdit(Code, R));
565return CB(Result);
566};
567WorkScheduler->runQuick("FormatOnType", File, std::move(Action));
568}
569
570void ClangdServer::prepareRename(PathRef File, Position Pos,
571std::optional<std::string> NewName,
572const RenameOptions &RenameOpts,
573Callback<RenameResult> CB) {
574auto Action = [Pos, File = File.str(), CB = std::move(CB),
575NewName = std::move(NewName),
576RenameOpts](llvm::Expected<InputsAndAST> InpAST) mutable {
577if (!InpAST)
578return CB(InpAST.takeError());
579// prepareRename is latency-sensitive: we don't query the index, as we
580// only need main-file references
581auto Results =
582clangd::rename({Pos, NewName.value_or("__clangd_rename_placeholder"),
583InpAST->AST, File, /*FS=*/nullptr,
584/*Index=*/nullptr, RenameOpts});
585if (!Results) {
586// LSP says to return null on failure, but that will result in a generic
587// failure message. If we send an LSP error response, clients can surface
588// the message to users (VSCode does).
589return CB(Results.takeError());
590}
591return CB(*Results);
592};
593WorkScheduler->runWithAST("PrepareRename", File, std::move(Action));
594}
595
596void ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName,
597const RenameOptions &Opts,
598Callback<RenameResult> CB) {
599auto Action = [File = File.str(), NewName = NewName.str(), Pos, Opts,
600CB = std::move(CB),
601this](llvm::Expected<InputsAndAST> InpAST) mutable {
602// Tracks number of files edited per invocation.
603static constexpr trace::Metric RenameFiles("rename_files",
604trace::Metric::Distribution);
605if (!InpAST)
606return CB(InpAST.takeError());
607auto R = clangd::rename({Pos, NewName, InpAST->AST, File,
608DirtyFS->view(std::nullopt), Index, Opts});
609if (!R)
610return CB(R.takeError());
611
612if (Opts.WantFormat) {
613auto Style = getFormatStyleForFile(File, InpAST->Inputs.Contents,
614*InpAST->Inputs.TFS, false);
615llvm::Error Err = llvm::Error::success();
616for (auto &E : R->GlobalChanges)
617Err =
618llvm::joinErrors(reformatEdit(E.getValue(), Style), std::move(Err));
619
620if (Err)
621return CB(std::move(Err));
622}
623RenameFiles.record(R->GlobalChanges.size());
624return CB(*R);
625};
626WorkScheduler->runWithAST("Rename", File, std::move(Action));
627}
628
629namespace {
630// May generate several candidate selections, due to SelectionTree ambiguity.
631// vector of pointers because GCC doesn't like non-copyable Selection.
632llvm::Expected<std::vector<std::unique_ptr<Tweak::Selection>>>
633tweakSelection(const Range &Sel, const InputsAndAST &AST,
634llvm::vfs::FileSystem *FS) {
635auto Begin = positionToOffset(AST.Inputs.Contents, Sel.start);
636if (!Begin)
637return Begin.takeError();
638auto End = positionToOffset(AST.Inputs.Contents, Sel.end);
639if (!End)
640return End.takeError();
641std::vector<std::unique_ptr<Tweak::Selection>> Result;
642SelectionTree::createEach(
643AST.AST.getASTContext(), AST.AST.getTokens(), *Begin, *End,
644[&](SelectionTree T) {
645Result.push_back(std::make_unique<Tweak::Selection>(
646AST.Inputs.Index, AST.AST, *Begin, *End, std::move(T), FS));
647return false;
648});
649assert(!Result.empty() && "Expected at least one SelectionTree");
650return std::move(Result);
651}
652
653// Some fixes may perform local renaming, we want to convert those to clangd
654// rename commands, such that we can leverage the index for more accurate
655// results.
656std::optional<ClangdServer::CodeActionResult::Rename>
657tryConvertToRename(const Diag *Diag, const Fix &Fix) {
658bool IsClangTidyRename = Diag->Source == Diag::ClangTidy &&
659Diag->Name == "readability-identifier-naming" &&
660!Fix.Edits.empty();
661if (IsClangTidyRename && Diag->InsideMainFile) {
662ClangdServer::CodeActionResult::Rename R;
663R.NewName = Fix.Edits.front().newText;
664R.FixMessage = Fix.Message;
665R.Diag = {Diag->Range, Diag->Message};
666return R;
667}
668
669return std::nullopt;
670}
671
672} // namespace
673
674void ClangdServer::codeAction(const CodeActionInputs &Params,
675Callback<CodeActionResult> CB) {
676auto Action = [Params, CB = std::move(CB),
677FeatureModules(this->FeatureModules)](
678Expected<InputsAndAST> InpAST) mutable {
679if (!InpAST)
680return CB(InpAST.takeError());
681auto KindAllowed =
682[Only(Params.RequestedActionKinds)](llvm::StringRef Kind) {
683if (Only.empty())
684return true;
685return llvm::any_of(Only, [&](llvm::StringRef Base) {
686return Kind.consume_front(Base) &&
687(Kind.empty() || Kind.starts_with("."));
688});
689};
690
691CodeActionResult Result;
692Result.Version = InpAST->AST.version().str();
693if (KindAllowed(CodeAction::QUICKFIX_KIND)) {
694auto FindMatchedDiag = [&InpAST](const DiagRef &DR) -> const Diag * {
695for (const auto &Diag : InpAST->AST.getDiagnostics())
696if (Diag.Range == DR.Range && Diag.Message == DR.Message)
697return &Diag;
698return nullptr;
699};
700for (const auto &DiagRef : Params.Diagnostics) {
701if (const auto *Diag = FindMatchedDiag(DiagRef))
702for (const auto &Fix : Diag->Fixes) {
703if (auto Rename = tryConvertToRename(Diag, Fix)) {
704Result.Renames.emplace_back(std::move(*Rename));
705} else {
706Result.QuickFixes.push_back({DiagRef, Fix});
707}
708}
709}
710}
711
712// Collect Tweaks
713auto Selections = tweakSelection(Params.Selection, *InpAST, /*FS=*/nullptr);
714if (!Selections)
715return CB(Selections.takeError());
716// Don't allow a tweak to fire more than once across ambiguous selections.
717llvm::DenseSet<llvm::StringRef> PreparedTweaks;
718auto DeduplicatingFilter = [&](const Tweak &T) {
719return KindAllowed(T.kind()) && Params.TweakFilter(T) &&
720!PreparedTweaks.count(T.id());
721};
722for (const auto &Sel : *Selections) {
723for (auto &T : prepareTweaks(*Sel, DeduplicatingFilter, FeatureModules)) {
724Result.TweakRefs.push_back(TweakRef{T->id(), T->title(), T->kind()});
725PreparedTweaks.insert(T->id());
726TweakAvailable.record(1, T->id());
727}
728}
729CB(std::move(Result));
730};
731
732WorkScheduler->runWithAST("codeAction", Params.File, std::move(Action),
733Transient);
734}
735
736void ClangdServer::applyTweak(PathRef File, Range Sel, StringRef TweakID,
737Callback<Tweak::Effect> CB) {
738// Tracks number of times a tweak has been attempted.
739static constexpr trace::Metric TweakAttempt(
740"tweak_attempt", trace::Metric::Counter, "tweak_id");
741// Tracks number of times a tweak has failed to produce edits.
742static constexpr trace::Metric TweakFailed(
743"tweak_failed", trace::Metric::Counter, "tweak_id");
744TweakAttempt.record(1, TweakID);
745auto Action = [File = File.str(), Sel, TweakID = TweakID.str(),
746CB = std::move(CB),
747this](Expected<InputsAndAST> InpAST) mutable {
748if (!InpAST)
749return CB(InpAST.takeError());
750auto FS = DirtyFS->view(std::nullopt);
751auto Selections = tweakSelection(Sel, *InpAST, FS.get());
752if (!Selections)
753return CB(Selections.takeError());
754std::optional<llvm::Expected<Tweak::Effect>> Effect;
755// Try each selection, take the first one that prepare()s.
756// If they all fail, Effect will hold get the last error.
757for (const auto &Selection : *Selections) {
758auto T = prepareTweak(TweakID, *Selection, FeatureModules);
759if (T) {
760Effect = (*T)->apply(*Selection);
761break;
762}
763Effect = T.takeError();
764}
765assert(Effect && "Expected at least one selection");
766if (*Effect && (*Effect)->FormatEdits) {
767// Format tweaks that require it centrally here.
768for (auto &It : (*Effect)->ApplyEdits) {
769Edit &E = It.second;
770format::FormatStyle Style =
771getFormatStyleForFile(File, E.InitialCode, TFS, false);
772if (llvm::Error Err = reformatEdit(E, Style))
773elog("Failed to format {0}: {1}", It.first(), std::move(Err));
774}
775} else {
776TweakFailed.record(1, TweakID);
777}
778return CB(std::move(*Effect));
779};
780WorkScheduler->runWithAST("ApplyTweak", File, std::move(Action));
781}
782
783void ClangdServer::locateSymbolAt(PathRef File, Position Pos,
784Callback<std::vector<LocatedSymbol>> CB) {
785auto Action = [Pos, CB = std::move(CB),
786this](llvm::Expected<InputsAndAST> InpAST) mutable {
787if (!InpAST)
788return CB(InpAST.takeError());
789CB(clangd::locateSymbolAt(InpAST->AST, Pos, Index));
790};
791
792WorkScheduler->runWithAST("Definitions", File, std::move(Action));
793}
794
795void ClangdServer::switchSourceHeader(
796PathRef Path, Callback<std::optional<clangd::Path>> CB) {
797// We want to return the result as fast as possible, strategy is:
798// 1) use the file-only heuristic, it requires some IO but it is much
799// faster than building AST, but it only works when .h/.cc files are in
800// the same directory.
801// 2) if 1) fails, we use the AST&Index approach, it is slower but supports
802// different code layout.
803if (auto CorrespondingFile =
804getCorrespondingHeaderOrSource(Path, TFS.view(std::nullopt)))
805return CB(std::move(CorrespondingFile));
806auto Action = [Path = Path.str(), CB = std::move(CB),
807this](llvm::Expected<InputsAndAST> InpAST) mutable {
808if (!InpAST)
809return CB(InpAST.takeError());
810CB(getCorrespondingHeaderOrSource(Path, InpAST->AST, Index));
811};
812WorkScheduler->runWithAST("SwitchHeaderSource", Path, std::move(Action));
813}
814
815void ClangdServer::findDocumentHighlights(
816PathRef File, Position Pos, Callback<std::vector<DocumentHighlight>> CB) {
817auto Action =
818[Pos, CB = std::move(CB)](llvm::Expected<InputsAndAST> InpAST) mutable {
819if (!InpAST)
820return CB(InpAST.takeError());
821CB(clangd::findDocumentHighlights(InpAST->AST, Pos));
822};
823
824WorkScheduler->runWithAST("Highlights", File, std::move(Action), Transient);
825}
826
827void ClangdServer::findHover(PathRef File, Position Pos,
828Callback<std::optional<HoverInfo>> CB) {
829auto Action = [File = File.str(), Pos, CB = std::move(CB),
830this](llvm::Expected<InputsAndAST> InpAST) mutable {
831if (!InpAST)
832return CB(InpAST.takeError());
833format::FormatStyle Style = getFormatStyleForFile(
834File, InpAST->Inputs.Contents, *InpAST->Inputs.TFS, false);
835CB(clangd::getHover(InpAST->AST, Pos, std::move(Style), Index));
836};
837
838WorkScheduler->runWithAST("Hover", File, std::move(Action), Transient);
839}
840
841void ClangdServer::typeHierarchy(PathRef File, Position Pos, int Resolve,
842TypeHierarchyDirection Direction,
843Callback<std::vector<TypeHierarchyItem>> CB) {
844auto Action = [File = File.str(), Pos, Resolve, Direction, CB = std::move(CB),
845this](Expected<InputsAndAST> InpAST) mutable {
846if (!InpAST)
847return CB(InpAST.takeError());
848CB(clangd::getTypeHierarchy(InpAST->AST, Pos, Resolve, Direction, Index,
849File));
850};
851
852WorkScheduler->runWithAST("TypeHierarchy", File, std::move(Action));
853}
854
855void ClangdServer::superTypes(
856const TypeHierarchyItem &Item,
857Callback<std::optional<std::vector<TypeHierarchyItem>>> CB) {
858WorkScheduler->run("typeHierarchy/superTypes", /*Path=*/"",
859[=, CB = std::move(CB)]() mutable {
860CB(clangd::superTypes(Item, Index));
861});
862}
863
864void ClangdServer::subTypes(const TypeHierarchyItem &Item,
865Callback<std::vector<TypeHierarchyItem>> CB) {
866WorkScheduler->run(
867"typeHierarchy/subTypes", /*Path=*/"",
868[=, CB = std::move(CB)]() mutable { CB(clangd::subTypes(Item, Index)); });
869}
870
871void ClangdServer::resolveTypeHierarchy(
872TypeHierarchyItem Item, int Resolve, TypeHierarchyDirection Direction,
873Callback<std::optional<TypeHierarchyItem>> CB) {
874WorkScheduler->run(
875"Resolve Type Hierarchy", "", [=, CB = std::move(CB)]() mutable {
876clangd::resolveTypeHierarchy(Item, Resolve, Direction, Index);
877CB(Item);
878});
879}
880
881void ClangdServer::prepareCallHierarchy(
882PathRef File, Position Pos, Callback<std::vector<CallHierarchyItem>> CB) {
883auto Action = [File = File.str(), Pos,
884CB = std::move(CB)](Expected<InputsAndAST> InpAST) mutable {
885if (!InpAST)
886return CB(InpAST.takeError());
887CB(clangd::prepareCallHierarchy(InpAST->AST, Pos, File));
888};
889WorkScheduler->runWithAST("CallHierarchy", File, std::move(Action));
890}
891
892void ClangdServer::incomingCalls(
893const CallHierarchyItem &Item,
894Callback<std::vector<CallHierarchyIncomingCall>> CB) {
895WorkScheduler->run("Incoming Calls", "",
896[CB = std::move(CB), Item, this]() mutable {
897CB(clangd::incomingCalls(Item, Index));
898});
899}
900
901void ClangdServer::inlayHints(PathRef File, std::optional<Range> RestrictRange,
902Callback<std::vector<InlayHint>> CB) {
903auto Action = [RestrictRange(std::move(RestrictRange)),
904CB = std::move(CB)](Expected<InputsAndAST> InpAST) mutable {
905if (!InpAST)
906return CB(InpAST.takeError());
907CB(clangd::inlayHints(InpAST->AST, std::move(RestrictRange)));
908};
909WorkScheduler->runWithAST("InlayHints", File, std::move(Action), Transient);
910}
911
912void ClangdServer::onFileEvent(const DidChangeWatchedFilesParams &Params) {
913// FIXME: Do nothing for now. This will be used for indexing and potentially
914// invalidating other caches.
915}
916
917void ClangdServer::workspaceSymbols(
918llvm::StringRef Query, int Limit,
919Callback<std::vector<SymbolInformation>> CB) {
920WorkScheduler->run(
921"getWorkspaceSymbols", /*Path=*/"",
922[Query = Query.str(), Limit, CB = std::move(CB), this]() mutable {
923CB(clangd::getWorkspaceSymbols(Query, Limit, Index,
924WorkspaceRoot.value_or("")));
925});
926}
927
928void ClangdServer::documentSymbols(llvm::StringRef File,
929Callback<std::vector<DocumentSymbol>> CB) {
930auto Action =
931[CB = std::move(CB)](llvm::Expected<InputsAndAST> InpAST) mutable {
932if (!InpAST)
933return CB(InpAST.takeError());
934CB(clangd::getDocumentSymbols(InpAST->AST));
935};
936WorkScheduler->runWithAST("DocumentSymbols", File, std::move(Action),
937Transient);
938}
939
940void ClangdServer::foldingRanges(llvm::StringRef File,
941Callback<std::vector<FoldingRange>> CB) {
942auto Code = getDraft(File);
943if (!Code)
944return CB(llvm::make_error<LSPError>(
945"trying to compute folding ranges for non-added document",
946ErrorCode::InvalidParams));
947auto Action = [LineFoldingOnly = LineFoldingOnly, CB = std::move(CB),
948Code = std::move(*Code)]() mutable {
949CB(clangd::getFoldingRanges(Code, LineFoldingOnly));
950};
951// We want to make sure folding ranges are always available for all the open
952// files, hence prefer runQuick to not wait for operations on other files.
953WorkScheduler->runQuick("FoldingRanges", File, std::move(Action));
954}
955
956void ClangdServer::findType(llvm::StringRef File, Position Pos,
957Callback<std::vector<LocatedSymbol>> CB) {
958auto Action = [Pos, CB = std::move(CB),
959this](llvm::Expected<InputsAndAST> InpAST) mutable {
960if (!InpAST)
961return CB(InpAST.takeError());
962CB(clangd::findType(InpAST->AST, Pos, Index));
963};
964WorkScheduler->runWithAST("FindType", File, std::move(Action));
965}
966
967void ClangdServer::findImplementations(
968PathRef File, Position Pos, Callback<std::vector<LocatedSymbol>> CB) {
969auto Action = [Pos, CB = std::move(CB),
970this](llvm::Expected<InputsAndAST> InpAST) mutable {
971if (!InpAST)
972return CB(InpAST.takeError());
973CB(clangd::findImplementations(InpAST->AST, Pos, Index));
974};
975
976WorkScheduler->runWithAST("Implementations", File, std::move(Action));
977}
978
979void ClangdServer::findReferences(PathRef File, Position Pos, uint32_t Limit,
980bool AddContainer,
981Callback<ReferencesResult> CB) {
982auto Action = [Pos, Limit, AddContainer, CB = std::move(CB),
983this](llvm::Expected<InputsAndAST> InpAST) mutable {
984if (!InpAST)
985return CB(InpAST.takeError());
986CB(clangd::findReferences(InpAST->AST, Pos, Limit, Index, AddContainer));
987};
988
989WorkScheduler->runWithAST("References", File, std::move(Action));
990}
991
992void ClangdServer::symbolInfo(PathRef File, Position Pos,
993Callback<std::vector<SymbolDetails>> CB) {
994auto Action =
995[Pos, CB = std::move(CB)](llvm::Expected<InputsAndAST> InpAST) mutable {
996if (!InpAST)
997return CB(InpAST.takeError());
998CB(clangd::getSymbolInfo(InpAST->AST, Pos));
999};
1000
1001WorkScheduler->runWithAST("SymbolInfo", File, std::move(Action));
1002}
1003
1004void ClangdServer::semanticRanges(PathRef File,
1005const std::vector<Position> &Positions,
1006Callback<std::vector<SelectionRange>> CB) {
1007auto Action = [Positions, CB = std::move(CB)](
1008llvm::Expected<InputsAndAST> InpAST) mutable {
1009if (!InpAST)
1010return CB(InpAST.takeError());
1011std::vector<SelectionRange> Result;
1012for (const auto &Pos : Positions) {
1013if (auto Range = clangd::getSemanticRanges(InpAST->AST, Pos))
1014Result.push_back(std::move(*Range));
1015else
1016return CB(Range.takeError());
1017}
1018CB(std::move(Result));
1019};
1020WorkScheduler->runWithAST("SemanticRanges", File, std::move(Action));
1021}
1022
1023void ClangdServer::documentLinks(PathRef File,
1024Callback<std::vector<DocumentLink>> CB) {
1025auto Action =
1026[CB = std::move(CB)](llvm::Expected<InputsAndAST> InpAST) mutable {
1027if (!InpAST)
1028return CB(InpAST.takeError());
1029CB(clangd::getDocumentLinks(InpAST->AST));
1030};
1031WorkScheduler->runWithAST("DocumentLinks", File, std::move(Action),
1032Transient);
1033}
1034
1035void ClangdServer::semanticHighlights(
1036PathRef File, Callback<std::vector<HighlightingToken>> CB) {
1037
1038auto Action = [CB = std::move(CB),
1039PublishInactiveRegions = PublishInactiveRegions](
1040llvm::Expected<InputsAndAST> InpAST) mutable {
1041if (!InpAST)
1042return CB(InpAST.takeError());
1043// Include inactive regions in semantic highlighting tokens only if the
1044// client doesn't support a dedicated protocol for being informed about
1045// them.
1046CB(clangd::getSemanticHighlightings(InpAST->AST, !PublishInactiveRegions));
1047};
1048WorkScheduler->runWithAST("SemanticHighlights", File, std::move(Action),
1049Transient);
1050}
1051
1052void ClangdServer::getAST(PathRef File, std::optional<Range> R,
1053Callback<std::optional<ASTNode>> CB) {
1054auto Action =
1055[R, CB(std::move(CB))](llvm::Expected<InputsAndAST> Inputs) mutable {
1056if (!Inputs)
1057return CB(Inputs.takeError());
1058if (!R) {
1059// It's safe to pass in the TU, as dumpAST() does not
1060// deserialize the preamble.
1061auto Node = DynTypedNode::create(
1062*Inputs->AST.getASTContext().getTranslationUnitDecl());
1063return CB(dumpAST(Node, Inputs->AST.getTokens(),
1064Inputs->AST.getASTContext()));
1065}
1066unsigned Start, End;
1067if (auto Offset = positionToOffset(Inputs->Inputs.Contents, R->start))
1068Start = *Offset;
1069else
1070return CB(Offset.takeError());
1071if (auto Offset = positionToOffset(Inputs->Inputs.Contents, R->end))
1072End = *Offset;
1073else
1074return CB(Offset.takeError());
1075bool Success = SelectionTree::createEach(
1076Inputs->AST.getASTContext(), Inputs->AST.getTokens(), Start, End,
1077[&](SelectionTree T) {
1078if (const SelectionTree::Node *N = T.commonAncestor()) {
1079CB(dumpAST(N->ASTNode, Inputs->AST.getTokens(),
1080Inputs->AST.getASTContext()));
1081return true;
1082}
1083return false;
1084});
1085if (!Success)
1086CB(std::nullopt);
1087};
1088WorkScheduler->runWithAST("GetAST", File, std::move(Action));
1089}
1090
1091void ClangdServer::customAction(PathRef File, llvm::StringRef Name,
1092Callback<InputsAndAST> Action) {
1093WorkScheduler->runWithAST(Name, File, std::move(Action));
1094}
1095
1096void ClangdServer::diagnostics(PathRef File, Callback<std::vector<Diag>> CB) {
1097auto Action =
1098[CB = std::move(CB)](llvm::Expected<InputsAndAST> InpAST) mutable {
1099if (!InpAST)
1100return CB(InpAST.takeError());
1101return CB(InpAST->AST.getDiagnostics());
1102};
1103
1104WorkScheduler->runWithAST("Diagnostics", File, std::move(Action));
1105}
1106
1107llvm::StringMap<TUScheduler::FileStats> ClangdServer::fileStats() const {
1108return WorkScheduler->fileStats();
1109}
1110
1111[[nodiscard]] bool
1112ClangdServer::blockUntilIdleForTest(std::optional<double> TimeoutSeconds) {
1113// Order is important here: we don't want to block on A and then B,
1114// if B might schedule work on A.
1115
1116#if defined(__has_feature) && \
1117(__has_feature(address_sanitizer) || __has_feature(hwaddress_sanitizer) || \
1118__has_feature(memory_sanitizer) || __has_feature(thread_sanitizer))
1119if (TimeoutSeconds.has_value())
1120(*TimeoutSeconds) *= 10;
1121#endif
1122
1123// Nothing else can schedule work on TUScheduler, because it's not threadsafe
1124// and we're blocking the main thread.
1125if (!WorkScheduler->blockUntilIdle(timeoutSeconds(TimeoutSeconds)))
1126return false;
1127// TUScheduler is the only thing that starts background indexing work.
1128if (IndexTasks && !IndexTasks->wait(timeoutSeconds(TimeoutSeconds)))
1129return false;
1130
1131// Unfortunately we don't have strict topological order between the rest of
1132// the components. E.g. CDB broadcast triggers backrgound indexing.
1133// This queries the CDB which may discover new work if disk has changed.
1134//
1135// So try each one a few times in a loop.
1136// If there are no tricky interactions then all after the first are no-ops.
1137// Then on the last iteration, verify they're idle without waiting.
1138//
1139// There's a small chance they're juggling work and we didn't catch them :-(
1140for (std::optional<double> Timeout :
1141{TimeoutSeconds, TimeoutSeconds, std::optional<double>(0)}) {
1142if (!CDB.blockUntilIdle(timeoutSeconds(Timeout)))
1143return false;
1144if (BackgroundIdx && !BackgroundIdx->blockUntilIdleForTest(Timeout))
1145return false;
1146if (FeatureModules && llvm::any_of(*FeatureModules, [&](FeatureModule &M) {
1147return !M.blockUntilIdle(timeoutSeconds(Timeout));
1148}))
1149return false;
1150}
1151
1152assert(WorkScheduler->blockUntilIdle(Deadline::zero()) &&
1153"Something scheduled work while we're blocking the main thread!");
1154return true;
1155}
1156
1157void ClangdServer::profile(MemoryTree &MT) const {
1158if (DynamicIdx)
1159DynamicIdx->profile(MT.child("dynamic_index"));
1160if (BackgroundIdx)
1161BackgroundIdx->profile(MT.child("background_index"));
1162WorkScheduler->profile(MT.child("tuscheduler"));
1163}
1164} // namespace clangd
1165} // namespace clang
1166