blob: 8da86860b01a7697916dff7fd7863dd5b5080708 [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 Biryukov82b59ae2018-01-23 15:07:52 +000035tooling::CompileCommand getCompileCommand(GlobalCompilationDatabase &CDB,
36 PathRef File, PathRef ResourceDir) {
37 llvm::Optional<tooling::CompileCommand> C = CDB.getCompileCommand(File);
38 if (!C) // FIXME: Suppress diagnostics? Let the user know?
39 C = CDB.getFallbackCommand(File);
40
41 // Inject the resource dir.
42 // FIXME: Don't overwrite it if it's already there.
43 C->CommandLine.push_back("-resource-dir=" + ResourceDir.str());
44 return std::move(*C);
45}
46
Ilya Biryukova46f7a92017-06-28 10:34:50 +000047std::string getStandardResourceDir() {
48 static int Dummy; // Just an address in this process.
49 return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy);
50}
51
Haojian Wu345099c2017-11-09 11:30:04 +000052class RefactoringResultCollector final
53 : public tooling::RefactoringResultConsumer {
54public:
55 void handleError(llvm::Error Err) override {
56 assert(!Result.hasValue());
57 // FIXME: figure out a way to return better message for DiagnosticError.
58 // clangd uses llvm::toString to convert the Err to string, however, for
59 // DiagnosticError, only "clang diagnostic" will be generated.
60 Result = std::move(Err);
61 }
62
63 // Using the handle(SymbolOccurrences) from parent class.
64 using tooling::RefactoringResultConsumer::handle;
65
66 void handle(tooling::AtomicChanges SourceReplacements) override {
67 assert(!Result.hasValue());
68 Result = std::move(SourceReplacements);
69 }
70
71 Optional<Expected<tooling::AtomicChanges>> Result;
72};
73
Ilya Biryukovafb55542017-05-16 14:40:30 +000074} // namespace
75
Ilya Biryukov22602992017-05-30 15:11:02 +000076Tagged<IntrusiveRefCntPtr<vfs::FileSystem>>
Ilya Biryukovaf0c04b2017-06-14 09:46:44 +000077RealFileSystemProvider::getTaggedFileSystem(PathRef File) {
Ilya Biryukov22602992017-05-30 15:11:02 +000078 return make_tagged(vfs::getRealFileSystem(), VFSTag());
Ilya Biryukov0f62ed22017-05-26 12:26:51 +000079}
80
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000081unsigned clangd::getDefaultAsyncThreadsCount() {
82 unsigned HardwareConcurrency = std::thread::hardware_concurrency();
83 // C++ standard says that hardware_concurrency()
84 // may return 0, fallback to 1 worker thread in
85 // that case.
86 if (HardwareConcurrency == 0)
87 return 1;
88 return HardwareConcurrency;
89}
90
91ClangdScheduler::ClangdScheduler(unsigned AsyncThreadsCount)
92 : RunSynchronously(AsyncThreadsCount == 0) {
Ilya Biryukov38d79772017-05-16 09:38:59 +000093 if (RunSynchronously) {
94 // Don't start the worker thread if we're running synchronously
95 return;
96 }
97
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000098 Workers.reserve(AsyncThreadsCount);
99 for (unsigned I = 0; I < AsyncThreadsCount; ++I) {
Sam McCall8567cb32017-11-02 09:21:51 +0000100 Workers.push_back(std::thread([this, I]() {
101 llvm::set_thread_name(llvm::formatv("scheduler/{0}", I));
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000102 while (true) {
Ilya Biryukov08e6ccb2017-10-09 16:26:26 +0000103 UniqueFunction<void()> Request;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000104
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000105 // Pick request from the queue
106 {
107 std::unique_lock<std::mutex> Lock(Mutex);
108 // Wait for more requests.
109 RequestCV.wait(Lock,
110 [this] { return !RequestQueue.empty() || Done; });
111 if (Done)
112 return;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000113
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000114 assert(!RequestQueue.empty() && "RequestQueue was empty");
Ilya Biryukov38d79772017-05-16 09:38:59 +0000115
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000116 // We process requests starting from the front of the queue. Users of
117 // ClangdScheduler have a way to prioritise their requests by putting
118 // them to the either side of the queue (using either addToEnd or
119 // addToFront).
120 Request = std::move(RequestQueue.front());
121 RequestQueue.pop_front();
122 } // unlock Mutex
Ilya Biryukov38d79772017-05-16 09:38:59 +0000123
Ilya Biryukov08e6ccb2017-10-09 16:26:26 +0000124 Request();
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000125 }
126 }));
127 }
Ilya Biryukov38d79772017-05-16 09:38:59 +0000128}
129
130ClangdScheduler::~ClangdScheduler() {
131 if (RunSynchronously)
132 return; // no worker thread is running in that case
133
134 {
135 std::lock_guard<std::mutex> Lock(Mutex);
136 // Wake up the worker thread
137 Done = true;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000138 } // unlock Mutex
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000139 RequestCV.notify_all();
140
141 for (auto &Worker : Workers)
142 Worker.join();
Ilya Biryukov38d79772017-05-16 09:38:59 +0000143}
144
Sam McCalladccab62017-11-23 16:58:22 +0000145ClangdServer::ClangdServer(GlobalCompilationDatabase &CDB,
146 DiagnosticsConsumer &DiagConsumer,
147 FileSystemProvider &FSProvider,
148 unsigned AsyncThreadsCount,
Ilya Biryukov940901e2017-12-13 12:51:22 +0000149 bool StorePreamblesInMemory,
Haojian Wuba28e9a2018-01-10 14:44:34 +0000150 bool BuildDynamicSymbolIndex, SymbolIndex *StaticIdx,
Sam McCalladccab62017-11-23 16:58:22 +0000151 llvm::Optional<StringRef> ResourceDir)
Ilya Biryukov940901e2017-12-13 12:51:22 +0000152 : CDB(CDB), DiagConsumer(DiagConsumer), FSProvider(FSProvider),
Eric Liubfac8f72017-12-19 18:00:37 +0000153 FileIdx(BuildDynamicSymbolIndex ? new FileIndex() : nullptr),
154 // Pass a callback into `Units` to extract symbols from a newly parsed
155 // file and rebuild the file index synchronously each time an AST is
156 // parsed.
157 // FIXME(ioeric): this can be slow and we may be able to index on less
158 // critical paths.
159 Units(FileIdx
160 ? [this](const Context &Ctx, PathRef Path,
161 ParsedAST *AST) { FileIdx->update(Ctx, Path, AST); }
162 : ASTParsedCallback()),
Ilya Biryukova46f7a92017-06-28 10:34:50 +0000163 ResourceDir(ResourceDir ? ResourceDir->str() : getStandardResourceDir()),
Ilya Biryukov38d79772017-05-16 09:38:59 +0000164 PCHs(std::make_shared<PCHContainerOperations>()),
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000165 StorePreamblesInMemory(StorePreamblesInMemory),
Sam McCall0faecf02018-01-15 12:33:00 +0000166 WorkScheduler(AsyncThreadsCount) {
167 if (FileIdx && StaticIdx) {
168 MergedIndex = mergeIndex(FileIdx.get(), StaticIdx);
169 Index = MergedIndex.get();
170 } else if (FileIdx)
171 Index = FileIdx.get();
172 else if (StaticIdx)
173 Index = StaticIdx;
174 else
175 Index = nullptr;
176}
Ilya Biryukov38d79772017-05-16 09:38:59 +0000177
Marc-Andre Laperle37de9712017-09-27 15:31:17 +0000178void ClangdServer::setRootPath(PathRef RootPath) {
179 std::string NewRootPath = llvm::sys::path::convert_to_slash(
180 RootPath, llvm::sys::path::Style::posix);
181 if (llvm::sys::fs::is_directory(NewRootPath))
182 this->RootPath = NewRootPath;
183}
184
Ilya Biryukov940901e2017-12-13 12:51:22 +0000185std::future<Context> ClangdServer::addDocument(Context Ctx, PathRef File,
186 StringRef Contents) {
Ilya Biryukovf01af682017-05-23 13:42:59 +0000187 DocVersion Version = DraftMgr.updateDraft(File, Contents);
Ilya Biryukovf01af682017-05-23 13:42:59 +0000188
Ilya Biryukov02d58702017-08-01 15:51:38 +0000189 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
Ilya Biryukov82b59ae2018-01-23 15:07:52 +0000190 std::shared_ptr<CppFile> Resources =
191 Units.getOrCreateFile(File, ResourceDir, StorePreamblesInMemory, PCHs);
Ilya Biryukov940901e2017-12-13 12:51:22 +0000192 return scheduleReparseAndDiags(std::move(Ctx), File,
193 VersionedDraft{Version, Contents.str()},
Ilya Biryukov82b59ae2018-01-23 15:07:52 +0000194 std::move(Resources), std::move(TaggedFS),
195 /*AllowCachedCompileFlags=*/true);
Ilya Biryukov38d79772017-05-16 09:38:59 +0000196}
197
Ilya Biryukov940901e2017-12-13 12:51:22 +0000198std::future<Context> ClangdServer::removeDocument(Context Ctx, PathRef File) {
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000199 DraftMgr.removeDraft(File);
200 std::shared_ptr<CppFile> Resources = Units.removeIfPresent(File);
Ilya Biryukov940901e2017-12-13 12:51:22 +0000201 return scheduleCancelRebuild(std::move(Ctx), std::move(Resources));
Ilya Biryukov38d79772017-05-16 09:38:59 +0000202}
203
Ilya Biryukov940901e2017-12-13 12:51:22 +0000204std::future<Context> ClangdServer::forceReparse(Context Ctx, PathRef File) {
Ilya Biryukov91dbf5b2017-08-14 08:37:32 +0000205 auto FileContents = DraftMgr.getDraft(File);
206 assert(FileContents.Draft &&
207 "forceReparse() was called for non-added document");
208
209 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
Ilya Biryukov82b59ae2018-01-23 15:07:52 +0000210 std::shared_ptr<CppFile> Resources =
211 Units.getOrCreateFile(File, ResourceDir, StorePreamblesInMemory, PCHs);
212 return scheduleReparseAndDiags(std::move(Ctx), File, FileContents,
213 std::move(Resources), std::move(TaggedFS),
214 /*AllowCachedCompileFlags=*/false);
Ilya Biryukov0f62ed22017-05-26 12:26:51 +0000215}
216
Ilya Biryukov940901e2017-12-13 12:51:22 +0000217std::future<std::pair<Context, Tagged<CompletionList>>>
218ClangdServer::codeComplete(Context Ctx, PathRef File, Position Pos,
Ilya Biryukovd3b04e32017-12-05 10:42:57 +0000219 const clangd::CodeCompleteOptions &Opts,
Ilya Biryukoved99e4c2017-07-31 17:09:29 +0000220 llvm::Optional<StringRef> OverridenContents,
221 IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) {
Ilya Biryukov940901e2017-12-13 12:51:22 +0000222 using ResultType = std::pair<Context, Tagged<CompletionList>>;
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000223
224 std::promise<ResultType> ResultPromise;
225
Ilya Biryukov940901e2017-12-13 12:51:22 +0000226 auto Callback = [](std::promise<ResultType> ResultPromise, Context Ctx,
227 Tagged<CompletionList> Result) -> void {
228 ResultPromise.set_value({std::move(Ctx), std::move(Result)});
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000229 };
230
231 std::future<ResultType> ResultFuture = ResultPromise.get_future();
Ilya Biryukov940901e2017-12-13 12:51:22 +0000232 codeComplete(std::move(Ctx), File, Pos, Opts,
233 BindWithForward(Callback, std::move(ResultPromise)),
234 OverridenContents, UsedFS);
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000235 return ResultFuture;
236}
237
238void ClangdServer::codeComplete(
Ilya Biryukov940901e2017-12-13 12:51:22 +0000239 Context Ctx, PathRef File, Position Pos,
240 const clangd::CodeCompleteOptions &Opts,
241 UniqueFunction<void(Context, Tagged<CompletionList>)> Callback,
Ilya Biryukovd3b04e32017-12-05 10:42:57 +0000242 llvm::Optional<StringRef> OverridenContents,
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000243 IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) {
Ilya Biryukov940901e2017-12-13 12:51:22 +0000244 using CallbackType = UniqueFunction<void(Context, Tagged<CompletionList>)>;
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000245
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000246 std::string Contents;
247 if (OverridenContents) {
248 Contents = *OverridenContents;
249 } else {
Ilya Biryukov0e27ce42017-06-13 14:15:56 +0000250 auto FileContents = DraftMgr.getDraft(File);
251 assert(FileContents.Draft &&
252 "codeComplete is called for non-added document");
253
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000254 Contents = std::move(*FileContents.Draft);
Ilya Biryukov0e27ce42017-06-13 14:15:56 +0000255 }
Ilya Biryukov38d79772017-05-16 09:38:59 +0000256
Ilya Biryukovaf0c04b2017-06-14 09:46:44 +0000257 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
Ilya Biryukoved99e4c2017-07-31 17:09:29 +0000258 if (UsedFS)
259 *UsedFS = TaggedFS.Value;
260
Ilya Biryukov02d58702017-08-01 15:51:38 +0000261 std::shared_ptr<CppFile> Resources = Units.getFile(File);
262 assert(Resources && "Calling completion on non-added file");
263
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000264 // Remember the current Preamble and use it when async task starts executing.
265 // At the point when async task starts executing, we may have a different
266 // Preamble in Resources. However, we assume the Preamble that we obtain here
267 // is reusable in completion more often.
268 std::shared_ptr<const PreambleData> Preamble =
269 Resources->getPossiblyStalePreamble();
Ilya Biryukovd3b04e32017-12-05 10:42:57 +0000270 // Copy completion options for passing them to async task handler.
271 auto CodeCompleteOpts = Opts;
Sam McCall0faecf02018-01-15 12:33:00 +0000272 if (!CodeCompleteOpts.Index) // Respect overridden index.
273 CodeCompleteOpts.Index = Index;
Ilya Biryukovf6e2b4c2018-01-09 14:39:27 +0000274
275 // Copy File, as it is a PathRef that will go out of scope before Task is
276 // executed.
277 Path FileStr = File;
278 // Copy PCHs to avoid accessing this->PCHs concurrently
279 std::shared_ptr<PCHContainerOperations> PCHs = this->PCHs;
Ilya Biryukov82b59ae2018-01-23 15:07:52 +0000280
281 assert(Resources->getLastCommand() &&
282 "CppFile is in inconsistent state, missing CompileCommand");
283 tooling::CompileCommand CompileCommand = *Resources->getLastCommand();
284
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000285 // A task that will be run asynchronously.
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000286 auto Task =
287 // 'mutable' to reassign Preamble variable.
Ilya Biryukovf6e2b4c2018-01-09 14:39:27 +0000288 [FileStr, Preamble, Resources, Contents, Pos, CodeCompleteOpts, TaggedFS,
Ilya Biryukov82b59ae2018-01-23 15:07:52 +0000289 PCHs, CompileCommand](Context Ctx, CallbackType Callback) mutable {
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000290 if (!Preamble) {
291 // Maybe we built some preamble before processing this request.
292 Preamble = Resources->getPossiblyStalePreamble();
293 }
294 // FIXME(ibiryukov): even if Preamble is non-null, we may want to check
295 // both the old and the new version in case only one of them matches.
Sam McCalla40371b2017-11-15 09:16:29 +0000296 CompletionList Result = clangd::codeComplete(
Ilya Biryukov82b59ae2018-01-23 15:07:52 +0000297 Ctx, FileStr, CompileCommand,
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000298 Preamble ? &Preamble->Preamble : nullptr, Contents, Pos,
Ilya Biryukov940901e2017-12-13 12:51:22 +0000299 TaggedFS.Value, PCHs, CodeCompleteOpts);
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000300
Ilya Biryukov940901e2017-12-13 12:51:22 +0000301 Callback(std::move(Ctx),
302 make_tagged(std::move(Result), std::move(TaggedFS.Tag)));
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000303 };
304
Ilya Biryukov940901e2017-12-13 12:51:22 +0000305 WorkScheduler.addToFront(std::move(Task), std::move(Ctx),
306 std::move(Callback));
Ilya Biryukov38d79772017-05-16 09:38:59 +0000307}
Ilya Biryukovf01af682017-05-23 13:42:59 +0000308
Benjamin Krameree19f162017-10-26 12:28:13 +0000309llvm::Expected<Tagged<SignatureHelp>>
Ilya Biryukov940901e2017-12-13 12:51:22 +0000310ClangdServer::signatureHelp(const Context &Ctx, PathRef File, Position Pos,
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000311 llvm::Optional<StringRef> OverridenContents,
312 IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) {
313 std::string DraftStorage;
314 if (!OverridenContents) {
315 auto FileContents = DraftMgr.getDraft(File);
Benjamin Krameree19f162017-10-26 12:28:13 +0000316 if (!FileContents.Draft)
317 return llvm::make_error<llvm::StringError>(
318 "signatureHelp is called for non-added document",
319 llvm::errc::invalid_argument);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000320
321 DraftStorage = std::move(*FileContents.Draft);
322 OverridenContents = DraftStorage;
323 }
324
325 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
326 if (UsedFS)
327 *UsedFS = TaggedFS.Value;
328
329 std::shared_ptr<CppFile> Resources = Units.getFile(File);
Benjamin Krameree19f162017-10-26 12:28:13 +0000330 if (!Resources)
331 return llvm::make_error<llvm::StringError>(
332 "signatureHelp is called for non-added document",
333 llvm::errc::invalid_argument);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000334
Ilya Biryukov82b59ae2018-01-23 15:07:52 +0000335 assert(Resources->getLastCommand() &&
336 "CppFile is in inconsistent state, missing CompileCommand");
337
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000338 auto Preamble = Resources->getPossiblyStalePreamble();
Ilya Biryukov940901e2017-12-13 12:51:22 +0000339 auto Result =
Ilya Biryukov82b59ae2018-01-23 15:07:52 +0000340 clangd::signatureHelp(Ctx, File, *Resources->getLastCommand(),
Ilya Biryukov940901e2017-12-13 12:51:22 +0000341 Preamble ? &Preamble->Preamble : nullptr,
342 *OverridenContents, Pos, TaggedFS.Value, PCHs);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000343 return make_tagged(std::move(Result), TaggedFS.Tag);
344}
345
Raoul Wols212bcf82017-12-12 20:25:06 +0000346llvm::Expected<tooling::Replacements>
347ClangdServer::formatRange(StringRef Code, PathRef File, Range Rng) {
Ilya Biryukovafb55542017-05-16 14:40:30 +0000348 size_t Begin = positionToOffset(Code, Rng.start);
349 size_t Len = positionToOffset(Code, Rng.end) - Begin;
350 return formatCode(Code, File, {tooling::Range(Begin, Len)});
351}
352
Raoul Wols212bcf82017-12-12 20:25:06 +0000353llvm::Expected<tooling::Replacements> ClangdServer::formatFile(StringRef Code,
354 PathRef File) {
Ilya Biryukovafb55542017-05-16 14:40:30 +0000355 // Format everything.
Ilya Biryukovafb55542017-05-16 14:40:30 +0000356 return formatCode(Code, File, {tooling::Range(0, Code.size())});
357}
358
Raoul Wols212bcf82017-12-12 20:25:06 +0000359llvm::Expected<tooling::Replacements>
360ClangdServer::formatOnType(StringRef Code, PathRef File, Position Pos) {
Ilya Biryukovafb55542017-05-16 14:40:30 +0000361 // Look for the previous opening brace from the character position and
362 // format starting from there.
Ilya Biryukovafb55542017-05-16 14:40:30 +0000363 size_t CursorPos = positionToOffset(Code, Pos);
364 size_t PreviousLBracePos = StringRef(Code).find_last_of('{', CursorPos);
365 if (PreviousLBracePos == StringRef::npos)
366 PreviousLBracePos = CursorPos;
Sam McCallb536a2a2017-12-19 12:23:48 +0000367 size_t Len = CursorPos - PreviousLBracePos;
Ilya Biryukovafb55542017-05-16 14:40:30 +0000368
369 return formatCode(Code, File, {tooling::Range(PreviousLBracePos, Len)});
370}
Ilya Biryukov38d79772017-05-16 09:38:59 +0000371
Haojian Wu345099c2017-11-09 11:30:04 +0000372Expected<std::vector<tooling::Replacement>>
Ilya Biryukov940901e2017-12-13 12:51:22 +0000373ClangdServer::rename(const Context &Ctx, PathRef File, Position Pos,
374 llvm::StringRef NewName) {
Haojian Wu345099c2017-11-09 11:30:04 +0000375 std::shared_ptr<CppFile> Resources = Units.getFile(File);
376 RefactoringResultCollector ResultCollector;
377 Resources->getAST().get()->runUnderLock([&](ParsedAST *AST) {
378 const SourceManager &SourceMgr = AST->getASTContext().getSourceManager();
379 const FileEntry *FE =
380 SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
381 if (!FE)
382 return;
383 SourceLocation SourceLocationBeg =
384 clangd::getBeginningOfIdentifier(*AST, Pos, FE);
385 tooling::RefactoringRuleContext Context(
386 AST->getASTContext().getSourceManager());
387 Context.setASTContext(AST->getASTContext());
388 auto Rename = clang::tooling::RenameOccurrences::initiate(
389 Context, SourceRange(SourceLocationBeg), NewName.str());
390 if (!Rename) {
391 ResultCollector.Result = Rename.takeError();
392 return;
393 }
394 Rename->invoke(ResultCollector, Context);
395 });
396 assert(ResultCollector.Result.hasValue());
397 if (!ResultCollector.Result.getValue())
398 return ResultCollector.Result->takeError();
399
400 std::vector<tooling::Replacement> Replacements;
401 for (const tooling::AtomicChange &Change : ResultCollector.Result->get()) {
402 tooling::Replacements ChangeReps = Change.getReplacements();
403 for (const auto &Rep : ChangeReps) {
404 // FIXME: Right now we only support renaming the main file, so we drop
405 // replacements not for the main file. In the future, we might consider to
406 // support:
407 // * rename in any included header
408 // * rename only in the "main" header
409 // * provide an error if there are symbols we won't rename (e.g.
410 // std::vector)
411 // * rename globally in project
412 // * rename in open files
413 if (Rep.getFilePath() == File)
414 Replacements.push_back(Rep);
415 }
416 }
417 return Replacements;
418}
419
Ilya Biryukov261c72e2018-01-17 12:30:24 +0000420llvm::Optional<std::string> ClangdServer::getDocument(PathRef File) {
421 auto Latest = DraftMgr.getDraft(File);
422 if (!Latest.Draft)
423 return llvm::None;
424 return std::move(*Latest.Draft);
Ilya Biryukov38d79772017-05-16 09:38:59 +0000425}
426
Ilya Biryukovf01af682017-05-23 13:42:59 +0000427std::string ClangdServer::dumpAST(PathRef File) {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000428 std::shared_ptr<CppFile> Resources = Units.getFile(File);
Ilya Biryukov261c72e2018-01-17 12:30:24 +0000429 if (!Resources)
430 return "<non-added file>";
Ilya Biryukov38d79772017-05-16 09:38:59 +0000431
Ilya Biryukov02d58702017-08-01 15:51:38 +0000432 std::string Result;
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +0000433 Resources->getAST().get()->runUnderLock([&Result](ParsedAST *AST) {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000434 llvm::raw_string_ostream ResultOS(Result);
435 if (AST) {
436 clangd::dumpAST(*AST, ResultOS);
437 } else {
438 ResultOS << "<no-ast>";
439 }
440 ResultOS.flush();
Ilya Biryukovf01af682017-05-23 13:42:59 +0000441 });
Ilya Biryukov02d58702017-08-01 15:51:38 +0000442 return Result;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000443}
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000444
Benjamin Krameree19f162017-10-26 12:28:13 +0000445llvm::Expected<Tagged<std::vector<Location>>>
Ilya Biryukov940901e2017-12-13 12:51:22 +0000446ClangdServer::findDefinitions(const Context &Ctx, PathRef File, Position Pos) {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000447 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
448
449 std::shared_ptr<CppFile> Resources = Units.getFile(File);
Benjamin Krameree19f162017-10-26 12:28:13 +0000450 if (!Resources)
451 return llvm::make_error<llvm::StringError>(
452 "findDefinitions called on non-added file",
453 llvm::errc::invalid_argument);
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000454
455 std::vector<Location> Result;
Ilya Biryukov940901e2017-12-13 12:51:22 +0000456 Resources->getAST().get()->runUnderLock([Pos, &Result, &Ctx](ParsedAST *AST) {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000457 if (!AST)
458 return;
Ilya Biryukov940901e2017-12-13 12:51:22 +0000459 Result = clangd::findDefinitions(Ctx, *AST, Pos);
Ilya Biryukov02d58702017-08-01 15:51:38 +0000460 });
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000461 return make_tagged(std::move(Result), TaggedFS.Tag);
462}
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000463
Marc-Andre Laperle6571b3e2017-09-28 03:14:40 +0000464llvm::Optional<Path> ClangdServer::switchSourceHeader(PathRef Path) {
465
466 StringRef SourceExtensions[] = {".cpp", ".c", ".cc", ".cxx",
467 ".c++", ".m", ".mm"};
468 StringRef HeaderExtensions[] = {".h", ".hh", ".hpp", ".hxx", ".inc"};
469
470 StringRef PathExt = llvm::sys::path::extension(Path);
471
472 // Lookup in a list of known extensions.
473 auto SourceIter =
474 std::find_if(std::begin(SourceExtensions), std::end(SourceExtensions),
475 [&PathExt](PathRef SourceExt) {
476 return SourceExt.equals_lower(PathExt);
477 });
478 bool IsSource = SourceIter != std::end(SourceExtensions);
479
480 auto HeaderIter =
481 std::find_if(std::begin(HeaderExtensions), std::end(HeaderExtensions),
482 [&PathExt](PathRef HeaderExt) {
483 return HeaderExt.equals_lower(PathExt);
484 });
485
486 bool IsHeader = HeaderIter != std::end(HeaderExtensions);
487
488 // We can only switch between extensions known extensions.
489 if (!IsSource && !IsHeader)
490 return llvm::None;
491
492 // Array to lookup extensions for the switch. An opposite of where original
493 // extension was found.
494 ArrayRef<StringRef> NewExts;
495 if (IsSource)
496 NewExts = HeaderExtensions;
497 else
498 NewExts = SourceExtensions;
499
500 // Storage for the new path.
501 SmallString<128> NewPath = StringRef(Path);
502
503 // Instance of vfs::FileSystem, used for file existence checks.
504 auto FS = FSProvider.getTaggedFileSystem(Path).Value;
505
506 // Loop through switched extension candidates.
507 for (StringRef NewExt : NewExts) {
508 llvm::sys::path::replace_extension(NewPath, NewExt);
509 if (FS->exists(NewPath))
510 return NewPath.str().str(); // First str() to convert from SmallString to
511 // StringRef, second to convert from StringRef
512 // to std::string
Ilya Biryukov33334942017-10-06 14:39:39 +0000513
Marc-Andre Laperle6571b3e2017-09-28 03:14:40 +0000514 // Also check NewExt in upper-case, just in case.
515 llvm::sys::path::replace_extension(NewPath, NewExt.upper());
516 if (FS->exists(NewPath))
517 return NewPath.str().str();
Marc-Andre Laperle6571b3e2017-09-28 03:14:40 +0000518 }
519
520 return llvm::None;
521}
522
Raoul Wols212bcf82017-12-12 20:25:06 +0000523llvm::Expected<tooling::Replacements>
524ClangdServer::formatCode(llvm::StringRef Code, PathRef File,
525 ArrayRef<tooling::Range> Ranges) {
526 // Call clang-format.
527 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
528 auto StyleOrError =
529 format::getStyle("file", File, "LLVM", Code, TaggedFS.Value.get());
530 if (!StyleOrError) {
531 return StyleOrError.takeError();
532 } else {
533 return format::reformat(StyleOrError.get(), Code, Ranges, File);
534 }
535}
536
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000537llvm::Expected<Tagged<std::vector<DocumentHighlight>>>
Ilya Biryukov940901e2017-12-13 12:51:22 +0000538ClangdServer::findDocumentHighlights(const Context &Ctx, PathRef File,
539 Position Pos) {
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000540 auto FileContents = DraftMgr.getDraft(File);
541 if (!FileContents.Draft)
542 return llvm::make_error<llvm::StringError>(
543 "findDocumentHighlights called on non-added file",
544 llvm::errc::invalid_argument);
545
546 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
547
548 std::shared_ptr<CppFile> Resources = Units.getFile(File);
549 if (!Resources)
550 return llvm::make_error<llvm::StringError>(
551 "findDocumentHighlights called on non-added file",
552 llvm::errc::invalid_argument);
553
554 std::vector<DocumentHighlight> Result;
555 llvm::Optional<llvm::Error> Err;
Ilya Biryukov940901e2017-12-13 12:51:22 +0000556 Resources->getAST().get()->runUnderLock([Pos, &Ctx, &Err,
557 &Result](ParsedAST *AST) {
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000558 if (!AST) {
559 Err = llvm::make_error<llvm::StringError>("Invalid AST",
560 llvm::errc::invalid_argument);
561 return;
562 }
Ilya Biryukov940901e2017-12-13 12:51:22 +0000563 Result = clangd::findDocumentHighlights(Ctx, *AST, Pos);
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000564 });
565
566 if (Err)
567 return std::move(*Err);
568 return make_tagged(Result, TaggedFS.Tag);
569}
570
Ilya Biryukov940901e2017-12-13 12:51:22 +0000571std::future<Context> ClangdServer::scheduleReparseAndDiags(
572 Context Ctx, PathRef File, VersionedDraft Contents,
573 std::shared_ptr<CppFile> Resources,
Ilya Biryukov82b59ae2018-01-23 15:07:52 +0000574 Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> TaggedFS,
575 bool AllowCachedCompileFlags) {
576 llvm::Optional<tooling::CompileCommand> ReusedCommand;
577 if (AllowCachedCompileFlags)
578 ReusedCommand = Resources->getLastCommand();
579 tooling::CompileCommand Command =
580 ReusedCommand ? std::move(*ReusedCommand)
581 : getCompileCommand(CDB, File, ResourceDir);
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000582
Ilya Biryukov82b59ae2018-01-23 15:07:52 +0000583 ParseInputs Inputs = {std::move(Command), std::move(TaggedFS.Value),
584 *std::move(Contents.Draft)};
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000585 assert(Contents.Draft && "Draft must have contents");
Ilya Biryukov940901e2017-12-13 12:51:22 +0000586 UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>(const Context &)>
Ilya Biryukov82b59ae2018-01-23 15:07:52 +0000587 DeferredRebuild = Resources->deferRebuild(std::move(Inputs));
Ilya Biryukov940901e2017-12-13 12:51:22 +0000588 std::promise<Context> DonePromise;
589 std::future<Context> DoneFuture = DonePromise.get_future();
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000590
591 DocVersion Version = Contents.Version;
592 Path FileStr = File;
593 VFSTag Tag = TaggedFS.Tag;
594 auto ReparseAndPublishDiags =
595 [this, FileStr, Version,
Ilya Biryukov940901e2017-12-13 12:51:22 +0000596 Tag](UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>(
597 const Context &)>
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000598 DeferredRebuild,
Ilya Biryukov940901e2017-12-13 12:51:22 +0000599 std::promise<Context> DonePromise, Context Ctx) -> void {
600 auto Guard = onScopeExit([&]() { DonePromise.set_value(std::move(Ctx)); });
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000601
602 auto CurrentVersion = DraftMgr.getVersion(FileStr);
603 if (CurrentVersion != Version)
604 return; // This request is outdated
605
Ilya Biryukov940901e2017-12-13 12:51:22 +0000606 auto Diags = DeferredRebuild(Ctx);
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000607 if (!Diags)
608 return; // A new reparse was requested before this one completed.
Ilya Biryukov47f22022017-09-20 12:58:55 +0000609
610 // We need to serialize access to resulting diagnostics to avoid calling
611 // `onDiagnosticsReady` in the wrong order.
612 std::lock_guard<std::mutex> DiagsLock(DiagnosticsMutex);
613 DocVersion &LastReportedDiagsVersion = ReportedDiagnosticVersions[FileStr];
614 // FIXME(ibiryukov): get rid of '<' comparison here. In the current
615 // implementation diagnostics will not be reported after version counters'
616 // overflow. This should not happen in practice, since DocVersion is a
617 // 64-bit unsigned integer.
618 if (Version < LastReportedDiagsVersion)
619 return;
620 LastReportedDiagsVersion = Version;
621
Ilya Biryukov95558392018-01-10 17:59:27 +0000622 DiagConsumer.onDiagnosticsReady(Ctx, FileStr,
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000623 make_tagged(std::move(*Diags), Tag));
624 };
625
626 WorkScheduler.addToFront(std::move(ReparseAndPublishDiags),
Ilya Biryukov940901e2017-12-13 12:51:22 +0000627 std::move(DeferredRebuild), std::move(DonePromise),
628 std::move(Ctx));
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000629 return DoneFuture;
630}
631
Ilya Biryukov940901e2017-12-13 12:51:22 +0000632std::future<Context>
633ClangdServer::scheduleCancelRebuild(Context Ctx,
634 std::shared_ptr<CppFile> Resources) {
635 std::promise<Context> DonePromise;
636 std::future<Context> DoneFuture = DonePromise.get_future();
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000637 if (!Resources) {
638 // No need to schedule any cleanup.
Ilya Biryukov940901e2017-12-13 12:51:22 +0000639 DonePromise.set_value(std::move(Ctx));
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000640 return DoneFuture;
641 }
642
Ilya Biryukov98a1fd72017-10-10 16:12:54 +0000643 UniqueFunction<void()> DeferredCancel = Resources->deferCancelRebuild();
Ilya Biryukov940901e2017-12-13 12:51:22 +0000644 auto CancelReparses = [Resources](std::promise<Context> DonePromise,
645 UniqueFunction<void()> DeferredCancel,
646 Context Ctx) {
Ilya Biryukov98a1fd72017-10-10 16:12:54 +0000647 DeferredCancel();
Ilya Biryukov940901e2017-12-13 12:51:22 +0000648 DonePromise.set_value(std::move(Ctx));
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000649 };
650 WorkScheduler.addToFront(std::move(CancelReparses), std::move(DonePromise),
Ilya Biryukov940901e2017-12-13 12:51:22 +0000651 std::move(DeferredCancel), std::move(Ctx));
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000652 return DoneFuture;
653}
Marc-Andre Laperlebf114242017-10-02 18:00:37 +0000654
655void ClangdServer::onFileEvent(const DidChangeWatchedFilesParams &Params) {
656 // FIXME: Do nothing for now. This will be used for indexing and potentially
657 // invalidating other caches.
658}