blob: 53f7264e565bdb77b14139cfec8a738c3fe4192b [file] [log] [blame]
Krasimir Georgiev95ef1712017-04-12 17:13:08 +00001//===--- ClangdMain.cpp - clangd server loop ------------------------------===//
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +00006//
7//===----------------------------------------------------------------------===//
8
Jan Korousdca9c7c2019-01-16 00:24:22 +00009#include "Features.inc"
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
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000028namespace clang {
29namespace clangd {
Sam McCall96f24892018-10-16 08:53:52 +000030// FIXME: remove this option when Dex is cheap enough.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000031static llvm::cl::opt<bool>
32 UseDex("use-dex-index",
33 llvm::cl::desc("Use experimental Dex dynamic index."),
Eric Liu4b68d912019-02-07 15:34:37 +000034 llvm::cl::init(true), llvm::cl::Hidden);
Kirill Bobyrevdc41bef2018-08-21 10:40:19 +000035
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000036static llvm::cl::opt<Path> CompileCommandsDir(
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000037 "compile-commands-dir",
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000038 llvm::cl::desc("Specify a path to look for compile_commands.json. If path "
39 "is invalid, clangd will look in the current directory and "
40 "parent paths of each source file."));
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000041
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000042static llvm::cl::opt<unsigned>
43 WorkerThreadsCount("j",
44 llvm::cl::desc("Number of async workers used by clangd"),
45 llvm::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 };
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000049static llvm::cl::opt<CompletionStyleFlag> CompletionStyle(
50 "completion-style",
51 llvm::cl::desc("Granularity of code completion suggestions"),
52 llvm::cl::values(
Sam McCallc18c2802018-06-15 11:06:29 +000053 clEnumValN(Detailed, "detailed",
54 "One completion item for each semantically distinct "
55 "completion, with full type information."),
56 clEnumValN(Bundled, "bundled",
57 "Similar completion items (e.g. function overloads) are "
58 "combined. Type information shown where possible.")),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000059 llvm::cl::init(Detailed));
Sam McCallc18c2802018-06-15 11:06:29 +000060
Sam McCalladccab62017-11-23 16:58:22 +000061// FIXME: Flags are the wrong mechanism for user preferences.
62// We should probably read a dotfile or similar.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000063static llvm::cl::opt<bool> IncludeIneligibleResults(
Sam McCalladccab62017-11-23 16:58:22 +000064 "include-ineligible-results",
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000065 llvm::cl::desc(
66 "Include ineligible completion results (e.g. private members)"),
67 llvm::cl::init(CodeCompleteOptions().IncludeIneligibleResults),
68 llvm::cl::Hidden);
Ilya Biryukovb33c1572017-09-12 13:57:14 +000069
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000070static llvm::cl::opt<JSONStreamStyle> InputStyle(
71 "input-style", llvm::cl::desc("Input JSON stream encoding"),
72 llvm::cl::values(
Sam McCall5ed599e2018-02-06 10:47:30 +000073 clEnumValN(JSONStreamStyle::Standard, "standard", "usual LSP protocol"),
74 clEnumValN(JSONStreamStyle::Delimited, "delimited",
75 "messages delimited by --- lines, with # comment support")),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000076 llvm::cl::init(JSONStreamStyle::Standard));
Sam McCall5ed599e2018-02-06 10:47:30 +000077
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000078static llvm::cl::opt<bool>
79 PrettyPrint("pretty", llvm::cl::desc("Pretty-print JSON output"),
80 llvm::cl::init(false));
Sam McCalldd0566b2017-11-06 15:40:30 +000081
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000082static llvm::cl::opt<Logger::Level> LogLevel(
83 "log", llvm::cl::desc("Verbosity of log messages written to stderr"),
84 llvm::cl::values(clEnumValN(Logger::Error, "error", "Error messages only"),
85 clEnumValN(Logger::Info, "info",
86 "High level execution tracing"),
87 clEnumValN(Logger::Debug, "verbose", "Low level details")),
88 llvm::cl::init(Logger::Info));
Sam McCallbed58852018-07-11 10:35:11 +000089
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000090static llvm::cl::opt<bool>
Eric Liuc0ac4bb2018-11-22 15:02:05 +000091 Test("lit-test",
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000092 llvm::cl::desc("Abbreviation for -input-style=delimited -pretty "
93 "-run-synchronously -enable-test-scheme. "
94 "Intended to simplify lit tests."),
95 llvm::cl::init(false), llvm::cl::Hidden);
Eric Liuc0ac4bb2018-11-22 15:02:05 +000096
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000097static llvm::cl::opt<bool> EnableTestScheme(
Eric Liuc0ac4bb2018-11-22 15:02:05 +000098 "enable-test-uri-scheme",
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000099 llvm::cl::desc("Enable 'test:' URI scheme. Only use in lit tests."),
100 llvm::cl::init(false), llvm::cl::Hidden);
Sam McCall5ed599e2018-02-06 10:47:30 +0000101
Sam McCall47feb572018-09-05 10:39:58 +0000102enum PCHStorageFlag { Disk, Memory };
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000103static llvm::cl::opt<PCHStorageFlag> PCHStorage(
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000104 "pch-storage",
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000105 llvm::cl::desc("Storing PCHs in memory increases memory usages, but may "
106 "improve performance"),
107 llvm::cl::values(
108 clEnumValN(PCHStorageFlag::Disk, "disk", "store PCHs on disk"),
109 clEnumValN(PCHStorageFlag::Memory, "memory", "store PCHs in memory")),
110 llvm::cl::init(PCHStorageFlag::Disk));
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000111
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000112static llvm::cl::opt<int> LimitResults(
113 "limit-results",
114 llvm::cl::desc("Limit the number of results returned by clangd. "
115 "0 means no limit."),
116 llvm::cl::init(100));
Haojian Wu48b48652018-01-25 09:20:09 +0000117
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000118static llvm::cl::opt<bool> RunSynchronously(
119 "run-synchronously",
120 llvm::cl::desc("Parse on main thread. If set, -j is ignored"),
121 llvm::cl::init(false), llvm::cl::Hidden);
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000122
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000123static llvm::cl::opt<Path>
124 ResourceDir("resource-dir",
125 llvm::cl::desc("Directory for system clang headers"),
126 llvm::cl::init(""), llvm::cl::Hidden);
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +0000127
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000128static llvm::cl::opt<Path> InputMirrorFile(
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000129 "input-mirror-file",
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000130 llvm::cl::desc(
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000131 "Mirror all LSP input to the specified file. Useful for debugging."),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000132 llvm::cl::init(""), llvm::cl::Hidden);
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000133
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000134static llvm::cl::opt<bool> EnableIndex(
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000135 "index",
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000136 llvm::cl::desc(
Eric Liu76b88d82018-09-13 12:53:23 +0000137 "Enable index-based features. By default, clangd maintains an index "
138 "built from symbols in opened files. Global index support needs to "
139 "enabled separatedly."),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000140 llvm::cl::init(true), llvm::cl::Hidden);
Eric Liubfac8f72017-12-19 18:00:37 +0000141
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000142static llvm::cl::opt<bool> AllScopesCompletion(
Eric Liu670c1472018-09-27 18:46:00 +0000143 "all-scopes-completion",
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000144 llvm::cl::desc(
Eric Liu670c1472018-09-27 18:46:00 +0000145 "If set to true, code completion will include index symbols that are "
146 "not defined in the scopes (e.g. "
147 "namespaces) visible from the code completion point. Such completions "
148 "can insert scope qualifiers."),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000149 llvm::cl::init(true));
Eric Liu670c1472018-09-27 18:46:00 +0000150
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000151static llvm::cl::opt<bool> ShowOrigins(
152 "debug-origin", llvm::cl::desc("Show origins of completion items"),
153 llvm::cl::init(CodeCompleteOptions().ShowOrigins), llvm::cl::Hidden);
Sam McCall2161ec72018-07-05 06:20:41 +0000154
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000155static llvm::cl::opt<bool> HeaderInsertionDecorators(
Raoul Wols8f5e06f2018-07-29 19:12:42 +0000156 "header-insertion-decorators",
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000157 llvm::cl::desc("Prepend a circular dot or space before the completion "
158 "label, depending on whether "
159 "an include line will be inserted or not."),
160 llvm::cl::init(true));
Raoul Wols8f5e06f2018-07-29 19:12:42 +0000161
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000162static llvm::cl::opt<Path> IndexFile(
Haojian Wu162510f2018-10-08 10:44:54 +0000163 "index-file",
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000164 llvm::cl::desc(
Haojian Wu162510f2018-10-08 10:44:54 +0000165 "Index file to build the static index. The file must have been created "
Ilya Biryukov97ed3c12019-02-20 12:31:44 +0000166 "by a compatible clangd-indexer.\n"
Haojian Wuba28e9a2018-01-10 14:44:34 +0000167 "WARNING: This option is experimental only, and will be removed "
168 "eventually. Don't rely on it."),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000169 llvm::cl::init(""), llvm::cl::Hidden);
Haojian Wuba28e9a2018-01-10 14:44:34 +0000170
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000171static llvm::cl::opt<bool> EnableBackgroundIndex(
Sam McCall422c8282018-11-26 16:00:11 +0000172 "background-index",
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000173 llvm::cl::desc(
174 "Index project code in the background and persist index on disk. "
175 "Experimental"),
176 llvm::cl::init(false), llvm::cl::Hidden);
Sam McCall422c8282018-11-26 16:00:11 +0000177
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000178static llvm::cl::opt<int> BackgroundIndexRebuildPeriod(
Eric Liu667e8ef2018-12-18 15:39:33 +0000179 "background-index-rebuild-period",
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000180 llvm::cl::desc(
Eric Liu667e8ef2018-12-18 15:39:33 +0000181 "If set to non-zero, the background index rebuilds the symbol index "
182 "periodically every X milliseconds; otherwise, the "
183 "symbol index will be updated for each indexed file."),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000184 llvm::cl::init(5000), llvm::cl::Hidden);
Eric Liu667e8ef2018-12-18 15:39:33 +0000185
Alex Lorenzf8087862018-08-01 17:39:29 +0000186enum CompileArgsFrom { LSPCompileArgs, FilesystemCompileArgs };
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000187static llvm::cl::opt<CompileArgsFrom> CompileArgsFrom(
188 "compile_args_from", llvm::cl::desc("The source of compile commands"),
189 llvm::cl::values(clEnumValN(LSPCompileArgs, "lsp",
190 "All compile commands come from LSP and "
191 "'compile_commands.json' files are ignored"),
192 clEnumValN(FilesystemCompileArgs, "filesystem",
193 "All compile commands come from the "
194 "'compile_commands.json' files")),
195 llvm::cl::init(FilesystemCompileArgs), llvm::cl::Hidden);
Alex Lorenzf8087862018-08-01 17:39:29 +0000196
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000197static llvm::cl::opt<bool> EnableFunctionArgSnippets(
Kadir Cetinkayae8d8aee2018-09-19 10:16:44 +0000198 "function-arg-placeholders",
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000199 llvm::cl::desc("When disabled, completions contain only parentheses for "
200 "function calls. When enabled, completions also contain "
201 "placeholders for method parameters."),
202 llvm::cl::init(CodeCompleteOptions().EnableFunctionArgSnippets));
Kadir Cetinkayae8d8aee2018-09-19 10:16:44 +0000203
Haojian Wu1ca0c582019-01-22 09:39:05 +0000204static llvm::cl::opt<std::string> ClangTidyChecks(
205 "clang-tidy-checks",
Haojian Wuac6d2e12019-02-06 09:10:47 +0000206 llvm::cl::desc(
207 "List of clang-tidy checks to run (this will override "
208 ".clang-tidy files). Only meaningful when -clang-tidy flag is on."),
Ilya Biryukov73afee22019-01-29 15:52:05 +0000209 llvm::cl::init(""));
Haojian Wu1ca0c582019-01-22 09:39:05 +0000210
Haojian Wuac6d2e12019-02-06 09:10:47 +0000211static llvm::cl::opt<bool> EnableClangTidy(
212 "clang-tidy",
213 llvm::cl::desc("Enable clang-tidy diagnostics."),
214 llvm::cl::init(false));
215
Eric Liudd662772019-01-28 14:01:55 +0000216static llvm::cl::opt<bool> SuggestMissingIncludes(
217 "suggest-missing-includes",
218 llvm::cl::desc("Attempts to fix diagnostic errors caused by missing "
219 "includes using index."),
Eric Liu9f7a7672019-03-01 14:17:55 +0000220 llvm::cl::init(true));
Eric Liudd662772019-01-28 14:01:55 +0000221
Eric Liuc0ac4bb2018-11-22 15:02:05 +0000222namespace {
223
224/// \brief Supports a test URI scheme with relaxed constraints for lit tests.
225/// The path in a test URI will be combined with a platform-specific fake
226/// directory to form an absolute path. For example, test:///a.cpp is resolved
227/// C:\clangd-test\a.cpp on Windows and /clangd-test/a.cpp on Unix.
228class TestScheme : public URIScheme {
229public:
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000230 llvm::Expected<std::string>
231 getAbsolutePath(llvm::StringRef /*Authority*/, llvm::StringRef Body,
232 llvm::StringRef /*HintPath*/) const override {
Eric Liuc0ac4bb2018-11-22 15:02:05 +0000233 using namespace llvm::sys;
234 // Still require "/" in body to mimic file scheme, as we want lengths of an
235 // equivalent URI in both schemes to be the same.
236 if (!Body.startswith("/"))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000237 return llvm::make_error<llvm::StringError>(
Eric Liuc0ac4bb2018-11-22 15:02:05 +0000238 "Expect URI body to be an absolute path starting with '/': " + Body,
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000239 llvm::inconvertibleErrorCode());
Eric Liuc0ac4bb2018-11-22 15:02:05 +0000240 Body = Body.ltrim('/');
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000241 llvm::SmallVector<char, 16> Path(Body.begin(), Body.end());
Eric Liuc0ac4bb2018-11-22 15:02:05 +0000242 path::native(Path);
Pavel Labath7b556542019-01-16 10:26:52 +0000243 fs::make_absolute(TestScheme::TestDir, Path);
Eric Liuc0ac4bb2018-11-22 15:02:05 +0000244 return std::string(Path.begin(), Path.end());
245 }
246
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000247 llvm::Expected<URI>
248 uriFromAbsolutePath(llvm::StringRef AbsolutePath) const override {
249 llvm::StringRef Body = AbsolutePath;
Eric Liuc0ac4bb2018-11-22 15:02:05 +0000250 if (!Body.consume_front(TestScheme::TestDir)) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000251 return llvm::make_error<llvm::StringError>(
252 "Path " + AbsolutePath + " doesn't start with root " + TestDir,
253 llvm::inconvertibleErrorCode());
Eric Liuc0ac4bb2018-11-22 15:02:05 +0000254 }
255
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000256 return URI("test", /*Authority=*/"",
257 llvm::sys::path::convert_to_slash(Body));
Eric Liuc0ac4bb2018-11-22 15:02:05 +0000258 }
259
260private:
261 const static char TestDir[];
262};
263
264#ifdef _WIN32
265const char TestScheme::TestDir[] = "C:\\clangd-test";
266#else
267const char TestScheme::TestDir[] = "/clangd-test";
268#endif
269
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000270} // namespace
271} // namespace clangd
272} // namespace clang
Eric Liuc0ac4bb2018-11-22 15:02:05 +0000273
Jan Korousdca9c7c2019-01-16 00:24:22 +0000274enum class ErrorResultCode : int {
275 NoShutdownRequest = 1,
276 CantRunAsXPCService = 2
277};
278
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000279int main(int argc, char *argv[]) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000280 using namespace clang;
281 using namespace clang::clangd;
282
283 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
284 llvm::cl::SetVersionPrinter([](llvm::raw_ostream &OS) {
Sam McCalle72d0972018-06-29 13:24:20 +0000285 OS << clang::getClangToolFullVersion("clangd") << "\n";
286 });
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000287 llvm::cl::ParseCommandLineOptions(
Sam McCalle72d0972018-06-29 13:24:20 +0000288 argc, argv,
289 "clangd is a language server that provides IDE-like features to editors. "
Sam McCallc008af62018-10-20 15:30:37 +0000290 "\n\nIt should be used via an editor plugin rather than invoked "
291 "directly. "
Sam McCalle72d0972018-06-29 13:24:20 +0000292 "For more information, see:"
293 "\n\thttps://clang.llvm.org/extra/clangd.html"
294 "\n\thttps://microsoft.github.io/language-server-protocol/");
Sam McCall5ed599e2018-02-06 10:47:30 +0000295 if (Test) {
296 RunSynchronously = true;
297 InputStyle = JSONStreamStyle::Delimited;
298 PrettyPrint = true;
Sam McCall032f3e72018-11-27 12:09:13 +0000299 preventThreadStarvationInTests(); // Ensure background index makes progress.
Sam McCall5ed599e2018-02-06 10:47:30 +0000300 }
Eric Liuc0ac4bb2018-11-22 15:02:05 +0000301 if (Test || EnableTestScheme) {
302 static URISchemeRegistry::Add<TestScheme> X(
303 "test", "Test scheme for clangd lit tests.");
304 }
Ilya Biryukovafb55542017-05-16 14:40:30 +0000305
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000306 if (!RunSynchronously && WorkerThreadsCount == 0) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000307 llvm::errs() << "A number of worker threads cannot be 0. Did you mean to "
308 "specify -run-synchronously?";
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000309 return 1;
310 }
311
Kirill Bobyrevbcaf3802018-02-25 07:21:16 +0000312 if (RunSynchronously) {
313 if (WorkerThreadsCount.getNumOccurrences())
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000314 llvm::errs() << "Ignoring -j because -run-synchronously is set.\n";
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000315 WorkerThreadsCount = 0;
Kirill Bobyrevbcaf3802018-02-25 07:21:16 +0000316 }
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000317
Benjamin Kramer74a18952017-10-26 10:07:04 +0000318 // Validate command line arguments.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000319 llvm::Optional<llvm::raw_fd_ostream> InputMirrorStream;
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000320 if (!InputMirrorFile.empty()) {
321 std::error_code EC;
Zachary Turner1f67a3c2018-06-07 19:58:58 +0000322 InputMirrorStream.emplace(InputMirrorFile, /*ref*/ EC,
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000323 llvm::sys::fs::FA_Read | llvm::sys::fs::FA_Write);
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000324 if (EC) {
325 InputMirrorStream.reset();
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000326 llvm::errs() << "Error while opening an input mirror file: "
327 << EC.message();
Sam McCall0d946182018-11-02 23:47:55 +0000328 } else {
329 InputMirrorStream->SetUnbuffered();
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000330 }
331 }
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000332
Sam McCalled2717a2018-02-14 03:20:07 +0000333 // Setup tracing facilities if CLANGD_TRACE is set. In practice enabling a
334 // trace flag in your editor's config is annoying, launching with
335 // `CLANGD_TRACE=trace.json vim` is easier.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000336 llvm::Optional<llvm::raw_fd_ostream> TraceStream;
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000337 std::unique_ptr<trace::EventTracer> Tracer;
Sam McCalled2717a2018-02-14 03:20:07 +0000338 if (auto *TraceFile = getenv("CLANGD_TRACE")) {
Sam McCall8567cb32017-11-02 09:21:51 +0000339 std::error_code EC;
Zachary Turner1f67a3c2018-06-07 19:58:58 +0000340 TraceStream.emplace(TraceFile, /*ref*/ EC,
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000341 llvm::sys::fs::FA_Read | llvm::sys::fs::FA_Write);
Sam McCall8567cb32017-11-02 09:21:51 +0000342 if (EC) {
Sam McCalled2717a2018-02-14 03:20:07 +0000343 TraceStream.reset();
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000344 llvm::errs() << "Error while opening trace file " << TraceFile << ": "
345 << EC.message();
Sam McCall8567cb32017-11-02 09:21:51 +0000346 } else {
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000347 Tracer = trace::createJSONTracer(*TraceStream, PrettyPrint);
Sam McCall8567cb32017-11-02 09:21:51 +0000348 }
349 }
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000350
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000351 llvm::Optional<trace::Session> TracingSession;
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000352 if (Tracer)
353 TracingSession.emplace(*Tracer);
354
Eric Liu4e4e5a42018-08-28 13:15:50 +0000355 // Use buffered stream to stderr (we still flush each log message). Unbuffered
356 // stream can cause significant (non-deterministic) latency for the logger.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000357 llvm::errs().SetBuffered();
358 StreamLogger Logger(llvm::errs(), LogLevel);
359 LoggingSession LoggingSession(Logger);
Ilya Biryukov940901e2017-12-13 12:51:22 +0000360
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000361 // If --compile-commands-dir arg was invoked, check value and override default
362 // path.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000363 llvm::Optional<Path> CompileCommandsDirPath;
Sam McCallf01ad102018-10-23 11:54:36 +0000364 if (!CompileCommandsDir.empty()) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000365 if (llvm::sys::fs::exists(CompileCommandsDir)) {
Sam McCallf01ad102018-10-23 11:54:36 +0000366 // We support passing both relative and absolute paths to the
367 // --compile-commands-dir argument, but we assume the path is absolute in
368 // the rest of clangd so we make sure the path is absolute before
369 // continuing.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000370 llvm::SmallString<128> Path(CompileCommandsDir);
371 if (std::error_code EC = llvm::sys::fs::make_absolute(Path)) {
372 llvm::errs() << "Error while converting the relative path specified by "
373 "--compile-commands-dir to an absolute path: "
374 << EC.message() << ". The argument will be ignored.\n";
Sam McCallf01ad102018-10-23 11:54:36 +0000375 } else {
376 CompileCommandsDirPath = Path.str();
377 }
378 } else {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000379 llvm::errs()
380 << "Path specified by --compile-commands-dir does not exist. The "
381 "argument will be ignored.\n";
Sam McCallf01ad102018-10-23 11:54:36 +0000382 }
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000383 }
Benjamin Kramer6a3d74e2017-02-07 12:40:59 +0000384
Sam McCall7363a2f2018-03-05 17:28:54 +0000385 ClangdServer::Options Opts;
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000386 switch (PCHStorage) {
387 case PCHStorageFlag::Memory:
Sam McCall7363a2f2018-03-05 17:28:54 +0000388 Opts.StorePreamblesInMemory = true;
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000389 break;
390 case PCHStorageFlag::Disk:
Sam McCall7363a2f2018-03-05 17:28:54 +0000391 Opts.StorePreamblesInMemory = false;
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000392 break;
393 }
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +0000394 if (!ResourceDir.empty())
Sam McCall7363a2f2018-03-05 17:28:54 +0000395 Opts.ResourceDir = ResourceDir;
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000396 Opts.BuildDynamicSymbolIndex = EnableIndex;
Sam McCall96f24892018-10-16 08:53:52 +0000397 Opts.HeavyweightDynamicSymbolIndex = UseDex;
Sam McCall422c8282018-11-26 16:00:11 +0000398 Opts.BackgroundIndex = EnableBackgroundIndex;
Eric Liu667e8ef2018-12-18 15:39:33 +0000399 Opts.BackgroundIndexRebuildPeriodMs = BackgroundIndexRebuildPeriod;
Haojian Wuba28e9a2018-01-10 14:44:34 +0000400 std::unique_ptr<SymbolIndex> StaticIdx;
Sam McCallf469c642018-09-10 10:00:47 +0000401 std::future<void> AsyncIndexLoad; // Block exit while loading the index.
Haojian Wu162510f2018-10-08 10:44:54 +0000402 if (EnableIndex && !IndexFile.empty()) {
Sam McCall76c4c3a2018-09-04 16:19:40 +0000403 // Load the index asynchronously. Meanwhile SwapIndex returns no results.
404 SwapIndex *Placeholder;
405 StaticIdx.reset(Placeholder = new SwapIndex(llvm::make_unique<MemIndex>()));
Eric Liuc0ac4bb2018-11-22 15:02:05 +0000406 AsyncIndexLoad = runAsync<void>([Placeholder] {
407 if (auto Idx = loadIndex(IndexFile, /*UseDex=*/true))
Sam McCall76c4c3a2018-09-04 16:19:40 +0000408 Placeholder->reset(std::move(Idx));
409 });
Sam McCallf469c642018-09-10 10:00:47 +0000410 if (RunSynchronously)
411 AsyncIndexLoad.wait();
Sam McCall7363a2f2018-03-05 17:28:54 +0000412 }
Sam McCall76c4c3a2018-09-04 16:19:40 +0000413 Opts.StaticIndex = StaticIdx.get();
Sam McCall7363a2f2018-03-05 17:28:54 +0000414 Opts.AsyncThreadsCount = WorkerThreadsCount;
415
Sam McCalladccab62017-11-23 16:58:22 +0000416 clangd::CodeCompleteOptions CCOpts;
Sam McCalladccab62017-11-23 16:58:22 +0000417 CCOpts.IncludeIneligibleResults = IncludeIneligibleResults;
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000418 CCOpts.Limit = LimitResults;
Sam McCallc18c2802018-06-15 11:06:29 +0000419 CCOpts.BundleOverloads = CompletionStyle != Detailed;
Sam McCall2161ec72018-07-05 06:20:41 +0000420 CCOpts.ShowOrigins = ShowOrigins;
Raoul Wols8f5e06f2018-07-29 19:12:42 +0000421 if (!HeaderInsertionDecorators) {
422 CCOpts.IncludeIndicator.Insert.clear();
423 CCOpts.IncludeIndicator.NoInsert.clear();
424 }
Eric Liu25d74e92018-08-24 11:23:56 +0000425 CCOpts.SpeculativeIndexRequest = Opts.StaticIndex;
Kadir Cetinkayae8d8aee2018-09-19 10:16:44 +0000426 CCOpts.EnableFunctionArgSnippets = EnableFunctionArgSnippets;
Eric Liu670c1472018-09-27 18:46:00 +0000427 CCOpts.AllScopes = AllScopesCompletion;
Sam McCall7363a2f2018-03-05 17:28:54 +0000428
Haojian Wu1ca0c582019-01-22 09:39:05 +0000429 RealFileSystemProvider FSProvider;
Benjamin Kramer74a18952017-10-26 10:07:04 +0000430 // Initialize and run ClangdLSPServer.
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000431 // Change stdin to binary to not lose \r\n on windows.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000432 llvm::sys::ChangeStdinToBinary();
Jan Korousdca9c7c2019-01-16 00:24:22 +0000433
434 std::unique_ptr<Transport> TransportLayer;
435 if (getenv("CLANGD_AS_XPC_SERVICE")) {
Fangrui Song54762df2019-01-16 08:13:15 +0000436#if CLANGD_BUILD_XPC
Jan Korousdca9c7c2019-01-16 00:24:22 +0000437 TransportLayer = newXPCTransport();
438#else
Fangrui Song54762df2019-01-16 08:13:15 +0000439 llvm::errs() << "This clangd binary wasn't built with XPC support.\n";
440 return (int)ErrorResultCode::CantRunAsXPCService;
Jan Korousdca9c7c2019-01-16 00:24:22 +0000441#endif
442 } else {
443 TransportLayer = newJSONTransport(
444 stdin, llvm::outs(),
445 InputMirrorStream ? InputMirrorStream.getPointer() : nullptr,
446 PrettyPrint, InputStyle);
447 }
448
Haojian Wu1ca0c582019-01-22 09:39:05 +0000449 // Create an empty clang-tidy option.
Haojian Wuac6d2e12019-02-06 09:10:47 +0000450 std::unique_ptr<tidy::ClangTidyOptionsProvider> ClangTidyOptProvider;
451 if (EnableClangTidy) {
452 auto OverrideClangTidyOptions = tidy::ClangTidyOptions::getDefaults();
453 OverrideClangTidyOptions.Checks = ClangTidyChecks;
454 ClangTidyOptProvider = llvm::make_unique<tidy::FileOptionsProvider>(
455 tidy::ClangTidyGlobalOptions(),
456 /* Default */ tidy::ClangTidyOptions::getDefaults(),
457 /* Override */ OverrideClangTidyOptions, FSProvider.getFileSystem());
458 }
459 Opts.ClangTidyOptProvider = ClangTidyOptProvider.get();
Eric Liudd662772019-01-28 14:01:55 +0000460 Opts.SuggestMissingIncludes = SuggestMissingIncludes;
Alex Lorenzf8087862018-08-01 17:39:29 +0000461 ClangdLSPServer LSPServer(
Haojian Wu1ca0c582019-01-22 09:39:05 +0000462 *TransportLayer, FSProvider, CCOpts, CompileCommandsDirPath,
Sam McCallc55d09a2018-11-02 13:09:36 +0000463 /*UseDirBasedCDB=*/CompileArgsFrom == FilesystemCompileArgs, Opts);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000464 llvm::set_thread_name("clangd.main");
Jan Korousdca9c7c2019-01-16 00:24:22 +0000465 return LSPServer.run() ? 0
466 : static_cast<int>(ErrorResultCode::NoShutdownRequest);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000467}