blob: ff8798536423b7988587df17a970a57bffe66ecd [file] [log] [blame]
Krasimir Georgiev95ef1712017-04-12 17:13:08 +00001//===--- ClangdMain.cpp - clangd server loop ------------------------------===//
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +00002//
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
Ilya Biryukov38d79772017-05-16 09:38:59 +000010#include "ClangdLSPServer.h"
Ilya Biryukove6dbb582017-10-10 09:08:47 +000011#include "Path.h"
Sam McCall8567cb32017-11-02 09:21:51 +000012#include "Trace.h"
Sam McCall2c30fbc2018-10-18 12:32:04 +000013#include "Transport.h"
Sam McCall02d600d2018-09-25 18:06:43 +000014#include "index/Serialization.h"
Raoul Wols8f5e06f2018-07-29 19:12:42 +000015#include "clang/Basic/Version.h"
Benjamin Kramerf0af3e62017-03-01 16:16:29 +000016#include "llvm/Support/CommandLine.h"
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000017#include "llvm/Support/FileSystem.h"
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000018#include "llvm/Support/Path.h"
Benjamin Kramer6a3d74e2017-02-07 12:40:59 +000019#include "llvm/Support/Program.h"
Eric Liuc5105f92018-02-16 14:15:55 +000020#include "llvm/Support/Signals.h"
Ilya Biryukove6dbb582017-10-10 09:08:47 +000021#include "llvm/Support/raw_ostream.h"
Sam McCalled2717a2018-02-14 03:20:07 +000022#include <cstdlib>
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000023#include <iostream>
Ilya Biryukov38d79772017-05-16 09:38:59 +000024#include <memory>
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000025#include <string>
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000026#include <thread>
Ilya Biryukov38d79772017-05-16 09:38:59 +000027
Sam McCallc008af62018-10-20 15:30:37 +000028using namespace llvm;
Ilya Biryukov38d79772017-05-16 09:38:59 +000029using namespace clang;
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000030using namespace clang::clangd;
31
Sam McCall96f24892018-10-16 08:53:52 +000032// FIXME: remove this option when Dex is cheap enough.
Sam McCallc008af62018-10-20 15:30:37 +000033static cl::opt<bool> UseDex("use-dex-index",
34 cl::desc("Use experimental Dex dynamic index."),
35 cl::init(false), cl::Hidden);
Kirill Bobyrevdc41bef2018-08-21 10:40:19 +000036
Sam McCallc008af62018-10-20 15:30:37 +000037static cl::opt<Path> CompileCommandsDir(
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000038 "compile-commands-dir",
Sam McCallc008af62018-10-20 15:30:37 +000039 cl::desc("Specify a path to look for compile_commands.json. If path "
40 "is invalid, clangd will look in the current directory and "
41 "parent paths of each source file."));
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000042
Sam McCallc008af62018-10-20 15:30:37 +000043static cl::opt<unsigned>
44 WorkerThreadsCount("j", cl::desc("Number of async workers used by clangd"),
45 cl::init(getDefaultAsyncThreadsCount()));
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000046
Sam McCallc18c2802018-06-15 11:06:29 +000047// FIXME: also support "plain" style where signatures are always omitted.
Sam McCall47feb572018-09-05 10:39:58 +000048enum CompletionStyleFlag { Detailed, Bundled };
Sam McCallc008af62018-10-20 15:30:37 +000049static cl::opt<CompletionStyleFlag> CompletionStyle(
50 "completion-style", cl::desc("Granularity of code completion suggestions"),
51 cl::values(
Sam McCallc18c2802018-06-15 11:06:29 +000052 clEnumValN(Detailed, "detailed",
53 "One completion item for each semantically distinct "
54 "completion, with full type information."),
55 clEnumValN(Bundled, "bundled",
56 "Similar completion items (e.g. function overloads) are "
57 "combined. Type information shown where possible.")),
Sam McCallc008af62018-10-20 15:30:37 +000058 cl::init(Detailed));
Sam McCallc18c2802018-06-15 11:06:29 +000059
Sam McCalladccab62017-11-23 16:58:22 +000060// FIXME: Flags are the wrong mechanism for user preferences.
61// We should probably read a dotfile or similar.
Sam McCallc008af62018-10-20 15:30:37 +000062static cl::opt<bool> IncludeIneligibleResults(
Sam McCalladccab62017-11-23 16:58:22 +000063 "include-ineligible-results",
Sam McCallc008af62018-10-20 15:30:37 +000064 cl::desc("Include ineligible completion results (e.g. private members)"),
65 cl::init(clangd::CodeCompleteOptions().IncludeIneligibleResults),
66 cl::Hidden);
Ilya Biryukovb33c1572017-09-12 13:57:14 +000067
Sam McCallc008af62018-10-20 15:30:37 +000068static cl::opt<JSONStreamStyle> InputStyle(
69 "input-style", cl::desc("Input JSON stream encoding"),
70 cl::values(
Sam McCall5ed599e2018-02-06 10:47:30 +000071 clEnumValN(JSONStreamStyle::Standard, "standard", "usual LSP protocol"),
72 clEnumValN(JSONStreamStyle::Delimited, "delimited",
73 "messages delimited by --- lines, with # comment support")),
Sam McCallc008af62018-10-20 15:30:37 +000074 cl::init(JSONStreamStyle::Standard));
Sam McCall5ed599e2018-02-06 10:47:30 +000075
Sam McCallc008af62018-10-20 15:30:37 +000076static cl::opt<bool> PrettyPrint("pretty", cl::desc("Pretty-print JSON output"),
77 cl::init(false));
Sam McCalldd0566b2017-11-06 15:40:30 +000078
Sam McCallc008af62018-10-20 15:30:37 +000079static cl::opt<Logger::Level> LogLevel(
80 "log", cl::desc("Verbosity of log messages written to stderr"),
81 cl::values(clEnumValN(Logger::Error, "error", "Error messages only"),
82 clEnumValN(Logger::Info, "info", "High level execution tracing"),
83 clEnumValN(Logger::Debug, "verbose", "Low level details")),
84 cl::init(Logger::Info));
Sam McCallbed58852018-07-11 10:35:11 +000085
Eric Liuc0ac4bb2018-11-22 15:02:05 +000086static cl::opt<bool>
87 Test("lit-test",
88 cl::desc("Abbreviation for -input-style=delimited -pretty "
89 "-run-synchronously -enable-test-scheme. "
90 "Intended to simplify lit tests."),
91 cl::init(false), cl::Hidden);
92
93static cl::opt<bool> EnableTestScheme(
94 "enable-test-uri-scheme",
95 cl::desc("Enable 'test:' URI scheme. Only use in lit tests."),
Sam McCallc008af62018-10-20 15:30:37 +000096 cl::init(false), cl::Hidden);
Sam McCall5ed599e2018-02-06 10:47:30 +000097
Sam McCall47feb572018-09-05 10:39:58 +000098enum PCHStorageFlag { Disk, Memory };
Sam McCallc008af62018-10-20 15:30:37 +000099static cl::opt<PCHStorageFlag> PCHStorage(
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000100 "pch-storage",
Sam McCallc008af62018-10-20 15:30:37 +0000101 cl::desc("Storing PCHs in memory increases memory usages, but may "
102 "improve performance"),
103 cl::values(clEnumValN(PCHStorageFlag::Disk, "disk", "store PCHs on disk"),
104 clEnumValN(PCHStorageFlag::Memory, "memory",
105 "store PCHs in memory")),
106 cl::init(PCHStorageFlag::Disk));
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000107
Sam McCallc008af62018-10-20 15:30:37 +0000108static cl::opt<int>
109 LimitResults("limit-results",
110 cl::desc("Limit the number of results returned by clangd. "
111 "0 means no limit."),
112 cl::init(100));
Haojian Wu48b48652018-01-25 09:20:09 +0000113
Sam McCallc008af62018-10-20 15:30:37 +0000114static cl::opt<bool>
115 RunSynchronously("run-synchronously",
116 cl::desc("Parse on main thread. If set, -j is ignored"),
117 cl::init(false), cl::Hidden);
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000118
Sam McCallc008af62018-10-20 15:30:37 +0000119static cl::opt<Path> ResourceDir("resource-dir",
120 cl::desc("Directory for system clang headers"),
121 cl::init(""), cl::Hidden);
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +0000122
Sam McCallc008af62018-10-20 15:30:37 +0000123static cl::opt<Path> InputMirrorFile(
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000124 "input-mirror-file",
Sam McCallc008af62018-10-20 15:30:37 +0000125 cl::desc(
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000126 "Mirror all LSP input to the specified file. Useful for debugging."),
Sam McCallc008af62018-10-20 15:30:37 +0000127 cl::init(""), cl::Hidden);
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000128
Sam McCallc008af62018-10-20 15:30:37 +0000129static cl::opt<bool> EnableIndex(
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000130 "index",
Sam McCallc008af62018-10-20 15:30:37 +0000131 cl::desc(
Eric Liu76b88d82018-09-13 12:53:23 +0000132 "Enable index-based features. By default, clangd maintains an index "
133 "built from symbols in opened files. Global index support needs to "
134 "enabled separatedly."),
Sam McCallc008af62018-10-20 15:30:37 +0000135 cl::init(true), cl::Hidden);
Eric Liubfac8f72017-12-19 18:00:37 +0000136
Sam McCallc008af62018-10-20 15:30:37 +0000137static cl::opt<bool> AllScopesCompletion(
Eric Liu670c1472018-09-27 18:46:00 +0000138 "all-scopes-completion",
Sam McCallc008af62018-10-20 15:30:37 +0000139 cl::desc(
Eric Liu670c1472018-09-27 18:46:00 +0000140 "If set to true, code completion will include index symbols that are "
141 "not defined in the scopes (e.g. "
142 "namespaces) visible from the code completion point. Such completions "
143 "can insert scope qualifiers."),
Sam McCallc008af62018-10-20 15:30:37 +0000144 cl::init(false), cl::Hidden);
Eric Liu670c1472018-09-27 18:46:00 +0000145
Sam McCallc008af62018-10-20 15:30:37 +0000146static cl::opt<bool>
147 ShowOrigins("debug-origin", cl::desc("Show origins of completion items"),
148 cl::init(clangd::CodeCompleteOptions().ShowOrigins),
149 cl::Hidden);
Sam McCall2161ec72018-07-05 06:20:41 +0000150
Sam McCallc008af62018-10-20 15:30:37 +0000151static cl::opt<bool> HeaderInsertionDecorators(
Raoul Wols8f5e06f2018-07-29 19:12:42 +0000152 "header-insertion-decorators",
Sam McCallc008af62018-10-20 15:30:37 +0000153 cl::desc("Prepend a circular dot or space before the completion "
154 "label, depending on whether "
155 "an include line will be inserted or not."),
156 cl::init(true));
Raoul Wols8f5e06f2018-07-29 19:12:42 +0000157
Sam McCallc008af62018-10-20 15:30:37 +0000158static cl::opt<Path> IndexFile(
Haojian Wu162510f2018-10-08 10:44:54 +0000159 "index-file",
Sam McCallc008af62018-10-20 15:30:37 +0000160 cl::desc(
Haojian Wu162510f2018-10-08 10:44:54 +0000161 "Index file to build the static index. The file must have been created "
162 "by a compatible clangd-index.\n"
Haojian Wuba28e9a2018-01-10 14:44:34 +0000163 "WARNING: This option is experimental only, and will be removed "
164 "eventually. Don't rely on it."),
Sam McCallc008af62018-10-20 15:30:37 +0000165 cl::init(""), cl::Hidden);
Haojian Wuba28e9a2018-01-10 14:44:34 +0000166
Alex Lorenzf8087862018-08-01 17:39:29 +0000167enum CompileArgsFrom { LSPCompileArgs, FilesystemCompileArgs };
Sam McCallc008af62018-10-20 15:30:37 +0000168static cl::opt<CompileArgsFrom> CompileArgsFrom(
169 "compile_args_from", cl::desc("The source of compile commands"),
170 cl::values(clEnumValN(LSPCompileArgs, "lsp",
171 "All compile commands come from LSP and "
172 "'compile_commands.json' files are ignored"),
173 clEnumValN(FilesystemCompileArgs, "filesystem",
174 "All compile commands come from the "
175 "'compile_commands.json' files")),
176 cl::init(FilesystemCompileArgs), cl::Hidden);
Alex Lorenzf8087862018-08-01 17:39:29 +0000177
Sam McCallc008af62018-10-20 15:30:37 +0000178static cl::opt<bool> EnableFunctionArgSnippets(
Kadir Cetinkayae8d8aee2018-09-19 10:16:44 +0000179 "function-arg-placeholders",
Sam McCallc008af62018-10-20 15:30:37 +0000180 cl::desc("When disabled, completions contain only parentheses for "
181 "function calls. When enabled, completions also contain "
182 "placeholders for method parameters."),
183 cl::init(clangd::CodeCompleteOptions().EnableFunctionArgSnippets));
Kadir Cetinkayae8d8aee2018-09-19 10:16:44 +0000184
Eric Liuc0ac4bb2018-11-22 15:02:05 +0000185namespace {
186
187/// \brief Supports a test URI scheme with relaxed constraints for lit tests.
188/// The path in a test URI will be combined with a platform-specific fake
189/// directory to form an absolute path. For example, test:///a.cpp is resolved
190/// C:\clangd-test\a.cpp on Windows and /clangd-test/a.cpp on Unix.
191class TestScheme : public URIScheme {
192public:
193 Expected<std::string> getAbsolutePath(StringRef /*Authority*/, StringRef Body,
194 StringRef /*HintPath*/) const override {
195 using namespace llvm::sys;
196 // Still require "/" in body to mimic file scheme, as we want lengths of an
197 // equivalent URI in both schemes to be the same.
198 if (!Body.startswith("/"))
199 return make_error<StringError>(
200 "Expect URI body to be an absolute path starting with '/': " + Body,
201 inconvertibleErrorCode());
202 Body = Body.ltrim('/');
203 SmallVector<char, 16> Path(Body.begin(), Body.end());
204 path::native(Path);
205 auto Err = fs::make_absolute(TestScheme::TestDir, Path);
206 if (Err)
207 llvm_unreachable("Failed to make absolute path in test scheme.");
208 return std::string(Path.begin(), Path.end());
209 }
210
211 Expected<URI> uriFromAbsolutePath(StringRef AbsolutePath) const override {
212 StringRef Body = AbsolutePath;
213 if (!Body.consume_front(TestScheme::TestDir)) {
214 return make_error<StringError>("Path " + AbsolutePath +
215 " doesn't start with root " + TestDir,
216 inconvertibleErrorCode());
217 }
218
219 return URI("test", /*Authority=*/"", sys::path::convert_to_slash(Body));
220 }
221
222private:
223 const static char TestDir[];
224};
225
226#ifdef _WIN32
227const char TestScheme::TestDir[] = "C:\\clangd-test";
228#else
229const char TestScheme::TestDir[] = "/clangd-test";
230#endif
231
232}
233
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000234int main(int argc, char *argv[]) {
Sam McCallc008af62018-10-20 15:30:37 +0000235 sys::PrintStackTraceOnErrorSignal(argv[0]);
236 cl::SetVersionPrinter([](raw_ostream &OS) {
Sam McCalle72d0972018-06-29 13:24:20 +0000237 OS << clang::getClangToolFullVersion("clangd") << "\n";
238 });
Sam McCallc008af62018-10-20 15:30:37 +0000239 cl::ParseCommandLineOptions(
Sam McCalle72d0972018-06-29 13:24:20 +0000240 argc, argv,
241 "clangd is a language server that provides IDE-like features to editors. "
Sam McCallc008af62018-10-20 15:30:37 +0000242 "\n\nIt should be used via an editor plugin rather than invoked "
243 "directly. "
Sam McCalle72d0972018-06-29 13:24:20 +0000244 "For more information, see:"
245 "\n\thttps://clang.llvm.org/extra/clangd.html"
246 "\n\thttps://microsoft.github.io/language-server-protocol/");
Sam McCall5ed599e2018-02-06 10:47:30 +0000247 if (Test) {
248 RunSynchronously = true;
249 InputStyle = JSONStreamStyle::Delimited;
250 PrettyPrint = true;
251 }
Eric Liuc0ac4bb2018-11-22 15:02:05 +0000252 if (Test || EnableTestScheme) {
253 static URISchemeRegistry::Add<TestScheme> X(
254 "test", "Test scheme for clangd lit tests.");
255 }
Ilya Biryukovafb55542017-05-16 14:40:30 +0000256
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000257 if (!RunSynchronously && WorkerThreadsCount == 0) {
Sam McCallc008af62018-10-20 15:30:37 +0000258 errs() << "A number of worker threads cannot be 0. Did you mean to "
259 "specify -run-synchronously?";
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000260 return 1;
261 }
262
Kirill Bobyrevbcaf3802018-02-25 07:21:16 +0000263 if (RunSynchronously) {
264 if (WorkerThreadsCount.getNumOccurrences())
Sam McCallc008af62018-10-20 15:30:37 +0000265 errs() << "Ignoring -j because -run-synchronously is set.\n";
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000266 WorkerThreadsCount = 0;
Kirill Bobyrevbcaf3802018-02-25 07:21:16 +0000267 }
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000268
Benjamin Kramer74a18952017-10-26 10:07:04 +0000269 // Validate command line arguments.
Sam McCallc008af62018-10-20 15:30:37 +0000270 Optional<raw_fd_ostream> InputMirrorStream;
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000271 if (!InputMirrorFile.empty()) {
272 std::error_code EC;
Zachary Turner1f67a3c2018-06-07 19:58:58 +0000273 InputMirrorStream.emplace(InputMirrorFile, /*ref*/ EC,
Sam McCallc008af62018-10-20 15:30:37 +0000274 sys::fs::FA_Read | sys::fs::FA_Write);
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000275 if (EC) {
276 InputMirrorStream.reset();
Sam McCallc008af62018-10-20 15:30:37 +0000277 errs() << "Error while opening an input mirror file: " << EC.message();
Sam McCall0d946182018-11-02 23:47:55 +0000278 } else {
279 InputMirrorStream->SetUnbuffered();
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000280 }
281 }
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000282
Sam McCalled2717a2018-02-14 03:20:07 +0000283 // Setup tracing facilities if CLANGD_TRACE is set. In practice enabling a
284 // trace flag in your editor's config is annoying, launching with
285 // `CLANGD_TRACE=trace.json vim` is easier.
Sam McCallc008af62018-10-20 15:30:37 +0000286 Optional<raw_fd_ostream> TraceStream;
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000287 std::unique_ptr<trace::EventTracer> Tracer;
Sam McCalled2717a2018-02-14 03:20:07 +0000288 if (auto *TraceFile = getenv("CLANGD_TRACE")) {
Sam McCall8567cb32017-11-02 09:21:51 +0000289 std::error_code EC;
Zachary Turner1f67a3c2018-06-07 19:58:58 +0000290 TraceStream.emplace(TraceFile, /*ref*/ EC,
Sam McCallc008af62018-10-20 15:30:37 +0000291 sys::fs::FA_Read | sys::fs::FA_Write);
Sam McCall8567cb32017-11-02 09:21:51 +0000292 if (EC) {
Sam McCalled2717a2018-02-14 03:20:07 +0000293 TraceStream.reset();
Sam McCallc008af62018-10-20 15:30:37 +0000294 errs() << "Error while opening trace file " << TraceFile << ": "
295 << EC.message();
Sam McCall8567cb32017-11-02 09:21:51 +0000296 } else {
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000297 Tracer = trace::createJSONTracer(*TraceStream, PrettyPrint);
Sam McCall8567cb32017-11-02 09:21:51 +0000298 }
299 }
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000300
Sam McCallc008af62018-10-20 15:30:37 +0000301 Optional<trace::Session> TracingSession;
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000302 if (Tracer)
303 TracingSession.emplace(*Tracer);
304
Eric Liu4e4e5a42018-08-28 13:15:50 +0000305 // Use buffered stream to stderr (we still flush each log message). Unbuffered
306 // stream can cause significant (non-deterministic) latency for the logger.
Sam McCallc008af62018-10-20 15:30:37 +0000307 errs().SetBuffered();
308 StreamLogger Logger(errs(), LogLevel);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000309 clangd::LoggingSession LoggingSession(Logger);
Ilya Biryukov940901e2017-12-13 12:51:22 +0000310
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000311 // If --compile-commands-dir arg was invoked, check value and override default
312 // path.
Sam McCallc008af62018-10-20 15:30:37 +0000313 Optional<Path> CompileCommandsDirPath;
Sam McCallf01ad102018-10-23 11:54:36 +0000314 if (!CompileCommandsDir.empty()) {
315 if (sys::fs::exists(CompileCommandsDir)) {
316 // We support passing both relative and absolute paths to the
317 // --compile-commands-dir argument, but we assume the path is absolute in
318 // the rest of clangd so we make sure the path is absolute before
319 // continuing.
320 SmallString<128> Path(CompileCommandsDir);
321 if (std::error_code EC = sys::fs::make_absolute(Path)) {
322 errs() << "Error while converting the relative path specified by "
323 "--compile-commands-dir to an absolute path: "
324 << EC.message() << ". The argument will be ignored.\n";
325 } else {
326 CompileCommandsDirPath = Path.str();
327 }
328 } else {
329 errs() << "Path specified by --compile-commands-dir does not exist. The "
330 "argument will be ignored.\n";
331 }
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000332 }
Benjamin Kramer6a3d74e2017-02-07 12:40:59 +0000333
Sam McCall7363a2f2018-03-05 17:28:54 +0000334 ClangdServer::Options Opts;
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000335 switch (PCHStorage) {
336 case PCHStorageFlag::Memory:
Sam McCall7363a2f2018-03-05 17:28:54 +0000337 Opts.StorePreamblesInMemory = true;
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000338 break;
339 case PCHStorageFlag::Disk:
Sam McCall7363a2f2018-03-05 17:28:54 +0000340 Opts.StorePreamblesInMemory = false;
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000341 break;
342 }
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +0000343 if (!ResourceDir.empty())
Sam McCall7363a2f2018-03-05 17:28:54 +0000344 Opts.ResourceDir = ResourceDir;
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000345 Opts.BuildDynamicSymbolIndex = EnableIndex;
Sam McCall96f24892018-10-16 08:53:52 +0000346 Opts.HeavyweightDynamicSymbolIndex = UseDex;
Haojian Wuba28e9a2018-01-10 14:44:34 +0000347 std::unique_ptr<SymbolIndex> StaticIdx;
Sam McCallf469c642018-09-10 10:00:47 +0000348 std::future<void> AsyncIndexLoad; // Block exit while loading the index.
Haojian Wu162510f2018-10-08 10:44:54 +0000349 if (EnableIndex && !IndexFile.empty()) {
Sam McCall76c4c3a2018-09-04 16:19:40 +0000350 // Load the index asynchronously. Meanwhile SwapIndex returns no results.
351 SwapIndex *Placeholder;
352 StaticIdx.reset(Placeholder = new SwapIndex(llvm::make_unique<MemIndex>()));
Eric Liuc0ac4bb2018-11-22 15:02:05 +0000353 AsyncIndexLoad = runAsync<void>([Placeholder] {
354 if (auto Idx = loadIndex(IndexFile, /*UseDex=*/true))
Sam McCall76c4c3a2018-09-04 16:19:40 +0000355 Placeholder->reset(std::move(Idx));
356 });
Sam McCallf469c642018-09-10 10:00:47 +0000357 if (RunSynchronously)
358 AsyncIndexLoad.wait();
Sam McCall7363a2f2018-03-05 17:28:54 +0000359 }
Sam McCall76c4c3a2018-09-04 16:19:40 +0000360 Opts.StaticIndex = StaticIdx.get();
Sam McCall7363a2f2018-03-05 17:28:54 +0000361 Opts.AsyncThreadsCount = WorkerThreadsCount;
362
Sam McCalladccab62017-11-23 16:58:22 +0000363 clangd::CodeCompleteOptions CCOpts;
Sam McCalladccab62017-11-23 16:58:22 +0000364 CCOpts.IncludeIneligibleResults = IncludeIneligibleResults;
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000365 CCOpts.Limit = LimitResults;
Sam McCallc18c2802018-06-15 11:06:29 +0000366 CCOpts.BundleOverloads = CompletionStyle != Detailed;
Sam McCall2161ec72018-07-05 06:20:41 +0000367 CCOpts.ShowOrigins = ShowOrigins;
Raoul Wols8f5e06f2018-07-29 19:12:42 +0000368 if (!HeaderInsertionDecorators) {
369 CCOpts.IncludeIndicator.Insert.clear();
370 CCOpts.IncludeIndicator.NoInsert.clear();
371 }
Eric Liu25d74e92018-08-24 11:23:56 +0000372 CCOpts.SpeculativeIndexRequest = Opts.StaticIndex;
Kadir Cetinkayae8d8aee2018-09-19 10:16:44 +0000373 CCOpts.EnableFunctionArgSnippets = EnableFunctionArgSnippets;
Eric Liu670c1472018-09-27 18:46:00 +0000374 CCOpts.AllScopes = AllScopesCompletion;
Sam McCall7363a2f2018-03-05 17:28:54 +0000375
Benjamin Kramer74a18952017-10-26 10:07:04 +0000376 // Initialize and run ClangdLSPServer.
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000377 // Change stdin to binary to not lose \r\n on windows.
Sam McCallc008af62018-10-20 15:30:37 +0000378 sys::ChangeStdinToBinary();
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000379 auto Transport = newJSONTransport(
Sam McCallc008af62018-10-20 15:30:37 +0000380 stdin, outs(),
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000381 InputMirrorStream ? InputMirrorStream.getPointer() : nullptr, PrettyPrint,
382 InputStyle);
Alex Lorenzf8087862018-08-01 17:39:29 +0000383 ClangdLSPServer LSPServer(
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000384 *Transport, CCOpts, CompileCommandsDirPath,
Sam McCallc55d09a2018-11-02 13:09:36 +0000385 /*UseDirBasedCDB=*/CompileArgsFrom == FilesystemCompileArgs, Opts);
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +0000386 constexpr int NoShutdownRequestErrorCode = 1;
Sam McCallc008af62018-10-20 15:30:37 +0000387 set_thread_name("clangd.main");
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000388 return LSPServer.run() ? 0 : NoShutdownRequestErrorCode;
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000389}