blob: d1fe1fd7c626b578d703c87dd9b5d4e4d5542e77 [file] [log] [blame]
Ilya Biryukov38d79772017-05-16 09:38:59 +00001//===--- ClangdServer.cpp - Main clangd server code --------------*- C++-*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===-------------------------------------------------------------------===//
9
10#include "ClangdServer.h"
Ilya Biryukovafb55542017-05-16 14:40:30 +000011#include "clang/Format/Format.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000012#include "clang/Frontend/CompilerInstance.h"
13#include "clang/Frontend/CompilerInvocation.h"
14#include "clang/Tooling/CompilationDatabase.h"
Ilya Biryukov9e11c4c2017-11-15 18:04:56 +000015#include "clang/Tooling/Refactoring/RefactoringResultConsumer.h"
16#include "clang/Tooling/Refactoring/Rename/RenamingAction.h"
Ilya Biryukovafb55542017-05-16 14:40:30 +000017#include "llvm/ADT/ArrayRef.h"
Benjamin Krameree19f162017-10-26 12:28:13 +000018#include "llvm/Support/Errc.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000019#include "llvm/Support/FileSystem.h"
Sam McCall8567cb32017-11-02 09:21:51 +000020#include "llvm/Support/FormatProviders.h"
21#include "llvm/Support/FormatVariadic.h"
Marc-Andre Laperle37de9712017-09-27 15:31:17 +000022#include "llvm/Support/Path.h"
Ilya Biryukovf01af682017-05-23 13:42:59 +000023#include "llvm/Support/raw_ostream.h"
24#include <future>
Ilya Biryukov38d79772017-05-16 09:38:59 +000025
Ilya Biryukov2f314102017-05-16 10:06:20 +000026using namespace clang;
Ilya Biryukov38d79772017-05-16 09:38:59 +000027using namespace clang::clangd;
28
Ilya Biryukovafb55542017-05-16 14:40:30 +000029namespace {
30
Ilya Biryukov02d58702017-08-01 15:51:38 +000031class FulfillPromiseGuard {
32public:
33 FulfillPromiseGuard(std::promise<void> &Promise) : Promise(Promise) {}
34
35 ~FulfillPromiseGuard() { Promise.set_value(); }
36
37private:
38 std::promise<void> &Promise;
39};
40
Ilya Biryukovafb55542017-05-16 14:40:30 +000041std::vector<tooling::Replacement> formatCode(StringRef Code, StringRef Filename,
42 ArrayRef<tooling::Range> Ranges) {
43 // Call clang-format.
44 // FIXME: Don't ignore style.
45 format::FormatStyle Style = format::getLLVMStyle();
46 auto Result = format::reformat(Style, Code, Ranges, Filename);
47
48 return std::vector<tooling::Replacement>(Result.begin(), Result.end());
49}
50
Ilya Biryukova46f7a92017-06-28 10:34:50 +000051std::string getStandardResourceDir() {
52 static int Dummy; // Just an address in this process.
53 return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy);
54}
55
Haojian Wu345099c2017-11-09 11:30:04 +000056class RefactoringResultCollector final
57 : public tooling::RefactoringResultConsumer {
58public:
59 void handleError(llvm::Error Err) override {
60 assert(!Result.hasValue());
61 // FIXME: figure out a way to return better message for DiagnosticError.
62 // clangd uses llvm::toString to convert the Err to string, however, for
63 // DiagnosticError, only "clang diagnostic" will be generated.
64 Result = std::move(Err);
65 }
66
67 // Using the handle(SymbolOccurrences) from parent class.
68 using tooling::RefactoringResultConsumer::handle;
69
70 void handle(tooling::AtomicChanges SourceReplacements) override {
71 assert(!Result.hasValue());
72 Result = std::move(SourceReplacements);
73 }
74
75 Optional<Expected<tooling::AtomicChanges>> Result;
76};
77
Ilya Biryukovafb55542017-05-16 14:40:30 +000078} // namespace
79
80size_t clangd::positionToOffset(StringRef Code, Position P) {
81 size_t Offset = 0;
82 for (int I = 0; I != P.line; ++I) {
83 // FIXME: \r\n
84 // FIXME: UTF-8
85 size_t F = Code.find('\n', Offset);
86 if (F == StringRef::npos)
87 return 0; // FIXME: Is this reasonable?
88 Offset = F + 1;
89 }
90 return (Offset == 0 ? 0 : (Offset - 1)) + P.character;
91}
92
93/// Turn an offset in Code into a [line, column] pair.
94Position clangd::offsetToPosition(StringRef Code, size_t Offset) {
95 StringRef JustBefore = Code.substr(0, Offset);
96 // FIXME: \r\n
97 // FIXME: UTF-8
98 int Lines = JustBefore.count('\n');
99 int Cols = JustBefore.size() - JustBefore.rfind('\n') - 1;
100 return {Lines, Cols};
101}
102
Ilya Biryukov22602992017-05-30 15:11:02 +0000103Tagged<IntrusiveRefCntPtr<vfs::FileSystem>>
Ilya Biryukovaf0c04b2017-06-14 09:46:44 +0000104RealFileSystemProvider::getTaggedFileSystem(PathRef File) {
Ilya Biryukov22602992017-05-30 15:11:02 +0000105 return make_tagged(vfs::getRealFileSystem(), VFSTag());
Ilya Biryukov0f62ed22017-05-26 12:26:51 +0000106}
107
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000108unsigned clangd::getDefaultAsyncThreadsCount() {
109 unsigned HardwareConcurrency = std::thread::hardware_concurrency();
110 // C++ standard says that hardware_concurrency()
111 // may return 0, fallback to 1 worker thread in
112 // that case.
113 if (HardwareConcurrency == 0)
114 return 1;
115 return HardwareConcurrency;
116}
117
118ClangdScheduler::ClangdScheduler(unsigned AsyncThreadsCount)
119 : RunSynchronously(AsyncThreadsCount == 0) {
Ilya Biryukov38d79772017-05-16 09:38:59 +0000120 if (RunSynchronously) {
121 // Don't start the worker thread if we're running synchronously
122 return;
123 }
124
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000125 Workers.reserve(AsyncThreadsCount);
126 for (unsigned I = 0; I < AsyncThreadsCount; ++I) {
Sam McCall8567cb32017-11-02 09:21:51 +0000127 Workers.push_back(std::thread([this, I]() {
128 llvm::set_thread_name(llvm::formatv("scheduler/{0}", I));
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000129 while (true) {
Ilya Biryukov08e6ccb2017-10-09 16:26:26 +0000130 UniqueFunction<void()> Request;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000131
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000132 // Pick request from the queue
133 {
134 std::unique_lock<std::mutex> Lock(Mutex);
135 // Wait for more requests.
136 RequestCV.wait(Lock,
137 [this] { return !RequestQueue.empty() || Done; });
138 if (Done)
139 return;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000140
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000141 assert(!RequestQueue.empty() && "RequestQueue was empty");
Ilya Biryukov38d79772017-05-16 09:38:59 +0000142
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000143 // We process requests starting from the front of the queue. Users of
144 // ClangdScheduler have a way to prioritise their requests by putting
145 // them to the either side of the queue (using either addToEnd or
146 // addToFront).
147 Request = std::move(RequestQueue.front());
148 RequestQueue.pop_front();
149 } // unlock Mutex
Ilya Biryukov38d79772017-05-16 09:38:59 +0000150
Ilya Biryukov08e6ccb2017-10-09 16:26:26 +0000151 Request();
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000152 }
153 }));
154 }
Ilya Biryukov38d79772017-05-16 09:38:59 +0000155}
156
157ClangdScheduler::~ClangdScheduler() {
158 if (RunSynchronously)
159 return; // no worker thread is running in that case
160
161 {
162 std::lock_guard<std::mutex> Lock(Mutex);
163 // Wake up the worker thread
164 Done = true;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000165 } // unlock Mutex
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000166 RequestCV.notify_all();
167
168 for (auto &Worker : Workers)
169 Worker.join();
Ilya Biryukov38d79772017-05-16 09:38:59 +0000170}
171
Sam McCalladccab62017-11-23 16:58:22 +0000172ClangdServer::ClangdServer(GlobalCompilationDatabase &CDB,
173 DiagnosticsConsumer &DiagConsumer,
174 FileSystemProvider &FSProvider,
175 unsigned AsyncThreadsCount,
Ilya Biryukovd3b04e32017-12-05 10:42:57 +0000176 bool StorePreamblesInMemory, clangd::Logger &Logger,
Sam McCalladccab62017-11-23 16:58:22 +0000177 llvm::Optional<StringRef> ResourceDir)
Ilya Biryukove5128f72017-09-20 07:24:15 +0000178 : Logger(Logger), CDB(CDB), DiagConsumer(DiagConsumer),
179 FSProvider(FSProvider),
Ilya Biryukova46f7a92017-06-28 10:34:50 +0000180 ResourceDir(ResourceDir ? ResourceDir->str() : getStandardResourceDir()),
Ilya Biryukov38d79772017-05-16 09:38:59 +0000181 PCHs(std::make_shared<PCHContainerOperations>()),
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000182 StorePreamblesInMemory(StorePreamblesInMemory),
Ilya Biryukovd3b04e32017-12-05 10:42:57 +0000183 WorkScheduler(AsyncThreadsCount) {}
Ilya Biryukov38d79772017-05-16 09:38:59 +0000184
Marc-Andre Laperle37de9712017-09-27 15:31:17 +0000185void ClangdServer::setRootPath(PathRef RootPath) {
186 std::string NewRootPath = llvm::sys::path::convert_to_slash(
187 RootPath, llvm::sys::path::Style::posix);
188 if (llvm::sys::fs::is_directory(NewRootPath))
189 this->RootPath = NewRootPath;
190}
191
Ilya Biryukov02d58702017-08-01 15:51:38 +0000192std::future<void> ClangdServer::addDocument(PathRef File, StringRef Contents) {
Ilya Biryukovf01af682017-05-23 13:42:59 +0000193 DocVersion Version = DraftMgr.updateDraft(File, Contents);
Ilya Biryukovf01af682017-05-23 13:42:59 +0000194
Ilya Biryukov02d58702017-08-01 15:51:38 +0000195 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000196 std::shared_ptr<CppFile> Resources = Units.getOrCreateFile(
197 File, ResourceDir, CDB, StorePreamblesInMemory, PCHs, Logger);
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000198 return scheduleReparseAndDiags(File, VersionedDraft{Version, Contents.str()},
199 std::move(Resources), std::move(TaggedFS));
Ilya Biryukov38d79772017-05-16 09:38:59 +0000200}
201
Ilya Biryukov02d58702017-08-01 15:51:38 +0000202std::future<void> ClangdServer::removeDocument(PathRef File) {
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000203 DraftMgr.removeDraft(File);
204 std::shared_ptr<CppFile> Resources = Units.removeIfPresent(File);
205 return scheduleCancelRebuild(std::move(Resources));
Ilya Biryukov38d79772017-05-16 09:38:59 +0000206}
207
Ilya Biryukov02d58702017-08-01 15:51:38 +0000208std::future<void> ClangdServer::forceReparse(PathRef File) {
Ilya Biryukov91dbf5b2017-08-14 08:37:32 +0000209 auto FileContents = DraftMgr.getDraft(File);
210 assert(FileContents.Draft &&
211 "forceReparse() was called for non-added document");
212
213 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000214 auto Recreated = Units.recreateFileIfCompileCommandChanged(
215 File, ResourceDir, CDB, StorePreamblesInMemory, PCHs, Logger);
Ilya Biryukov91dbf5b2017-08-14 08:37:32 +0000216
217 // Note that std::future from this cleanup action is ignored.
218 scheduleCancelRebuild(std::move(Recreated.RemovedFile));
219 // Schedule a reparse.
220 return scheduleReparseAndDiags(File, std::move(FileContents),
221 std::move(Recreated.FileInCollection),
222 std::move(TaggedFS));
Ilya Biryukov0f62ed22017-05-26 12:26:51 +0000223}
224
Sam McCalla40371b2017-11-15 09:16:29 +0000225std::future<Tagged<CompletionList>>
Ilya Biryukov0e27ce42017-06-13 14:15:56 +0000226ClangdServer::codeComplete(PathRef File, Position Pos,
Ilya Biryukovd3b04e32017-12-05 10:42:57 +0000227 const clangd::CodeCompleteOptions &Opts,
Ilya Biryukoved99e4c2017-07-31 17:09:29 +0000228 llvm::Optional<StringRef> OverridenContents,
229 IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) {
Sam McCalla40371b2017-11-15 09:16:29 +0000230 using ResultType = Tagged<CompletionList>;
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000231
232 std::promise<ResultType> ResultPromise;
233
234 auto Callback = [](std::promise<ResultType> ResultPromise,
235 ResultType Result) -> void {
236 ResultPromise.set_value(std::move(Result));
237 };
238
239 std::future<ResultType> ResultFuture = ResultPromise.get_future();
240 codeComplete(BindWithForward(Callback, std::move(ResultPromise)), File, Pos,
Ilya Biryukovd3b04e32017-12-05 10:42:57 +0000241 Opts, OverridenContents, UsedFS);
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000242 return ResultFuture;
243}
244
245void ClangdServer::codeComplete(
Sam McCalla40371b2017-11-15 09:16:29 +0000246 UniqueFunction<void(Tagged<CompletionList>)> Callback, PathRef File,
Ilya Biryukovd3b04e32017-12-05 10:42:57 +0000247 Position Pos, const clangd::CodeCompleteOptions &Opts,
248 llvm::Optional<StringRef> OverridenContents,
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000249 IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) {
Sam McCalla40371b2017-11-15 09:16:29 +0000250 using CallbackType = UniqueFunction<void(Tagged<CompletionList>)>;
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000251
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000252 std::string Contents;
253 if (OverridenContents) {
254 Contents = *OverridenContents;
255 } else {
Ilya Biryukov0e27ce42017-06-13 14:15:56 +0000256 auto FileContents = DraftMgr.getDraft(File);
257 assert(FileContents.Draft &&
258 "codeComplete is called for non-added document");
259
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000260 Contents = std::move(*FileContents.Draft);
Ilya Biryukov0e27ce42017-06-13 14:15:56 +0000261 }
Ilya Biryukov38d79772017-05-16 09:38:59 +0000262
Ilya Biryukovaf0c04b2017-06-14 09:46:44 +0000263 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
Ilya Biryukoved99e4c2017-07-31 17:09:29 +0000264 if (UsedFS)
265 *UsedFS = TaggedFS.Value;
266
Ilya Biryukov02d58702017-08-01 15:51:38 +0000267 std::shared_ptr<CppFile> Resources = Units.getFile(File);
268 assert(Resources && "Calling completion on non-added file");
269
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000270 // Remember the current Preamble and use it when async task starts executing.
271 // At the point when async task starts executing, we may have a different
272 // Preamble in Resources. However, we assume the Preamble that we obtain here
273 // is reusable in completion more often.
274 std::shared_ptr<const PreambleData> Preamble =
275 Resources->getPossiblyStalePreamble();
Ilya Biryukovd3b04e32017-12-05 10:42:57 +0000276 // Copy completion options for passing them to async task handler.
277 auto CodeCompleteOpts = Opts;
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000278 // A task that will be run asynchronously.
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000279 auto Task =
280 // 'mutable' to reassign Preamble variable.
281 [=](CallbackType Callback) mutable {
282 if (!Preamble) {
283 // Maybe we built some preamble before processing this request.
284 Preamble = Resources->getPossiblyStalePreamble();
285 }
286 // FIXME(ibiryukov): even if Preamble is non-null, we may want to check
287 // both the old and the new version in case only one of them matches.
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000288
Sam McCalla40371b2017-11-15 09:16:29 +0000289 CompletionList Result = clangd::codeComplete(
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000290 File, Resources->getCompileCommand(),
291 Preamble ? &Preamble->Preamble : nullptr, Contents, Pos,
292 TaggedFS.Value, PCHs, CodeCompleteOpts, Logger);
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000293
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000294 Callback(make_tagged(std::move(Result), std::move(TaggedFS.Tag)));
295 };
296
297 WorkScheduler.addToFront(std::move(Task), std::move(Callback));
Ilya Biryukov38d79772017-05-16 09:38:59 +0000298}
Ilya Biryukovf01af682017-05-23 13:42:59 +0000299
Benjamin Krameree19f162017-10-26 12:28:13 +0000300llvm::Expected<Tagged<SignatureHelp>>
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000301ClangdServer::signatureHelp(PathRef File, Position Pos,
302 llvm::Optional<StringRef> OverridenContents,
303 IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) {
304 std::string DraftStorage;
305 if (!OverridenContents) {
306 auto FileContents = DraftMgr.getDraft(File);
Benjamin Krameree19f162017-10-26 12:28:13 +0000307 if (!FileContents.Draft)
308 return llvm::make_error<llvm::StringError>(
309 "signatureHelp is called for non-added document",
310 llvm::errc::invalid_argument);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000311
312 DraftStorage = std::move(*FileContents.Draft);
313 OverridenContents = DraftStorage;
314 }
315
316 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
317 if (UsedFS)
318 *UsedFS = TaggedFS.Value;
319
320 std::shared_ptr<CppFile> Resources = Units.getFile(File);
Benjamin Krameree19f162017-10-26 12:28:13 +0000321 if (!Resources)
322 return llvm::make_error<llvm::StringError>(
323 "signatureHelp is called for non-added document",
324 llvm::errc::invalid_argument);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000325
326 auto Preamble = Resources->getPossiblyStalePreamble();
327 auto Result = clangd::signatureHelp(File, Resources->getCompileCommand(),
328 Preamble ? &Preamble->Preamble : nullptr,
329 *OverridenContents, Pos, TaggedFS.Value,
330 PCHs, Logger);
331 return make_tagged(std::move(Result), TaggedFS.Tag);
332}
333
Ilya Biryukovafb55542017-05-16 14:40:30 +0000334std::vector<tooling::Replacement> ClangdServer::formatRange(PathRef File,
335 Range Rng) {
336 std::string Code = getDocument(File);
337
338 size_t Begin = positionToOffset(Code, Rng.start);
339 size_t Len = positionToOffset(Code, Rng.end) - Begin;
340 return formatCode(Code, File, {tooling::Range(Begin, Len)});
341}
342
343std::vector<tooling::Replacement> ClangdServer::formatFile(PathRef File) {
344 // Format everything.
345 std::string Code = getDocument(File);
346 return formatCode(Code, File, {tooling::Range(0, Code.size())});
347}
348
349std::vector<tooling::Replacement> ClangdServer::formatOnType(PathRef File,
350 Position Pos) {
351 // Look for the previous opening brace from the character position and
352 // format starting from there.
353 std::string Code = getDocument(File);
354 size_t CursorPos = positionToOffset(Code, Pos);
355 size_t PreviousLBracePos = StringRef(Code).find_last_of('{', CursorPos);
356 if (PreviousLBracePos == StringRef::npos)
357 PreviousLBracePos = CursorPos;
358 size_t Len = 1 + CursorPos - PreviousLBracePos;
359
360 return formatCode(Code, File, {tooling::Range(PreviousLBracePos, Len)});
361}
Ilya Biryukov38d79772017-05-16 09:38:59 +0000362
Haojian Wu345099c2017-11-09 11:30:04 +0000363Expected<std::vector<tooling::Replacement>>
364ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName) {
365 std::string Code = getDocument(File);
366 std::shared_ptr<CppFile> Resources = Units.getFile(File);
367 RefactoringResultCollector ResultCollector;
368 Resources->getAST().get()->runUnderLock([&](ParsedAST *AST) {
369 const SourceManager &SourceMgr = AST->getASTContext().getSourceManager();
370 const FileEntry *FE =
371 SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
372 if (!FE)
373 return;
374 SourceLocation SourceLocationBeg =
375 clangd::getBeginningOfIdentifier(*AST, Pos, FE);
376 tooling::RefactoringRuleContext Context(
377 AST->getASTContext().getSourceManager());
378 Context.setASTContext(AST->getASTContext());
379 auto Rename = clang::tooling::RenameOccurrences::initiate(
380 Context, SourceRange(SourceLocationBeg), NewName.str());
381 if (!Rename) {
382 ResultCollector.Result = Rename.takeError();
383 return;
384 }
385 Rename->invoke(ResultCollector, Context);
386 });
387 assert(ResultCollector.Result.hasValue());
388 if (!ResultCollector.Result.getValue())
389 return ResultCollector.Result->takeError();
390
391 std::vector<tooling::Replacement> Replacements;
392 for (const tooling::AtomicChange &Change : ResultCollector.Result->get()) {
393 tooling::Replacements ChangeReps = Change.getReplacements();
394 for (const auto &Rep : ChangeReps) {
395 // FIXME: Right now we only support renaming the main file, so we drop
396 // replacements not for the main file. In the future, we might consider to
397 // support:
398 // * rename in any included header
399 // * rename only in the "main" header
400 // * provide an error if there are symbols we won't rename (e.g.
401 // std::vector)
402 // * rename globally in project
403 // * rename in open files
404 if (Rep.getFilePath() == File)
405 Replacements.push_back(Rep);
406 }
407 }
408 return Replacements;
409}
410
Ilya Biryukov38d79772017-05-16 09:38:59 +0000411std::string ClangdServer::getDocument(PathRef File) {
412 auto draft = DraftMgr.getDraft(File);
413 assert(draft.Draft && "File is not tracked, cannot get contents");
414 return *draft.Draft;
415}
416
Ilya Biryukovf01af682017-05-23 13:42:59 +0000417std::string ClangdServer::dumpAST(PathRef File) {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000418 std::shared_ptr<CppFile> Resources = Units.getFile(File);
419 assert(Resources && "dumpAST is called for non-added document");
Ilya Biryukov38d79772017-05-16 09:38:59 +0000420
Ilya Biryukov02d58702017-08-01 15:51:38 +0000421 std::string Result;
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +0000422 Resources->getAST().get()->runUnderLock([&Result](ParsedAST *AST) {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000423 llvm::raw_string_ostream ResultOS(Result);
424 if (AST) {
425 clangd::dumpAST(*AST, ResultOS);
426 } else {
427 ResultOS << "<no-ast>";
428 }
429 ResultOS.flush();
Ilya Biryukovf01af682017-05-23 13:42:59 +0000430 });
Ilya Biryukov02d58702017-08-01 15:51:38 +0000431 return Result;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000432}
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000433
Benjamin Krameree19f162017-10-26 12:28:13 +0000434llvm::Expected<Tagged<std::vector<Location>>>
435ClangdServer::findDefinitions(PathRef File, Position Pos) {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000436 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
437
438 std::shared_ptr<CppFile> Resources = Units.getFile(File);
Benjamin Krameree19f162017-10-26 12:28:13 +0000439 if (!Resources)
440 return llvm::make_error<llvm::StringError>(
441 "findDefinitions called on non-added file",
442 llvm::errc::invalid_argument);
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000443
444 std::vector<Location> Result;
Ilya Biryukove5128f72017-09-20 07:24:15 +0000445 Resources->getAST().get()->runUnderLock([Pos, &Result, this](ParsedAST *AST) {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000446 if (!AST)
447 return;
Ilya Biryukove5128f72017-09-20 07:24:15 +0000448 Result = clangd::findDefinitions(*AST, Pos, Logger);
Ilya Biryukov02d58702017-08-01 15:51:38 +0000449 });
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000450 return make_tagged(std::move(Result), TaggedFS.Tag);
451}
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000452
Marc-Andre Laperle6571b3e2017-09-28 03:14:40 +0000453llvm::Optional<Path> ClangdServer::switchSourceHeader(PathRef Path) {
454
455 StringRef SourceExtensions[] = {".cpp", ".c", ".cc", ".cxx",
456 ".c++", ".m", ".mm"};
457 StringRef HeaderExtensions[] = {".h", ".hh", ".hpp", ".hxx", ".inc"};
458
459 StringRef PathExt = llvm::sys::path::extension(Path);
460
461 // Lookup in a list of known extensions.
462 auto SourceIter =
463 std::find_if(std::begin(SourceExtensions), std::end(SourceExtensions),
464 [&PathExt](PathRef SourceExt) {
465 return SourceExt.equals_lower(PathExt);
466 });
467 bool IsSource = SourceIter != std::end(SourceExtensions);
468
469 auto HeaderIter =
470 std::find_if(std::begin(HeaderExtensions), std::end(HeaderExtensions),
471 [&PathExt](PathRef HeaderExt) {
472 return HeaderExt.equals_lower(PathExt);
473 });
474
475 bool IsHeader = HeaderIter != std::end(HeaderExtensions);
476
477 // We can only switch between extensions known extensions.
478 if (!IsSource && !IsHeader)
479 return llvm::None;
480
481 // Array to lookup extensions for the switch. An opposite of where original
482 // extension was found.
483 ArrayRef<StringRef> NewExts;
484 if (IsSource)
485 NewExts = HeaderExtensions;
486 else
487 NewExts = SourceExtensions;
488
489 // Storage for the new path.
490 SmallString<128> NewPath = StringRef(Path);
491
492 // Instance of vfs::FileSystem, used for file existence checks.
493 auto FS = FSProvider.getTaggedFileSystem(Path).Value;
494
495 // Loop through switched extension candidates.
496 for (StringRef NewExt : NewExts) {
497 llvm::sys::path::replace_extension(NewPath, NewExt);
498 if (FS->exists(NewPath))
499 return NewPath.str().str(); // First str() to convert from SmallString to
500 // StringRef, second to convert from StringRef
501 // to std::string
Ilya Biryukov33334942017-10-06 14:39:39 +0000502
Marc-Andre Laperle6571b3e2017-09-28 03:14:40 +0000503 // Also check NewExt in upper-case, just in case.
504 llvm::sys::path::replace_extension(NewPath, NewExt.upper());
505 if (FS->exists(NewPath))
506 return NewPath.str().str();
Marc-Andre Laperle6571b3e2017-09-28 03:14:40 +0000507 }
508
509 return llvm::None;
510}
511
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000512std::future<void> ClangdServer::scheduleReparseAndDiags(
513 PathRef File, VersionedDraft Contents, std::shared_ptr<CppFile> Resources,
514 Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> TaggedFS) {
515
516 assert(Contents.Draft && "Draft must have contents");
Ilya Biryukov98a1fd72017-10-10 16:12:54 +0000517 UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>()>
518 DeferredRebuild =
519 Resources->deferRebuild(*Contents.Draft, TaggedFS.Value);
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000520 std::promise<void> DonePromise;
521 std::future<void> DoneFuture = DonePromise.get_future();
522
523 DocVersion Version = Contents.Version;
524 Path FileStr = File;
525 VFSTag Tag = TaggedFS.Tag;
526 auto ReparseAndPublishDiags =
527 [this, FileStr, Version,
Ilya Biryukov98a1fd72017-10-10 16:12:54 +0000528 Tag](UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>()>
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000529 DeferredRebuild,
530 std::promise<void> DonePromise) -> void {
531 FulfillPromiseGuard Guard(DonePromise);
532
533 auto CurrentVersion = DraftMgr.getVersion(FileStr);
534 if (CurrentVersion != Version)
535 return; // This request is outdated
536
Ilya Biryukov98a1fd72017-10-10 16:12:54 +0000537 auto Diags = DeferredRebuild();
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000538 if (!Diags)
539 return; // A new reparse was requested before this one completed.
Ilya Biryukov47f22022017-09-20 12:58:55 +0000540
541 // We need to serialize access to resulting diagnostics to avoid calling
542 // `onDiagnosticsReady` in the wrong order.
543 std::lock_guard<std::mutex> DiagsLock(DiagnosticsMutex);
544 DocVersion &LastReportedDiagsVersion = ReportedDiagnosticVersions[FileStr];
545 // FIXME(ibiryukov): get rid of '<' comparison here. In the current
546 // implementation diagnostics will not be reported after version counters'
547 // overflow. This should not happen in practice, since DocVersion is a
548 // 64-bit unsigned integer.
549 if (Version < LastReportedDiagsVersion)
550 return;
551 LastReportedDiagsVersion = Version;
552
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000553 DiagConsumer.onDiagnosticsReady(FileStr,
554 make_tagged(std::move(*Diags), Tag));
555 };
556
557 WorkScheduler.addToFront(std::move(ReparseAndPublishDiags),
558 std::move(DeferredRebuild), std::move(DonePromise));
559 return DoneFuture;
560}
561
562std::future<void>
563ClangdServer::scheduleCancelRebuild(std::shared_ptr<CppFile> Resources) {
564 std::promise<void> DonePromise;
565 std::future<void> DoneFuture = DonePromise.get_future();
566 if (!Resources) {
567 // No need to schedule any cleanup.
568 DonePromise.set_value();
569 return DoneFuture;
570 }
571
Ilya Biryukov98a1fd72017-10-10 16:12:54 +0000572 UniqueFunction<void()> DeferredCancel = Resources->deferCancelRebuild();
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000573 auto CancelReparses = [Resources](std::promise<void> DonePromise,
Ilya Biryukov98a1fd72017-10-10 16:12:54 +0000574 UniqueFunction<void()> DeferredCancel) {
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000575 FulfillPromiseGuard Guard(DonePromise);
Ilya Biryukov98a1fd72017-10-10 16:12:54 +0000576 DeferredCancel();
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000577 };
578 WorkScheduler.addToFront(std::move(CancelReparses), std::move(DonePromise),
579 std::move(DeferredCancel));
580 return DoneFuture;
581}
Marc-Andre Laperlebf114242017-10-02 18:00:37 +0000582
583void ClangdServer::onFileEvent(const DidChangeWatchedFilesParams &Params) {
584 // FIXME: Do nothing for now. This will be used for indexing and potentially
585 // invalidating other caches.
586}