blob: e96b185ab11ce9726ebea706978e4e179488d3f0 [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
Sam McCallc008af62018-10-20 15:30:37 +000086static cl::opt<bool> Test(
Sam McCall5ed599e2018-02-06 10:47:30 +000087 "lit-test",
Sam McCallc008af62018-10-20 15:30:37 +000088 cl::desc(
Sam McCall5ed599e2018-02-06 10:47:30 +000089 "Abbreviation for -input-style=delimited -pretty -run-synchronously. "
90 "Intended to simplify lit tests."),
Sam McCallc008af62018-10-20 15:30:37 +000091 cl::init(false), cl::Hidden);
Sam McCall5ed599e2018-02-06 10:47:30 +000092
Sam McCall47feb572018-09-05 10:39:58 +000093enum PCHStorageFlag { Disk, Memory };
Sam McCallc008af62018-10-20 15:30:37 +000094static cl::opt<PCHStorageFlag> PCHStorage(
Ilya Biryukove9eb7f02017-11-16 16:25:18 +000095 "pch-storage",
Sam McCallc008af62018-10-20 15:30:37 +000096 cl::desc("Storing PCHs in memory increases memory usages, but may "
97 "improve performance"),
98 cl::values(clEnumValN(PCHStorageFlag::Disk, "disk", "store PCHs on disk"),
99 clEnumValN(PCHStorageFlag::Memory, "memory",
100 "store PCHs in memory")),
101 cl::init(PCHStorageFlag::Disk));
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000102
Sam McCallc008af62018-10-20 15:30:37 +0000103static cl::opt<int>
104 LimitResults("limit-results",
105 cl::desc("Limit the number of results returned by clangd. "
106 "0 means no limit."),
107 cl::init(100));
Haojian Wu48b48652018-01-25 09:20:09 +0000108
Sam McCallc008af62018-10-20 15:30:37 +0000109static cl::opt<bool>
110 RunSynchronously("run-synchronously",
111 cl::desc("Parse on main thread. If set, -j is ignored"),
112 cl::init(false), cl::Hidden);
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000113
Sam McCallc008af62018-10-20 15:30:37 +0000114static cl::opt<Path> ResourceDir("resource-dir",
115 cl::desc("Directory for system clang headers"),
116 cl::init(""), cl::Hidden);
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +0000117
Sam McCallc008af62018-10-20 15:30:37 +0000118static cl::opt<Path> InputMirrorFile(
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000119 "input-mirror-file",
Sam McCallc008af62018-10-20 15:30:37 +0000120 cl::desc(
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000121 "Mirror all LSP input to the specified file. Useful for debugging."),
Sam McCallc008af62018-10-20 15:30:37 +0000122 cl::init(""), cl::Hidden);
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000123
Sam McCallc008af62018-10-20 15:30:37 +0000124static cl::opt<bool> EnableIndex(
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000125 "index",
Sam McCallc008af62018-10-20 15:30:37 +0000126 cl::desc(
Eric Liu76b88d82018-09-13 12:53:23 +0000127 "Enable index-based features. By default, clangd maintains an index "
128 "built from symbols in opened files. Global index support needs to "
129 "enabled separatedly."),
Sam McCallc008af62018-10-20 15:30:37 +0000130 cl::init(true), cl::Hidden);
Eric Liubfac8f72017-12-19 18:00:37 +0000131
Sam McCallc008af62018-10-20 15:30:37 +0000132static cl::opt<bool> AllScopesCompletion(
Eric Liu670c1472018-09-27 18:46:00 +0000133 "all-scopes-completion",
Sam McCallc008af62018-10-20 15:30:37 +0000134 cl::desc(
Eric Liu670c1472018-09-27 18:46:00 +0000135 "If set to true, code completion will include index symbols that are "
136 "not defined in the scopes (e.g. "
137 "namespaces) visible from the code completion point. Such completions "
138 "can insert scope qualifiers."),
Sam McCallc008af62018-10-20 15:30:37 +0000139 cl::init(false), cl::Hidden);
Eric Liu670c1472018-09-27 18:46:00 +0000140
Sam McCallc008af62018-10-20 15:30:37 +0000141static cl::opt<bool>
142 ShowOrigins("debug-origin", cl::desc("Show origins of completion items"),
143 cl::init(clangd::CodeCompleteOptions().ShowOrigins),
144 cl::Hidden);
Sam McCall2161ec72018-07-05 06:20:41 +0000145
Sam McCallc008af62018-10-20 15:30:37 +0000146static cl::opt<bool> HeaderInsertionDecorators(
Raoul Wols8f5e06f2018-07-29 19:12:42 +0000147 "header-insertion-decorators",
Sam McCallc008af62018-10-20 15:30:37 +0000148 cl::desc("Prepend a circular dot or space before the completion "
149 "label, depending on whether "
150 "an include line will be inserted or not."),
151 cl::init(true));
Raoul Wols8f5e06f2018-07-29 19:12:42 +0000152
Sam McCallc008af62018-10-20 15:30:37 +0000153static cl::opt<Path> IndexFile(
Haojian Wu162510f2018-10-08 10:44:54 +0000154 "index-file",
Sam McCallc008af62018-10-20 15:30:37 +0000155 cl::desc(
Haojian Wu162510f2018-10-08 10:44:54 +0000156 "Index file to build the static index. The file must have been created "
157 "by a compatible clangd-index.\n"
Haojian Wuba28e9a2018-01-10 14:44:34 +0000158 "WARNING: This option is experimental only, and will be removed "
159 "eventually. Don't rely on it."),
Sam McCallc008af62018-10-20 15:30:37 +0000160 cl::init(""), cl::Hidden);
Haojian Wuba28e9a2018-01-10 14:44:34 +0000161
Alex Lorenzf8087862018-08-01 17:39:29 +0000162enum CompileArgsFrom { LSPCompileArgs, FilesystemCompileArgs };
Sam McCallc008af62018-10-20 15:30:37 +0000163static cl::opt<CompileArgsFrom> CompileArgsFrom(
164 "compile_args_from", cl::desc("The source of compile commands"),
165 cl::values(clEnumValN(LSPCompileArgs, "lsp",
166 "All compile commands come from LSP and "
167 "'compile_commands.json' files are ignored"),
168 clEnumValN(FilesystemCompileArgs, "filesystem",
169 "All compile commands come from the "
170 "'compile_commands.json' files")),
171 cl::init(FilesystemCompileArgs), cl::Hidden);
Alex Lorenzf8087862018-08-01 17:39:29 +0000172
Sam McCallc008af62018-10-20 15:30:37 +0000173static cl::opt<bool> EnableFunctionArgSnippets(
Kadir Cetinkayae8d8aee2018-09-19 10:16:44 +0000174 "function-arg-placeholders",
Sam McCallc008af62018-10-20 15:30:37 +0000175 cl::desc("When disabled, completions contain only parentheses for "
176 "function calls. When enabled, completions also contain "
177 "placeholders for method parameters."),
178 cl::init(clangd::CodeCompleteOptions().EnableFunctionArgSnippets));
Kadir Cetinkayae8d8aee2018-09-19 10:16:44 +0000179
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000180int main(int argc, char *argv[]) {
Sam McCallc008af62018-10-20 15:30:37 +0000181 sys::PrintStackTraceOnErrorSignal(argv[0]);
182 cl::SetVersionPrinter([](raw_ostream &OS) {
Sam McCalle72d0972018-06-29 13:24:20 +0000183 OS << clang::getClangToolFullVersion("clangd") << "\n";
184 });
Sam McCallc008af62018-10-20 15:30:37 +0000185 cl::ParseCommandLineOptions(
Sam McCalle72d0972018-06-29 13:24:20 +0000186 argc, argv,
187 "clangd is a language server that provides IDE-like features to editors. "
Sam McCallc008af62018-10-20 15:30:37 +0000188 "\n\nIt should be used via an editor plugin rather than invoked "
189 "directly. "
Sam McCalle72d0972018-06-29 13:24:20 +0000190 "For more information, see:"
191 "\n\thttps://clang.llvm.org/extra/clangd.html"
192 "\n\thttps://microsoft.github.io/language-server-protocol/");
Sam McCall5ed599e2018-02-06 10:47:30 +0000193 if (Test) {
194 RunSynchronously = true;
195 InputStyle = JSONStreamStyle::Delimited;
196 PrettyPrint = true;
197 }
Ilya Biryukovafb55542017-05-16 14:40:30 +0000198
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000199 if (!RunSynchronously && WorkerThreadsCount == 0) {
Sam McCallc008af62018-10-20 15:30:37 +0000200 errs() << "A number of worker threads cannot be 0. Did you mean to "
201 "specify -run-synchronously?";
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000202 return 1;
203 }
204
Kirill Bobyrevbcaf3802018-02-25 07:21:16 +0000205 if (RunSynchronously) {
206 if (WorkerThreadsCount.getNumOccurrences())
Sam McCallc008af62018-10-20 15:30:37 +0000207 errs() << "Ignoring -j because -run-synchronously is set.\n";
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000208 WorkerThreadsCount = 0;
Kirill Bobyrevbcaf3802018-02-25 07:21:16 +0000209 }
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000210
Benjamin Kramer74a18952017-10-26 10:07:04 +0000211 // Validate command line arguments.
Sam McCallc008af62018-10-20 15:30:37 +0000212 Optional<raw_fd_ostream> InputMirrorStream;
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000213 if (!InputMirrorFile.empty()) {
214 std::error_code EC;
Zachary Turner1f67a3c2018-06-07 19:58:58 +0000215 InputMirrorStream.emplace(InputMirrorFile, /*ref*/ EC,
Sam McCallc008af62018-10-20 15:30:37 +0000216 sys::fs::FA_Read | sys::fs::FA_Write);
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000217 if (EC) {
218 InputMirrorStream.reset();
Sam McCallc008af62018-10-20 15:30:37 +0000219 errs() << "Error while opening an input mirror file: " << EC.message();
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000220 }
221 }
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000222
Sam McCalled2717a2018-02-14 03:20:07 +0000223 // Setup tracing facilities if CLANGD_TRACE is set. In practice enabling a
224 // trace flag in your editor's config is annoying, launching with
225 // `CLANGD_TRACE=trace.json vim` is easier.
Sam McCallc008af62018-10-20 15:30:37 +0000226 Optional<raw_fd_ostream> TraceStream;
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000227 std::unique_ptr<trace::EventTracer> Tracer;
Sam McCalled2717a2018-02-14 03:20:07 +0000228 if (auto *TraceFile = getenv("CLANGD_TRACE")) {
Sam McCall8567cb32017-11-02 09:21:51 +0000229 std::error_code EC;
Zachary Turner1f67a3c2018-06-07 19:58:58 +0000230 TraceStream.emplace(TraceFile, /*ref*/ EC,
Sam McCallc008af62018-10-20 15:30:37 +0000231 sys::fs::FA_Read | sys::fs::FA_Write);
Sam McCall8567cb32017-11-02 09:21:51 +0000232 if (EC) {
Sam McCalled2717a2018-02-14 03:20:07 +0000233 TraceStream.reset();
Sam McCallc008af62018-10-20 15:30:37 +0000234 errs() << "Error while opening trace file " << TraceFile << ": "
235 << EC.message();
Sam McCall8567cb32017-11-02 09:21:51 +0000236 } else {
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000237 Tracer = trace::createJSONTracer(*TraceStream, PrettyPrint);
Sam McCall8567cb32017-11-02 09:21:51 +0000238 }
239 }
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000240
Sam McCallc008af62018-10-20 15:30:37 +0000241 Optional<trace::Session> TracingSession;
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000242 if (Tracer)
243 TracingSession.emplace(*Tracer);
244
Eric Liu4e4e5a42018-08-28 13:15:50 +0000245 // Use buffered stream to stderr (we still flush each log message). Unbuffered
246 // stream can cause significant (non-deterministic) latency for the logger.
Sam McCallc008af62018-10-20 15:30:37 +0000247 errs().SetBuffered();
248 StreamLogger Logger(errs(), LogLevel);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000249 clangd::LoggingSession LoggingSession(Logger);
Ilya Biryukov940901e2017-12-13 12:51:22 +0000250
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000251 // If --compile-commands-dir arg was invoked, check value and override default
252 // path.
Sam McCallc008af62018-10-20 15:30:37 +0000253 Optional<Path> CompileCommandsDirPath;
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000254 if (CompileCommandsDir.empty()) {
Sam McCallc008af62018-10-20 15:30:37 +0000255 CompileCommandsDirPath = None;
256 } else if (!sys::path::is_absolute(CompileCommandsDir) ||
257 !sys::fs::exists(CompileCommandsDir)) {
258 errs() << "Path specified by --compile-commands-dir either does not "
259 "exist or is not an absolute "
260 "path. The argument will be ignored.\n";
261 CompileCommandsDirPath = None;
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000262 } else {
263 CompileCommandsDirPath = CompileCommandsDir;
264 }
Benjamin Kramer6a3d74e2017-02-07 12:40:59 +0000265
Sam McCall7363a2f2018-03-05 17:28:54 +0000266 ClangdServer::Options Opts;
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000267 switch (PCHStorage) {
268 case PCHStorageFlag::Memory:
Sam McCall7363a2f2018-03-05 17:28:54 +0000269 Opts.StorePreamblesInMemory = true;
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000270 break;
271 case PCHStorageFlag::Disk:
Sam McCall7363a2f2018-03-05 17:28:54 +0000272 Opts.StorePreamblesInMemory = false;
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000273 break;
274 }
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +0000275 if (!ResourceDir.empty())
Sam McCall7363a2f2018-03-05 17:28:54 +0000276 Opts.ResourceDir = ResourceDir;
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000277 Opts.BuildDynamicSymbolIndex = EnableIndex;
Sam McCall96f24892018-10-16 08:53:52 +0000278 Opts.HeavyweightDynamicSymbolIndex = UseDex;
Haojian Wuba28e9a2018-01-10 14:44:34 +0000279 std::unique_ptr<SymbolIndex> StaticIdx;
Sam McCallf469c642018-09-10 10:00:47 +0000280 std::future<void> AsyncIndexLoad; // Block exit while loading the index.
Haojian Wu162510f2018-10-08 10:44:54 +0000281 if (EnableIndex && !IndexFile.empty()) {
Sam McCall76c4c3a2018-09-04 16:19:40 +0000282 // Load the index asynchronously. Meanwhile SwapIndex returns no results.
283 SwapIndex *Placeholder;
284 StaticIdx.reset(Placeholder = new SwapIndex(llvm::make_unique<MemIndex>()));
Sam McCallf469c642018-09-10 10:00:47 +0000285 AsyncIndexLoad = runAsync<void>([Placeholder, &Opts] {
Sam McCall96f24892018-10-16 08:53:52 +0000286 if (auto Idx = loadIndex(IndexFile, Opts.URISchemes, /*UseDex=*/true))
Sam McCall76c4c3a2018-09-04 16:19:40 +0000287 Placeholder->reset(std::move(Idx));
288 });
Sam McCallf469c642018-09-10 10:00:47 +0000289 if (RunSynchronously)
290 AsyncIndexLoad.wait();
Sam McCall7363a2f2018-03-05 17:28:54 +0000291 }
Sam McCall76c4c3a2018-09-04 16:19:40 +0000292 Opts.StaticIndex = StaticIdx.get();
Sam McCall7363a2f2018-03-05 17:28:54 +0000293 Opts.AsyncThreadsCount = WorkerThreadsCount;
294
Sam McCalladccab62017-11-23 16:58:22 +0000295 clangd::CodeCompleteOptions CCOpts;
Sam McCalladccab62017-11-23 16:58:22 +0000296 CCOpts.IncludeIneligibleResults = IncludeIneligibleResults;
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000297 CCOpts.Limit = LimitResults;
Sam McCallc18c2802018-06-15 11:06:29 +0000298 CCOpts.BundleOverloads = CompletionStyle != Detailed;
Sam McCall2161ec72018-07-05 06:20:41 +0000299 CCOpts.ShowOrigins = ShowOrigins;
Raoul Wols8f5e06f2018-07-29 19:12:42 +0000300 if (!HeaderInsertionDecorators) {
301 CCOpts.IncludeIndicator.Insert.clear();
302 CCOpts.IncludeIndicator.NoInsert.clear();
303 }
Eric Liu25d74e92018-08-24 11:23:56 +0000304 CCOpts.SpeculativeIndexRequest = Opts.StaticIndex;
Kadir Cetinkayae8d8aee2018-09-19 10:16:44 +0000305 CCOpts.EnableFunctionArgSnippets = EnableFunctionArgSnippets;
Eric Liu670c1472018-09-27 18:46:00 +0000306 CCOpts.AllScopes = AllScopesCompletion;
Sam McCall7363a2f2018-03-05 17:28:54 +0000307
Benjamin Kramer74a18952017-10-26 10:07:04 +0000308 // Initialize and run ClangdLSPServer.
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000309 // Change stdin to binary to not lose \r\n on windows.
Sam McCallc008af62018-10-20 15:30:37 +0000310 sys::ChangeStdinToBinary();
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000311 auto Transport = newJSONTransport(
Sam McCallc008af62018-10-20 15:30:37 +0000312 stdin, outs(),
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000313 InputMirrorStream ? InputMirrorStream.getPointer() : nullptr, PrettyPrint,
314 InputStyle);
Alex Lorenzf8087862018-08-01 17:39:29 +0000315 ClangdLSPServer LSPServer(
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000316 *Transport, CCOpts, CompileCommandsDirPath,
Alex Lorenzf8087862018-08-01 17:39:29 +0000317 /*ShouldUseInMemoryCDB=*/CompileArgsFrom == LSPCompileArgs, Opts);
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +0000318 constexpr int NoShutdownRequestErrorCode = 1;
Sam McCallc008af62018-10-20 15:30:37 +0000319 set_thread_name("clangd.main");
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000320 return LSPServer.run() ? 0 : NoShutdownRequestErrorCode;
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000321}