blob: 46c9204149cf49756cbaf4fc808229dd75b04f00 [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"
Sam McCalla66d2cb2017-12-19 17:06:07 +000011#include "CodeComplete.h"
Sam McCallb536a2a2017-12-19 12:23:48 +000012#include "SourceCode.h"
Sam McCalla66d2cb2017-12-19 17:06:07 +000013#include "XRefs.h"
Sam McCall0faecf02018-01-15 12:33:00 +000014#include "index/Merge.h"
Ilya Biryukovafb55542017-05-16 14:40:30 +000015#include "clang/Format/Format.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000016#include "clang/Frontend/CompilerInstance.h"
17#include "clang/Frontend/CompilerInvocation.h"
18#include "clang/Tooling/CompilationDatabase.h"
Ilya Biryukov9e11c4c2017-11-15 18:04:56 +000019#include "clang/Tooling/Refactoring/RefactoringResultConsumer.h"
20#include "clang/Tooling/Refactoring/Rename/RenamingAction.h"
Ilya Biryukovafb55542017-05-16 14:40:30 +000021#include "llvm/ADT/ArrayRef.h"
Sam McCallb5f5eb62018-01-25 17:01:39 +000022#include "llvm/ADT/ScopeExit.h"
Benjamin Krameree19f162017-10-26 12:28:13 +000023#include "llvm/Support/Errc.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000024#include "llvm/Support/FileSystem.h"
Sam McCall8567cb32017-11-02 09:21:51 +000025#include "llvm/Support/FormatProviders.h"
26#include "llvm/Support/FormatVariadic.h"
Marc-Andre Laperle37de9712017-09-27 15:31:17 +000027#include "llvm/Support/Path.h"
Ilya Biryukovf01af682017-05-23 13:42:59 +000028#include "llvm/Support/raw_ostream.h"
29#include <future>
Ilya Biryukov38d79772017-05-16 09:38:59 +000030
Ilya Biryukov2f314102017-05-16 10:06:20 +000031using namespace clang;
Ilya Biryukov38d79772017-05-16 09:38:59 +000032using namespace clang::clangd;
33
Ilya Biryukovafb55542017-05-16 14:40:30 +000034namespace {
35
Ilya Biryukova46f7a92017-06-28 10:34:50 +000036std::string getStandardResourceDir() {
37 static int Dummy; // Just an address in this process.
38 return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy);
39}
40
Haojian Wu345099c2017-11-09 11:30:04 +000041class RefactoringResultCollector final
42 : public tooling::RefactoringResultConsumer {
43public:
44 void handleError(llvm::Error Err) override {
45 assert(!Result.hasValue());
46 // FIXME: figure out a way to return better message for DiagnosticError.
47 // clangd uses llvm::toString to convert the Err to string, however, for
48 // DiagnosticError, only "clang diagnostic" will be generated.
49 Result = std::move(Err);
50 }
51
52 // Using the handle(SymbolOccurrences) from parent class.
53 using tooling::RefactoringResultConsumer::handle;
54
55 void handle(tooling::AtomicChanges SourceReplacements) override {
56 assert(!Result.hasValue());
57 Result = std::move(SourceReplacements);
58 }
59
60 Optional<Expected<tooling::AtomicChanges>> Result;
61};
62
Ilya Biryukovafb55542017-05-16 14:40:30 +000063} // namespace
64
Ilya Biryukov22602992017-05-30 15:11:02 +000065Tagged<IntrusiveRefCntPtr<vfs::FileSystem>>
Ilya Biryukovaf0c04b2017-06-14 09:46:44 +000066RealFileSystemProvider::getTaggedFileSystem(PathRef File) {
Ilya Biryukov22602992017-05-30 15:11:02 +000067 return make_tagged(vfs::getRealFileSystem(), VFSTag());
Ilya Biryukov0f62ed22017-05-26 12:26:51 +000068}
69
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000070unsigned clangd::getDefaultAsyncThreadsCount() {
71 unsigned HardwareConcurrency = std::thread::hardware_concurrency();
72 // C++ standard says that hardware_concurrency()
73 // may return 0, fallback to 1 worker thread in
74 // that case.
75 if (HardwareConcurrency == 0)
76 return 1;
77 return HardwareConcurrency;
78}
79
80ClangdScheduler::ClangdScheduler(unsigned AsyncThreadsCount)
81 : RunSynchronously(AsyncThreadsCount == 0) {
Ilya Biryukov38d79772017-05-16 09:38:59 +000082 if (RunSynchronously) {
83 // Don't start the worker thread if we're running synchronously
84 return;
85 }
86
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000087 Workers.reserve(AsyncThreadsCount);
88 for (unsigned I = 0; I < AsyncThreadsCount; ++I) {
Sam McCall8567cb32017-11-02 09:21:51 +000089 Workers.push_back(std::thread([this, I]() {
90 llvm::set_thread_name(llvm::formatv("scheduler/{0}", I));
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000091 while (true) {
Ilya Biryukov08e6ccb2017-10-09 16:26:26 +000092 UniqueFunction<void()> Request;
Ilya Biryukov38d79772017-05-16 09:38:59 +000093
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000094 // Pick request from the queue
95 {
96 std::unique_lock<std::mutex> Lock(Mutex);
97 // Wait for more requests.
98 RequestCV.wait(Lock,
99 [this] { return !RequestQueue.empty() || Done; });
100 if (Done)
101 return;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000102
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000103 assert(!RequestQueue.empty() && "RequestQueue was empty");
Ilya Biryukov38d79772017-05-16 09:38:59 +0000104
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000105 // We process requests starting from the front of the queue. Users of
106 // ClangdScheduler have a way to prioritise their requests by putting
107 // them to the either side of the queue (using either addToEnd or
108 // addToFront).
109 Request = std::move(RequestQueue.front());
110 RequestQueue.pop_front();
111 } // unlock Mutex
Ilya Biryukov38d79772017-05-16 09:38:59 +0000112
Ilya Biryukov08e6ccb2017-10-09 16:26:26 +0000113 Request();
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000114 }
115 }));
116 }
Ilya Biryukov38d79772017-05-16 09:38:59 +0000117}
118
119ClangdScheduler::~ClangdScheduler() {
120 if (RunSynchronously)
121 return; // no worker thread is running in that case
122
123 {
124 std::lock_guard<std::mutex> Lock(Mutex);
125 // Wake up the worker thread
126 Done = true;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000127 } // unlock Mutex
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000128 RequestCV.notify_all();
129
130 for (auto &Worker : Workers)
131 Worker.join();
Ilya Biryukov38d79772017-05-16 09:38:59 +0000132}
133
Sam McCalladccab62017-11-23 16:58:22 +0000134ClangdServer::ClangdServer(GlobalCompilationDatabase &CDB,
135 DiagnosticsConsumer &DiagConsumer,
136 FileSystemProvider &FSProvider,
137 unsigned AsyncThreadsCount,
Ilya Biryukov940901e2017-12-13 12:51:22 +0000138 bool StorePreamblesInMemory,
Haojian Wuba28e9a2018-01-10 14:44:34 +0000139 bool BuildDynamicSymbolIndex, SymbolIndex *StaticIdx,
Sam McCalladccab62017-11-23 16:58:22 +0000140 llvm::Optional<StringRef> ResourceDir)
Ilya Biryukov929697b2018-01-25 14:19:21 +0000141 : CompileArgs(CDB,
142 ResourceDir ? ResourceDir->str() : getStandardResourceDir()),
143 DiagConsumer(DiagConsumer), FSProvider(FSProvider),
Eric Liubfac8f72017-12-19 18:00:37 +0000144 FileIdx(BuildDynamicSymbolIndex ? new FileIndex() : nullptr),
145 // Pass a callback into `Units` to extract symbols from a newly parsed
146 // file and rebuild the file index synchronously each time an AST is
147 // parsed.
148 // FIXME(ioeric): this can be slow and we may be able to index on less
149 // critical paths.
150 Units(FileIdx
151 ? [this](const Context &Ctx, PathRef Path,
152 ParsedAST *AST) { FileIdx->update(Ctx, Path, AST); }
153 : ASTParsedCallback()),
Ilya Biryukov38d79772017-05-16 09:38:59 +0000154 PCHs(std::make_shared<PCHContainerOperations>()),
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000155 StorePreamblesInMemory(StorePreamblesInMemory),
Sam McCall0faecf02018-01-15 12:33:00 +0000156 WorkScheduler(AsyncThreadsCount) {
157 if (FileIdx && StaticIdx) {
158 MergedIndex = mergeIndex(FileIdx.get(), StaticIdx);
159 Index = MergedIndex.get();
160 } else if (FileIdx)
161 Index = FileIdx.get();
162 else if (StaticIdx)
163 Index = StaticIdx;
164 else
165 Index = nullptr;
166}
Ilya Biryukov38d79772017-05-16 09:38:59 +0000167
Marc-Andre Laperle37de9712017-09-27 15:31:17 +0000168void ClangdServer::setRootPath(PathRef RootPath) {
169 std::string NewRootPath = llvm::sys::path::convert_to_slash(
170 RootPath, llvm::sys::path::Style::posix);
171 if (llvm::sys::fs::is_directory(NewRootPath))
172 this->RootPath = NewRootPath;
173}
174
Ilya Biryukov940901e2017-12-13 12:51:22 +0000175std::future<Context> ClangdServer::addDocument(Context Ctx, PathRef File,
176 StringRef Contents) {
Ilya Biryukovf01af682017-05-23 13:42:59 +0000177 DocVersion Version = DraftMgr.updateDraft(File, Contents);
Ilya Biryukovf01af682017-05-23 13:42:59 +0000178
Ilya Biryukov02d58702017-08-01 15:51:38 +0000179 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
Ilya Biryukov82b59ae2018-01-23 15:07:52 +0000180 std::shared_ptr<CppFile> Resources =
Ilya Biryukov929697b2018-01-25 14:19:21 +0000181 Units.getOrCreateFile(File, StorePreamblesInMemory, PCHs);
Ilya Biryukov940901e2017-12-13 12:51:22 +0000182 return scheduleReparseAndDiags(std::move(Ctx), File,
183 VersionedDraft{Version, Contents.str()},
Ilya Biryukov929697b2018-01-25 14:19:21 +0000184 std::move(Resources), std::move(TaggedFS));
Ilya Biryukov38d79772017-05-16 09:38:59 +0000185}
186
Ilya Biryukov940901e2017-12-13 12:51:22 +0000187std::future<Context> ClangdServer::removeDocument(Context Ctx, PathRef File) {
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000188 DraftMgr.removeDraft(File);
Ilya Biryukov929697b2018-01-25 14:19:21 +0000189 CompileArgs.invalidate(File);
190
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000191 std::shared_ptr<CppFile> Resources = Units.removeIfPresent(File);
Ilya Biryukov940901e2017-12-13 12:51:22 +0000192 return scheduleCancelRebuild(std::move(Ctx), std::move(Resources));
Ilya Biryukov38d79772017-05-16 09:38:59 +0000193}
194
Ilya Biryukov940901e2017-12-13 12:51:22 +0000195std::future<Context> ClangdServer::forceReparse(Context Ctx, PathRef File) {
Ilya Biryukov91dbf5b2017-08-14 08:37:32 +0000196 auto FileContents = DraftMgr.getDraft(File);
197 assert(FileContents.Draft &&
198 "forceReparse() was called for non-added document");
199
Ilya Biryukov929697b2018-01-25 14:19:21 +0000200 // forceReparse promises to request new compilation flags from CDB, so we
201 // remove any cahced flags.
202 CompileArgs.invalidate(File);
203
Ilya Biryukov91dbf5b2017-08-14 08:37:32 +0000204 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
Ilya Biryukov82b59ae2018-01-23 15:07:52 +0000205 std::shared_ptr<CppFile> Resources =
Ilya Biryukov929697b2018-01-25 14:19:21 +0000206 Units.getOrCreateFile(File, StorePreamblesInMemory, PCHs);
Ilya Biryukov82b59ae2018-01-23 15:07:52 +0000207 return scheduleReparseAndDiags(std::move(Ctx), File, FileContents,
Ilya Biryukov929697b2018-01-25 14:19:21 +0000208 std::move(Resources), std::move(TaggedFS));
Ilya Biryukov0f62ed22017-05-26 12:26:51 +0000209}
210
Ilya Biryukov940901e2017-12-13 12:51:22 +0000211std::future<std::pair<Context, Tagged<CompletionList>>>
212ClangdServer::codeComplete(Context Ctx, PathRef File, Position Pos,
Ilya Biryukovd3b04e32017-12-05 10:42:57 +0000213 const clangd::CodeCompleteOptions &Opts,
Ilya Biryukoved99e4c2017-07-31 17:09:29 +0000214 llvm::Optional<StringRef> OverridenContents,
215 IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) {
Ilya Biryukov940901e2017-12-13 12:51:22 +0000216 using ResultType = std::pair<Context, Tagged<CompletionList>>;
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000217
218 std::promise<ResultType> ResultPromise;
219
Ilya Biryukov940901e2017-12-13 12:51:22 +0000220 auto Callback = [](std::promise<ResultType> ResultPromise, Context Ctx,
221 Tagged<CompletionList> Result) -> void {
222 ResultPromise.set_value({std::move(Ctx), std::move(Result)});
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000223 };
224
225 std::future<ResultType> ResultFuture = ResultPromise.get_future();
Ilya Biryukov940901e2017-12-13 12:51:22 +0000226 codeComplete(std::move(Ctx), File, Pos, Opts,
227 BindWithForward(Callback, std::move(ResultPromise)),
228 OverridenContents, UsedFS);
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000229 return ResultFuture;
230}
231
232void ClangdServer::codeComplete(
Ilya Biryukov940901e2017-12-13 12:51:22 +0000233 Context Ctx, PathRef File, Position Pos,
234 const clangd::CodeCompleteOptions &Opts,
235 UniqueFunction<void(Context, Tagged<CompletionList>)> Callback,
Ilya Biryukovd3b04e32017-12-05 10:42:57 +0000236 llvm::Optional<StringRef> OverridenContents,
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000237 IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) {
Ilya Biryukov940901e2017-12-13 12:51:22 +0000238 using CallbackType = UniqueFunction<void(Context, Tagged<CompletionList>)>;
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000239
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000240 std::string Contents;
241 if (OverridenContents) {
242 Contents = *OverridenContents;
243 } else {
Ilya Biryukov0e27ce42017-06-13 14:15:56 +0000244 auto FileContents = DraftMgr.getDraft(File);
245 assert(FileContents.Draft &&
246 "codeComplete is called for non-added document");
247
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000248 Contents = std::move(*FileContents.Draft);
Ilya Biryukov0e27ce42017-06-13 14:15:56 +0000249 }
Ilya Biryukov38d79772017-05-16 09:38:59 +0000250
Ilya Biryukovaf0c04b2017-06-14 09:46:44 +0000251 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
Ilya Biryukoved99e4c2017-07-31 17:09:29 +0000252 if (UsedFS)
253 *UsedFS = TaggedFS.Value;
254
Ilya Biryukov02d58702017-08-01 15:51:38 +0000255 std::shared_ptr<CppFile> Resources = Units.getFile(File);
256 assert(Resources && "Calling completion on non-added file");
257
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000258 // Remember the current Preamble and use it when async task starts executing.
259 // At the point when async task starts executing, we may have a different
260 // Preamble in Resources. However, we assume the Preamble that we obtain here
261 // is reusable in completion more often.
262 std::shared_ptr<const PreambleData> Preamble =
263 Resources->getPossiblyStalePreamble();
Ilya Biryukovd3b04e32017-12-05 10:42:57 +0000264 // Copy completion options for passing them to async task handler.
265 auto CodeCompleteOpts = Opts;
Sam McCall0faecf02018-01-15 12:33:00 +0000266 if (!CodeCompleteOpts.Index) // Respect overridden index.
267 CodeCompleteOpts.Index = Index;
Ilya Biryukovf6e2b4c2018-01-09 14:39:27 +0000268
269 // Copy File, as it is a PathRef that will go out of scope before Task is
270 // executed.
271 Path FileStr = File;
272 // Copy PCHs to avoid accessing this->PCHs concurrently
273 std::shared_ptr<PCHContainerOperations> PCHs = this->PCHs;
Ilya Biryukov929697b2018-01-25 14:19:21 +0000274 tooling::CompileCommand CompileCommand = CompileArgs.getCompileCommand(File);
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000275 // A task that will be run asynchronously.
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000276 auto Task =
277 // 'mutable' to reassign Preamble variable.
Ilya Biryukovf6e2b4c2018-01-09 14:39:27 +0000278 [FileStr, Preamble, Resources, Contents, Pos, CodeCompleteOpts, TaggedFS,
Ilya Biryukov82b59ae2018-01-23 15:07:52 +0000279 PCHs, CompileCommand](Context Ctx, CallbackType Callback) mutable {
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000280 if (!Preamble) {
281 // Maybe we built some preamble before processing this request.
282 Preamble = Resources->getPossiblyStalePreamble();
283 }
284 // FIXME(ibiryukov): even if Preamble is non-null, we may want to check
285 // both the old and the new version in case only one of them matches.
Sam McCalla40371b2017-11-15 09:16:29 +0000286 CompletionList Result = clangd::codeComplete(
Ilya Biryukov82b59ae2018-01-23 15:07:52 +0000287 Ctx, FileStr, CompileCommand,
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000288 Preamble ? &Preamble->Preamble : nullptr, Contents, Pos,
Ilya Biryukov940901e2017-12-13 12:51:22 +0000289 TaggedFS.Value, PCHs, CodeCompleteOpts);
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000290
Ilya Biryukov940901e2017-12-13 12:51:22 +0000291 Callback(std::move(Ctx),
292 make_tagged(std::move(Result), std::move(TaggedFS.Tag)));
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000293 };
294
Ilya Biryukov940901e2017-12-13 12:51:22 +0000295 WorkScheduler.addToFront(std::move(Task), std::move(Ctx),
296 std::move(Callback));
Ilya Biryukov38d79772017-05-16 09:38:59 +0000297}
Ilya Biryukovf01af682017-05-23 13:42:59 +0000298
Benjamin Krameree19f162017-10-26 12:28:13 +0000299llvm::Expected<Tagged<SignatureHelp>>
Ilya Biryukov940901e2017-12-13 12:51:22 +0000300ClangdServer::signatureHelp(const Context &Ctx, PathRef File, Position Pos,
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000301 llvm::Optional<StringRef> OverridenContents,
302 IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) {
303 std::string DraftStorage;
304 if (!OverridenContents) {
305 auto FileContents = DraftMgr.getDraft(File);
Benjamin Krameree19f162017-10-26 12:28:13 +0000306 if (!FileContents.Draft)
307 return llvm::make_error<llvm::StringError>(
308 "signatureHelp is called for non-added document",
309 llvm::errc::invalid_argument);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000310
311 DraftStorage = std::move(*FileContents.Draft);
312 OverridenContents = DraftStorage;
313 }
314
315 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
316 if (UsedFS)
317 *UsedFS = TaggedFS.Value;
318
319 std::shared_ptr<CppFile> Resources = Units.getFile(File);
Benjamin Krameree19f162017-10-26 12:28:13 +0000320 if (!Resources)
321 return llvm::make_error<llvm::StringError>(
322 "signatureHelp is called for non-added document",
323 llvm::errc::invalid_argument);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000324
325 auto Preamble = Resources->getPossiblyStalePreamble();
Ilya Biryukov940901e2017-12-13 12:51:22 +0000326 auto Result =
Ilya Biryukov929697b2018-01-25 14:19:21 +0000327 clangd::signatureHelp(Ctx, File, CompileArgs.getCompileCommand(File),
Ilya Biryukov940901e2017-12-13 12:51:22 +0000328 Preamble ? &Preamble->Preamble : nullptr,
329 *OverridenContents, Pos, TaggedFS.Value, PCHs);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000330 return make_tagged(std::move(Result), TaggedFS.Tag);
331}
332
Raoul Wols212bcf82017-12-12 20:25:06 +0000333llvm::Expected<tooling::Replacements>
334ClangdServer::formatRange(StringRef Code, PathRef File, Range Rng) {
Ilya Biryukovafb55542017-05-16 14:40:30 +0000335 size_t Begin = positionToOffset(Code, Rng.start);
336 size_t Len = positionToOffset(Code, Rng.end) - Begin;
337 return formatCode(Code, File, {tooling::Range(Begin, Len)});
338}
339
Raoul Wols212bcf82017-12-12 20:25:06 +0000340llvm::Expected<tooling::Replacements> ClangdServer::formatFile(StringRef Code,
341 PathRef File) {
Ilya Biryukovafb55542017-05-16 14:40:30 +0000342 // Format everything.
Ilya Biryukovafb55542017-05-16 14:40:30 +0000343 return formatCode(Code, File, {tooling::Range(0, Code.size())});
344}
345
Raoul Wols212bcf82017-12-12 20:25:06 +0000346llvm::Expected<tooling::Replacements>
347ClangdServer::formatOnType(StringRef Code, PathRef File, Position Pos) {
Ilya Biryukovafb55542017-05-16 14:40:30 +0000348 // Look for the previous opening brace from the character position and
349 // format starting from there.
Ilya Biryukovafb55542017-05-16 14:40:30 +0000350 size_t CursorPos = positionToOffset(Code, Pos);
351 size_t PreviousLBracePos = StringRef(Code).find_last_of('{', CursorPos);
352 if (PreviousLBracePos == StringRef::npos)
353 PreviousLBracePos = CursorPos;
Sam McCallb536a2a2017-12-19 12:23:48 +0000354 size_t Len = CursorPos - PreviousLBracePos;
Ilya Biryukovafb55542017-05-16 14:40:30 +0000355
356 return formatCode(Code, File, {tooling::Range(PreviousLBracePos, Len)});
357}
Ilya Biryukov38d79772017-05-16 09:38:59 +0000358
Haojian Wu345099c2017-11-09 11:30:04 +0000359Expected<std::vector<tooling::Replacement>>
Ilya Biryukov940901e2017-12-13 12:51:22 +0000360ClangdServer::rename(const Context &Ctx, PathRef File, Position Pos,
361 llvm::StringRef NewName) {
Haojian Wu345099c2017-11-09 11:30:04 +0000362 std::shared_ptr<CppFile> Resources = Units.getFile(File);
363 RefactoringResultCollector ResultCollector;
364 Resources->getAST().get()->runUnderLock([&](ParsedAST *AST) {
365 const SourceManager &SourceMgr = AST->getASTContext().getSourceManager();
366 const FileEntry *FE =
367 SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
368 if (!FE)
369 return;
370 SourceLocation SourceLocationBeg =
371 clangd::getBeginningOfIdentifier(*AST, Pos, FE);
372 tooling::RefactoringRuleContext Context(
373 AST->getASTContext().getSourceManager());
374 Context.setASTContext(AST->getASTContext());
375 auto Rename = clang::tooling::RenameOccurrences::initiate(
376 Context, SourceRange(SourceLocationBeg), NewName.str());
377 if (!Rename) {
378 ResultCollector.Result = Rename.takeError();
379 return;
380 }
381 Rename->invoke(ResultCollector, Context);
382 });
383 assert(ResultCollector.Result.hasValue());
384 if (!ResultCollector.Result.getValue())
385 return ResultCollector.Result->takeError();
386
387 std::vector<tooling::Replacement> Replacements;
388 for (const tooling::AtomicChange &Change : ResultCollector.Result->get()) {
389 tooling::Replacements ChangeReps = Change.getReplacements();
390 for (const auto &Rep : ChangeReps) {
391 // FIXME: Right now we only support renaming the main file, so we drop
392 // replacements not for the main file. In the future, we might consider to
393 // support:
394 // * rename in any included header
395 // * rename only in the "main" header
396 // * provide an error if there are symbols we won't rename (e.g.
397 // std::vector)
398 // * rename globally in project
399 // * rename in open files
400 if (Rep.getFilePath() == File)
401 Replacements.push_back(Rep);
402 }
403 }
404 return Replacements;
405}
406
Ilya Biryukov261c72e2018-01-17 12:30:24 +0000407llvm::Optional<std::string> ClangdServer::getDocument(PathRef File) {
408 auto Latest = DraftMgr.getDraft(File);
409 if (!Latest.Draft)
410 return llvm::None;
411 return std::move(*Latest.Draft);
Ilya Biryukov38d79772017-05-16 09:38:59 +0000412}
413
Ilya Biryukovf01af682017-05-23 13:42:59 +0000414std::string ClangdServer::dumpAST(PathRef File) {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000415 std::shared_ptr<CppFile> Resources = Units.getFile(File);
Ilya Biryukov261c72e2018-01-17 12:30:24 +0000416 if (!Resources)
417 return "<non-added file>";
Ilya Biryukov38d79772017-05-16 09:38:59 +0000418
Ilya Biryukov02d58702017-08-01 15:51:38 +0000419 std::string Result;
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +0000420 Resources->getAST().get()->runUnderLock([&Result](ParsedAST *AST) {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000421 llvm::raw_string_ostream ResultOS(Result);
422 if (AST) {
423 clangd::dumpAST(*AST, ResultOS);
424 } else {
425 ResultOS << "<no-ast>";
426 }
427 ResultOS.flush();
Ilya Biryukovf01af682017-05-23 13:42:59 +0000428 });
Ilya Biryukov02d58702017-08-01 15:51:38 +0000429 return Result;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000430}
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000431
Benjamin Krameree19f162017-10-26 12:28:13 +0000432llvm::Expected<Tagged<std::vector<Location>>>
Ilya Biryukov940901e2017-12-13 12:51:22 +0000433ClangdServer::findDefinitions(const Context &Ctx, PathRef File, Position Pos) {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000434 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
435
436 std::shared_ptr<CppFile> Resources = Units.getFile(File);
Benjamin Krameree19f162017-10-26 12:28:13 +0000437 if (!Resources)
438 return llvm::make_error<llvm::StringError>(
439 "findDefinitions called on non-added file",
440 llvm::errc::invalid_argument);
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000441
442 std::vector<Location> Result;
Ilya Biryukov940901e2017-12-13 12:51:22 +0000443 Resources->getAST().get()->runUnderLock([Pos, &Result, &Ctx](ParsedAST *AST) {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000444 if (!AST)
445 return;
Ilya Biryukov940901e2017-12-13 12:51:22 +0000446 Result = clangd::findDefinitions(Ctx, *AST, Pos);
Ilya Biryukov02d58702017-08-01 15:51:38 +0000447 });
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000448 return make_tagged(std::move(Result), TaggedFS.Tag);
449}
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000450
Marc-Andre Laperle6571b3e2017-09-28 03:14:40 +0000451llvm::Optional<Path> ClangdServer::switchSourceHeader(PathRef Path) {
452
453 StringRef SourceExtensions[] = {".cpp", ".c", ".cc", ".cxx",
454 ".c++", ".m", ".mm"};
455 StringRef HeaderExtensions[] = {".h", ".hh", ".hpp", ".hxx", ".inc"};
456
457 StringRef PathExt = llvm::sys::path::extension(Path);
458
459 // Lookup in a list of known extensions.
460 auto SourceIter =
461 std::find_if(std::begin(SourceExtensions), std::end(SourceExtensions),
462 [&PathExt](PathRef SourceExt) {
463 return SourceExt.equals_lower(PathExt);
464 });
465 bool IsSource = SourceIter != std::end(SourceExtensions);
466
467 auto HeaderIter =
468 std::find_if(std::begin(HeaderExtensions), std::end(HeaderExtensions),
469 [&PathExt](PathRef HeaderExt) {
470 return HeaderExt.equals_lower(PathExt);
471 });
472
473 bool IsHeader = HeaderIter != std::end(HeaderExtensions);
474
475 // We can only switch between extensions known extensions.
476 if (!IsSource && !IsHeader)
477 return llvm::None;
478
479 // Array to lookup extensions for the switch. An opposite of where original
480 // extension was found.
481 ArrayRef<StringRef> NewExts;
482 if (IsSource)
483 NewExts = HeaderExtensions;
484 else
485 NewExts = SourceExtensions;
486
487 // Storage for the new path.
488 SmallString<128> NewPath = StringRef(Path);
489
490 // Instance of vfs::FileSystem, used for file existence checks.
491 auto FS = FSProvider.getTaggedFileSystem(Path).Value;
492
493 // Loop through switched extension candidates.
494 for (StringRef NewExt : NewExts) {
495 llvm::sys::path::replace_extension(NewPath, NewExt);
496 if (FS->exists(NewPath))
497 return NewPath.str().str(); // First str() to convert from SmallString to
498 // StringRef, second to convert from StringRef
499 // to std::string
Ilya Biryukov33334942017-10-06 14:39:39 +0000500
Marc-Andre Laperle6571b3e2017-09-28 03:14:40 +0000501 // Also check NewExt in upper-case, just in case.
502 llvm::sys::path::replace_extension(NewPath, NewExt.upper());
503 if (FS->exists(NewPath))
504 return NewPath.str().str();
Marc-Andre Laperle6571b3e2017-09-28 03:14:40 +0000505 }
506
507 return llvm::None;
508}
509
Raoul Wols212bcf82017-12-12 20:25:06 +0000510llvm::Expected<tooling::Replacements>
511ClangdServer::formatCode(llvm::StringRef Code, PathRef File,
512 ArrayRef<tooling::Range> Ranges) {
513 // Call clang-format.
514 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
515 auto StyleOrError =
516 format::getStyle("file", File, "LLVM", Code, TaggedFS.Value.get());
517 if (!StyleOrError) {
518 return StyleOrError.takeError();
519 } else {
520 return format::reformat(StyleOrError.get(), Code, Ranges, File);
521 }
522}
523
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000524llvm::Expected<Tagged<std::vector<DocumentHighlight>>>
Ilya Biryukov940901e2017-12-13 12:51:22 +0000525ClangdServer::findDocumentHighlights(const Context &Ctx, PathRef File,
526 Position Pos) {
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000527 auto FileContents = DraftMgr.getDraft(File);
528 if (!FileContents.Draft)
529 return llvm::make_error<llvm::StringError>(
530 "findDocumentHighlights called on non-added file",
531 llvm::errc::invalid_argument);
532
533 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
534
535 std::shared_ptr<CppFile> Resources = Units.getFile(File);
536 if (!Resources)
537 return llvm::make_error<llvm::StringError>(
538 "findDocumentHighlights called on non-added file",
539 llvm::errc::invalid_argument);
540
541 std::vector<DocumentHighlight> Result;
542 llvm::Optional<llvm::Error> Err;
Ilya Biryukov940901e2017-12-13 12:51:22 +0000543 Resources->getAST().get()->runUnderLock([Pos, &Ctx, &Err,
544 &Result](ParsedAST *AST) {
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000545 if (!AST) {
546 Err = llvm::make_error<llvm::StringError>("Invalid AST",
547 llvm::errc::invalid_argument);
548 return;
549 }
Ilya Biryukov940901e2017-12-13 12:51:22 +0000550 Result = clangd::findDocumentHighlights(Ctx, *AST, Pos);
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000551 });
552
553 if (Err)
554 return std::move(*Err);
555 return make_tagged(Result, TaggedFS.Tag);
556}
557
Ilya Biryukov940901e2017-12-13 12:51:22 +0000558std::future<Context> ClangdServer::scheduleReparseAndDiags(
559 Context Ctx, PathRef File, VersionedDraft Contents,
560 std::shared_ptr<CppFile> Resources,
Ilya Biryukov929697b2018-01-25 14:19:21 +0000561 Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> TaggedFS) {
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000562 assert(Contents.Draft && "Draft must have contents");
Ilya Biryukov929697b2018-01-25 14:19:21 +0000563 ParseInputs Inputs = {CompileArgs.getCompileCommand(File),
564 std::move(TaggedFS.Value), *std::move(Contents.Draft)};
565
Ilya Biryukov940901e2017-12-13 12:51:22 +0000566 UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>(const Context &)>
Ilya Biryukov82b59ae2018-01-23 15:07:52 +0000567 DeferredRebuild = Resources->deferRebuild(std::move(Inputs));
Ilya Biryukov940901e2017-12-13 12:51:22 +0000568 std::promise<Context> DonePromise;
569 std::future<Context> DoneFuture = DonePromise.get_future();
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000570
571 DocVersion Version = Contents.Version;
572 Path FileStr = File;
573 VFSTag Tag = TaggedFS.Tag;
574 auto ReparseAndPublishDiags =
575 [this, FileStr, Version,
Ilya Biryukov940901e2017-12-13 12:51:22 +0000576 Tag](UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>(
577 const Context &)>
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000578 DeferredRebuild,
Ilya Biryukov940901e2017-12-13 12:51:22 +0000579 std::promise<Context> DonePromise, Context Ctx) -> void {
Sam McCallb5f5eb62018-01-25 17:01:39 +0000580 auto Guard =
581 llvm::make_scope_exit([&]() { DonePromise.set_value(std::move(Ctx)); });
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000582
583 auto CurrentVersion = DraftMgr.getVersion(FileStr);
584 if (CurrentVersion != Version)
585 return; // This request is outdated
586
Ilya Biryukov940901e2017-12-13 12:51:22 +0000587 auto Diags = DeferredRebuild(Ctx);
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000588 if (!Diags)
589 return; // A new reparse was requested before this one completed.
Ilya Biryukov47f22022017-09-20 12:58:55 +0000590
591 // We need to serialize access to resulting diagnostics to avoid calling
592 // `onDiagnosticsReady` in the wrong order.
593 std::lock_guard<std::mutex> DiagsLock(DiagnosticsMutex);
594 DocVersion &LastReportedDiagsVersion = ReportedDiagnosticVersions[FileStr];
595 // FIXME(ibiryukov): get rid of '<' comparison here. In the current
596 // implementation diagnostics will not be reported after version counters'
597 // overflow. This should not happen in practice, since DocVersion is a
598 // 64-bit unsigned integer.
599 if (Version < LastReportedDiagsVersion)
600 return;
601 LastReportedDiagsVersion = Version;
602
Ilya Biryukov95558392018-01-10 17:59:27 +0000603 DiagConsumer.onDiagnosticsReady(Ctx, FileStr,
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000604 make_tagged(std::move(*Diags), Tag));
605 };
606
607 WorkScheduler.addToFront(std::move(ReparseAndPublishDiags),
Ilya Biryukov940901e2017-12-13 12:51:22 +0000608 std::move(DeferredRebuild), std::move(DonePromise),
609 std::move(Ctx));
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000610 return DoneFuture;
611}
612
Ilya Biryukov940901e2017-12-13 12:51:22 +0000613std::future<Context>
614ClangdServer::scheduleCancelRebuild(Context Ctx,
615 std::shared_ptr<CppFile> Resources) {
616 std::promise<Context> DonePromise;
617 std::future<Context> DoneFuture = DonePromise.get_future();
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000618 if (!Resources) {
619 // No need to schedule any cleanup.
Ilya Biryukov940901e2017-12-13 12:51:22 +0000620 DonePromise.set_value(std::move(Ctx));
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000621 return DoneFuture;
622 }
623
Ilya Biryukov98a1fd72017-10-10 16:12:54 +0000624 UniqueFunction<void()> DeferredCancel = Resources->deferCancelRebuild();
Ilya Biryukov940901e2017-12-13 12:51:22 +0000625 auto CancelReparses = [Resources](std::promise<Context> DonePromise,
626 UniqueFunction<void()> DeferredCancel,
627 Context Ctx) {
Ilya Biryukov98a1fd72017-10-10 16:12:54 +0000628 DeferredCancel();
Ilya Biryukov940901e2017-12-13 12:51:22 +0000629 DonePromise.set_value(std::move(Ctx));
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000630 };
631 WorkScheduler.addToFront(std::move(CancelReparses), std::move(DonePromise),
Ilya Biryukov940901e2017-12-13 12:51:22 +0000632 std::move(DeferredCancel), std::move(Ctx));
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000633 return DoneFuture;
634}
Marc-Andre Laperlebf114242017-10-02 18:00:37 +0000635
636void ClangdServer::onFileEvent(const DidChangeWatchedFilesParams &Params) {
637 // FIXME: Do nothing for now. This will be used for indexing and potentially
638 // invalidating other caches.
639}
Ilya Biryukovdf842342018-01-25 14:32:21 +0000640
641std::vector<std::pair<Path, std::size_t>>
642ClangdServer::getUsedBytesPerFile() const {
643 return Units.getUsedBytesPerFile();
644}