blob: e349ee027513759f875de1a1153d7ea0678ef16d [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
Sam McCall422c8282018-11-26 16:00:11 +0000167static cl::opt<bool> EnableBackgroundIndex(
168 "background-index",
169 cl::desc("Index project code in the background and persist index on disk. "
170 "Experimental"),
171 cl::init(false), cl::Hidden);
172
Alex Lorenzf8087862018-08-01 17:39:29 +0000173enum CompileArgsFrom { LSPCompileArgs, FilesystemCompileArgs };
Sam McCallc008af62018-10-20 15:30:37 +0000174static cl::opt<CompileArgsFrom> CompileArgsFrom(
175 "compile_args_from", cl::desc("The source of compile commands"),
176 cl::values(clEnumValN(LSPCompileArgs, "lsp",
177 "All compile commands come from LSP and "
178 "'compile_commands.json' files are ignored"),
179 clEnumValN(FilesystemCompileArgs, "filesystem",
180 "All compile commands come from the "
181 "'compile_commands.json' files")),
182 cl::init(FilesystemCompileArgs), cl::Hidden);
Alex Lorenzf8087862018-08-01 17:39:29 +0000183
Sam McCallc008af62018-10-20 15:30:37 +0000184static cl::opt<bool> EnableFunctionArgSnippets(
Kadir Cetinkayae8d8aee2018-09-19 10:16:44 +0000185 "function-arg-placeholders",
Sam McCallc008af62018-10-20 15:30:37 +0000186 cl::desc("When disabled, completions contain only parentheses for "
187 "function calls. When enabled, completions also contain "
188 "placeholders for method parameters."),
189 cl::init(clangd::CodeCompleteOptions().EnableFunctionArgSnippets));
Kadir Cetinkayae8d8aee2018-09-19 10:16:44 +0000190
Eric Liuc0ac4bb2018-11-22 15:02:05 +0000191namespace {
192
193/// \brief Supports a test URI scheme with relaxed constraints for lit tests.
194/// The path in a test URI will be combined with a platform-specific fake
195/// directory to form an absolute path. For example, test:///a.cpp is resolved
196/// C:\clangd-test\a.cpp on Windows and /clangd-test/a.cpp on Unix.
197class TestScheme : public URIScheme {
198public:
199 Expected<std::string> getAbsolutePath(StringRef /*Authority*/, StringRef Body,
200 StringRef /*HintPath*/) const override {
201 using namespace llvm::sys;
202 // Still require "/" in body to mimic file scheme, as we want lengths of an
203 // equivalent URI in both schemes to be the same.
204 if (!Body.startswith("/"))
205 return make_error<StringError>(
206 "Expect URI body to be an absolute path starting with '/': " + Body,
207 inconvertibleErrorCode());
208 Body = Body.ltrim('/');
209 SmallVector<char, 16> Path(Body.begin(), Body.end());
210 path::native(Path);
211 auto Err = fs::make_absolute(TestScheme::TestDir, Path);
212 if (Err)
213 llvm_unreachable("Failed to make absolute path in test scheme.");
214 return std::string(Path.begin(), Path.end());
215 }
216
217 Expected<URI> uriFromAbsolutePath(StringRef AbsolutePath) const override {
218 StringRef Body = AbsolutePath;
219 if (!Body.consume_front(TestScheme::TestDir)) {
220 return make_error<StringError>("Path " + AbsolutePath +
221 " doesn't start with root " + TestDir,
222 inconvertibleErrorCode());
223 }
224
225 return URI("test", /*Authority=*/"", sys::path::convert_to_slash(Body));
226 }
227
228private:
229 const static char TestDir[];
230};
231
232#ifdef _WIN32
233const char TestScheme::TestDir[] = "C:\\clangd-test";
234#else
235const char TestScheme::TestDir[] = "/clangd-test";
236#endif
237
238}
239
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000240int main(int argc, char *argv[]) {
Sam McCallc008af62018-10-20 15:30:37 +0000241 sys::PrintStackTraceOnErrorSignal(argv[0]);
242 cl::SetVersionPrinter([](raw_ostream &OS) {
Sam McCalle72d0972018-06-29 13:24:20 +0000243 OS << clang::getClangToolFullVersion("clangd") << "\n";
244 });
Sam McCallc008af62018-10-20 15:30:37 +0000245 cl::ParseCommandLineOptions(
Sam McCalle72d0972018-06-29 13:24:20 +0000246 argc, argv,
247 "clangd is a language server that provides IDE-like features to editors. "
Sam McCallc008af62018-10-20 15:30:37 +0000248 "\n\nIt should be used via an editor plugin rather than invoked "
249 "directly. "
Sam McCalle72d0972018-06-29 13:24:20 +0000250 "For more information, see:"
251 "\n\thttps://clang.llvm.org/extra/clangd.html"
252 "\n\thttps://microsoft.github.io/language-server-protocol/");
Sam McCall5ed599e2018-02-06 10:47:30 +0000253 if (Test) {
254 RunSynchronously = true;
255 InputStyle = JSONStreamStyle::Delimited;
256 PrettyPrint = true;
257 }
Eric Liuc0ac4bb2018-11-22 15:02:05 +0000258 if (Test || EnableTestScheme) {
259 static URISchemeRegistry::Add<TestScheme> X(
260 "test", "Test scheme for clangd lit tests.");
261 }
Ilya Biryukovafb55542017-05-16 14:40:30 +0000262
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000263 if (!RunSynchronously && WorkerThreadsCount == 0) {
Sam McCallc008af62018-10-20 15:30:37 +0000264 errs() << "A number of worker threads cannot be 0. Did you mean to "
265 "specify -run-synchronously?";
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000266 return 1;
267 }
268
Kirill Bobyrevbcaf3802018-02-25 07:21:16 +0000269 if (RunSynchronously) {
270 if (WorkerThreadsCount.getNumOccurrences())
Sam McCallc008af62018-10-20 15:30:37 +0000271 errs() << "Ignoring -j because -run-synchronously is set.\n";
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000272 WorkerThreadsCount = 0;
Kirill Bobyrevbcaf3802018-02-25 07:21:16 +0000273 }
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000274
Benjamin Kramer74a18952017-10-26 10:07:04 +0000275 // Validate command line arguments.
Sam McCallc008af62018-10-20 15:30:37 +0000276 Optional<raw_fd_ostream> InputMirrorStream;
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000277 if (!InputMirrorFile.empty()) {
278 std::error_code EC;
Zachary Turner1f67a3c2018-06-07 19:58:58 +0000279 InputMirrorStream.emplace(InputMirrorFile, /*ref*/ EC,
Sam McCallc008af62018-10-20 15:30:37 +0000280 sys::fs::FA_Read | sys::fs::FA_Write);
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000281 if (EC) {
282 InputMirrorStream.reset();
Sam McCallc008af62018-10-20 15:30:37 +0000283 errs() << "Error while opening an input mirror file: " << EC.message();
Sam McCall0d946182018-11-02 23:47:55 +0000284 } else {
285 InputMirrorStream->SetUnbuffered();
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000286 }
287 }
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000288
Sam McCalled2717a2018-02-14 03:20:07 +0000289 // Setup tracing facilities if CLANGD_TRACE is set. In practice enabling a
290 // trace flag in your editor's config is annoying, launching with
291 // `CLANGD_TRACE=trace.json vim` is easier.
Sam McCallc008af62018-10-20 15:30:37 +0000292 Optional<raw_fd_ostream> TraceStream;
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000293 std::unique_ptr<trace::EventTracer> Tracer;
Sam McCalled2717a2018-02-14 03:20:07 +0000294 if (auto *TraceFile = getenv("CLANGD_TRACE")) {
Sam McCall8567cb32017-11-02 09:21:51 +0000295 std::error_code EC;
Zachary Turner1f67a3c2018-06-07 19:58:58 +0000296 TraceStream.emplace(TraceFile, /*ref*/ EC,
Sam McCallc008af62018-10-20 15:30:37 +0000297 sys::fs::FA_Read | sys::fs::FA_Write);
Sam McCall8567cb32017-11-02 09:21:51 +0000298 if (EC) {
Sam McCalled2717a2018-02-14 03:20:07 +0000299 TraceStream.reset();
Sam McCallc008af62018-10-20 15:30:37 +0000300 errs() << "Error while opening trace file " << TraceFile << ": "
301 << EC.message();
Sam McCall8567cb32017-11-02 09:21:51 +0000302 } else {
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000303 Tracer = trace::createJSONTracer(*TraceStream, PrettyPrint);
Sam McCall8567cb32017-11-02 09:21:51 +0000304 }
305 }
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000306
Sam McCallc008af62018-10-20 15:30:37 +0000307 Optional<trace::Session> TracingSession;
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000308 if (Tracer)
309 TracingSession.emplace(*Tracer);
310
Eric Liu4e4e5a42018-08-28 13:15:50 +0000311 // Use buffered stream to stderr (we still flush each log message). Unbuffered
312 // stream can cause significant (non-deterministic) latency for the logger.
Sam McCallc008af62018-10-20 15:30:37 +0000313 errs().SetBuffered();
314 StreamLogger Logger(errs(), LogLevel);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000315 clangd::LoggingSession LoggingSession(Logger);
Ilya Biryukov940901e2017-12-13 12:51:22 +0000316
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000317 // If --compile-commands-dir arg was invoked, check value and override default
318 // path.
Sam McCallc008af62018-10-20 15:30:37 +0000319 Optional<Path> CompileCommandsDirPath;
Sam McCallf01ad102018-10-23 11:54:36 +0000320 if (!CompileCommandsDir.empty()) {
321 if (sys::fs::exists(CompileCommandsDir)) {
322 // We support passing both relative and absolute paths to the
323 // --compile-commands-dir argument, but we assume the path is absolute in
324 // the rest of clangd so we make sure the path is absolute before
325 // continuing.
326 SmallString<128> Path(CompileCommandsDir);
327 if (std::error_code EC = sys::fs::make_absolute(Path)) {
328 errs() << "Error while converting the relative path specified by "
329 "--compile-commands-dir to an absolute path: "
330 << EC.message() << ". The argument will be ignored.\n";
331 } else {
332 CompileCommandsDirPath = Path.str();
333 }
334 } else {
335 errs() << "Path specified by --compile-commands-dir does not exist. The "
336 "argument will be ignored.\n";
337 }
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000338 }
Benjamin Kramer6a3d74e2017-02-07 12:40:59 +0000339
Sam McCall7363a2f2018-03-05 17:28:54 +0000340 ClangdServer::Options Opts;
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000341 switch (PCHStorage) {
342 case PCHStorageFlag::Memory:
Sam McCall7363a2f2018-03-05 17:28:54 +0000343 Opts.StorePreamblesInMemory = true;
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000344 break;
345 case PCHStorageFlag::Disk:
Sam McCall7363a2f2018-03-05 17:28:54 +0000346 Opts.StorePreamblesInMemory = false;
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000347 break;
348 }
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +0000349 if (!ResourceDir.empty())
Sam McCall7363a2f2018-03-05 17:28:54 +0000350 Opts.ResourceDir = ResourceDir;
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000351 Opts.BuildDynamicSymbolIndex = EnableIndex;
Sam McCall96f24892018-10-16 08:53:52 +0000352 Opts.HeavyweightDynamicSymbolIndex = UseDex;
Sam McCall422c8282018-11-26 16:00:11 +0000353 Opts.BackgroundIndex = EnableBackgroundIndex;
Haojian Wuba28e9a2018-01-10 14:44:34 +0000354 std::unique_ptr<SymbolIndex> StaticIdx;
Sam McCallf469c642018-09-10 10:00:47 +0000355 std::future<void> AsyncIndexLoad; // Block exit while loading the index.
Haojian Wu162510f2018-10-08 10:44:54 +0000356 if (EnableIndex && !IndexFile.empty()) {
Sam McCall76c4c3a2018-09-04 16:19:40 +0000357 // Load the index asynchronously. Meanwhile SwapIndex returns no results.
358 SwapIndex *Placeholder;
359 StaticIdx.reset(Placeholder = new SwapIndex(llvm::make_unique<MemIndex>()));
Eric Liuc0ac4bb2018-11-22 15:02:05 +0000360 AsyncIndexLoad = runAsync<void>([Placeholder] {
361 if (auto Idx = loadIndex(IndexFile, /*UseDex=*/true))
Sam McCall76c4c3a2018-09-04 16:19:40 +0000362 Placeholder->reset(std::move(Idx));
363 });
Sam McCallf469c642018-09-10 10:00:47 +0000364 if (RunSynchronously)
365 AsyncIndexLoad.wait();
Sam McCall7363a2f2018-03-05 17:28:54 +0000366 }
Sam McCall76c4c3a2018-09-04 16:19:40 +0000367 Opts.StaticIndex = StaticIdx.get();
Sam McCall7363a2f2018-03-05 17:28:54 +0000368 Opts.AsyncThreadsCount = WorkerThreadsCount;
369
Sam McCalladccab62017-11-23 16:58:22 +0000370 clangd::CodeCompleteOptions CCOpts;
Sam McCalladccab62017-11-23 16:58:22 +0000371 CCOpts.IncludeIneligibleResults = IncludeIneligibleResults;
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000372 CCOpts.Limit = LimitResults;
Sam McCallc18c2802018-06-15 11:06:29 +0000373 CCOpts.BundleOverloads = CompletionStyle != Detailed;
Sam McCall2161ec72018-07-05 06:20:41 +0000374 CCOpts.ShowOrigins = ShowOrigins;
Raoul Wols8f5e06f2018-07-29 19:12:42 +0000375 if (!HeaderInsertionDecorators) {
376 CCOpts.IncludeIndicator.Insert.clear();
377 CCOpts.IncludeIndicator.NoInsert.clear();
378 }
Eric Liu25d74e92018-08-24 11:23:56 +0000379 CCOpts.SpeculativeIndexRequest = Opts.StaticIndex;
Kadir Cetinkayae8d8aee2018-09-19 10:16:44 +0000380 CCOpts.EnableFunctionArgSnippets = EnableFunctionArgSnippets;
Eric Liu670c1472018-09-27 18:46:00 +0000381 CCOpts.AllScopes = AllScopesCompletion;
Sam McCall7363a2f2018-03-05 17:28:54 +0000382
Benjamin Kramer74a18952017-10-26 10:07:04 +0000383 // Initialize and run ClangdLSPServer.
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000384 // Change stdin to binary to not lose \r\n on windows.
Sam McCallc008af62018-10-20 15:30:37 +0000385 sys::ChangeStdinToBinary();
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000386 auto Transport = newJSONTransport(
Sam McCallc008af62018-10-20 15:30:37 +0000387 stdin, outs(),
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000388 InputMirrorStream ? InputMirrorStream.getPointer() : nullptr, PrettyPrint,
389 InputStyle);
Alex Lorenzf8087862018-08-01 17:39:29 +0000390 ClangdLSPServer LSPServer(
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000391 *Transport, CCOpts, CompileCommandsDirPath,
Sam McCallc55d09a2018-11-02 13:09:36 +0000392 /*UseDirBasedCDB=*/CompileArgsFrom == FilesystemCompileArgs, Opts);
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +0000393 constexpr int NoShutdownRequestErrorCode = 1;
Sam McCallc008af62018-10-20 15:30:37 +0000394 set_thread_name("clangd.main");
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000395 return LSPServer.run() ? 0 : NoShutdownRequestErrorCode;
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000396}