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 "DumpAST.h"
13 #include "FindSymbols.h"
14 #include "Format.h"
15 #include "HeaderSourceSwitch.h"
16 #include "Headers.h"
17 #include "ParsedAST.h"
18 #include "Preamble.h"
19 #include "Protocol.h"
20 #include "SemanticHighlighting.h"
21 #include "SemanticSelection.h"
22 #include "SourceCode.h"
23 #include "TUScheduler.h"
24 #include "XRefs.h"
25 #include "index/CanonicalIncludes.h"
26 #include "index/FileIndex.h"
27 #include "index/Merge.h"
28 #include "refactor/Rename.h"
29 #include "refactor/Tweak.h"
30 #include "support/Logger.h"
31 #include "support/Markup.h"
32 #include "support/MemoryTree.h"
33 #include "support/ThreadsafeFS.h"
34 #include "support/Trace.h"
35 #include "clang/Format/Format.h"
36 #include "clang/Frontend/CompilerInstance.h"
37 #include "clang/Frontend/CompilerInvocation.h"
38 #include "clang/Lex/Preprocessor.h"
39 #include "clang/Tooling/CompilationDatabase.h"
40 #include "clang/Tooling/Core/Replacement.h"
41 #include "llvm/ADT/ArrayRef.h"
42 #include "llvm/ADT/Optional.h"
43 #include "llvm/ADT/STLExtras.h"
44 #include "llvm/ADT/ScopeExit.h"
45 #include "llvm/ADT/StringExtras.h"
46 #include "llvm/ADT/StringRef.h"
47 #include "llvm/Support/Errc.h"
48 #include "llvm/Support/Error.h"
49 #include "llvm/Support/FileSystem.h"
50 #include "llvm/Support/Path.h"
51 #include "llvm/Support/ScopedPrinter.h"
52 #include "llvm/Support/raw_ostream.h"
53 #include <algorithm>
54 #include <chrono>
55 #include <future>
56 #include <memory>
57 #include <mutex>
58 #include <string>
59 #include <type_traits>
60 
61 namespace clang {
62 namespace clangd {
63 namespace {
64 
65 // Update the FileIndex with new ASTs and plumb the diagnostics responses.
66 struct UpdateIndexCallbacks : public ParsingCallbacks {
UpdateIndexCallbacksclang::clangd::__anon60aac7130111::UpdateIndexCallbacks67   UpdateIndexCallbacks(FileIndex *FIndex,
68                        ClangdServer::Callbacks *ServerCallbacks,
69                        bool TheiaSemanticHighlighting)
70       : FIndex(FIndex), ServerCallbacks(ServerCallbacks),
71         TheiaSemanticHighlighting(TheiaSemanticHighlighting) {}
72 
onPreambleASTclang::clangd::__anon60aac7130111::UpdateIndexCallbacks73   void onPreambleAST(PathRef Path, llvm::StringRef Version, ASTContext &Ctx,
74                      std::shared_ptr<clang::Preprocessor> PP,
75                      const CanonicalIncludes &CanonIncludes) override {
76     if (FIndex)
77       FIndex->updatePreamble(Path, Version, Ctx, std::move(PP), CanonIncludes);
78   }
79 
onMainASTclang::clangd::__anon60aac7130111::UpdateIndexCallbacks80   void onMainAST(PathRef Path, ParsedAST &AST, PublishFn Publish) override {
81     if (FIndex)
82       FIndex->updateMain(Path, AST);
83 
84     std::vector<Diag> Diagnostics = AST.getDiagnostics();
85     std::vector<HighlightingToken> Highlightings;
86     if (TheiaSemanticHighlighting)
87       Highlightings = getSemanticHighlightings(AST);
88 
89     if (ServerCallbacks)
90       Publish([&]() {
91         ServerCallbacks->onDiagnosticsReady(Path, AST.version(),
92                                             std::move(Diagnostics));
93         if (TheiaSemanticHighlighting)
94           ServerCallbacks->onHighlightingsReady(Path, AST.version(),
95                                                 std::move(Highlightings));
96       });
97   }
98 
onFailedASTclang::clangd::__anon60aac7130111::UpdateIndexCallbacks99   void onFailedAST(PathRef Path, llvm::StringRef Version,
100                    std::vector<Diag> Diags, PublishFn Publish) override {
101     if (ServerCallbacks)
102       Publish(
103           [&]() { ServerCallbacks->onDiagnosticsReady(Path, Version, Diags); });
104   }
105 
onFileUpdatedclang::clangd::__anon60aac7130111::UpdateIndexCallbacks106   void onFileUpdated(PathRef File, const TUStatus &Status) override {
107     if (ServerCallbacks)
108       ServerCallbacks->onFileUpdated(File, Status);
109   }
110 
111 private:
112   FileIndex *FIndex;
113   ClangdServer::Callbacks *ServerCallbacks;
114   bool TheiaSemanticHighlighting;
115 };
116 
117 } // namespace
118 
optsForTest()119 ClangdServer::Options ClangdServer::optsForTest() {
120   ClangdServer::Options Opts;
121   Opts.UpdateDebounce = DebouncePolicy::fixed(/*zero*/ {});
122   Opts.StorePreamblesInMemory = true;
123   Opts.AsyncThreadsCount = 4; // Consistent!
124   Opts.TheiaSemanticHighlighting = true;
125   Opts.AsyncPreambleBuilds = true;
126   return Opts;
127 }
128 
operator TUScheduler::Options() const129 ClangdServer::Options::operator TUScheduler::Options() const {
130   TUScheduler::Options Opts;
131   Opts.AsyncThreadsCount = AsyncThreadsCount;
132   Opts.RetentionPolicy = RetentionPolicy;
133   Opts.StorePreamblesInMemory = StorePreamblesInMemory;
134   Opts.UpdateDebounce = UpdateDebounce;
135   Opts.AsyncPreambleBuilds = AsyncPreambleBuilds;
136   Opts.ContextProvider = ContextProvider;
137   return Opts;
138 }
139 
ClangdServer(const GlobalCompilationDatabase & CDB,const ThreadsafeFS & TFS,const Options & Opts,Callbacks * Callbacks)140 ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB,
141                            const ThreadsafeFS &TFS, const Options &Opts,
142                            Callbacks *Callbacks)
143     : CDB(CDB), TFS(TFS),
144       DynamicIdx(Opts.BuildDynamicSymbolIndex
145                      ? new FileIndex(Opts.HeavyweightDynamicSymbolIndex,
146                                      Opts.CollectMainFileRefs)
147                      : nullptr),
148       ClangTidyProvider(Opts.ClangTidyProvider),
149       WorkspaceRoot(Opts.WorkspaceRoot),
150       // Pass a callback into `WorkScheduler` to extract symbols from a newly
151       // parsed file and rebuild the file index synchronously each time an AST
152       // is parsed.
153       // FIXME(ioeric): this can be slow and we may be able to index on less
154       // critical paths.
155       WorkScheduler(
156           CDB, TUScheduler::Options(Opts),
157           std::make_unique<UpdateIndexCallbacks>(
158               DynamicIdx.get(), Callbacks, Opts.TheiaSemanticHighlighting)) {
159   // Adds an index to the stack, at higher priority than existing indexes.
160   auto AddIndex = [&](SymbolIndex *Idx) {
161     if (this->Index != nullptr) {
162       MergedIdx.push_back(std::make_unique<MergedIndex>(Idx, this->Index));
163       this->Index = MergedIdx.back().get();
164     } else {
165       this->Index = Idx;
166     }
167   };
168   if (Opts.StaticIndex)
169     AddIndex(Opts.StaticIndex);
170   if (Opts.BackgroundIndex) {
171     BackgroundIndex::Options BGOpts;
172     BGOpts.ThreadPoolSize = std::max(Opts.AsyncThreadsCount, 1u);
173     BGOpts.OnProgress = [Callbacks](BackgroundQueue::Stats S) {
174       if (Callbacks)
175         Callbacks->onBackgroundIndexProgress(S);
176     };
177     BGOpts.ContextProvider = Opts.ContextProvider;
178     BGOpts.CollectMainFileRefs = Opts.CollectMainFileRefs;
179     BackgroundIdx = std::make_unique<BackgroundIndex>(
180         TFS, CDB,
181         BackgroundIndexStorage::createDiskBackedStorageFactory(
182             [&CDB](llvm::StringRef File) { return CDB.getProjectInfo(File); }),
183         std::move(BGOpts));
184     AddIndex(BackgroundIdx.get());
185   }
186   if (DynamicIdx)
187     AddIndex(DynamicIdx.get());
188 }
189 
addDocument(PathRef File,llvm::StringRef Contents,llvm::StringRef Version,WantDiagnostics WantDiags,bool ForceRebuild)190 void ClangdServer::addDocument(PathRef File, llvm::StringRef Contents,
191                                llvm::StringRef Version,
192                                WantDiagnostics WantDiags, bool ForceRebuild) {
193   ParseOptions Opts;
194 
195   // Compile command is set asynchronously during update, as it can be slow.
196   ParseInputs Inputs;
197   Inputs.TFS = &TFS;
198   Inputs.Contents = std::string(Contents);
199   Inputs.Version = Version.str();
200   Inputs.ForceRebuild = ForceRebuild;
201   Inputs.Opts = std::move(Opts);
202   Inputs.Index = Index;
203   Inputs.ClangTidyProvider = ClangTidyProvider;
204   bool NewFile = WorkScheduler.update(File, Inputs, WantDiags);
205   // If we loaded Foo.h, we want to make sure Foo.cpp is indexed.
206   if (NewFile && BackgroundIdx)
207     BackgroundIdx->boostRelated(File);
208 }
209 
210 std::function<Context(PathRef)>
createConfiguredContextProvider(const config::Provider * Provider,Callbacks * Publish)211 ClangdServer::createConfiguredContextProvider(const config::Provider *Provider,
212                                               Callbacks *Publish) {
213   if (!Provider)
214     return [](llvm::StringRef) { return Context::current().clone(); };
215 
216   struct Impl {
217     const config::Provider *Provider;
218     ClangdServer::Callbacks *Publish;
219     std::mutex PublishMu;
220 
221     Impl(const config::Provider *Provider, ClangdServer::Callbacks *Publish)
222         : Provider(Provider), Publish(Publish) {}
223 
224     Context operator()(llvm::StringRef File) {
225       config::Params Params;
226       // Don't reread config files excessively often.
227       // FIXME: when we see a config file change event, use the event timestamp?
228       Params.FreshTime =
229           std::chrono::steady_clock::now() - std::chrono::seconds(5);
230       llvm::SmallString<256> PosixPath;
231       if (!File.empty()) {
232         assert(llvm::sys::path::is_absolute(File));
233         llvm::sys::path::native(File, PosixPath, llvm::sys::path::Style::posix);
234         Params.Path = PosixPath.str();
235       }
236 
237       llvm::StringMap<std::vector<Diag>> ReportableDiagnostics;
238       Config C = Provider->getConfig(Params, [&](const llvm::SMDiagnostic &D) {
239         // Create the map entry even for note diagnostics we don't report.
240         // This means that when the file is parsed with no warnings, we
241         // publish an empty set of diagnostics, clearing any the client has.
242         handleDiagnostic(D, !Publish || D.getFilename().empty()
243                                 ? nullptr
244                                 : &ReportableDiagnostics[D.getFilename()]);
245       });
246       // Blindly publish diagnostics for the (unopened) parsed config files.
247       // We must avoid reporting diagnostics for *the same file* concurrently.
248       // Source diags are published elsewhere, but those are different files.
249       if (!ReportableDiagnostics.empty()) {
250         std::lock_guard<std::mutex> Lock(PublishMu);
251         for (auto &Entry : ReportableDiagnostics)
252           Publish->onDiagnosticsReady(Entry.first(), /*Version=*/"",
253                                       std::move(Entry.second));
254       }
255       return Context::current().derive(Config::Key, std::move(C));
256     }
257 
258     void handleDiagnostic(const llvm::SMDiagnostic &D,
259                           std::vector<Diag> *ClientDiagnostics) {
260       switch (D.getKind()) {
261       case llvm::SourceMgr::DK_Error:
262         elog("config error at {0}:{1}:{2}: {3}", D.getFilename(), D.getLineNo(),
263              D.getColumnNo(), D.getMessage());
264         break;
265       case llvm::SourceMgr::DK_Warning:
266         log("config warning at {0}:{1}:{2}: {3}", D.getFilename(),
267             D.getLineNo(), D.getColumnNo(), D.getMessage());
268         break;
269       case llvm::SourceMgr::DK_Note:
270       case llvm::SourceMgr::DK_Remark:
271         vlog("config note at {0}:{1}:{2}: {3}", D.getFilename(), D.getLineNo(),
272              D.getColumnNo(), D.getMessage());
273         ClientDiagnostics = nullptr; // Don't emit notes as LSP diagnostics.
274         break;
275       }
276       if (ClientDiagnostics)
277         ClientDiagnostics->push_back(toDiag(D, Diag::ClangdConfig));
278     }
279   };
280 
281   // Copyable wrapper.
282   return [I(std::make_shared<Impl>(Provider, Publish))](llvm::StringRef Path) {
283     return (*I)(Path);
284   };
285 }
286 
removeDocument(PathRef File)287 void ClangdServer::removeDocument(PathRef File) { WorkScheduler.remove(File); }
288 
codeComplete(PathRef File,Position Pos,const clangd::CodeCompleteOptions & Opts,Callback<CodeCompleteResult> CB)289 void ClangdServer::codeComplete(PathRef File, Position Pos,
290                                 const clangd::CodeCompleteOptions &Opts,
291                                 Callback<CodeCompleteResult> CB) {
292   // Copy completion options for passing them to async task handler.
293   auto CodeCompleteOpts = Opts;
294   if (!CodeCompleteOpts.Index) // Respect overridden index.
295     CodeCompleteOpts.Index = Index;
296 
297   auto Task = [Pos, CodeCompleteOpts, File = File.str(), CB = std::move(CB),
298                this](llvm::Expected<InputsAndPreamble> IP) mutable {
299     if (!IP)
300       return CB(IP.takeError());
301     if (auto Reason = isCancelled())
302       return CB(llvm::make_error<CancelledError>(Reason));
303 
304     llvm::Optional<SpeculativeFuzzyFind> SpecFuzzyFind;
305     if (!IP->Preamble) {
306       // No speculation in Fallback mode, as it's supposed to be much faster
307       // without compiling.
308       vlog("Build for file {0} is not ready. Enter fallback mode.", File);
309     } else if (CodeCompleteOpts.Index) {
310       SpecFuzzyFind.emplace();
311       {
312         std::lock_guard<std::mutex> Lock(CachedCompletionFuzzyFindRequestMutex);
313         SpecFuzzyFind->CachedReq = CachedCompletionFuzzyFindRequestByFile[File];
314       }
315     }
316     ParseInputs ParseInput{IP->Command, &TFS, IP->Contents.str()};
317     ParseInput.Index = Index;
318 
319     CodeCompleteOpts.MainFileSignals = IP->Signals;
320     // FIXME(ibiryukov): even if Preamble is non-null, we may want to check
321     // both the old and the new version in case only one of them matches.
322     CodeCompleteResult Result = clangd::codeComplete(
323         File, Pos, IP->Preamble, ParseInput, CodeCompleteOpts,
324         SpecFuzzyFind ? SpecFuzzyFind.getPointer() : nullptr);
325     {
326       clang::clangd::trace::Span Tracer("Completion results callback");
327       CB(std::move(Result));
328     }
329     if (SpecFuzzyFind && SpecFuzzyFind->NewReq.hasValue()) {
330       std::lock_guard<std::mutex> Lock(CachedCompletionFuzzyFindRequestMutex);
331       CachedCompletionFuzzyFindRequestByFile[File] =
332           SpecFuzzyFind->NewReq.getValue();
333     }
334     // SpecFuzzyFind is only destroyed after speculative fuzzy find finishes.
335     // We don't want `codeComplete` to wait for the async call if it doesn't use
336     // the result (e.g. non-index completion, speculation fails), so that `CB`
337     // is called as soon as results are available.
338   };
339 
340   // We use a potentially-stale preamble because latency is critical here.
341   WorkScheduler.runWithPreamble(
342       "CodeComplete", File,
343       (Opts.RunParser == CodeCompleteOptions::AlwaysParse)
344           ? TUScheduler::Stale
345           : TUScheduler::StaleOrAbsent,
346       std::move(Task));
347 }
348 
signatureHelp(PathRef File,Position Pos,Callback<SignatureHelp> CB)349 void ClangdServer::signatureHelp(PathRef File, Position Pos,
350                                  Callback<SignatureHelp> CB) {
351 
352   auto Action = [Pos, File = File.str(), CB = std::move(CB),
353                  this](llvm::Expected<InputsAndPreamble> IP) mutable {
354     if (!IP)
355       return CB(IP.takeError());
356 
357     const auto *PreambleData = IP->Preamble;
358     if (!PreambleData)
359       return CB(error("Failed to parse includes"));
360 
361     ParseInputs ParseInput{IP->Command, &TFS, IP->Contents.str()};
362     ParseInput.Index = Index;
363     CB(clangd::signatureHelp(File, Pos, *PreambleData, ParseInput));
364   };
365 
366   // Unlike code completion, we wait for a preamble here.
367   WorkScheduler.runWithPreamble("SignatureHelp", File, TUScheduler::Stale,
368                                 std::move(Action));
369 }
370 
formatRange(PathRef File,llvm::StringRef Code,Range Rng,Callback<tooling::Replacements> CB)371 void ClangdServer::formatRange(PathRef File, llvm::StringRef Code, Range Rng,
372                                Callback<tooling::Replacements> CB) {
373   llvm::Expected<size_t> Begin = positionToOffset(Code, Rng.start);
374   if (!Begin)
375     return CB(Begin.takeError());
376   llvm::Expected<size_t> End = positionToOffset(Code, Rng.end);
377   if (!End)
378     return CB(End.takeError());
379   formatCode(File, Code, {tooling::Range(*Begin, *End - *Begin)},
380              std::move(CB));
381 }
382 
formatFile(PathRef File,llvm::StringRef Code,Callback<tooling::Replacements> CB)383 void ClangdServer::formatFile(PathRef File, llvm::StringRef Code,
384                               Callback<tooling::Replacements> CB) {
385   // Format everything.
386   formatCode(File, Code, {tooling::Range(0, Code.size())}, std::move(CB));
387 }
388 
formatOnType(PathRef File,llvm::StringRef Code,Position Pos,StringRef TriggerText,Callback<std::vector<TextEdit>> CB)389 void ClangdServer::formatOnType(PathRef File, llvm::StringRef Code,
390                                 Position Pos, StringRef TriggerText,
391                                 Callback<std::vector<TextEdit>> CB) {
392   llvm::Expected<size_t> CursorPos = positionToOffset(Code, Pos);
393   if (!CursorPos)
394     return CB(CursorPos.takeError());
395   auto Action = [File = File.str(), Code = Code.str(),
396                  TriggerText = TriggerText.str(), CursorPos = *CursorPos,
397                  CB = std::move(CB), this]() mutable {
398     auto Style = format::getStyle(format::DefaultFormatStyle, File,
399                                   format::DefaultFallbackStyle, Code,
400                                   TFS.view(/*CWD=*/llvm::None).get());
401     if (!Style)
402       return CB(Style.takeError());
403 
404     std::vector<TextEdit> Result;
405     for (const tooling::Replacement &R :
406          formatIncremental(Code, CursorPos, TriggerText, *Style))
407       Result.push_back(replacementToEdit(Code, R));
408     return CB(Result);
409   };
410   WorkScheduler.runQuick("FormatOnType", File, std::move(Action));
411 }
412 
prepareRename(PathRef File,Position Pos,llvm::Optional<std::string> NewName,const RenameOptions & RenameOpts,Callback<RenameResult> CB)413 void ClangdServer::prepareRename(PathRef File, Position Pos,
414                                  llvm::Optional<std::string> NewName,
415                                  const RenameOptions &RenameOpts,
416                                  Callback<RenameResult> CB) {
417   auto Action = [Pos, File = File.str(), CB = std::move(CB),
418                  NewName = std::move(NewName), RenameOpts,
419                  this](llvm::Expected<InputsAndAST> InpAST) mutable {
420     if (!InpAST)
421       return CB(InpAST.takeError());
422     // prepareRename is latency-sensitive:
423     //  - for single-file rename, performing rename isn't substantially more
424     //    expensive than doing an AST-based check (the index is used to see if
425     //    the rename is complete);
426     //  - for cross-file rename, we deliberately pass a nullptr index to save
427     //    the cost, thus the result may be incomplete as it only contains
428     //    main-file occurrences;
429     auto Results = clangd::rename(
430         {Pos, NewName.getValueOr("__clangd_rename_dummy"), InpAST->AST, File,
431          RenameOpts.AllowCrossFile ? nullptr : Index, RenameOpts});
432     if (!Results) {
433       // LSP says to return null on failure, but that will result in a generic
434       // failure message. If we send an LSP error response, clients can surface
435       // the message to users (VSCode does).
436       return CB(Results.takeError());
437     }
438     return CB(*Results);
439   };
440   WorkScheduler.runWithAST("PrepareRename", File, std::move(Action));
441 }
442 
rename(PathRef File,Position Pos,llvm::StringRef NewName,const RenameOptions & Opts,Callback<RenameResult> CB)443 void ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName,
444                           const RenameOptions &Opts,
445                           Callback<RenameResult> CB) {
446   // A snapshot of all file dirty buffers.
447   llvm::StringMap<std::string> Snapshot = WorkScheduler.getAllFileContents();
448   auto Action = [File = File.str(), NewName = NewName.str(), Pos, Opts,
449                  CB = std::move(CB), Snapshot = std::move(Snapshot),
450                  this](llvm::Expected<InputsAndAST> InpAST) mutable {
451     // Tracks number of files edited per invocation.
452     static constexpr trace::Metric RenameFiles("rename_files",
453                                                trace::Metric::Distribution);
454     if (!InpAST)
455       return CB(InpAST.takeError());
456     auto GetDirtyBuffer =
457         [&Snapshot](PathRef AbsPath) -> llvm::Optional<std::string> {
458       auto It = Snapshot.find(AbsPath);
459       if (It == Snapshot.end())
460         return llvm::None;
461       return It->second;
462     };
463     auto R = clangd::rename(
464         {Pos, NewName, InpAST->AST, File, Index, Opts, GetDirtyBuffer});
465     if (!R)
466       return CB(R.takeError());
467 
468     if (Opts.WantFormat) {
469       auto Style = getFormatStyleForFile(File, InpAST->Inputs.Contents,
470                                          *InpAST->Inputs.TFS);
471       llvm::Error Err = llvm::Error::success();
472       for (auto &E : R->GlobalChanges)
473         Err =
474             llvm::joinErrors(reformatEdit(E.getValue(), Style), std::move(Err));
475 
476       if (Err)
477         return CB(std::move(Err));
478     }
479     RenameFiles.record(R->GlobalChanges.size());
480     return CB(*R);
481   };
482   WorkScheduler.runWithAST("Rename", File, std::move(Action));
483 }
484 
485 // May generate several candidate selections, due to SelectionTree ambiguity.
486 // vector of pointers because GCC doesn't like non-copyable Selection.
487 static llvm::Expected<std::vector<std::unique_ptr<Tweak::Selection>>>
tweakSelection(const Range & Sel,const InputsAndAST & AST)488 tweakSelection(const Range &Sel, const InputsAndAST &AST) {
489   auto Begin = positionToOffset(AST.Inputs.Contents, Sel.start);
490   if (!Begin)
491     return Begin.takeError();
492   auto End = positionToOffset(AST.Inputs.Contents, Sel.end);
493   if (!End)
494     return End.takeError();
495   std::vector<std::unique_ptr<Tweak::Selection>> Result;
496   SelectionTree::createEach(
497       AST.AST.getASTContext(), AST.AST.getTokens(), *Begin, *End,
498       [&](SelectionTree T) {
499         Result.push_back(std::make_unique<Tweak::Selection>(
500             AST.Inputs.Index, AST.AST, *Begin, *End, std::move(T)));
501         return false;
502       });
503   assert(!Result.empty() && "Expected at least one SelectionTree");
504   return std::move(Result);
505 }
506 
enumerateTweaks(PathRef File,Range Sel,llvm::unique_function<bool (const Tweak &)> Filter,Callback<std::vector<TweakRef>> CB)507 void ClangdServer::enumerateTweaks(
508     PathRef File, Range Sel, llvm::unique_function<bool(const Tweak &)> Filter,
509     Callback<std::vector<TweakRef>> CB) {
510   // Tracks number of times a tweak has been offered.
511   static constexpr trace::Metric TweakAvailable(
512       "tweak_available", trace::Metric::Counter, "tweak_id");
513   auto Action = [File = File.str(), Sel, CB = std::move(CB),
514                  Filter =
515                      std::move(Filter)](Expected<InputsAndAST> InpAST) mutable {
516     if (!InpAST)
517       return CB(InpAST.takeError());
518     auto Selections = tweakSelection(Sel, *InpAST);
519     if (!Selections)
520       return CB(Selections.takeError());
521     std::vector<TweakRef> Res;
522     // Don't allow a tweak to fire more than once across ambiguous selections.
523     llvm::DenseSet<llvm::StringRef> PreparedTweaks;
524     auto DeduplicatingFilter = [&](const Tweak &T) {
525       return Filter(T) && !PreparedTweaks.count(T.id());
526     };
527     for (const auto &Sel : *Selections) {
528       for (auto &T : prepareTweaks(*Sel, DeduplicatingFilter)) {
529         Res.push_back({T->id(), T->title(), T->kind()});
530         PreparedTweaks.insert(T->id());
531         TweakAvailable.record(1, T->id());
532       }
533     }
534 
535     CB(std::move(Res));
536   };
537 
538   WorkScheduler.runWithAST("EnumerateTweaks", File, std::move(Action),
539                            TUScheduler::InvalidateOnUpdate);
540 }
541 
applyTweak(PathRef File,Range Sel,StringRef TweakID,Callback<Tweak::Effect> CB)542 void ClangdServer::applyTweak(PathRef File, Range Sel, StringRef TweakID,
543                               Callback<Tweak::Effect> CB) {
544   // Tracks number of times a tweak has been attempted.
545   static constexpr trace::Metric TweakAttempt(
546       "tweak_attempt", trace::Metric::Counter, "tweak_id");
547   // Tracks number of times a tweak has failed to produce edits.
548   static constexpr trace::Metric TweakFailed(
549       "tweak_failed", trace::Metric::Counter, "tweak_id");
550   TweakAttempt.record(1, TweakID);
551   auto Action = [File = File.str(), Sel, TweakID = TweakID.str(),
552                  CB = std::move(CB),
553                  this](Expected<InputsAndAST> InpAST) mutable {
554     if (!InpAST)
555       return CB(InpAST.takeError());
556     auto Selections = tweakSelection(Sel, *InpAST);
557     if (!Selections)
558       return CB(Selections.takeError());
559     llvm::Optional<llvm::Expected<Tweak::Effect>> Effect;
560     // Try each selection, take the first one that prepare()s.
561     // If they all fail, Effect will hold get the last error.
562     for (const auto &Selection : *Selections) {
563       auto T = prepareTweak(TweakID, *Selection);
564       if (T) {
565         Effect = (*T)->apply(*Selection);
566         break;
567       }
568       Effect = T.takeError();
569     }
570     assert(Effect.hasValue() && "Expected at least one selection");
571     if (*Effect) {
572       // Tweaks don't apply clang-format, do that centrally here.
573       for (auto &It : (*Effect)->ApplyEdits) {
574         Edit &E = It.second;
575         format::FormatStyle Style =
576             getFormatStyleForFile(File, E.InitialCode, TFS);
577         if (llvm::Error Err = reformatEdit(E, Style))
578           elog("Failed to format {0}: {1}", It.first(), std::move(Err));
579       }
580     } else {
581       TweakFailed.record(1, TweakID);
582     }
583     return CB(std::move(*Effect));
584   };
585   WorkScheduler.runWithAST("ApplyTweak", File, std::move(Action));
586 }
587 
locateSymbolAt(PathRef File,Position Pos,Callback<std::vector<LocatedSymbol>> CB)588 void ClangdServer::locateSymbolAt(PathRef File, Position Pos,
589                                   Callback<std::vector<LocatedSymbol>> CB) {
590   auto Action = [Pos, CB = std::move(CB),
591                  this](llvm::Expected<InputsAndAST> InpAST) mutable {
592     if (!InpAST)
593       return CB(InpAST.takeError());
594     CB(clangd::locateSymbolAt(InpAST->AST, Pos, Index));
595   };
596 
597   WorkScheduler.runWithAST("Definitions", File, std::move(Action));
598 }
599 
switchSourceHeader(PathRef Path,Callback<llvm::Optional<clangd::Path>> CB)600 void ClangdServer::switchSourceHeader(
601     PathRef Path, Callback<llvm::Optional<clangd::Path>> CB) {
602   // We want to return the result as fast as possible, strategy is:
603   //  1) use the file-only heuristic, it requires some IO but it is much
604   //     faster than building AST, but it only works when .h/.cc files are in
605   //     the same directory.
606   //  2) if 1) fails, we use the AST&Index approach, it is slower but supports
607   //     different code layout.
608   if (auto CorrespondingFile =
609           getCorrespondingHeaderOrSource(Path, TFS.view(llvm::None)))
610     return CB(std::move(CorrespondingFile));
611   auto Action = [Path = Path.str(), CB = std::move(CB),
612                  this](llvm::Expected<InputsAndAST> InpAST) mutable {
613     if (!InpAST)
614       return CB(InpAST.takeError());
615     CB(getCorrespondingHeaderOrSource(Path, InpAST->AST, Index));
616   };
617   WorkScheduler.runWithAST("SwitchHeaderSource", Path, std::move(Action));
618 }
619 
formatCode(PathRef File,llvm::StringRef Code,llvm::ArrayRef<tooling::Range> Ranges,Callback<tooling::Replacements> CB)620 void ClangdServer::formatCode(PathRef File, llvm::StringRef Code,
621                               llvm::ArrayRef<tooling::Range> Ranges,
622                               Callback<tooling::Replacements> CB) {
623   // Call clang-format.
624   auto Action = [File = File.str(), Code = Code.str(), Ranges = Ranges.vec(),
625                  CB = std::move(CB), this]() mutable {
626     format::FormatStyle Style = getFormatStyleForFile(File, Code, TFS);
627     tooling::Replacements IncludeReplaces =
628         format::sortIncludes(Style, Code, Ranges, File);
629     auto Changed = tooling::applyAllReplacements(Code, IncludeReplaces);
630     if (!Changed)
631       return CB(Changed.takeError());
632 
633     CB(IncludeReplaces.merge(format::reformat(
634         Style, *Changed,
635         tooling::calculateRangesAfterReplacements(IncludeReplaces, Ranges),
636         File)));
637   };
638   WorkScheduler.runQuick("Format", File, std::move(Action));
639 }
640 
findDocumentHighlights(PathRef File,Position Pos,Callback<std::vector<DocumentHighlight>> CB)641 void ClangdServer::findDocumentHighlights(
642     PathRef File, Position Pos, Callback<std::vector<DocumentHighlight>> CB) {
643   auto Action =
644       [Pos, CB = std::move(CB)](llvm::Expected<InputsAndAST> InpAST) mutable {
645         if (!InpAST)
646           return CB(InpAST.takeError());
647         CB(clangd::findDocumentHighlights(InpAST->AST, Pos));
648       };
649 
650   WorkScheduler.runWithAST("Highlights", File, std::move(Action),
651                            TUScheduler::InvalidateOnUpdate);
652 }
653 
findHover(PathRef File,Position Pos,Callback<llvm::Optional<HoverInfo>> CB)654 void ClangdServer::findHover(PathRef File, Position Pos,
655                              Callback<llvm::Optional<HoverInfo>> CB) {
656   auto Action = [File = File.str(), Pos, CB = std::move(CB),
657                  this](llvm::Expected<InputsAndAST> InpAST) mutable {
658     if (!InpAST)
659       return CB(InpAST.takeError());
660     format::FormatStyle Style = getFormatStyleForFile(
661         File, InpAST->Inputs.Contents, *InpAST->Inputs.TFS);
662     CB(clangd::getHover(InpAST->AST, Pos, std::move(Style), Index));
663   };
664 
665   WorkScheduler.runWithAST("Hover", File, std::move(Action),
666                            TUScheduler::InvalidateOnUpdate);
667 }
668 
typeHierarchy(PathRef File,Position Pos,int Resolve,TypeHierarchyDirection Direction,Callback<Optional<TypeHierarchyItem>> CB)669 void ClangdServer::typeHierarchy(PathRef File, Position Pos, int Resolve,
670                                  TypeHierarchyDirection Direction,
671                                  Callback<Optional<TypeHierarchyItem>> CB) {
672   auto Action = [File = File.str(), Pos, Resolve, Direction, CB = std::move(CB),
673                  this](Expected<InputsAndAST> InpAST) mutable {
674     if (!InpAST)
675       return CB(InpAST.takeError());
676     CB(clangd::getTypeHierarchy(InpAST->AST, Pos, Resolve, Direction, Index,
677                                 File));
678   };
679 
680   WorkScheduler.runWithAST("TypeHierarchy", File, std::move(Action));
681 }
682 
resolveTypeHierarchy(TypeHierarchyItem Item,int Resolve,TypeHierarchyDirection Direction,Callback<llvm::Optional<TypeHierarchyItem>> CB)683 void ClangdServer::resolveTypeHierarchy(
684     TypeHierarchyItem Item, int Resolve, TypeHierarchyDirection Direction,
685     Callback<llvm::Optional<TypeHierarchyItem>> CB) {
686   WorkScheduler.run(
687       "Resolve Type Hierarchy", "", [=, CB = std::move(CB)]() mutable {
688         clangd::resolveTypeHierarchy(Item, Resolve, Direction, Index);
689         CB(Item);
690       });
691 }
692 
prepareCallHierarchy(PathRef File,Position Pos,Callback<std::vector<CallHierarchyItem>> CB)693 void ClangdServer::prepareCallHierarchy(
694     PathRef File, Position Pos, Callback<std::vector<CallHierarchyItem>> CB) {
695   auto Action = [File = File.str(), Pos,
696                  CB = std::move(CB)](Expected<InputsAndAST> InpAST) mutable {
697     if (!InpAST)
698       return CB(InpAST.takeError());
699     CB(clangd::prepareCallHierarchy(InpAST->AST, Pos, File));
700   };
701   WorkScheduler.runWithAST("CallHierarchy", File, std::move(Action));
702 }
703 
incomingCalls(const CallHierarchyItem & Item,Callback<std::vector<CallHierarchyIncomingCall>> CB)704 void ClangdServer::incomingCalls(
705     const CallHierarchyItem &Item,
706     Callback<std::vector<CallHierarchyIncomingCall>> CB) {
707   WorkScheduler.run("Incoming Calls", "",
708                     [CB = std::move(CB), Item, this]() mutable {
709                       CB(clangd::incomingCalls(Item, Index));
710                     });
711 }
712 
onFileEvent(const DidChangeWatchedFilesParams & Params)713 void ClangdServer::onFileEvent(const DidChangeWatchedFilesParams &Params) {
714   // FIXME: Do nothing for now. This will be used for indexing and potentially
715   // invalidating other caches.
716 }
717 
workspaceSymbols(llvm::StringRef Query,int Limit,Callback<std::vector<SymbolInformation>> CB)718 void ClangdServer::workspaceSymbols(
719     llvm::StringRef Query, int Limit,
720     Callback<std::vector<SymbolInformation>> CB) {
721   WorkScheduler.run(
722       "getWorkspaceSymbols", /*Path=*/"",
723       [Query = Query.str(), Limit, CB = std::move(CB), this]() mutable {
724         CB(clangd::getWorkspaceSymbols(Query, Limit, Index,
725                                        WorkspaceRoot.getValueOr("")));
726       });
727 }
728 
documentSymbols(llvm::StringRef File,Callback<std::vector<DocumentSymbol>> CB)729 void ClangdServer::documentSymbols(llvm::StringRef File,
730                                    Callback<std::vector<DocumentSymbol>> CB) {
731   auto Action =
732       [CB = std::move(CB)](llvm::Expected<InputsAndAST> InpAST) mutable {
733         if (!InpAST)
734           return CB(InpAST.takeError());
735         CB(clangd::getDocumentSymbols(InpAST->AST));
736       };
737   WorkScheduler.runWithAST("DocumentSymbols", File, std::move(Action),
738                            TUScheduler::InvalidateOnUpdate);
739 }
740 
foldingRanges(llvm::StringRef File,Callback<std::vector<FoldingRange>> CB)741 void ClangdServer::foldingRanges(llvm::StringRef File,
742                                  Callback<std::vector<FoldingRange>> CB) {
743   auto Action =
744       [CB = std::move(CB)](llvm::Expected<InputsAndAST> InpAST) mutable {
745         if (!InpAST)
746           return CB(InpAST.takeError());
747         CB(clangd::getFoldingRanges(InpAST->AST));
748       };
749   WorkScheduler.runWithAST("FoldingRanges", File, std::move(Action),
750                            TUScheduler::InvalidateOnUpdate);
751 }
752 
findImplementations(PathRef File,Position Pos,Callback<std::vector<LocatedSymbol>> CB)753 void ClangdServer::findImplementations(
754     PathRef File, Position Pos, Callback<std::vector<LocatedSymbol>> CB) {
755   auto Action = [Pos, CB = std::move(CB),
756                  this](llvm::Expected<InputsAndAST> InpAST) mutable {
757     if (!InpAST)
758       return CB(InpAST.takeError());
759     CB(clangd::findImplementations(InpAST->AST, Pos, Index));
760   };
761 
762   WorkScheduler.runWithAST("Implementations", File, std::move(Action));
763 }
764 
findReferences(PathRef File,Position Pos,uint32_t Limit,Callback<ReferencesResult> CB)765 void ClangdServer::findReferences(PathRef File, Position Pos, uint32_t Limit,
766                                   Callback<ReferencesResult> CB) {
767   auto Action = [Pos, Limit, CB = std::move(CB),
768                  this](llvm::Expected<InputsAndAST> InpAST) mutable {
769     if (!InpAST)
770       return CB(InpAST.takeError());
771     CB(clangd::findReferences(InpAST->AST, Pos, Limit, Index));
772   };
773 
774   WorkScheduler.runWithAST("References", File, std::move(Action));
775 }
776 
symbolInfo(PathRef File,Position Pos,Callback<std::vector<SymbolDetails>> CB)777 void ClangdServer::symbolInfo(PathRef File, Position Pos,
778                               Callback<std::vector<SymbolDetails>> CB) {
779   auto Action =
780       [Pos, CB = std::move(CB)](llvm::Expected<InputsAndAST> InpAST) mutable {
781         if (!InpAST)
782           return CB(InpAST.takeError());
783         CB(clangd::getSymbolInfo(InpAST->AST, Pos));
784       };
785 
786   WorkScheduler.runWithAST("SymbolInfo", File, std::move(Action));
787 }
788 
semanticRanges(PathRef File,const std::vector<Position> & Positions,Callback<std::vector<SelectionRange>> CB)789 void ClangdServer::semanticRanges(PathRef File,
790                                   const std::vector<Position> &Positions,
791                                   Callback<std::vector<SelectionRange>> CB) {
792   auto Action = [Positions, CB = std::move(CB)](
793                     llvm::Expected<InputsAndAST> InpAST) mutable {
794     if (!InpAST)
795       return CB(InpAST.takeError());
796     std::vector<SelectionRange> Result;
797     for (const auto &Pos : Positions) {
798       if (auto Range = clangd::getSemanticRanges(InpAST->AST, Pos))
799         Result.push_back(std::move(*Range));
800       else
801         return CB(Range.takeError());
802     }
803     CB(std::move(Result));
804   };
805   WorkScheduler.runWithAST("SemanticRanges", File, std::move(Action));
806 }
807 
documentLinks(PathRef File,Callback<std::vector<DocumentLink>> CB)808 void ClangdServer::documentLinks(PathRef File,
809                                  Callback<std::vector<DocumentLink>> CB) {
810   auto Action =
811       [CB = std::move(CB)](llvm::Expected<InputsAndAST> InpAST) mutable {
812         if (!InpAST)
813           return CB(InpAST.takeError());
814         CB(clangd::getDocumentLinks(InpAST->AST));
815       };
816   WorkScheduler.runWithAST("DocumentLinks", File, std::move(Action),
817                            TUScheduler::InvalidateOnUpdate);
818 }
819 
semanticHighlights(PathRef File,Callback<std::vector<HighlightingToken>> CB)820 void ClangdServer::semanticHighlights(
821     PathRef File, Callback<std::vector<HighlightingToken>> CB) {
822   auto Action =
823       [CB = std::move(CB)](llvm::Expected<InputsAndAST> InpAST) mutable {
824         if (!InpAST)
825           return CB(InpAST.takeError());
826         CB(clangd::getSemanticHighlightings(InpAST->AST));
827       };
828   WorkScheduler.runWithAST("SemanticHighlights", File, std::move(Action),
829                            TUScheduler::InvalidateOnUpdate);
830 }
831 
getAST(PathRef File,Range R,Callback<llvm::Optional<ASTNode>> CB)832 void ClangdServer::getAST(PathRef File, Range R,
833                           Callback<llvm::Optional<ASTNode>> CB) {
834   auto Action =
835       [R, CB(std::move(CB))](llvm::Expected<InputsAndAST> Inputs) mutable {
836         if (!Inputs)
837           return CB(Inputs.takeError());
838         unsigned Start, End;
839         if (auto Offset = positionToOffset(Inputs->Inputs.Contents, R.start))
840           Start = *Offset;
841         else
842           return CB(Offset.takeError());
843         if (auto Offset = positionToOffset(Inputs->Inputs.Contents, R.end))
844           End = *Offset;
845         else
846           return CB(Offset.takeError());
847 
848         bool Success = SelectionTree::createEach(
849             Inputs->AST.getASTContext(), Inputs->AST.getTokens(), Start, End,
850             [&](SelectionTree T) {
851               if (const SelectionTree::Node *N = T.commonAncestor()) {
852                 CB(dumpAST(N->ASTNode, Inputs->AST.getTokens(),
853                            Inputs->AST.getASTContext()));
854                 return true;
855               }
856               return false;
857             });
858         if (!Success)
859           CB(llvm::None);
860       };
861   WorkScheduler.runWithAST("GetAST", File, std::move(Action));
862 }
863 
customAction(PathRef File,llvm::StringRef Name,Callback<InputsAndAST> Action)864 void ClangdServer::customAction(PathRef File, llvm::StringRef Name,
865                                 Callback<InputsAndAST> Action) {
866   WorkScheduler.runWithAST(Name, File, std::move(Action));
867 }
868 
fileStats() const869 llvm::StringMap<TUScheduler::FileStats> ClangdServer::fileStats() const {
870   return WorkScheduler.fileStats();
871 }
872 
873 LLVM_NODISCARD bool
blockUntilIdleForTest(llvm::Optional<double> TimeoutSeconds)874 ClangdServer::blockUntilIdleForTest(llvm::Optional<double> TimeoutSeconds) {
875   return WorkScheduler.blockUntilIdle(timeoutSeconds(TimeoutSeconds)) &&
876          CDB.blockUntilIdle(timeoutSeconds(TimeoutSeconds)) &&
877          (!BackgroundIdx ||
878           BackgroundIdx->blockUntilIdleForTest(TimeoutSeconds));
879 }
880 
profile(MemoryTree & MT) const881 void ClangdServer::profile(MemoryTree &MT) const {
882   if (DynamicIdx)
883     DynamicIdx->profile(MT.child("dynamic_index"));
884   if (BackgroundIdx)
885     BackgroundIdx->profile(MT.child("background_index"));
886   WorkScheduler.profile(MT.child("tuscheduler"));
887 }
888 } // namespace clangd
889 } // namespace clang
890