blob: 588cbac63c7b8a5d0263a15cadecf9e65fc8db48 [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"
Benjamin Krameree19f162017-10-26 12:28:13 +000022#include "llvm/Support/Errc.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000023#include "llvm/Support/FileSystem.h"
Sam McCall8567cb32017-11-02 09:21:51 +000024#include "llvm/Support/FormatProviders.h"
25#include "llvm/Support/FormatVariadic.h"
Marc-Andre Laperle37de9712017-09-27 15:31:17 +000026#include "llvm/Support/Path.h"
Ilya Biryukovf01af682017-05-23 13:42:59 +000027#include "llvm/Support/raw_ostream.h"
28#include <future>
Ilya Biryukov38d79772017-05-16 09:38:59 +000029
Ilya Biryukov2f314102017-05-16 10:06:20 +000030using namespace clang;
Ilya Biryukov38d79772017-05-16 09:38:59 +000031using namespace clang::clangd;
32
Ilya Biryukovafb55542017-05-16 14:40:30 +000033namespace {
34
Ilya Biryukova46f7a92017-06-28 10:34:50 +000035std::string getStandardResourceDir() {
36 static int Dummy; // Just an address in this process.
37 return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy);
38}
39
Haojian Wu345099c2017-11-09 11:30:04 +000040class RefactoringResultCollector final
41 : public tooling::RefactoringResultConsumer {
42public:
43 void handleError(llvm::Error Err) override {
44 assert(!Result.hasValue());
45 // FIXME: figure out a way to return better message for DiagnosticError.
46 // clangd uses llvm::toString to convert the Err to string, however, for
47 // DiagnosticError, only "clang diagnostic" will be generated.
48 Result = std::move(Err);
49 }
50
51 // Using the handle(SymbolOccurrences) from parent class.
52 using tooling::RefactoringResultConsumer::handle;
53
54 void handle(tooling::AtomicChanges SourceReplacements) override {
55 assert(!Result.hasValue());
56 Result = std::move(SourceReplacements);
57 }
58
59 Optional<Expected<tooling::AtomicChanges>> Result;
60};
61
Ilya Biryukovafb55542017-05-16 14:40:30 +000062} // namespace
63
Ilya Biryukov22602992017-05-30 15:11:02 +000064Tagged<IntrusiveRefCntPtr<vfs::FileSystem>>
Ilya Biryukovaf0c04b2017-06-14 09:46:44 +000065RealFileSystemProvider::getTaggedFileSystem(PathRef File) {
Ilya Biryukov22602992017-05-30 15:11:02 +000066 return make_tagged(vfs::getRealFileSystem(), VFSTag());
Ilya Biryukov0f62ed22017-05-26 12:26:51 +000067}
68
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000069unsigned clangd::getDefaultAsyncThreadsCount() {
70 unsigned HardwareConcurrency = std::thread::hardware_concurrency();
71 // C++ standard says that hardware_concurrency()
72 // may return 0, fallback to 1 worker thread in
73 // that case.
74 if (HardwareConcurrency == 0)
75 return 1;
76 return HardwareConcurrency;
77}
78
79ClangdScheduler::ClangdScheduler(unsigned AsyncThreadsCount)
80 : RunSynchronously(AsyncThreadsCount == 0) {
Ilya Biryukov38d79772017-05-16 09:38:59 +000081 if (RunSynchronously) {
82 // Don't start the worker thread if we're running synchronously
83 return;
84 }
85
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000086 Workers.reserve(AsyncThreadsCount);
87 for (unsigned I = 0; I < AsyncThreadsCount; ++I) {
Sam McCall8567cb32017-11-02 09:21:51 +000088 Workers.push_back(std::thread([this, I]() {
89 llvm::set_thread_name(llvm::formatv("scheduler/{0}", I));
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000090 while (true) {
Ilya Biryukov08e6ccb2017-10-09 16:26:26 +000091 UniqueFunction<void()> Request;
Ilya Biryukov38d79772017-05-16 09:38:59 +000092
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000093 // Pick request from the queue
94 {
95 std::unique_lock<std::mutex> Lock(Mutex);
96 // Wait for more requests.
97 RequestCV.wait(Lock,
98 [this] { return !RequestQueue.empty() || Done; });
99 if (Done)
100 return;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000101
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000102 assert(!RequestQueue.empty() && "RequestQueue was empty");
Ilya Biryukov38d79772017-05-16 09:38:59 +0000103
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000104 // We process requests starting from the front of the queue. Users of
105 // ClangdScheduler have a way to prioritise their requests by putting
106 // them to the either side of the queue (using either addToEnd or
107 // addToFront).
108 Request = std::move(RequestQueue.front());
109 RequestQueue.pop_front();
110 } // unlock Mutex
Ilya Biryukov38d79772017-05-16 09:38:59 +0000111
Ilya Biryukov08e6ccb2017-10-09 16:26:26 +0000112 Request();
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000113 }
114 }));
115 }
Ilya Biryukov38d79772017-05-16 09:38:59 +0000116}
117
118ClangdScheduler::~ClangdScheduler() {
119 if (RunSynchronously)
120 return; // no worker thread is running in that case
121
122 {
123 std::lock_guard<std::mutex> Lock(Mutex);
124 // Wake up the worker thread
125 Done = true;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000126 } // unlock Mutex
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000127 RequestCV.notify_all();
128
129 for (auto &Worker : Workers)
130 Worker.join();
Ilya Biryukov38d79772017-05-16 09:38:59 +0000131}
132
Sam McCalladccab62017-11-23 16:58:22 +0000133ClangdServer::ClangdServer(GlobalCompilationDatabase &CDB,
134 DiagnosticsConsumer &DiagConsumer,
135 FileSystemProvider &FSProvider,
136 unsigned AsyncThreadsCount,
Ilya Biryukov940901e2017-12-13 12:51:22 +0000137 bool StorePreamblesInMemory,
Haojian Wuba28e9a2018-01-10 14:44:34 +0000138 bool BuildDynamicSymbolIndex, SymbolIndex *StaticIdx,
Sam McCalladccab62017-11-23 16:58:22 +0000139 llvm::Optional<StringRef> ResourceDir)
Ilya Biryukov929697b2018-01-25 14:19:21 +0000140 : CompileArgs(CDB,
141 ResourceDir ? ResourceDir->str() : getStandardResourceDir()),
142 DiagConsumer(DiagConsumer), FSProvider(FSProvider),
Eric Liubfac8f72017-12-19 18:00:37 +0000143 FileIdx(BuildDynamicSymbolIndex ? new FileIndex() : nullptr),
144 // Pass a callback into `Units` to extract symbols from a newly parsed
145 // file and rebuild the file index synchronously each time an AST is
146 // parsed.
147 // FIXME(ioeric): this can be slow and we may be able to index on less
148 // critical paths.
149 Units(FileIdx
150 ? [this](const Context &Ctx, PathRef Path,
151 ParsedAST *AST) { FileIdx->update(Ctx, Path, AST); }
152 : ASTParsedCallback()),
Ilya Biryukov38d79772017-05-16 09:38:59 +0000153 PCHs(std::make_shared<PCHContainerOperations>()),
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000154 StorePreamblesInMemory(StorePreamblesInMemory),
Sam McCall0faecf02018-01-15 12:33:00 +0000155 WorkScheduler(AsyncThreadsCount) {
156 if (FileIdx && StaticIdx) {
157 MergedIndex = mergeIndex(FileIdx.get(), StaticIdx);
158 Index = MergedIndex.get();
159 } else if (FileIdx)
160 Index = FileIdx.get();
161 else if (StaticIdx)
162 Index = StaticIdx;
163 else
164 Index = nullptr;
165}
Ilya Biryukov38d79772017-05-16 09:38:59 +0000166
Marc-Andre Laperle37de9712017-09-27 15:31:17 +0000167void ClangdServer::setRootPath(PathRef RootPath) {
168 std::string NewRootPath = llvm::sys::path::convert_to_slash(
169 RootPath, llvm::sys::path::Style::posix);
170 if (llvm::sys::fs::is_directory(NewRootPath))
171 this->RootPath = NewRootPath;
172}
173
Ilya Biryukov940901e2017-12-13 12:51:22 +0000174std::future<Context> ClangdServer::addDocument(Context Ctx, PathRef File,
175 StringRef Contents) {
Ilya Biryukovf01af682017-05-23 13:42:59 +0000176 DocVersion Version = DraftMgr.updateDraft(File, Contents);
Ilya Biryukovf01af682017-05-23 13:42:59 +0000177
Ilya Biryukov02d58702017-08-01 15:51:38 +0000178 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
Ilya Biryukov82b59ae2018-01-23 15:07:52 +0000179 std::shared_ptr<CppFile> Resources =
Ilya Biryukov929697b2018-01-25 14:19:21 +0000180 Units.getOrCreateFile(File, StorePreamblesInMemory, PCHs);
Ilya Biryukov940901e2017-12-13 12:51:22 +0000181 return scheduleReparseAndDiags(std::move(Ctx), File,
182 VersionedDraft{Version, Contents.str()},
Ilya Biryukov929697b2018-01-25 14:19:21 +0000183 std::move(Resources), std::move(TaggedFS));
Ilya Biryukov38d79772017-05-16 09:38:59 +0000184}
185
Ilya Biryukov940901e2017-12-13 12:51:22 +0000186std::future<Context> ClangdServer::removeDocument(Context Ctx, PathRef File) {
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000187 DraftMgr.removeDraft(File);
Ilya Biryukov929697b2018-01-25 14:19:21 +0000188 CompileArgs.invalidate(File);
189
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000190 std::shared_ptr<CppFile> Resources = Units.removeIfPresent(File);
Ilya Biryukov940901e2017-12-13 12:51:22 +0000191 return scheduleCancelRebuild(std::move(Ctx), std::move(Resources));
Ilya Biryukov38d79772017-05-16 09:38:59 +0000192}
193
Ilya Biryukov940901e2017-12-13 12:51:22 +0000194std::future<Context> ClangdServer::forceReparse(Context Ctx, PathRef File) {
Ilya Biryukov91dbf5b2017-08-14 08:37:32 +0000195 auto FileContents = DraftMgr.getDraft(File);
196 assert(FileContents.Draft &&
197 "forceReparse() was called for non-added document");
198
Ilya Biryukov929697b2018-01-25 14:19:21 +0000199 // forceReparse promises to request new compilation flags from CDB, so we
200 // remove any cahced flags.
201 CompileArgs.invalidate(File);
202
Ilya Biryukov91dbf5b2017-08-14 08:37:32 +0000203 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
Ilya Biryukov82b59ae2018-01-23 15:07:52 +0000204 std::shared_ptr<CppFile> Resources =
Ilya Biryukov929697b2018-01-25 14:19:21 +0000205 Units.getOrCreateFile(File, StorePreamblesInMemory, PCHs);
Ilya Biryukov82b59ae2018-01-23 15:07:52 +0000206 return scheduleReparseAndDiags(std::move(Ctx), File, FileContents,
Ilya Biryukov929697b2018-01-25 14:19:21 +0000207 std::move(Resources), std::move(TaggedFS));
Ilya Biryukov0f62ed22017-05-26 12:26:51 +0000208}
209
Ilya Biryukov940901e2017-12-13 12:51:22 +0000210std::future<std::pair<Context, Tagged<CompletionList>>>
211ClangdServer::codeComplete(Context Ctx, PathRef File, Position Pos,
Ilya Biryukovd3b04e32017-12-05 10:42:57 +0000212 const clangd::CodeCompleteOptions &Opts,
Ilya Biryukoved99e4c2017-07-31 17:09:29 +0000213 llvm::Optional<StringRef> OverridenContents,
214 IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) {
Ilya Biryukov940901e2017-12-13 12:51:22 +0000215 using ResultType = std::pair<Context, Tagged<CompletionList>>;
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000216
217 std::promise<ResultType> ResultPromise;
218
Ilya Biryukov940901e2017-12-13 12:51:22 +0000219 auto Callback = [](std::promise<ResultType> ResultPromise, Context Ctx,
220 Tagged<CompletionList> Result) -> void {
221 ResultPromise.set_value({std::move(Ctx), std::move(Result)});
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000222 };
223
224 std::future<ResultType> ResultFuture = ResultPromise.get_future();
Ilya Biryukov940901e2017-12-13 12:51:22 +0000225 codeComplete(std::move(Ctx), File, Pos, Opts,
226 BindWithForward(Callback, std::move(ResultPromise)),
227 OverridenContents, UsedFS);
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000228 return ResultFuture;
229}
230
231void ClangdServer::codeComplete(
Ilya Biryukov940901e2017-12-13 12:51:22 +0000232 Context Ctx, PathRef File, Position Pos,
233 const clangd::CodeCompleteOptions &Opts,
234 UniqueFunction<void(Context, Tagged<CompletionList>)> Callback,
Ilya Biryukovd3b04e32017-12-05 10:42:57 +0000235 llvm::Optional<StringRef> OverridenContents,
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000236 IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) {
Ilya Biryukov940901e2017-12-13 12:51:22 +0000237 using CallbackType = UniqueFunction<void(Context, Tagged<CompletionList>)>;
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000238
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000239 std::string Contents;
240 if (OverridenContents) {
241 Contents = *OverridenContents;
242 } else {
Ilya Biryukov0e27ce42017-06-13 14:15:56 +0000243 auto FileContents = DraftMgr.getDraft(File);
244 assert(FileContents.Draft &&
245 "codeComplete is called for non-added document");
246
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000247 Contents = std::move(*FileContents.Draft);
Ilya Biryukov0e27ce42017-06-13 14:15:56 +0000248 }
Ilya Biryukov38d79772017-05-16 09:38:59 +0000249
Ilya Biryukovaf0c04b2017-06-14 09:46:44 +0000250 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
Ilya Biryukoved99e4c2017-07-31 17:09:29 +0000251 if (UsedFS)
252 *UsedFS = TaggedFS.Value;
253
Ilya Biryukov02d58702017-08-01 15:51:38 +0000254 std::shared_ptr<CppFile> Resources = Units.getFile(File);
255 assert(Resources && "Calling completion on non-added file");
256
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000257 // Remember the current Preamble and use it when async task starts executing.
258 // At the point when async task starts executing, we may have a different
259 // Preamble in Resources. However, we assume the Preamble that we obtain here
260 // is reusable in completion more often.
261 std::shared_ptr<const PreambleData> Preamble =
262 Resources->getPossiblyStalePreamble();
Ilya Biryukovd3b04e32017-12-05 10:42:57 +0000263 // Copy completion options for passing them to async task handler.
264 auto CodeCompleteOpts = Opts;
Sam McCall0faecf02018-01-15 12:33:00 +0000265 if (!CodeCompleteOpts.Index) // Respect overridden index.
266 CodeCompleteOpts.Index = Index;
Ilya Biryukovf6e2b4c2018-01-09 14:39:27 +0000267
268 // Copy File, as it is a PathRef that will go out of scope before Task is
269 // executed.
270 Path FileStr = File;
271 // Copy PCHs to avoid accessing this->PCHs concurrently
272 std::shared_ptr<PCHContainerOperations> PCHs = this->PCHs;
Ilya Biryukov929697b2018-01-25 14:19:21 +0000273 tooling::CompileCommand CompileCommand = CompileArgs.getCompileCommand(File);
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000274 // A task that will be run asynchronously.
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000275 auto Task =
276 // 'mutable' to reassign Preamble variable.
Ilya Biryukovf6e2b4c2018-01-09 14:39:27 +0000277 [FileStr, Preamble, Resources, Contents, Pos, CodeCompleteOpts, TaggedFS,
Ilya Biryukov82b59ae2018-01-23 15:07:52 +0000278 PCHs, CompileCommand](Context Ctx, CallbackType Callback) mutable {
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000279 if (!Preamble) {
280 // Maybe we built some preamble before processing this request.
281 Preamble = Resources->getPossiblyStalePreamble();
282 }
283 // FIXME(ibiryukov): even if Preamble is non-null, we may want to check
284 // both the old and the new version in case only one of them matches.
Sam McCalla40371b2017-11-15 09:16:29 +0000285 CompletionList Result = clangd::codeComplete(
Ilya Biryukov82b59ae2018-01-23 15:07:52 +0000286 Ctx, FileStr, CompileCommand,
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000287 Preamble ? &Preamble->Preamble : nullptr, Contents, Pos,
Ilya Biryukov940901e2017-12-13 12:51:22 +0000288 TaggedFS.Value, PCHs, CodeCompleteOpts);
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000289
Ilya Biryukov940901e2017-12-13 12:51:22 +0000290 Callback(std::move(Ctx),
291 make_tagged(std::move(Result), std::move(TaggedFS.Tag)));
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000292 };
293
Ilya Biryukov940901e2017-12-13 12:51:22 +0000294 WorkScheduler.addToFront(std::move(Task), std::move(Ctx),
295 std::move(Callback));
Ilya Biryukov38d79772017-05-16 09:38:59 +0000296}
Ilya Biryukovf01af682017-05-23 13:42:59 +0000297
Benjamin Krameree19f162017-10-26 12:28:13 +0000298llvm::Expected<Tagged<SignatureHelp>>
Ilya Biryukov940901e2017-12-13 12:51:22 +0000299ClangdServer::signatureHelp(const Context &Ctx, PathRef File, Position Pos,
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000300 llvm::Optional<StringRef> OverridenContents,
301 IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) {
302 std::string DraftStorage;
303 if (!OverridenContents) {
304 auto FileContents = DraftMgr.getDraft(File);
Benjamin Krameree19f162017-10-26 12:28:13 +0000305 if (!FileContents.Draft)
306 return llvm::make_error<llvm::StringError>(
307 "signatureHelp is called for non-added document",
308 llvm::errc::invalid_argument);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000309
310 DraftStorage = std::move(*FileContents.Draft);
311 OverridenContents = DraftStorage;
312 }
313
314 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
315 if (UsedFS)
316 *UsedFS = TaggedFS.Value;
317
318 std::shared_ptr<CppFile> Resources = Units.getFile(File);
Benjamin Krameree19f162017-10-26 12:28:13 +0000319 if (!Resources)
320 return llvm::make_error<llvm::StringError>(
321 "signatureHelp is called for non-added document",
322 llvm::errc::invalid_argument);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000323
324 auto Preamble = Resources->getPossiblyStalePreamble();
Ilya Biryukov940901e2017-12-13 12:51:22 +0000325 auto Result =
Ilya Biryukov929697b2018-01-25 14:19:21 +0000326 clangd::signatureHelp(Ctx, File, CompileArgs.getCompileCommand(File),
Ilya Biryukov940901e2017-12-13 12:51:22 +0000327 Preamble ? &Preamble->Preamble : nullptr,
328 *OverridenContents, Pos, TaggedFS.Value, PCHs);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000329 return make_tagged(std::move(Result), TaggedFS.Tag);
330}
331
Raoul Wols212bcf82017-12-12 20:25:06 +0000332llvm::Expected<tooling::Replacements>
333ClangdServer::formatRange(StringRef Code, PathRef File, Range Rng) {
Ilya Biryukovafb55542017-05-16 14:40:30 +0000334 size_t Begin = positionToOffset(Code, Rng.start);
335 size_t Len = positionToOffset(Code, Rng.end) - Begin;
336 return formatCode(Code, File, {tooling::Range(Begin, Len)});
337}
338
Raoul Wols212bcf82017-12-12 20:25:06 +0000339llvm::Expected<tooling::Replacements> ClangdServer::formatFile(StringRef Code,
340 PathRef File) {
Ilya Biryukovafb55542017-05-16 14:40:30 +0000341 // Format everything.
Ilya Biryukovafb55542017-05-16 14:40:30 +0000342 return formatCode(Code, File, {tooling::Range(0, Code.size())});
343}
344
Raoul Wols212bcf82017-12-12 20:25:06 +0000345llvm::Expected<tooling::Replacements>
346ClangdServer::formatOnType(StringRef Code, PathRef File, Position Pos) {
Ilya Biryukovafb55542017-05-16 14:40:30 +0000347 // Look for the previous opening brace from the character position and
348 // format starting from there.
Ilya Biryukovafb55542017-05-16 14:40:30 +0000349 size_t CursorPos = positionToOffset(Code, Pos);
350 size_t PreviousLBracePos = StringRef(Code).find_last_of('{', CursorPos);
351 if (PreviousLBracePos == StringRef::npos)
352 PreviousLBracePos = CursorPos;
Sam McCallb536a2a2017-12-19 12:23:48 +0000353 size_t Len = CursorPos - PreviousLBracePos;
Ilya Biryukovafb55542017-05-16 14:40:30 +0000354
355 return formatCode(Code, File, {tooling::Range(PreviousLBracePos, Len)});
356}
Ilya Biryukov38d79772017-05-16 09:38:59 +0000357
Haojian Wu345099c2017-11-09 11:30:04 +0000358Expected<std::vector<tooling::Replacement>>
Ilya Biryukov940901e2017-12-13 12:51:22 +0000359ClangdServer::rename(const Context &Ctx, PathRef File, Position Pos,
360 llvm::StringRef NewName) {
Haojian Wu345099c2017-11-09 11:30:04 +0000361 std::shared_ptr<CppFile> Resources = Units.getFile(File);
362 RefactoringResultCollector ResultCollector;
363 Resources->getAST().get()->runUnderLock([&](ParsedAST *AST) {
364 const SourceManager &SourceMgr = AST->getASTContext().getSourceManager();
365 const FileEntry *FE =
366 SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
367 if (!FE)
368 return;
369 SourceLocation SourceLocationBeg =
370 clangd::getBeginningOfIdentifier(*AST, Pos, FE);
371 tooling::RefactoringRuleContext Context(
372 AST->getASTContext().getSourceManager());
373 Context.setASTContext(AST->getASTContext());
374 auto Rename = clang::tooling::RenameOccurrences::initiate(
375 Context, SourceRange(SourceLocationBeg), NewName.str());
376 if (!Rename) {
377 ResultCollector.Result = Rename.takeError();
378 return;
379 }
380 Rename->invoke(ResultCollector, Context);
381 });
382 assert(ResultCollector.Result.hasValue());
383 if (!ResultCollector.Result.getValue())
384 return ResultCollector.Result->takeError();
385
386 std::vector<tooling::Replacement> Replacements;
387 for (const tooling::AtomicChange &Change : ResultCollector.Result->get()) {
388 tooling::Replacements ChangeReps = Change.getReplacements();
389 for (const auto &Rep : ChangeReps) {
390 // FIXME: Right now we only support renaming the main file, so we drop
391 // replacements not for the main file. In the future, we might consider to
392 // support:
393 // * rename in any included header
394 // * rename only in the "main" header
395 // * provide an error if there are symbols we won't rename (e.g.
396 // std::vector)
397 // * rename globally in project
398 // * rename in open files
399 if (Rep.getFilePath() == File)
400 Replacements.push_back(Rep);
401 }
402 }
403 return Replacements;
404}
405
Ilya Biryukov261c72e2018-01-17 12:30:24 +0000406llvm::Optional<std::string> ClangdServer::getDocument(PathRef File) {
407 auto Latest = DraftMgr.getDraft(File);
408 if (!Latest.Draft)
409 return llvm::None;
410 return std::move(*Latest.Draft);
Ilya Biryukov38d79772017-05-16 09:38:59 +0000411}
412
Ilya Biryukovf01af682017-05-23 13:42:59 +0000413std::string ClangdServer::dumpAST(PathRef File) {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000414 std::shared_ptr<CppFile> Resources = Units.getFile(File);
Ilya Biryukov261c72e2018-01-17 12:30:24 +0000415 if (!Resources)
416 return "<non-added file>";
Ilya Biryukov38d79772017-05-16 09:38:59 +0000417
Ilya Biryukov02d58702017-08-01 15:51:38 +0000418 std::string Result;
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +0000419 Resources->getAST().get()->runUnderLock([&Result](ParsedAST *AST) {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000420 llvm::raw_string_ostream ResultOS(Result);
421 if (AST) {
422 clangd::dumpAST(*AST, ResultOS);
423 } else {
424 ResultOS << "<no-ast>";
425 }
426 ResultOS.flush();
Ilya Biryukovf01af682017-05-23 13:42:59 +0000427 });
Ilya Biryukov02d58702017-08-01 15:51:38 +0000428 return Result;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000429}
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000430
Benjamin Krameree19f162017-10-26 12:28:13 +0000431llvm::Expected<Tagged<std::vector<Location>>>
Ilya Biryukov940901e2017-12-13 12:51:22 +0000432ClangdServer::findDefinitions(const Context &Ctx, PathRef File, Position Pos) {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000433 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
434
435 std::shared_ptr<CppFile> Resources = Units.getFile(File);
Benjamin Krameree19f162017-10-26 12:28:13 +0000436 if (!Resources)
437 return llvm::make_error<llvm::StringError>(
438 "findDefinitions called on non-added file",
439 llvm::errc::invalid_argument);
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000440
441 std::vector<Location> Result;
Ilya Biryukov940901e2017-12-13 12:51:22 +0000442 Resources->getAST().get()->runUnderLock([Pos, &Result, &Ctx](ParsedAST *AST) {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000443 if (!AST)
444 return;
Ilya Biryukov940901e2017-12-13 12:51:22 +0000445 Result = clangd::findDefinitions(Ctx, *AST, Pos);
Ilya Biryukov02d58702017-08-01 15:51:38 +0000446 });
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000447 return make_tagged(std::move(Result), TaggedFS.Tag);
448}
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000449
Marc-Andre Laperle6571b3e2017-09-28 03:14:40 +0000450llvm::Optional<Path> ClangdServer::switchSourceHeader(PathRef Path) {
451
452 StringRef SourceExtensions[] = {".cpp", ".c", ".cc", ".cxx",
453 ".c++", ".m", ".mm"};
454 StringRef HeaderExtensions[] = {".h", ".hh", ".hpp", ".hxx", ".inc"};
455
456 StringRef PathExt = llvm::sys::path::extension(Path);
457
458 // Lookup in a list of known extensions.
459 auto SourceIter =
460 std::find_if(std::begin(SourceExtensions), std::end(SourceExtensions),
461 [&PathExt](PathRef SourceExt) {
462 return SourceExt.equals_lower(PathExt);
463 });
464 bool IsSource = SourceIter != std::end(SourceExtensions);
465
466 auto HeaderIter =
467 std::find_if(std::begin(HeaderExtensions), std::end(HeaderExtensions),
468 [&PathExt](PathRef HeaderExt) {
469 return HeaderExt.equals_lower(PathExt);
470 });
471
472 bool IsHeader = HeaderIter != std::end(HeaderExtensions);
473
474 // We can only switch between extensions known extensions.
475 if (!IsSource && !IsHeader)
476 return llvm::None;
477
478 // Array to lookup extensions for the switch. An opposite of where original
479 // extension was found.
480 ArrayRef<StringRef> NewExts;
481 if (IsSource)
482 NewExts = HeaderExtensions;
483 else
484 NewExts = SourceExtensions;
485
486 // Storage for the new path.
487 SmallString<128> NewPath = StringRef(Path);
488
489 // Instance of vfs::FileSystem, used for file existence checks.
490 auto FS = FSProvider.getTaggedFileSystem(Path).Value;
491
492 // Loop through switched extension candidates.
493 for (StringRef NewExt : NewExts) {
494 llvm::sys::path::replace_extension(NewPath, NewExt);
495 if (FS->exists(NewPath))
496 return NewPath.str().str(); // First str() to convert from SmallString to
497 // StringRef, second to convert from StringRef
498 // to std::string
Ilya Biryukov33334942017-10-06 14:39:39 +0000499
Marc-Andre Laperle6571b3e2017-09-28 03:14:40 +0000500 // Also check NewExt in upper-case, just in case.
501 llvm::sys::path::replace_extension(NewPath, NewExt.upper());
502 if (FS->exists(NewPath))
503 return NewPath.str().str();
Marc-Andre Laperle6571b3e2017-09-28 03:14:40 +0000504 }
505
506 return llvm::None;
507}
508
Raoul Wols212bcf82017-12-12 20:25:06 +0000509llvm::Expected<tooling::Replacements>
510ClangdServer::formatCode(llvm::StringRef Code, PathRef File,
511 ArrayRef<tooling::Range> Ranges) {
512 // Call clang-format.
513 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
514 auto StyleOrError =
515 format::getStyle("file", File, "LLVM", Code, TaggedFS.Value.get());
516 if (!StyleOrError) {
517 return StyleOrError.takeError();
518 } else {
519 return format::reformat(StyleOrError.get(), Code, Ranges, File);
520 }
521}
522
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000523llvm::Expected<Tagged<std::vector<DocumentHighlight>>>
Ilya Biryukov940901e2017-12-13 12:51:22 +0000524ClangdServer::findDocumentHighlights(const Context &Ctx, PathRef File,
525 Position Pos) {
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000526 auto FileContents = DraftMgr.getDraft(File);
527 if (!FileContents.Draft)
528 return llvm::make_error<llvm::StringError>(
529 "findDocumentHighlights called on non-added file",
530 llvm::errc::invalid_argument);
531
532 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
533
534 std::shared_ptr<CppFile> Resources = Units.getFile(File);
535 if (!Resources)
536 return llvm::make_error<llvm::StringError>(
537 "findDocumentHighlights called on non-added file",
538 llvm::errc::invalid_argument);
539
540 std::vector<DocumentHighlight> Result;
541 llvm::Optional<llvm::Error> Err;
Ilya Biryukov940901e2017-12-13 12:51:22 +0000542 Resources->getAST().get()->runUnderLock([Pos, &Ctx, &Err,
543 &Result](ParsedAST *AST) {
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000544 if (!AST) {
545 Err = llvm::make_error<llvm::StringError>("Invalid AST",
546 llvm::errc::invalid_argument);
547 return;
548 }
Ilya Biryukov940901e2017-12-13 12:51:22 +0000549 Result = clangd::findDocumentHighlights(Ctx, *AST, Pos);
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000550 });
551
552 if (Err)
553 return std::move(*Err);
554 return make_tagged(Result, TaggedFS.Tag);
555}
556
Ilya Biryukov940901e2017-12-13 12:51:22 +0000557std::future<Context> ClangdServer::scheduleReparseAndDiags(
558 Context Ctx, PathRef File, VersionedDraft Contents,
559 std::shared_ptr<CppFile> Resources,
Ilya Biryukov929697b2018-01-25 14:19:21 +0000560 Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> TaggedFS) {
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000561 assert(Contents.Draft && "Draft must have contents");
Ilya Biryukov929697b2018-01-25 14:19:21 +0000562 ParseInputs Inputs = {CompileArgs.getCompileCommand(File),
563 std::move(TaggedFS.Value), *std::move(Contents.Draft)};
564
Ilya Biryukov940901e2017-12-13 12:51:22 +0000565 UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>(const Context &)>
Ilya Biryukov82b59ae2018-01-23 15:07:52 +0000566 DeferredRebuild = Resources->deferRebuild(std::move(Inputs));
Ilya Biryukov940901e2017-12-13 12:51:22 +0000567 std::promise<Context> DonePromise;
568 std::future<Context> DoneFuture = DonePromise.get_future();
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000569
570 DocVersion Version = Contents.Version;
571 Path FileStr = File;
572 VFSTag Tag = TaggedFS.Tag;
573 auto ReparseAndPublishDiags =
574 [this, FileStr, Version,
Ilya Biryukov940901e2017-12-13 12:51:22 +0000575 Tag](UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>(
576 const Context &)>
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000577 DeferredRebuild,
Ilya Biryukov940901e2017-12-13 12:51:22 +0000578 std::promise<Context> DonePromise, Context Ctx) -> void {
579 auto Guard = onScopeExit([&]() { DonePromise.set_value(std::move(Ctx)); });
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000580
581 auto CurrentVersion = DraftMgr.getVersion(FileStr);
582 if (CurrentVersion != Version)
583 return; // This request is outdated
584
Ilya Biryukov940901e2017-12-13 12:51:22 +0000585 auto Diags = DeferredRebuild(Ctx);
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000586 if (!Diags)
587 return; // A new reparse was requested before this one completed.
Ilya Biryukov47f22022017-09-20 12:58:55 +0000588
589 // We need to serialize access to resulting diagnostics to avoid calling
590 // `onDiagnosticsReady` in the wrong order.
591 std::lock_guard<std::mutex> DiagsLock(DiagnosticsMutex);
592 DocVersion &LastReportedDiagsVersion = ReportedDiagnosticVersions[FileStr];
593 // FIXME(ibiryukov): get rid of '<' comparison here. In the current
594 // implementation diagnostics will not be reported after version counters'
595 // overflow. This should not happen in practice, since DocVersion is a
596 // 64-bit unsigned integer.
597 if (Version < LastReportedDiagsVersion)
598 return;
599 LastReportedDiagsVersion = Version;
600
Ilya Biryukov95558392018-01-10 17:59:27 +0000601 DiagConsumer.onDiagnosticsReady(Ctx, FileStr,
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000602 make_tagged(std::move(*Diags), Tag));
603 };
604
605 WorkScheduler.addToFront(std::move(ReparseAndPublishDiags),
Ilya Biryukov940901e2017-12-13 12:51:22 +0000606 std::move(DeferredRebuild), std::move(DonePromise),
607 std::move(Ctx));
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000608 return DoneFuture;
609}
610
Ilya Biryukov940901e2017-12-13 12:51:22 +0000611std::future<Context>
612ClangdServer::scheduleCancelRebuild(Context Ctx,
613 std::shared_ptr<CppFile> Resources) {
614 std::promise<Context> DonePromise;
615 std::future<Context> DoneFuture = DonePromise.get_future();
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000616 if (!Resources) {
617 // No need to schedule any cleanup.
Ilya Biryukov940901e2017-12-13 12:51:22 +0000618 DonePromise.set_value(std::move(Ctx));
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000619 return DoneFuture;
620 }
621
Ilya Biryukov98a1fd72017-10-10 16:12:54 +0000622 UniqueFunction<void()> DeferredCancel = Resources->deferCancelRebuild();
Ilya Biryukov940901e2017-12-13 12:51:22 +0000623 auto CancelReparses = [Resources](std::promise<Context> DonePromise,
624 UniqueFunction<void()> DeferredCancel,
625 Context Ctx) {
Ilya Biryukov98a1fd72017-10-10 16:12:54 +0000626 DeferredCancel();
Ilya Biryukov940901e2017-12-13 12:51:22 +0000627 DonePromise.set_value(std::move(Ctx));
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000628 };
629 WorkScheduler.addToFront(std::move(CancelReparses), std::move(DonePromise),
Ilya Biryukov940901e2017-12-13 12:51:22 +0000630 std::move(DeferredCancel), std::move(Ctx));
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000631 return DoneFuture;
632}
Marc-Andre Laperlebf114242017-10-02 18:00:37 +0000633
634void ClangdServer::onFileEvent(const DidChangeWatchedFilesParams &Params) {
635 // FIXME: Do nothing for now. This will be used for indexing and potentially
636 // invalidating other caches.
637}
Ilya Biryukovdf842342018-01-25 14:32:21 +0000638
639std::vector<std::pair<Path, std::size_t>>
640ClangdServer::getUsedBytesPerFile() const {
641 return Units.getUsedBytesPerFile();
642}